Answer:
Complete question is:
When the C-language code is generated, -1 is used to create the initialization state for each of the Tick
functions.
The challenge is if this code is used with C++ compiler, there is an incorrect mix between integer and
the enum data types.
1. Take the generated C code, edit it, and
replace the -1 state with an enum data type state
so it will
compile properly.
2. Replace the integer notation for the call and return arguments for Tick functions where they should
also be the enum data types.
2. From the listing lines of code, please give the line numbers for code which are concerned with the
following:
A)
Main function initialization of values
B)
Main function idle loop
C)
Main function schedule loop has already been moved to the ISR, but give those numbers too.
This action is required when moving this state machine model to a C++ GUI system.
Submit the C code with the corrections made
and this is my code
/*
This code was automatically generated using the Riverside-Irvine State machine Builder tool
Version 2.8 --- 6/23/2015 16:19:15 PST
*/
#include "rims.h"
/*This code will be shared between state machines.*/
unsigned char TimerFlag = 0;
void TimerISR() {
TimerFlag = 1;
}
enum BL_States { BL_LedOff, BL_LedOn } BL_State;
TickFct_BlinkLeds() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here. No functions; make them global.*/
switch(BL_State) { // Transitions
case -1:
BL_State = BL_LedOff;
break;
case BL_LedOff:
if (1) {
BL_State = BL_LedOn;
}
break;
case BL_LedOn:
if (1) {
BL_State = BL_LedOff;
}
break;
default:
BL_State = BL_LedOff;
} // Transitions
switch(BL_State) { // State actions
case BL_LedOff:
B0= 0;
break;
case BL_LedOn:
B0 = 1;
break;
default: // ADD default behaviour below
break;
} // State actions
}
enum TL_States { TL_T0, TL_T1, TL_T2 } TL_State;
TickFct_ThreeLeds() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here. No functions; make them global.*/
switch(TL_State) { // Transitions
case -1:
TL_State = TL_T0;
break;
case TL_T0:
if (1) {
TL_State = TL_T1;
}
break;
case TL_T1:
if (1) {
TL_State = TL_T2;
}
break;
case TL_T2:
if (1) {
TL_State = TL_T0;
}
break;
default:
TL_State = TL_T0;
} // Transitions
switch(TL_State) { // State actions
case TL_T0:
B5 = 1;
B6 = 0;
B7 = 0;
break;
case TL_T1:
B5 = 0;
B6 = 1;
B7 = 0;
break;
case TL_T2:
B5 = 0;
B5 = 0;
B5 = 1;
break;
default: // ADD default behaviour below
break;
} // State actions
}
int main() {
B = 0; //Init outputs
TimerSet(1000);
TimerOn();
BL_State = -1;
TL_State = -1;
while(1) {
TickFct_BlinkLeds();
TickFct_ThreeLeds();
while (!TimerFlag);
TimerFlag = 0;
}
}
Answer is:
#include "rims.h"
/*This code will be shared between state machines.*/
unsigned char TimerFlag = 0;
void TimerISR() {
TimerFlag = 1;
}
enum BL_States { BL_LedOff, BL_LedOn, BL_LedOnOff } BL_State;
TickFct_BlinkLeds() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here. No functions; make them global.*/
switch(BL_State) { // Transitions
case BL_LedOnOff:
BL_State = BL_LedOff;
break;
case BL_LedOff:
if (BL_LedOn) {
BL_State = BL_LedOn;
}
break;
case BL_LedOn:
if (true) {
BL_State = BL_LedOff;
}
break;
default:
BL_State = BL_LedOff;
} // Transitions
switch(BL_State) { // State actions
case BL_LedOff:
B0= 0;
break;
case BL_LedOn:
B0 = 1;
break;
default: // ADD default behaviour below
break;
} // State actions
}
enum TL_States { TL_T0, TL_T1, TL_T2 } TL_State;
TickFct_ThreeLeds() {
/*VARIABLES MUST BE DECLARED STATIC*/
/*e.g., static int x = 0;*/
/*Define user variables for this state machine here. No functions; make them global.*/
switch(TL_State) { // Transitions
case BL_LedOnOff:
TL_State = TL_T0;
break;
case TL_T0:
if (1) {
TL_State = TL_T1;
}
break;
case TL_T1:
if (true) {
TL_State = TL_T2;
}
break;
case TL_T2:
if (true) {
TL_State = TL_T0;
}
break;
default:
TL_State = TL_T0;
} // Transitions
switch(TL_State) { // State actions
case TL_T0:
B5 = 1;
B6 = 0;
B7 = 0;
break;
case TL_T1:
B5 = 0;
B6 = 1;
B7 = 0;
break;
case TL_T2:
B5 = 0;
B5 = 0;
B5 = 1;
break;
default: // ADD default behaviour below
break;
} // State actions
}
int main() {
B = 0; //Init outputs
TimerSet(1000);
TimerOn();
BL_State = -1;
TL_State = -1;
while(1) {
TickFct_BlinkLeds();
TickFct_ThreeLeds();
while (!TimerFlag);
TimerFlag = 0;
}
}
Explanation: