Periodically saving text on ESP32 Flash Memory(e-Typewriter)

Thread Starter

Avi08

Joined Dec 29, 2021
23
Hi.
I am building an e-Typewriter using ESP32(Lilygo t-display-s3 long). It has 16MB of flash. My plan is to save multiple text files on that flash. I want to know if the following functionality is feasible for such a microcontroller.

I want to save the texts files as they are being written( as frequently as possible).

The problem I am encountering is with the implementation using FatFS or any other file system. As far as my understanding goes one cannot insert data in the middle of a file. If that needs to be done I'd have to store the entire text, which follows the position where I am inserting something, in a buffer, delete that text from the file and then add it again after the insertion is complete. With constant saving ON this process would happen multiple times for a single editing session. That seems very unproductive given the limited number of write cycles the flash has? Is there a way to achieve this? Another idea I had was to work on the entire file in a RAM buffer and save it to the flash at the end of editing but that is not exactly what I want. Any help or advice would be appreciated.
 
Last edited:

Ya’akov

Joined Jan 27, 2019
10,226
Hi.
I am building an e-Typewriter using ESP32(Lilygo t-display-s3 long). It has 16MB of flash. My plan is to save multiple text files on that flash. I want to know if the following functionality is feasible for such a microcontroller.

I want to save the texts files as they are being written( as frequently as possible).

The problem I am encountering is with the implementation using FatFS or any other file system. As far as my understanding goes one cannot insert data in the middle of a file. If that needs to be done I'd have to store the entire text, which follows the position where I am inserting something, in a buffer, delete that text from the file and then add it again after the insertion is complete. With constant saving ON this process would happen multiple times for a single editing session. That seems very unproductive given the limited number of write cycles the flash has? Is there a way to achieve this? Another idea I had was to work on the entire file in a RAM buffer and save it to the flash at the end of editing but that is not exactly what I want. Any help or advice would be appreciated.
Welcome to AAC.

Your requirement to save constantly is at odds with the storage you are using. There is no way to cheat the limited write cycles of the flash, and even if you could “insert something in the middle of a file” you would be doing the same number of writes, and have a fragmented file system that would hamper performance.

Writing to a buffer in RAM is probably the best optimization you have available. You could use a little logic to use time and buffer size to determine when to write, and get some of your desired functionality. For example you could choose to expire the buffer after x seconds, or after y bytes—whichever comes first.

This way, you reduce the risk of losing data while managing the write cycles of the flash. You could tune this anyway you want. Alternatively, you could treat the ESP32 as a peripheral to a conventional computer and write your editor for that, committing to the ESP32 flash only after the user chooses to save.

I realize the last suggestion could be at odds with other requirements you have, but in the absence of more information, I offered it. Good luck with your project.
 
Top