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 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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 } |