FSST - Hardwarenahe Programmierung

STM Implementierung mit Funktionen

C++ Seite

Task Hardware Config

DigitalOut Led1(LED1);
DigitalOut Led2(LED2);
DigitalOut Led3(LED3);
DigitalOut Led4(LED4);
InterruptIn  SW1(p14);

States

//typedef enum {ST_EIN, ST_AUS, ST_STATE2, ST_STATE3} nextState;
enum nextState {ST_EIN=0, ST_AUS, ST_STATE2, ST_STATE3};
nextState state;
// alternativ
//const int ST_EIN = 0;
//const int ST_AUS = 1;

//uint8_t state = ST_AUS;

Transitions

bool pressed = false;

void fall(void) { 
    wait_ms(20);
    pressed = true; 
}

bool CheckFlag()
{
    if( pressed ) { 
        pressed=false; 
        return true; 
    }
    return false;
}

State Functions

void ST_Ein(){
    // entry
    // do
    while(true) {
        Led1 = 1;
        if(CheckFlag()) {
            state = ST_AUS;
            // exit
            return;
        }
    }
}

void ST_Aus(void){
    while(true) {
        Led1 = 0;
        if(CheckFlag()) {
            state = ST_EIN;
            return;
        }
    }
}

void ST_Error(){
    while(1) {
        Led3 = !Led3;
        wait_ms(200);
    }
}

State Machine

void stateMachine()
{
    switch (state)
    {
    case ST_EIN: ST_Ein();
        break;
    case ST_AUS: ST_Aus();
        break;
    default: ST_Error();
        break;
    }
}

Main

int main()
{
    printf("Hello STM\n");
    SW1.fall(&fall); //.rise(&rise);
    while(1) {
        stateMachine(); 
    }
}

Erweiterung Timeout


All wikipages