For the Nerds and Programmers...

Discussion in 'Unrelated Discussion' started by maxpowerz, July 9, 2015.

  1. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    I got bored and made this ..
    Can you guess what it does :D 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(){
    }
    
    
    Last edited: July 9, 2015
  2. roadkillgrill

    roadkillgrill Active Member

    Messages:
    230
    Likes Received:
    14
    PrioritizeFuctions?

    also case 9..... i don't think an int can be called like a function :p

    Also case 0 is only reachable the first time in the loop i think.
    maxpowerz likes this.
  3. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    Good find :D

    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
    Last edited: July 9, 2015
  4. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    had to edit last post a million times .. lol
    I should hire someone to type for me :(
  5. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    It could also be used as a base for "Procedural Generation"
    By using an algorithm to dictate what is in the FunctionCall array ...
  6. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    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) ;
    }
    
    Last edited: July 9, 2015
  7. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    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 :D
  8. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    But sizeof(int) == sizeof(int*) if you are in a 32 bit system right?
  9. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    I will compare the 2 ways of doing to see what the ram difference is.
  10. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    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 .
  11. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    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.
  12. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    That is fine. You should probably test for the sentinel value in the loop as well.
  13. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    I'm limited to 16bit variable at the most
  14. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    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 :D
  15. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    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?
    maxpowerz likes this.
  16. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    it's c and c++
    I have been learning it for the last few months to write sketches in the Arduino IDE.
  17. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    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.
  18. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    Okay the correct syntax for function pointer typedef is as follows:
    Code:
    typedef void (*f_ptr_t) (void);
    maxpowerz likes this.
  19. maxpowerz

    maxpowerz Post Master General

    Messages:
    2,208
    Likes Received:
    885
    Hats off to you good sir :D

    This code below is only using 31 bytes of dynamic ram :D
    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(){
    }
    
    dom314 likes this.
  20. dom314

    dom314 Post Master General

    Messages:
    896
    Likes Received:
    1,196
    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.
    maxpowerz likes this.

Share This Page