State Maschine Übungen von fPucher https://os.mbed.com/users/fpucher/code/HIM0Board/wiki/STM-Schalter

Dependencies:   mbed C12832

main.cpp

Committer:
kunphil
Date:
2018-11-15
Revision:
2:7dafc3e72a72
Parent:
1:fdfc2454217b
Child:
3:b5c28432ee25

File content as of revision 2:7dafc3e72a72:

#include "mbed.h"
#include "C12832.h"

DigitalOut Led1(LED1, 0);
DigitalOut Led2(LED2, 0);
DigitalOut Led3(LED3, 0);
DigitalOut Led4(LED4, 0);
InterruptIn  SW1(p14);
InterruptIn  SW2(p15); 
InterruptIn  SW3(p12); 
InterruptIn  SW4(p16); 

Ticker T1;
Timer T2;
volatile float timerValue = 0;
volatile int blinkCount = 0; 

//************** LCD ****************

C12832 lcd(p5, p7, p6, p8, p11);

//************* VARIABLEN ****************

enum State {ST_AUS=0, ST_EIN, ST_TIMEOUT};
State state;

bool pressed1 = false, pressed2 = false;

//************* andere Funktionen ****************

void blink()
{
    blinkCount++;
    
    Led4 = !Led4;
}


//************* EREIGNISSE ****************
 
void rise1(void)
{  
    wait_ms(100); //Entprellung
    pressed1 = true;
    
    timerValue = T2.read();
    T2.reset();
    T2.start();
}

bool CheckFlag1() 
{
    if (pressed1) {
        pressed1=false;
        return true;
        }
    return false;
}

void rise2(void)
{  
    wait_ms(100);
    if(state == ST_EIN) pressed2 = true;
}

bool CheckFlag2() 
{
    if (pressed2) {
        pressed2=false;
        return true;
        }
    return false;
}

//************* STATES ****************

void ST_Aus (void)
{
    //Status auf LCD und Serial
    lcd.cls();      // löscht lcd (clear screen)
    lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
    lcd.printf("State: 1 (Aus)");
    printf("State: 1 (Aus)");
    // entry
    
    // do
    while(true) {
        Led1 = 0;
    
        if(CheckFlag1()) {
            state = ST_EIN;
            
    // exit
            return;
            }
        }
}

void ST_Ein (void)
{
    //Status auf LCD und Serial
    lcd.cls();      // löscht lcd (clear screen)
    lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
    lcd.printf("State: 2 (Ein)");
    printf("State: 2 (Ein)");
    
    // entry
    T1.attach(&blink, 0.3);
    while (true)
    {
      if(blinkCount >= 4) break;
    }
    T1.detach();
    blinkCount = 0;
    
    // do
    while(true) {
        Led1 = 1;
        
    if(CheckFlag1()) {
            state = ST_AUS;
    // exit
            return;
            }
            
    if(CheckFlag2()) {
            state = ST_TIMEOUT;
    // exit
            return;
            }
        }
}

void ST_TimeOut (void)
{
    //Status auf LCD und Serial
    lcd.cls();      // löscht lcd (clear screen)
    lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
    lcd.printf("State: 3 (TimeOut)");
    printf("State: 3 (TimeOut)");
    
    // entry
    
    
    // do
    while(true) {
        lcd.locate(0,12);   // x-position, y-position (x: 0-128; y: 0-32)
        lcd.printf("Time: %f", timerValue);
        printf("Time: %f", timerValue);
           
    // exit
            
            }
}

void ST_Error (void)
{
    //Status auf LCD und Serial
    lcd.cls();      // löscht lcd (clear screen)
    lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
    lcd.printf("State: ERROR!!!");
    printf("State: ERROR!!!");
    return;
}

//************* STATE MACHINE ****************

void stateMachine()
{
    switch (state)
    {
    case ST_AUS: ST_Aus();
        break;
    case ST_EIN: ST_Ein();
        break;
    case ST_TIMEOUT: ST_TimeOut();
        break;
    default: ST_Error();  // sollte nicht auftreten :-)
        break;
    }
}

//************* MAIN ****************
 
int main()
{
    SW1.rise(&rise1);      //.fall(&fall);
    SW2.rise(&rise2);
    state = ST_AUS;
    while(true){
        stateMachine(); 
        }
}