30 likes | 111 Views
main (). int main(void) { unsigned int buttons, dir, mode, sm_code , step_delay ; system_init (); /* Initialize Cerebot MX7ck */ while(1) /* Infinite application loop */ { buttons= read_buttons (); decode_buttons (buttons, & step_delay , &dir , &mode);
E N D
main () intmain(void) { unsigned int buttons, dir, mode, sm_code, step_delay; system_init(); /* Initialize Cerebot MX7ck */ while(1) /* Infinite application loop */ { buttons=read_buttons(); decode_buttons(buttons, &step_delay, &dir, &mode); sm_code=sw_fsm(dir, mode); output_sm_code(sm_code); sw_msDelay(step_delay); } return(1); } /* End of main */ Use the address operator (&) to pass the address of the variable to a function!!
decode_buttons() void decode_buttons(unsigned int buttons, unsigned int *step_delay, unsigned int *dir, unsigned int *mode) { switch (buttons) { case BTN1 : *dir = CW; *mode = HS; *step_delay = ####; break; [...] } // case } // decode_buttons step_delay, dir,and mode are pointer variables local to decode_buttons
sw_fsm() unsigned intsw_fsm(unsigned intdir, unsigned int mode) { enum {S0=0, S0_5, S1, S1_5, S2, S2_5, S3, S3_5}; static unsigned intpstate; const unsigned intsm_code[] = {0x02, 0x0A, [...] }; switch (pstate) // Next State Logic { case S0: if (dir==CW) { if (mode == HS) pstate= S0_5; else // FS pstate = S1; } else // CCW [...] break; [...] No magic numbers! Return sm_code(pstate)