p kj
/
LPC824-BlynkWeatherstation
Microduino
Fork of Io_moon by
Blynk_v0_3_7/Blynk/utility/utility.cpp@0:740c1eb2df13, 2016-06-23 (annotated)
- Committer:
- lixianyu
- Date:
- Thu Jun 23 11:16:14 2016 +0000
- Revision:
- 0:740c1eb2df13
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:740c1eb2df13 | 1 | #include <Blynk/BlynkDebug.h> |
lixianyu | 0:740c1eb2df13 | 2 | |
lixianyu | 0:740c1eb2df13 | 3 | #if defined(ESP8266) && !defined(BLYNK_NO_FLOAT) |
lixianyu | 0:740c1eb2df13 | 4 | #include <string.h> |
lixianyu | 0:740c1eb2df13 | 5 | #include <math.h> |
lixianyu | 0:740c1eb2df13 | 6 | |
lixianyu | 0:740c1eb2df13 | 7 | char* dtostrf_internal(double number, signed char width, unsigned char prec, char *s) { |
lixianyu | 0:740c1eb2df13 | 8 | if(isnan(number)) { |
lixianyu | 0:740c1eb2df13 | 9 | strcpy(s, "nan"); |
lixianyu | 0:740c1eb2df13 | 10 | return s; |
lixianyu | 0:740c1eb2df13 | 11 | } |
lixianyu | 0:740c1eb2df13 | 12 | if(isinf(number)) { |
lixianyu | 0:740c1eb2df13 | 13 | strcpy(s, "inf"); |
lixianyu | 0:740c1eb2df13 | 14 | return s; |
lixianyu | 0:740c1eb2df13 | 15 | } |
lixianyu | 0:740c1eb2df13 | 16 | |
lixianyu | 0:740c1eb2df13 | 17 | if(number > 4294967040.0 || number < -4294967040.0) { |
lixianyu | 0:740c1eb2df13 | 18 | strcpy(s, "ovf"); |
lixianyu | 0:740c1eb2df13 | 19 | return s; |
lixianyu | 0:740c1eb2df13 | 20 | } |
lixianyu | 0:740c1eb2df13 | 21 | char* out = s; |
lixianyu | 0:740c1eb2df13 | 22 | // Handle negative numbers |
lixianyu | 0:740c1eb2df13 | 23 | if(number < 0.0) { |
lixianyu | 0:740c1eb2df13 | 24 | *out = '-'; |
lixianyu | 0:740c1eb2df13 | 25 | ++out; |
lixianyu | 0:740c1eb2df13 | 26 | number = -number; |
lixianyu | 0:740c1eb2df13 | 27 | } |
lixianyu | 0:740c1eb2df13 | 28 | |
lixianyu | 0:740c1eb2df13 | 29 | // Round correctly so that print(1.999, 2) prints as "2.00" |
lixianyu | 0:740c1eb2df13 | 30 | double rounding = 0.5; |
lixianyu | 0:740c1eb2df13 | 31 | for(uint8_t i = 0; i < prec; ++i) |
lixianyu | 0:740c1eb2df13 | 32 | rounding /= 10.0; |
lixianyu | 0:740c1eb2df13 | 33 | |
lixianyu | 0:740c1eb2df13 | 34 | number += rounding; |
lixianyu | 0:740c1eb2df13 | 35 | |
lixianyu | 0:740c1eb2df13 | 36 | // Extract the integer part of the number and print it |
lixianyu | 0:740c1eb2df13 | 37 | unsigned long int_part = (unsigned long) number; |
lixianyu | 0:740c1eb2df13 | 38 | double remainder = number - (double) int_part; |
lixianyu | 0:740c1eb2df13 | 39 | out += sprintf(out, "%d", int_part); |
lixianyu | 0:740c1eb2df13 | 40 | |
lixianyu | 0:740c1eb2df13 | 41 | // Print the decimal point, but only if there are digits beyond |
lixianyu | 0:740c1eb2df13 | 42 | if(prec > 0) { |
lixianyu | 0:740c1eb2df13 | 43 | *out = '.'; |
lixianyu | 0:740c1eb2df13 | 44 | ++out; |
lixianyu | 0:740c1eb2df13 | 45 | } |
lixianyu | 0:740c1eb2df13 | 46 | |
lixianyu | 0:740c1eb2df13 | 47 | while(prec-- > 0) { |
lixianyu | 0:740c1eb2df13 | 48 | remainder *= 10.0; |
lixianyu | 0:740c1eb2df13 | 49 | if((int)remainder == 0){ |
lixianyu | 0:740c1eb2df13 | 50 | *out = '0'; |
lixianyu | 0:740c1eb2df13 | 51 | ++out; |
lixianyu | 0:740c1eb2df13 | 52 | } |
lixianyu | 0:740c1eb2df13 | 53 | } |
lixianyu | 0:740c1eb2df13 | 54 | sprintf(out, "%d", (int) remainder); |
lixianyu | 0:740c1eb2df13 | 55 | |
lixianyu | 0:740c1eb2df13 | 56 | return s; |
lixianyu | 0:740c1eb2df13 | 57 | } |
lixianyu | 0:740c1eb2df13 | 58 | |
lixianyu | 0:740c1eb2df13 | 59 | #endif |
lixianyu | 0:740c1eb2df13 | 60 | |
lixianyu | 0:740c1eb2df13 | 61 |