I got bored and made this .. Can you guess what it does or what it is useful for ?? Code: int CallFunction[10] {0,1,2,3,4,5,6,7,8,9}; int MaxFunctionLoopLength = 10; int FunctionToCall = 0; int LoopCounter = 0; void setup() { } void loop() { FunctionToCall=CallFunction[LoopCounter]; switch (FunctionToCall) { case 0: //Fuction Call List PrioritizeFuctions(); break; case 1: Function1(); break; case 2: Function2(); break; case 3: Function3(); break; case 4: Function4(); break; case 5: Function5(); break; case 6: Function6(); break; case 7: Function7(); break; case 8: Function8(); break; case 9: LoopCounter=0; break; } LoopCounter++; if(LoopCounter>MaxFunctionLoopLength){LoopCounter=0;} } void PrioritizeFuctions(){ // code goes here to modify the data in the "FunctionCall" array, this allows you to modify which functions are called and the order/priority they are run. } void Function1(){ // the main functions go here in these functions below } void Function2(){ } void Function3(){ } void Function4(){ } void Function5(){ } void Function6(){ } void Function7(){ } void Function8(){ }
PrioritizeFuctions? also case 9..... i don't think an int can be called like a function Also case 0 is only reachable the first time in the loop i think.
Good find And yes, it's for prioritizing functions. But mainly it's designed to be dynamic and scaleable so it can be done on the fly to adjust program efficiency on the cpu.. edit.. Case 0 can be called anytime FunctionCall contains a 0 in it's array. so if the array is changed from {0,1,2,3,4,5,6,7,8,9} to {0,1,8,0,9} Case 0 would be called twice before the loop is reset by case 9 LoopCounter isn't incremented until after the case check
It could also be used as a base for "Procedural Generation" By using an algorithm to dictate what is in the FunctionCall array ...
Why wouldn't you just use a function pointer array? Code: typedef (void (*)(void)) f_ptr_t; f_ptr_t functions[] = {prioritise, fun1, fun2 ...}; void loop() { for(int i=0;i[functions]();i=(i+1)%num_functions) ; }
I'm attempting to minimize on dynamic memory used by the variables, creating structs and larger arrays uses more. I have 2k of dynamic memory available on a ATmega328p and a limit of 30k for total storage of code. That array and code uses 35bytes of ram
Hmmmn , Your code makes "i" a private variable of the loop function so it can't be reset from inside another function. I wanted to keep the loop counter a global so it can be reset at anytime from any function. edit , i'll use same code but will bump i up to being a global variable .
And i'm not usng a 32bit system i'm coding on an ARM based 16mhz 8bit SoC with 3ok flash rom and 2k dymanic memory.
The IDE i have can't handle this kind of type def typedef (void (*)(void)) f_ptr_t; I'll try to find a way around it because im very curious to see the ram diff
I did that from memory, so it could be wrong. Essentially, the ram difference will be in the difference of size between int and int* I assume it's some variant of c you are using? Does it support pointers?
Yeah the Arduino supports pointers but there is very little write up on it, I will need to hunt down another sketch someones written and look at how pointers are defined/declared in the IDE i'm using.
Okay the correct syntax for function pointer typedef is as follows: Code: typedef void (*f_ptr_t) (void);
Hats off to you good sir This code below is only using 31 bytes of dynamic ram Code: int num_functions = 10; int i = 0; typedef void (*f_ptr_t) (void); f_ptr_t functions[] = {p, f1, f2, f3, f4, f5, f6, f7, f8}; void setup(){ } void loop() { i[functions](); i=(i+1)%num_functions; } void p(){ } void f1(){ } void f2(){ } void f3(){ } void f4(){ } void f5(){ } void f6(){ } void f7(){ } void f8(){ }
Nice! A general hint I suppose, when ever you see a switch that is essentially doing a one to one mapping of a range of ints, you can just use the array indices as the 'key' so to speak. One other thing to note, the following two lines are actually identical: Code: i[functions](); functions[i](); The second one is more idiomatic though, I was just being tricky xD.