I'm looking for best practice programming and speed wise.
What I mean with a registry of variables is shown here:
More specific the functions uint16_t registers_read_word(...) and void registers_write_word(...)
I understand that this way of working makes sense in a way that all variables are in a fixed block of data, they can be accessed from everywhere in the code (inside other parallel functions).
The data type might be limited (2 bytes) but that seems to be ok.
I cannot estimate if this is slow or acceptable. And it's the only location I really find this use, the code is old and maybe outdated. Also interrupts are stopped and started to prevent for variables being updated by interrupts.
An in use example: OpenServo/pid.c at master · ginge/OpenServo · GitHub
What I mean with global variables is shown here:
Variables are made globally, pointers are passed on to the modules on initialisation.
Different data types can be used. I would guess this is fast, except the variable can be stored everywhere, on a slower to access location (if that excitsts)? Also in case of a lot of variables to be made available to a class the initialisation line will become overloaded.
What would be the best way to solve the problem where variables could be accessed and written to on different parts in the code? Maybe passing a pointer to a variable table but that is similar to the first code.
An example code that is up to date with todays practices would be nice.
Thanks for all replies in advance.
More specific the functions uint16_t registers_read_word(...) and void registers_write_word(...)
I understand that this way of working makes sense in a way that all variables are in a fixed block of data, they can be accessed from everywhere in the code (inside other parallel functions).
I don't know what you actually mean. These functions simply use a global array. The fact that the author named the array "registers" is not relevant.
The data type might be limited (2 bytes) but that seems to be ok.
What tells you there's such a limitation?
Variables are made globally, pointers are passed on to the modules on initialisation.
Different data types can be used. I would guess this is fast, except the variable can be stored everywhere, on a slower to access location (if that excitsts)? Also in case of a lot of variables to be made available to a class the initialisation line will become overloaded.
That code simply also uses global variables. Where exactly do you see a difference, except that the former variable is an array?
What would be the best way to solve the problem where variables could be accessed and written to on different parts in the code? Maybe passing a pointer to a variable table but that is similar to the first code.
The best way is to avoid global variables. But in embedded programming (like Arduino) memory considerations and speed often lead to the use of global variables. So a recommendation is difficult if you don't tell what type of program you have in mind.
Thanks for the detailed reply. I'm working on an updated version of the first linked code, and want to use the second linked PID library code in the update. Actually you make a good point that the code uses a globally declared table.
I was in the impression that centralising (global) variables (in a block) is somehow better. As to prevent fractioning the memory.
It's ok to get rid of the table and the functions to access them and directly use/access the variables declared globally then?
I was in the impression that centralising (global) variables (in a block) is somehow better. As to prevent fractioning the memory.
Please define what you think "centralising (global) variables" mean. You either have global or local variables but I never used the term "centralising" in this context. You fraction the memory if you allocate the space for the variables (p.e. by using the String class) but if you use the global or local variables directly the compiler is responsible to clean up memory at the end of the function (for local variables), global variables are reserved in a block at the program start.
It's ok to get rid of the table and the functions to access them and directly use/access the variables declared globally then?
That depends. In the linked case you cannot access the library global variable directly because the header file doesn't reference it. So at compile time the compiler doesn't know that the variable exist, publicly available are only the functions.
I didn't know that the location of variables was defined by the compilor, I guess I didn't expect it to be that clever.
You fraction the memory if you allocate the space for the variables (p.e. by using the String class) but ...
This is what I had in mind that was happening, that somehow the memory can run out or be less efficiënt. Luckily I don't use any Strings.
I now declared necessary 'global' variables on the needed locations. I know in the original code their focus was to be able to change every value (in that table) over Serial. I dropped this functionality and therefore only have a limited amount of global variables. Constant values will be changed by re-uploading code atm, some only need tuning and will never be changed anyway.