Using the F macro in the presentation function



  • Since version 2.2 it has become possible to use the F( ) macro in a few more places. For example this would give an error in 2.1:

    void presentation()
    {
      sendSketchInfo(F("SMS receiver"), F("1.6"));
    }
    

    Arduino's compiler says this saves a bit of memory. Does it? Or is all the memory returned anyway once the presentation function is done?

    If not, then it would rock if it also becomes possible to use the macro in the present lines. This currently gives an error:

      present(SMS_CHILD_ID, S_INFO, F("received sms"));
    

    Before I make a feature request I thought I'd ask here if this would actually save memory or not.


  • Mod

    @alowhum yes it saves ram (but not flash, to be clear - the term memory is often used for flash as well)

    When a sketch is executed on the microcontroller, all variables are normally loaded into ram, "locking" them there. The F macro prevents this, and only loads the text to ram temporarily when needed.

    More information: https://www.arduino.cc/reference/en/language/variables/utilities/progmem/



  • @mfalkvidd thanks, I understand that. That's why I like using F( ) as much as possible, to save ram.

    That's why I would like to use it on the child descriptions too.

    But I suspected that the creators of MySensors to have already thought about this problem, and that I was just missing the obvious because of my limited knowledge. All I could think up though, was that the ram used by the child description strings is released.

    But I also remember how people don't like strings. And as far as I understand these child descriptions would be turned into strings at runtime? So they could (still) have a memory fragmenting effect?


  • Mod

    @alowhum when using the F macro, I think the (temporary) ram for the string will be allocated on the stack, so there will be no fragmentation. When not using the F macro, the string will be placed permanently in ram, so there will be no fragmentation.

    It could be that using the F macro for presentation causes problems inside the library, but it could also be as simple as nobody thought of adding support (or had enough time/energy to do it).



  • I thought the F( ) macro told Arduino to not load the variable into ram permanently? That it would just re-load it from the flash whenever it needed it?


  • Mod

    @alowhum yes that is correct


 

355
Online

7.9k
Users

8.7k
Topics

93.6k
Posts