Skip to content

Commit e4bd099

Browse files
committed
Fixed AVR and Arduino IDE compilation error.
1 parent f791aed commit e4bd099

File tree

5 files changed

+143
-90
lines changed

5 files changed

+143
-90
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Firebase RTDB Arduino Client for ARM/AVR WIFI Dev Boards
22

33

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

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

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=Firebase Arduino based on WiFiNINA
22

3-
version=1.2.2
3+
version=1.2.3
44

55
author=Mobizt
66

src/Firebase.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Firebase.cpp, version 1.0.1
2+
* Firebase.cpp, version 1.0.2
33
*
44
*
5-
* Created: October 20, 2021
5+
* Created: November 10, 2021
66
*
77
* 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
88
* and delete calls.
@@ -1385,9 +1385,13 @@ void Firebase_Class::setDataType(FirebaseData &fbdo, const char *data)
13851385
}
13861386
else
13871387
{
1388-
char *eptr;
13891388

1389+
#if defined(__AVR__)
1390+
unsigned long long v1 = (data[0] == '-') ? wstrtoull(&data[1]) : wstrtoull(data);
1391+
#else
1392+
char *eptr;
13901393
unsigned long long v1 = (data[0] == '-') ? strtoull(&data[1], &eptr, 10) : strtoull(data, &eptr, 10);
1394+
#endif
13911395

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

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

1416+
unsigned long long Firebase_Class::wstrtoull(const char *s)
1417+
{
1418+
unsigned long long y = 0;
1419+
1420+
for (int i = 0; i < strlen(s); i++)
1421+
{
1422+
char c = s[i];
1423+
if (c < '0' || c > '9')
1424+
break;
1425+
y *= 10;
1426+
y += (c - '0');
1427+
}
1428+
1429+
return y;
1430+
}
1431+
1432+
long long Firebase_Class::wstrtoll(const char *s)
1433+
{
1434+
long long y = 0;
1435+
1436+
int ofs = s[0] == '-' ? 1 : 0;
1437+
1438+
for (int i = ofs; i < strlen(s); i++)
1439+
{
1440+
char c = s[i];
1441+
if (c < '0' || c > '9')
1442+
break;
1443+
y *= 10;
1444+
y += (c - '0');
1445+
}
1446+
1447+
if (ofs == 1)
1448+
y *= -1;
1449+
1450+
return y;
1451+
}
1452+
14121453
void Firebase_Class::resetFirebasedataFlag(FirebaseData &fbdo)
14131454
{
14141455
fbdo._bufferOverflow = false;
@@ -1715,8 +1756,13 @@ unsigned long long FirebaseData::uint64Data()
17151756
{
17161757
if (_data != "" && (_dataType == firebase_data_type_unsigned_integer64 || _dataType == firebase_data_type_integer || _dataType == firebase_data_type_double || _dataType == firebase_data_type_float))
17171758
{
1759+
#if defined(__AVR__)
1760+
unsigned long long v = Firebase.wstrtoull(_data.c_str());
1761+
#else
17181762
char *eptr;
17191763
unsigned long long v = strtoull(_data.c_str(), &eptr, 10);
1764+
#endif
1765+
17201766
return v;
17211767
}
17221768
else
@@ -1727,8 +1773,12 @@ long long FirebaseData::int64Data()
17271773
{
17281774
if (_data != "" && (_dataType == firebase_data_type_integer64 || _dataType == firebase_data_type_integer || _dataType == firebase_data_type_double || _dataType == firebase_data_type_float))
17291775
{
1776+
#if defined(__AVR__)
1777+
long long v = Firebase.wstrtoll(_data.c_str());
1778+
#else
17301779
char *eptr;
17311780
long long v = strtoll(_data.c_str(), &eptr, 10);
1781+
#endif
17321782
return v;
17331783
}
17341784
else

src/Firebase.h

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Firebase.h, version 1.0.1
2+
* Firebase.h, version 1.0.2
33
*
44
*
5-
* Created: October 20, 2021
5+
* Created: November 10, 2021
66
*
77
* 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
88
* and delete calls.
@@ -45,12 +45,10 @@
4545
#else
4646
#error Architecture or board not supported.
4747
#endif
48-
4948
#define FIEBASE_PORT 443
5049
#define FIREBASE_RESPONSE_SIZE 400
5150
#define KEEP_ALIVE_TIMEOUT 30000
5251

53-
5452
const char C_STR_0[] PROGMEM = "{\".sv\": \"timestamp\"}";
5553
const char C_STR_1[] PROGMEM = "/";
5654
const char C_STR_2[] PROGMEM = ".json?auth=";
@@ -674,7 +672,8 @@ class Firebase_Class
674672
char *newS(char *p, size_t len, char *d);
675673
char *strP(PGM_P pgm);
676674
void strP(char *buf, PGM_P pgm, bool empty = false);
677-
675+
unsigned long long wstrtoull(const char *s);
676+
long long wstrtoll(const char *s);
678677
void sendHeader(FirebaseData &fbdo, const char *host, uint8_t _method, const char *path, const char *auth, uint16_t payloadLength);
679678
void resetFirebasedataFlag(FirebaseData &fbdo);
680679
bool handleNetClientNotConnected(FirebaseData &fbdo);
@@ -740,105 +739,109 @@ class NumToString
740739
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
741740
*/
742741

743-
char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
744-
{
745-
//Commented code is the original version
746-
/***
742+
#if defined (__arm__)
743+
744+
char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
745+
{
746+
//Commented code is the original version
747+
/***
747748
char fmt[20];
748749
sprintf(fmt, "%%%d.%df", width, prec);
749750
sprintf(sout, fmt, val);
750751
return sout;
751752
*/
752753

753-
// Handle negative numbers
754-
uint8_t negative = 0;
755-
if (val < 0.0)
756-
{
757-
negative = 1;
758-
val = -val;
759-
}
760-
761-
// Round correctly so that print(1.999, 2) prints as "2.00"
762-
double rounding = 0.5;
763-
for (int i = 0; i < prec; ++i)
764-
{
765-
rounding /= 10.0;
766-
}
767-
768-
val += rounding;
769-
770-
// Extract the integer part of the number
771-
unsigned long int_part = (unsigned long)val;
772-
double remainder = val - (double)int_part;
754+
// Handle negative numbers
755+
uint8_t negative = 0;
756+
if (val < 0.0)
757+
{
758+
negative = 1;
759+
val = -val;
760+
}
773761

774-
if (prec > 0)
775-
{
776-
// Extract digits from the remainder
777-
unsigned long dec_part = 0;
778-
double decade = 1.0;
779-
for (int i = 0; i < prec; i++)
762+
// Round correctly so that print(1.999, 2) prints as "2.00"
763+
double rounding = 0.5;
764+
for (int i = 0; i < prec; ++i)
780765
{
781-
decade *= 10.0;
766+
rounding /= 10.0;
782767
}
783-
remainder *= decade;
784-
dec_part = (int)remainder;
785768

786-
if (negative)
769+
val += rounding;
770+
771+
// Extract the integer part of the number
772+
unsigned long int_part = (unsigned long)val;
773+
double remainder = val - (double)int_part;
774+
775+
if (prec > 0)
787776
{
788-
sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part);
777+
// Extract digits from the remainder
778+
unsigned long dec_part = 0;
779+
double decade = 1.0;
780+
for (int i = 0; i < prec; i++)
781+
{
782+
decade *= 10.0;
783+
}
784+
remainder *= decade;
785+
dec_part = (int)remainder;
786+
787+
if (negative)
788+
{
789+
sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part);
790+
}
791+
else
792+
{
793+
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
794+
}
789795
}
790796
else
791797
{
792-
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
798+
if (negative)
799+
{
800+
sprintf(sout, "-%ld", int_part);
801+
}
802+
else
803+
{
804+
sprintf(sout, "%ld", int_part);
805+
}
793806
}
794-
}
795-
else
796-
{
797-
if (negative)
807+
// Handle minimum field width of the output string
808+
// width is signed value, negative for left adjustment.
809+
// Range -128,127
810+
char fmt[129] = "";
811+
unsigned int w = width;
812+
if (width < 0)
798813
{
799-
sprintf(sout, "-%ld", int_part);
814+
negative = 1;
815+
w = -width;
800816
}
801817
else
802818
{
803-
sprintf(sout, "%ld", int_part);
819+
negative = 0;
804820
}
805-
}
806-
// Handle minimum field width of the output string
807-
// width is signed value, negative for left adjustment.
808-
// Range -128,127
809-
char fmt[129] = "";
810-
unsigned int w = width;
811-
if (width < 0)
812-
{
813-
negative = 1;
814-
w = -width;
815-
}
816-
else
817-
{
818-
negative = 0;
819-
}
820821

821-
if (strlen(sout) < w)
822-
{
823-
memset(fmt, ' ', 128);
824-
fmt[w - strlen(sout)] = '\0';
825-
if (negative == 0)
822+
if (strlen(sout) < w)
826823
{
827-
char *tmp = (char *)malloc(strlen(sout) + 1);
828-
strcpy(tmp, sout);
829-
strcpy(sout, fmt);
830-
strcat(sout, tmp);
831-
free(tmp);
832-
}
833-
else
834-
{
835-
// left adjustment
836-
strcat(sout, fmt);
824+
memset(fmt, ' ', 128);
825+
fmt[w - strlen(sout)] = '\0';
826+
if (negative == 0)
827+
{
828+
char *tmp = (char *)malloc(strlen(sout) + 1);
829+
strcpy(tmp, sout);
830+
strcpy(sout, fmt);
831+
strcat(sout, tmp);
832+
free(tmp);
833+
}
834+
else
835+
{
836+
// left adjustment
837+
strcat(sout, fmt);
838+
}
837839
}
840+
841+
return sout;
838842
}
839843

840-
return sout;
841-
}
844+
#endif
842845

843846
void init(size_t sz)
844847
{

src/Firebase_Arduino_WiFiNINA.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/**
2-
* Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Dev Boards based on WiFiNINA library, version 1.2.2
2+
* Google's Firebase Realtime Database Arduino Library for ARM/AVR WIFI Dev Boards based on WiFiNINA library, version 1.2.3
33
*
44
* This library required WiFiNINA Library to be installed.
55
* https://github.com/arduino-libraries/WiFiNINA
66
*
7-
* Created: October 20, 2021
7+
* Created: November 10, 2021
88
*
9-
* Feature Added:
10-
* - Add JSON array supports in getArray, setArray and pushArray.
11-
* - Add generic get function.
129
*
1310
* Feature Fixed:
14-
* - Fix invalid error reason report.
11+
* - Fix compilation error in avr and Arduino IDE.
12+
* - Fix conversion from double to string issue in avr.
1513
*
1614
*
1715
* 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
@@ -42,4 +40,6 @@
4240
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4341
*/
4442

43+
#include <SPI.h>
44+
#include <WiFiNINA.h>
4545
#include "Firebase.h"

0 commit comments

Comments
 (0)