Skip to content
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

Printf formatters (String and Int types) #19

Merged
merged 4 commits into from
Mar 29, 2020
Merged

Printf formatters (String and Int types) #19

merged 4 commits into from
Mar 29, 2020

Conversation

nullstalgia
Copy link
Contributor

@nullstalgia nullstalgia commented Mar 26, 2020

Basically, strftime but printf!

So you can duplicate the value (%s %s %s | %d %d %d)

Write a number in Hex (%X)

Add a prefix/suffic (John: %s!!!)

Don't use stack static stack memory for the client ID.  async-mqtt-client doesn't make a copy.
@sidoh
Copy link
Owner

sidoh commented Mar 27, 2020

Love it! Gonna take a closer look this evening.

@nullstalgia
Copy link
Contributor Author

I tried to do a float type instead of int, but no matter what number I gave it, it always gave me crap random values. Even using a plain atol() with c_str didn't work. (But it turns out that's how String's toFloat() works. And even that screwed up.)

If you can figure that part out, that would likely be preferred by both of us. :)

@sidoh
Copy link
Owner

sidoh commented Mar 29, 2020

I think printf with floats is disabled by default in the SDK. Adding these build flags is supposed to enable it:

  -Wl,-u,_printf_float,-u,_scanf_float

but it generates a linker error:

/Users/cmullins/.platformio/packages/framework-arduinoespressif32@2.10002.190628/tools/sdk/lib/libc_nano.a(lib_a-nano-vfprintf_float.o):(.literal+0x18): undefined reference to `_printf_common'
/Users/cmullins/.platformio/packages/framework-arduinoespressif32@2.10002.190628/tools/sdk/lib/libc_nano.a(lib_a-nano-vfprintf_float.o): In function `_printf_float':
/Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/nano-vfprintf_float.c:287: undefined reference to `_printf_common'

which I think probably means it isn't implemented in the ESP32 SDK. there's likely a workaround, but nothing springs to mind immediately.

Gonna push a patch to use snprintf instead of sprintf to avoid a potential overflow. Otherwise, think this is ready to go.

Thanks again -- great idea!

@nullstalgia
Copy link
Contributor Author

nullstalgia commented Mar 29, 2020

Shame with the float thing. Luckily, we can send strings that are preformatted with whatever we need! :)


I didn't know that existed! Good idea.

Merge when ready!

@sidoh sidoh merged commit df1f026 into sidoh:master Mar 29, 2020
@sidoh
Copy link
Owner

sidoh commented Mar 29, 2020

I'll tag a release with your changes shortly. :)

@nullstalgia nullstalgia deleted the printf_formatters branch March 29, 2020 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants