Skip to content

Commit

Permalink
Fixed AVR and Arduino IDE compilation error.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Nov 10, 2021
1 parent f791aed commit e4bd099
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase RTDB Arduino Client for ARM/AVR WIFI Dev Boards


Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Development Boards based on WiFiNINA library, v 1.2.2
Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Development Boards based on WiFiNINA library, v 1.2.3

This client library provides the most reliable operations for read, store, and update the Firebase RTDB through the REST API.

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=Firebase Arduino based on WiFiNINA

version=1.2.2
version=1.2.3

author=Mobizt

Expand Down
56 changes: 53 additions & 3 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Firebase.cpp, version 1.0.1
* Firebase.cpp, version 1.0.2
*
*
* Created: October 20, 2021
* Created: November 10, 2021
*
* This library provides ARM/AVR WIFI Development Boards to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
* and delete calls.
Expand Down Expand Up @@ -1385,9 +1385,13 @@ void Firebase_Class::setDataType(FirebaseData &fbdo, const char *data)
}
else
{
char *eptr;

#if defined(__AVR__)
unsigned long long v1 = (data[0] == '-') ? wstrtoull(&data[1]) : wstrtoull(data);
#else
char *eptr;
unsigned long long v1 = (data[0] == '-') ? strtoull(&data[1], &eptr, 10) : strtoull(data, &eptr, 10);
#endif

unsigned long long v2 = (sizeof(int) == 2) ? 0xffff / 2 : 0xffffffff / 2;

Expand All @@ -1409,6 +1413,43 @@ void Firebase_Class::setDataType(FirebaseData &fbdo, const char *data)
fbdo._dataTypeNum = fbdo._dataType;
}

unsigned long long Firebase_Class::wstrtoull(const char *s)
{
unsigned long long y = 0;

for (int i = 0; i < strlen(s); i++)
{
char c = s[i];
if (c < '0' || c > '9')
break;
y *= 10;
y += (c - '0');
}

return y;
}

long long Firebase_Class::wstrtoll(const char *s)
{
long long y = 0;

int ofs = s[0] == '-' ? 1 : 0;

for (int i = ofs; i < strlen(s); i++)
{
char c = s[i];
if (c < '0' || c > '9')
break;
y *= 10;
y += (c - '0');
}

if (ofs == 1)
y *= -1;

return y;
}

void Firebase_Class::resetFirebasedataFlag(FirebaseData &fbdo)
{
fbdo._bufferOverflow = false;
Expand Down Expand Up @@ -1715,8 +1756,13 @@ unsigned long long FirebaseData::uint64Data()
{
if (_data != "" && (_dataType == firebase_data_type_unsigned_integer64 || _dataType == firebase_data_type_integer || _dataType == firebase_data_type_double || _dataType == firebase_data_type_float))
{
#if defined(__AVR__)
unsigned long long v = Firebase.wstrtoull(_data.c_str());
#else
char *eptr;
unsigned long long v = strtoull(_data.c_str(), &eptr, 10);
#endif

return v;
}
else
Expand All @@ -1727,8 +1773,12 @@ long long FirebaseData::int64Data()
{
if (_data != "" && (_dataType == firebase_data_type_integer64 || _dataType == firebase_data_type_integer || _dataType == firebase_data_type_double || _dataType == firebase_data_type_float))
{
#if defined(__AVR__)
long long v = Firebase.wstrtoll(_data.c_str());
#else
char *eptr;
long long v = strtoll(_data.c_str(), &eptr, 10);
#endif
return v;
}
else
Expand Down
161 changes: 82 additions & 79 deletions src/Firebase.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Firebase.h, version 1.0.1
* Firebase.h, version 1.0.2
*
*
* Created: October 20, 2021
* Created: November 10, 2021
*
* This library provides ARM/AVR WIFI Development Boards to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
* and delete calls.
Expand Down Expand Up @@ -45,12 +45,10 @@
#else
#error Architecture or board not supported.
#endif

#define FIEBASE_PORT 443
#define FIREBASE_RESPONSE_SIZE 400
#define KEEP_ALIVE_TIMEOUT 30000


const char C_STR_0[] PROGMEM = "{\".sv\": \"timestamp\"}";
const char C_STR_1[] PROGMEM = "/";
const char C_STR_2[] PROGMEM = ".json?auth=";
Expand Down Expand Up @@ -674,7 +672,8 @@ class Firebase_Class
char *newS(char *p, size_t len, char *d);
char *strP(PGM_P pgm);
void strP(char *buf, PGM_P pgm, bool empty = false);

unsigned long long wstrtoull(const char *s);
long long wstrtoll(const char *s);
void sendHeader(FirebaseData &fbdo, const char *host, uint8_t _method, const char *path, const char *auth, uint16_t payloadLength);
void resetFirebasedataFlag(FirebaseData &fbdo);
bool handleNetClientNotConnected(FirebaseData &fbdo);
Expand Down Expand Up @@ -740,105 +739,109 @@ class NumToString
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
{
//Commented code is the original version
/***
#if defined (__arm__)

char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
{
//Commented code is the original version
/***
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
*/

// Handle negative numbers
uint8_t negative = 0;
if (val < 0.0)
{
negative = 1;
val = -val;
}

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (int i = 0; i < prec; ++i)
{
rounding /= 10.0;
}

val += rounding;

// Extract the integer part of the number
unsigned long int_part = (unsigned long)val;
double remainder = val - (double)int_part;
// Handle negative numbers
uint8_t negative = 0;
if (val < 0.0)
{
negative = 1;
val = -val;
}

if (prec > 0)
{
// Extract digits from the remainder
unsigned long dec_part = 0;
double decade = 1.0;
for (int i = 0; i < prec; i++)
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (int i = 0; i < prec; ++i)
{
decade *= 10.0;
rounding /= 10.0;
}
remainder *= decade;
dec_part = (int)remainder;

if (negative)
val += rounding;

// Extract the integer part of the number
unsigned long int_part = (unsigned long)val;
double remainder = val - (double)int_part;

if (prec > 0)
{
sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part);
// Extract digits from the remainder
unsigned long dec_part = 0;
double decade = 1.0;
for (int i = 0; i < prec; i++)
{
decade *= 10.0;
}
remainder *= decade;
dec_part = (int)remainder;

if (negative)
{
sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part);
}
else
{
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
}
}
else
{
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
if (negative)
{
sprintf(sout, "-%ld", int_part);
}
else
{
sprintf(sout, "%ld", int_part);
}
}
}
else
{
if (negative)
// Handle minimum field width of the output string
// width is signed value, negative for left adjustment.
// Range -128,127
char fmt[129] = "";
unsigned int w = width;
if (width < 0)
{
sprintf(sout, "-%ld", int_part);
negative = 1;
w = -width;
}
else
{
sprintf(sout, "%ld", int_part);
negative = 0;
}
}
// Handle minimum field width of the output string
// width is signed value, negative for left adjustment.
// Range -128,127
char fmt[129] = "";
unsigned int w = width;
if (width < 0)
{
negative = 1;
w = -width;
}
else
{
negative = 0;
}

if (strlen(sout) < w)
{
memset(fmt, ' ', 128);
fmt[w - strlen(sout)] = '\0';
if (negative == 0)
if (strlen(sout) < w)
{
char *tmp = (char *)malloc(strlen(sout) + 1);
strcpy(tmp, sout);
strcpy(sout, fmt);
strcat(sout, tmp);
free(tmp);
}
else
{
// left adjustment
strcat(sout, fmt);
memset(fmt, ' ', 128);
fmt[w - strlen(sout)] = '\0';
if (negative == 0)
{
char *tmp = (char *)malloc(strlen(sout) + 1);
strcpy(tmp, sout);
strcpy(sout, fmt);
strcat(sout, tmp);
free(tmp);
}
else
{
// left adjustment
strcat(sout, fmt);
}
}

return sout;
}

return sout;
}
#endif

void init(size_t sz)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Firebase_Arduino_WiFiNINA.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/**
* Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Dev Boards based on WiFiNINA library, version 1.2.2
* Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Dev Boards based on WiFiNINA library, version 1.2.3
*
* This library required WiFiNINA Library to be installed.
* https://github.com/arduino-libraries/WiFiNINA
*
* Created: October 20, 2021
* Created: November 10, 2021
*
* Feature Added:
* - Add JSON array supports in getArray, setArray and pushArray.
* - Add generic get function.
*
* Feature Fixed:
* - Fix invalid error reason report.
* - Fix compilation error in avr and Arduino IDE.
* - Fix conversion from double to string issue in avr.
*
*
* This library provides ARM/AVR WIFI Development Boards to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
Expand Down Expand Up @@ -42,4 +40,6 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <SPI.h>
#include <WiFiNINA.h>
#include "Firebase.h"

0 comments on commit e4bd099

Please sign in to comment.