-3

Hi im trying to call functions from a string the microcontroller receives. To do this the micro controller find recieves and compares this to the names in the peripheral name list. It uses the index to get a pointer to a list of functions names available for that peripheral and then compares then function name to that list to get a functions index. These two indices are then used to call the functions.

The system works fine for 2 peripherals but then adding a third causes the program to stop running, sometimes it gives the warning stack pointer is out of range, other times it will just show no position when the program is paused. Adding a third peripheral to the peripheral_fnct_pntr_pntr list causes this to happen, it functions fine with all combinations of 2 peripherals and commenting the line will work aswell.

I made custom types for functions pointers as shown:

typedef void (*fp)(int); //Declares a type of a void function that accepts an int
typedef char (*p)[6][20]; //pointer to array 

the lists of funstions and pointers are created as shown:

    char peripheral_names[6][20] = {"rgb","button"};  //[no of strs][size of str]
    char rgb_fnct_names[6][20] = {"rgb_brightness","red_brightness","green_brightness","blue_brightness","rgb_onTime","rgb_breath"};
    char led1_fnct_names[6][20] = {"led1_brightness","led1_onTime","led1_breath"};
    char led2_fnct_names[6][20] = {"led2_brightness","led2_onTime","led2_breath"};

        //store pointers linking name to functiion index to function
    //rgb led functions pointers
    fp rgb_fnct_pntrs[] = {&rgb_brightness,&red_brightness,&green_brightness,&blue_brightness,&rgb_on_time,&rgb_breath};

    
    //led function pointers
    fp led1_fnct_pntrs[] = {&led1_brightness,&led1_on_time};
    fp led2_fnct_pntrs[] = {&led2_brightness,&led2_on_time};
    
    //store list of ponters to peripheral function pointer lists
    fp *perph_fnct_pntr_pntr[]  = {rgb_fnct_pntrs, led1_fnct_pntrs, led2_fnct_pntrs};


    
   //store pointers linking peripheral name index to list of function names
   p name_to_fnct_lst[3]= {&rgb_fnct_names,&led1_fnct_names,&led2_fnct_names};

Im not sure whats causing this problem so any help would be appreciated.

JAck
  • 15
  • 2
  • 5
    Please provide a [minimal reproducable example](https://stackoverflow.com/help/minimal-reproducible-example). – Devolus Feb 13 '21 at 21:30
  • The `led1_fnct_pntrs` don't match the `led1_fnct_names` ... and numerous other inconsistencies. – user3386109 Feb 13 '21 at 21:35
  • The function names list related to how there stored on a web server, so dont necessarly have to match the msp function names as long as the index is the same – JAck Feb 13 '21 at 22:11
  • Usually if you have a number in your variable names, that means that you really want an array with the number as the index. You might think that's just style sniping, but sometimes things like that hide more fundamental issues and things get clearer when they're fixed. – John Bayko Feb 13 '21 at 22:30
  • 1
    I would suggest that it is a bad idea to typedef pointer types, and even worse array types. Write the types out in full and I suspect the errors will become more obvious. – Tom V Feb 13 '21 at 22:50
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Sabito stands with Ukraine Feb 15 '21 at 01:12

1 Answers1

2

I'm guessing the way this works is, you start with a string, and find the matching string by iterating through name_to_fnct_lst[], then searching the arrays it points to (rgb_fnct_names, led1_fnct_names, led2_fnct_names). When there's a match, you have two indexes (function list index, and name within the list).

You use the first index to look up a function pointer list in perph_fnct_pntr_pntr, then the second to look up the index in rgb_fnct_pntrs, led1_fnct_pntrs, or led2_fnct_pntrs.

Which works in the rgb case, because both rgb_fnct_names and rgb_fnct_pntrs have 6 entries, but not for led1/2 because led1_fnct_names and led2_fnct_names have 3 entries, but led1_fnct_pntrs and led2_fnct_pntrs have 2 entries. Am I following correctly?

If so, it looks like the solution is to add the missing function pointers to led1_fnct_pntrs and led2_fnct_pntrs. Or it might simplify things to combine the arrays into a char[3][6][20] and a fp[3][6] array (no downside in the first case, you're allocating unused chars anyway, the second case loses a few unused pointer addresses which you gain back from smaller code).

Another way to structure it is:

struct f_nam_ptr {
    fp func;
    char name[20];
};
// Using a single map array here, or split it
// into rgb, led1, led2 maps.
struct f_nam_ptr f_nam_ptr_map[] = {
    { &rgb_brightness, "rgb_brightness" },
    // etc...
};

That ensures every name has a matching pointer (and doesn't waste as much memory).

John Bayko
  • 746
  • 4
  • 7