![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
lololololol
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
Diff: main.cpp
- Revision:
- 6:61618bf71a08
- Parent:
- 5:dc3a076bbb10
- Child:
- 7:d307e31f7391
diff -r dc3a076bbb10 -r 61618bf71a08 main.cpp --- a/main.cpp Thu Sep 12 13:48:12 2019 +0000 +++ b/main.cpp Fri Sep 13 11:00:52 2019 +0000 @@ -5,75 +5,112 @@ DigitalOut ledr(LED_RED); DigitalOut ledg(LED_GREEN); DigitalOut ledb(LED_BLUE); -Ticker ReadEnCoder; +Ticker RE; +Timer R; +Timer G; +Timer B; -char color='r'; +volatile char Command; +enum states {red, green, blue}; -void flicker() +states CurrentState = red; +bool StateChanged = true; + +void ES() { - if (color == 'r') { - ledr=!ledr; - ledg=1; - ledb=1; - } - if (color == 'g') { - ledr=1; - ledg=!ledg; - ledb=1; - } - if (color == 'b') { - ledr=1; - ledg=1; - ledb=!ledb; - } - if (color == 'y') { - ledr=!ledr; - ledg=!ledg; - ledb=1; - } - if (color == 'c') { - ledr=1; - ledg=!ledg; - ledb=!ledb; - } - if (color == 'm') { - ledr=!ledr; - ledg=1; - ledb=!ledb; - } - if (color == 'w') { - ledr=!ledr; - ledg=!ledg; - ledb=!ledb; - } + Command = 'r'; +} + +void CheckCommandFromTerminal(void) +{ + Command = pc.getcNb(); } +void StateMachine(void) +{ + switch(CurrentState) { + case red: + if (StateChanged) { + pc.printf("Initiating turning LED red\n\r"); + StateChanged= false; + ledr = 0; + ledg = 1; + ledb = 1; + R.start(); + pc.printf("LED is now red!\n\r"); + } + if (Command == 'g') { + R.stop(); + pc.printf("The LED has been red for %f seconds!\n\r", R.read()); + CurrentState= green; + StateChanged= true; + } + if (Command == 'b') { + R.stop(); + pc.printf("The LED has been red for %f seconds!\n\r", R.read()); + CurrentState= blue; + StateChanged= true; + } + else {} + break; + case green: + if (StateChanged) { + pc.printf("Initiating turning LED green\n\r"); + StateChanged= false; + ledr = 1; + ledg = 0; + ledb = 1; + G.start(); + pc.printf("LED is now green!\n\r"); + } + if (Command == 'r') { + G.stop(); + pc.printf("The LED has been green for %f seconds!\n\r", G.read()); + CurrentState= red; + StateChanged= true; + } + if (Command == 'b') { + G.stop(); + pc.printf("The LED has been green for %f seconds!\n\r", G.read()); + CurrentState= blue; + StateChanged= true; + } + else {} + break; + case blue: + if (StateChanged) { + pc.printf("Initiating turning LED blue\n\r"); + StateChanged= false; + ledr = 1; + ledg = 1; + ledb = 0; + B.start(); + pc.printf("LED is now blue!\n\r"); + } + if (Command == 'r') { + B.stop(); + pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); + CurrentState= red; + StateChanged= true; + } + if (Command == 'g') { + B.stop(); + pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); + CurrentState= green; + StateChanged= true; + } + else {} + break; + default: + pc.printf("Unknown or unimplemented state reached!\n"); + } +} int main() { pc.baud(115200); - while (true) { - pc.printf("%c\n\r",color); - ReadEnCoder.attach(flicker,0.5); - color = pc.getc(); - if (color == 'w') { - ledr=0; - ledg=0; - ledb=0; - } - if (color == 'y') { - ledr=0; - ledg=0; - ledb=1; - } - if (color == 'm') { - ledr=0; - ledg=1; - ledb=0; - } - if (color == 'c') { - ledr=1; - ledg=0; - ledb=0; - } + RE.attach(ES,3); + while(true) { + CheckCommandFromTerminal(); + StateMachine(); } } \ No newline at end of file