Click here to Skip to main content
15,887,376 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
PraiseRe: Error while using static int for incremental record. Pin
CPallini10-Mar-18 11:27
mveCPallini10-Mar-18 11:27 
Questiontwo errors Pin
Member 137186819-Mar-18 21:24
Member 137186819-Mar-18 21:24 
AnswerRe: two errors Pin
Victor Nijegorodov9-Mar-18 21:44
Victor Nijegorodov9-Mar-18 21:44 
Questionhow to get a string as input ? Pin
Tarun Jha9-Mar-18 16:45
Tarun Jha9-Mar-18 16:45 
AnswerRe: how to get a string as input ? Pin
Sampath5799-Mar-18 20:15
Sampath5799-Mar-18 20:15 
QuestionRe: how to get a string as input ? Pin
David Crow10-Mar-18 11:07
David Crow10-Mar-18 11:07 
AnswerRe: how to get a string as input ? Pin
Tarun Jha15-Mar-18 7:50
Tarun Jha15-Mar-18 7:50 
QuestionHow can I minimize memory usage in my table-based statemachine with several hundred states? Pin
arnold_w9-Mar-18 9:02
arnold_w9-Mar-18 9:02 
I am working with an ARM microcontroller and I have kind of a statemachine with several hundred states. In order to be able to overview it, I have written the code so that it has a 1:1 correspondence with a flowchart specification. I have removed irrelevant code, but basically the code works like this:
Bool_t (*returnTrueOrFalseFunctionPtr) (void);
void (*actionFunctionPtr) (int);

struct myStruct_s {
    returnTrueOrFalseFunctionPtr   trueOrFalse;
    struct myStruct_s*             whereToGoIfTrue;
    struct myStruct_s*             whereToGoIfFalse;
    actionFunctionPtr              actionsToPerformInThisState[5];
    int                            actionArguments[5];
};

static struct myStruct_s myStateMachine1[];
static struct myStruct_s myStateMachine2[];
static struct myStruct_s myStateMachine3[];
static struct myStruct_s myStateMachine4[];

define myStateMachine1 ms1
define myStateMachine2 ms2
define myStateMachine3 ms3
define myStateMachine4 ms4
static struct myStruct_s myStateMachine1[] = {
    {evaluateSomething,         &ms1[1], &ms1[3], {startMotor,increaseSpeed,   NULL,         NULL, NULL, 3,5, 0, 0,0},
        {evaluateSomething,     &ms1[2], &ms2[0], {stopMotor, openExhaust,     decreaseSpeed,NULL, NULL, 1,20,13,0,0},
            {evaluateSomething, &ms1[0], &ms1[2], {openRelay, writeStatusToLog,increaseSpeed,NULL, NULL, 4,5, 87,0,0},
        {evaluateSomething,     &ms1[4], &ms2[1], {blah blah blah... },
            {evaluateSomething, &ms2[1], &ms2[2], {blah blah blah... },
};

struct myStruct_s myStateMachine2[] = {
    {evaluateSomething,         &ms2[1], &ms2[3], { blah blah blah... },
        {evaluateSomething,     &ms2[2], &ms3[0], { blah blah blah... },
            {evaluateSomething, &ms3[1], &ms3[3], { blah blah blah... },
        {evaluateSomething,     &ms2[4], &ms4[1], { blah blah blah... },
            {evaluateSomething, &ms4[0], &ms2[2], { blah blah blah... },
};

I am very happy with this code format, it make it very easy for me to follow where I am in the statemachine/flowchart and decide where to go next. My problem is that I need to port this code to a microcontroller that has little memory, so I would like to memory-optimize the code heavily. I have already replaced my action function pointers with an enum instead:
enum actionsToPerformInThisState_e {
    startMotor_,
    stopMotor_,
    openRelay_,
    openExhaust_,
    writeStatusToLog_,
    decreaseSpeed_,
    increaseSpeed_,
    NULL_
};

struct myStruct_s {
    returnTrueOrFalseFunctionPtr   trueOrFalse;
    struct myStruct_s*             whereToGoIfTrue;
    struct myStruct_s*             whereToGoIfFalse;
    actionsToPerformInThisState_e  actionToPerformInThisState0 : 3;
    actionsToPerformInThisState_e  actionToPerformInThisState1 : 3;
    actionsToPerformInThisState_e  actionToPerformInThisState2 : 3;
    actionsToPerformInThisState_e  actionToPerformInThisState3 : 3;
    actionsToPerformInThisState_e  actionToPerformInThisState4 : 3;
    int                            actionArguments[5];
};

What else can I do to reduce memory usage? Since most states don't use all 5 actions, I would like to have variable number of actions, but the elements in the array must be of same size, or can anybody think of a workaround (I can't place the actions outside the array and have a pointer to them because then the nice readability is lost). What about all pointers (each occupy a whopping 4 bytes!!!) to next states, can anybody think of another way to accomplish this linking? Other suggestions?

-- modified 9-Mar-18 15:17pm.
GeneralRe: How can I minimize memory usage in my table-based statemachine with several hundred states? Pin
harold aptroot9-Mar-18 9:54
harold aptroot9-Mar-18 9:54 
GeneralRe: How can I minimize memory usage in my table-based statemachine with several hundred states? Pin
arnold_w9-Mar-18 10:00
arnold_w9-Mar-18 10:00 
GeneralRe: How can I minimize memory usage in my table-based statemachine with several hundred states? Pin
harold aptroot9-Mar-18 10:14
harold aptroot9-Mar-18 10:14 
AnswerRe: How can I minimize memory usage in my table-based statemachine with several hundred states? Pin
leon de boer9-Mar-18 15:16
leon de boer9-Mar-18 15:16 
QuestionHow to get object of a view which is created using AddView(). Pin
Sampath5799-Mar-18 0:38
Sampath5799-Mar-18 0:38 
AnswerRe: How to get object of a view which is created using AddView(). Pin
Victor Nijegorodov9-Mar-18 1:45
Victor Nijegorodov9-Mar-18 1:45 
GeneralRe: How to get object of a view which is created using AddView(). Pin
Sampath57910-Mar-18 23:20
Sampath57910-Mar-18 23:20 
GeneralRe: How to get object of a view which is created using AddView(). Pin
Victor Nijegorodov11-Mar-18 2:05
Victor Nijegorodov11-Mar-18 2:05 
AnswerRe: How to get object of a view which is created using AddView(). Pin
Richard MacCutchan9-Mar-18 5:18
mveRichard MacCutchan9-Mar-18 5:18 
GeneralRe: How to get object of a view which is created using AddView(). Pin
Sampath57911-Mar-18 6:04
Sampath57911-Mar-18 6:04 
GeneralRe: How to get object of a view which is created using AddView(). Pin
Victor Nijegorodov11-Mar-18 7:12
Victor Nijegorodov11-Mar-18 7:12 
GeneralRe: How to get object of a view which is created using AddView(). Pin
Richard MacCutchan11-Mar-18 22:32
mveRichard MacCutchan11-Mar-18 22:32 
QuestionHow to release IDispatch memory. Pin
Sampath5797-Mar-18 2:45
Sampath5797-Mar-18 2:45 
AnswerRe: How to release IDispatch memory. Pin
Jochen Arndt7-Mar-18 3:10
professionalJochen Arndt7-Mar-18 3:10 
GeneralRe: How to release IDispatch memory. Pin
Sampath5798-Mar-18 18:40
Sampath5798-Mar-18 18:40 
GeneralRe: How to release IDispatch memory. Pin
Jochen Arndt8-Mar-18 21:21
professionalJochen Arndt8-Mar-18 21:21 
GeneralRe: How to release IDispatch memory. Pin
Sampath5798-Mar-18 22:27
Sampath5798-Mar-18 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.