Well this weekend turned out wonderful. I manage to finish this little piece of and this solution saved me a HUGE mount of heap space. This function I wrote to cycle through flash memory and format SprintF on the fly from Flash memory instead of having to double buffer the formatted HTML page. In my case my HTML page is around 4-8k depending on what I am loading, thats over half the ESP’s heapspace just to send results to the client, YIKES! Now those day’s are over 🙂

uint16 flashsprintf(char * dest, uint32_t address, char * Terminator, int n, ...)
{
    int i = 0;
    int val;
    int sizeofTerminator = os_strlen(Terminator);

    va_list ap;
    va_start(ap, n);

    uint16 destindex = 0;
    uint16 addressindex = 0;
    char buffer[4];
    bool percentflag = false; //This Flag is used for if the last char is a %
    while(true)
    {
        system_soft_wdt_feed();
        if (spi_flash_read(address+(addressindex*4), buffer, 4) != 0)
        {
            os_printf("Read failed at offset=0x%02x index=%d\r\n", address, index);
            return 0;
        }
        os_printf("string: %d - %c%c%c%c\r\n", (addressindex*4), *(buffer), *(buffer+1), *(buffer+2), *(buffer+3));
        addressindex++;
        for (i=0; i<=3; i++)
        {   
            if (percentflag)
            {
                switch(buffer[i]) 
                {
                    case 's'  :
                    val=va_arg(ap,char *);
                    destindex += os_sprintf(dest+destindex, "%s", val);
                    break; /* optional */

                    case 'd'  :
                    val=va_arg(ap,int);
                    destindex += os_sprintf(dest+destindex, "%d", val);
                    break; /* optional */

                    /* you can have any number of case statements */
                    default : /* Optional */
                    os_printf("Unknown ");

                }
                os_printf("Removing %%");
                percentflag = false;
                continue;
            }

            if (buffer[i] == '%')
            {
                os_printf("Setting %%");
                percentflag = true;
                continue;
            }
            else
            {
                *(dest+destindex)=buffer[i];
                destindex++;
                if (os_strncmp((dest+destindex-sizeofTerminator), Terminator, sizeofTerminator) == 0)
                {
                    *(dest+destindex)=0x00;
                    os_printf("Exiting");
                    return destindex;
                }
            }
        }
    }

    os_printf("done");
    va_end(ap);
    return destindex; //Returns SizeOf Resource
}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax