Storing and retriving a linked list from eeprom

Hi everyone,

I' am making a project with an Arduino (for now I'am using the nano but maybe I will use an UNO in the future) as a challenge and as a final project for high school. I am trying to build a "PC" with arduino :sweat_smile: :rofl:. I want to create something similar to TinyBasicPlus but I'am developing the project in my way.
So far everything is going great, I can write the command in the Serial monitor, the arduino will store it in a linked list an then execute it.

This is an example of a line of my "pseudo-code": 10 PRINT "HELLO WORLD".

The first number is the line number (each line will be executed according to it's line number), the other part of the string is the instruction that will be executed.

typedef struct {
  short int lineNumber;
  char instruction[100];
} line_t;

typedef struct node {
  line_t line;
  node* next;
} node;

This is the node of the string.

Now I need to store this linked list containing every line of my code in the EEPROM.
My goals are:

  • save one or more program (so one or more list) on the eeprom
  • retrieve the list from the eeprom
  • modify the list, and save it againg

I am considering my alternatives.

I could merge the list in a long string, this is the easyest method but then i will have trouble when i need to edit the list and maybe add some line of my pseudo-code. I don't want to rewrite all the eeprom again.

So i would like to keep the list structure. I was thinking about adding a node at the beginning of my program list containing it's name, a pointer to the first element of the list and a pointer to another list. Some kind of list of list.

So something like this:
"first program", *firstProgramPointer , *nextProgram

Mantaining the list structure would also be helpfull to add some node to the list without having to rebuild the entire eeprom structure, i could create an overflow zone at the end and put there every new node.

Is this a good approach? I don't have much expirience with classic database...

I hope I was clear. Thanks for reading.

If you don't have an issue with additional hardware you could keep the list as a file on external flash or a SD module, when ever you modified the list/file then you just need to write an updated copy to the memory bank and it would be a little faster to access also.

if you assign 104 bytes for each line, your EEPROM will manage at most 9 lines of basic (your EEPROM size is 1024 bytes). That's going to be a short program...

Could you consider changing the board and using an ESP32 ?

If so, then you could use the LittleFS library to create an on chip file system to hold the data

Yes, you are right :joy:. When I wrote it was thinking about getting it to work saving was not in my mind yet. At the end the line would hardly be longer then 30 char, so I will definitely decrease it

I would prefer using the nano or the uno because I already have these board. Thanks for the answer

Yes, i could use it and I definitely will. I recently built a sd card reader and it's working well. I wanted to use the eeprom to have a little storage to keep something. For example an autorun programs. But in the end I could use the SD for everything. I' ve already worked with file so this shouldn't be difficult. I was just curious about using the eeprom but if it's impossible or too much work I can easily skip this

you might also want to do a bit of parsing before storing so that you could encode all the language tokens like PRINT GOTO etc in a non ASCII form. will save space for storing and also at run time interpreting the command.

You are right, I will definitely do that. I have an array stored in PROGMEM continuing all the token of the language and than a function that at runtime decode them in a number to be used in a switch. I can add to the struct line_t a short int and do this parsing when I insert the instructions. It's easy to do and the function that execute these instructions shouldn't have problem

may be even a byte would do. 255 tokens (and keep 0 as a separator)

In this way I will save some space, moreover the code is already done, I just need to parse the token when I store them and not at runtime.
Now I just need to figure how to put a list in the eeprom.
Then as @sumguy said I will also add an SD card, so i can have a more space and it could also be plugged to a pc to write some program on in quicker

Since everything is fixed size why do you bother with a linked list? (And loosing pointer memory space)

Create a large (whatever fits) fixed size array of lines and you can just put() and get() that array in one go using the eeprom class

I would prefer keeping the dynamic structure because i don't want to fill all the ram with a huge array. I read on the forum that put() and gets() works with every data type. What if i save on the eeprom the struct line_t? I could leave the first part of the eeprom free so i could put new line of an existing program there. I will leave an empty byte at the end of every program so i can put the address for the overflow zone (i would use the first eeprom address as overflow zone so i can locate them with only one byte)

that would work too, you can walk through the linked list and save them one by one

you'll end up there with your linked list anyway when the program grows so you need to have space for that.

having an array saves the pointers in your linked list and the possibility of holes in the heap that would consume memory for nothing if you dynamically allocate and deallocate lines. the linked list makes inserting lines in between two lines easier (but a sort on the line number to reorder the array would not be too difficult either)

have you written your Basic interpreter yet? how much RAM does it requires? (have you seen BASIC on Arduinos | Arduino Project Hub ?)

Yes, i've wrote an interpreter, i am quite happy with the result. I decided to write that on my own instead of using an exisiting one because I wanted to be sure to understand how it works.
The one you linked is really cool but is also a lot more complex. I don't know exactly how much RAM it needs, it uses an array of 26 int as the variables (given that the variables must be named as an char we can't have more than 26 of them) so it should be 104byte. Then there is the list... It will also need some memory for the operation but is should be freed when it goes to the next line. The array with the single token (as PRINT) is stored in PROGMEM so it shouldn't take RAM

cool. hope this will work for you

Thanks, i'll let you know how this turns out. Now I will think about the EEPROM. I will either use an array or give up and go to the SD card and store something else on the EEPROM, maybe some configuration. Thanks for your suggestions.

you could also use FRAM instead of the SD card ➜ plenty of space for many programs

Cool, I didn't know about this technology. I will read something more, it's interesting

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.