dstroy0 / InputHandler

Arduino input handler
https://dstroy0.github.io/InputHandler/
GNU General Public License v3.0
1 stars 0 forks source link

reduce pgm size by consolidating string literals #67

Open dstroy0 opened 1 year ago

dstroy0 commented 1 year ago

I will start with canned error messages.

dstroy0 commented 1 year ago

This is going well but they need to be split into their own namespace. I'm going to try putting lib specific namespaces into their own file, and nesting the lib specific namespaces inside of the class to reduce code length.

2bndy5 commented 1 year ago

I recently wrote Arduino wrappers for Linux and used a lib-specific namespace (which is long winded). So, I ended up doing:

// in common.h

#ifdef ARDUINO
    // use a blank macro to keep the lib's core code agnostic of underlying implementation
    #define PINNACLE_ARDUINO_API
#else
    #include "utility/linux_kernel/spi.h" // defines the namespace
    #include "utility/linux_kernel/i2c.h" // defines the namespace
    #include "utility/linux_kernel/gpio.h" // defines the namespace
    #include "utility/linux_kernel/time_keeping.h" // defines the namespace

    #define PINNACLE_ARDUINO_API using namespace cirque_pinnacle_arduino_wrappers;
#endif

Then in my lib's core code:

#include "common.h"

void PinnacleTouch::begin()
{
    // pull in the namespace if defined
    PINNACLE_ARDUINO_API
    spi = &SPI; // resolves to `cirque_pinnacle_arduino_wrappers::SPI` if !defined(ARDUINO)
}
otherwise it looked like ifdef soup ```cpp #include "common.h" void PinnacleTouch::begin() { #ifndef ARDUINO using namespace cirque_pinnacle_arduino_wrappers; #endif spi = &SPI; // resolves to `cirque_pinnacle_arduino_wrappers::SPI` if !defined(ARDUINO) } ```

This is so the using namespace statement doesn't alter the namespaces in user code. Rather, the namespace is scoped to the definition of the function that has PINNACLE_ARDUINO_API in it.

dstroy0 commented 1 year ago

This is how I'll implement it after everything is moved around.

dstroy0 commented 1 year ago

https://github.com/dstroy0/InputHandler/blob/main/src/config/utility/namespace.h#L628

The next step is to get rid of the preallocated fixed string widths where possible to further reduce program space. I'll be using strnlen_P, and use a buffer one char larger than that terminated with '\0' if for some reason the char array isn't already. Then the prealloc defines become MAX instead of allocating all that space. The most savings will come from doing this for commandString, which is 32 char fixed right now when most commands that I've been using are 8 char in length or less.

dstroy0 commented 1 year ago

I also have to add the "non-verbose" terminal output format strings and error messages.

2bndy5 commented 1 year ago

I also have to add the "non-verbose" terminal output format strings and error messages.

I assume this isn't a reference to the ASCII control sequences because the Arduino IDE Serial Monitor doesn't support them. This might be interesting if ported to Linux, but most Linux distros ship with ncurses.