Skip to content

restore dtostrf when floats are disabled in printf/scanf + round fix #7087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ void configTime(int timezone, int daylightOffset_sec, const char* server1,
void configTime(const char* tz, const char* server1,
const char* server2 = nullptr, const char* server3 = nullptr);

// esp32 api compatibility
inline void configTzTime(const char* tz, const char* server1,
const char* server2 = nullptr, const char* server3 = nullptr)
{
configTime(tz, server1, server2, server3);
}

#endif // __cplusplus

#include "pins_arduino.h"
Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/StackThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ uint32_t stack_thunk_get_max_usage()
/* Print the stack from the first used 16-byte chunk to the top, decodable by the exception decoder */
void stack_thunk_dump_stack()
{
uint32_t *pos = stack_thunk_top;
while (pos < stack_thunk_ptr) {
uint32_t *pos = stack_thunk_ptr;
while (pos < stack_thunk_top) {
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
break;
pos += 4;
}
ets_printf(">>>stack>>>\n");
while (pos < stack_thunk_ptr) {
while (pos < stack_thunk_top) {
ets_printf("%08x: %08x %08x %08x %08x\n", (int32_t)pos, pos[0], pos[1], pos[2], pos[3]);
pos += 4;
}
Expand Down
88 changes: 85 additions & 3 deletions cores/esp8266/core_esp8266_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include <limits>
#include "stdlib_noniso.h"

extern "C" {
Expand All @@ -41,9 +42,90 @@ char* ultoa(unsigned long value, char* result, int base) {
}

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
char fmt[32];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(s, fmt, number);
if (!_printf_float) {
return __dtostrf(number, width, prec, s);
} else {
char fmt[32];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(s, fmt, number);
return s;
}
}

static char * __dtostrf(double number, signed char width, unsigned char prec, char *s) {
bool negative = false;

if (isnan(number)) {
strcpy(s, "nan");
return s;
}
if (isinf(number)) {
strcpy(s, "inf");
return s;
}

char* out = s;

int fillme = width; // how many cells to fill for the integer part
if (prec > 0) {
fillme -= (prec+1);
}

// Handle negative numbers
if (number < 0.0) {
negative = true;
fillme--;
number = -number;
}

// Round correctly so that print(1.999, 2) prints as "2.00"
// I optimized out most of the divisions
double rounding = 2.0;
for (uint8_t i = 0; i < prec; ++i)
rounding *= 10.0;
rounding = 1.0 / rounding;

number += rounding;

// Figure out how big our number really is
double tenpow = 1.0;
int digitcount = 1;
double nextpow;
while (number >= (nextpow = (10.0 * tenpow))) {
tenpow = nextpow;
digitcount++;
}

// minimal compensation for possible lack of precision (#7087 addition)
number *= 1 + std::numeric_limits<decltype(number)>::epsilon();

number /= tenpow;
fillme -= digitcount;

// Pad unused cells with spaces
while (fillme-- > 0) {
*out++ = ' ';
}

// Handle negative sign
if (negative) *out++ = '-';

// Print the digits, and if necessary, the decimal point
digitcount += prec;
int8_t digit = 0;
while (digitcount-- > 0) {
digit = (int8_t)number;
if (digit > 9) digit = 9; // insurance
*out++ = (char)('0' | digit);
if ((digitcount == prec) && (prec > 0)) {
*out++ = '.';
}
number -= digit;
number *= 10.0;
}

// make sure the string is terminated
*out = 0;
return s;
}

Expand Down
2 changes: 2 additions & 0 deletions cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ extern "C" {
#include <cont.h> // g_pcont declaration

extern bool timeshift64_is_set;
extern uint32_t sntp_real_timestamp;

bool can_yield();
void esp_yield();
void esp_schedule();
void tune_timeshift64 (uint64_t now_us);
void disable_extra4k_at_link_time (void) __attribute__((noinline));
bool sntp_set_timezone_in_seconds(int32_t timezone);

uint32_t sqrt32 (uint32_t n);
uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);
Expand Down
Loading