![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
lololololol
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@6:61618bf71a08, 2019-09-13 (annotated)
- Committer:
- Hendrikvg
- Date:
- Fri Sep 13 11:00:52 2019 +0000
- Revision:
- 6:61618bf71a08
- Parent:
- 5:dc3a076bbb10
- Child:
- 7:d307e31f7391
State Machine seems to work, with timing. Although the ticker function doesn't seem to be the most reliant thing in the world.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
RobertoO | 1:b862262a9d14 | 2 | #include "MODSERIAL.h" |
RobertoO | 0:67c50348f842 | 3 | |
RobertoO | 1:b862262a9d14 | 4 | MODSERIAL pc(USBTX, USBRX); |
Hendrikvg | 3:68d78770639b | 5 | DigitalOut ledr(LED_RED); |
Hendrikvg | 3:68d78770639b | 6 | DigitalOut ledg(LED_GREEN); |
Hendrikvg | 3:68d78770639b | 7 | DigitalOut ledb(LED_BLUE); |
Hendrikvg | 6:61618bf71a08 | 8 | Ticker RE; |
Hendrikvg | 6:61618bf71a08 | 9 | Timer R; |
Hendrikvg | 6:61618bf71a08 | 10 | Timer G; |
Hendrikvg | 6:61618bf71a08 | 11 | Timer B; |
Hendrikvg | 2:d9b0ebf3fcca | 12 | |
Hendrikvg | 6:61618bf71a08 | 13 | volatile char Command; |
Hendrikvg | 6:61618bf71a08 | 14 | enum states {red, green, blue}; |
Hendrikvg | 2:d9b0ebf3fcca | 15 | |
Hendrikvg | 6:61618bf71a08 | 16 | states CurrentState = red; |
Hendrikvg | 6:61618bf71a08 | 17 | bool StateChanged = true; |
Hendrikvg | 6:61618bf71a08 | 18 | |
Hendrikvg | 6:61618bf71a08 | 19 | void ES() |
Hendrikvg | 2:d9b0ebf3fcca | 20 | { |
Hendrikvg | 6:61618bf71a08 | 21 | Command = 'r'; |
Hendrikvg | 6:61618bf71a08 | 22 | } |
Hendrikvg | 6:61618bf71a08 | 23 | |
Hendrikvg | 6:61618bf71a08 | 24 | void CheckCommandFromTerminal(void) |
Hendrikvg | 6:61618bf71a08 | 25 | { |
Hendrikvg | 6:61618bf71a08 | 26 | Command = pc.getcNb(); |
Hendrikvg | 2:d9b0ebf3fcca | 27 | } |
RobertoO | 0:67c50348f842 | 28 | |
Hendrikvg | 6:61618bf71a08 | 29 | void StateMachine(void) |
Hendrikvg | 6:61618bf71a08 | 30 | { |
Hendrikvg | 6:61618bf71a08 | 31 | switch(CurrentState) { |
Hendrikvg | 6:61618bf71a08 | 32 | case red: |
Hendrikvg | 6:61618bf71a08 | 33 | if (StateChanged) { |
Hendrikvg | 6:61618bf71a08 | 34 | pc.printf("Initiating turning LED red\n\r"); |
Hendrikvg | 6:61618bf71a08 | 35 | StateChanged= false; |
Hendrikvg | 6:61618bf71a08 | 36 | ledr = 0; |
Hendrikvg | 6:61618bf71a08 | 37 | ledg = 1; |
Hendrikvg | 6:61618bf71a08 | 38 | ledb = 1; |
Hendrikvg | 6:61618bf71a08 | 39 | R.start(); |
Hendrikvg | 6:61618bf71a08 | 40 | pc.printf("LED is now red!\n\r"); |
Hendrikvg | 6:61618bf71a08 | 41 | } |
Hendrikvg | 6:61618bf71a08 | 42 | if (Command == 'g') { |
Hendrikvg | 6:61618bf71a08 | 43 | R.stop(); |
Hendrikvg | 6:61618bf71a08 | 44 | pc.printf("The LED has been red for %f seconds!\n\r", R.read()); |
Hendrikvg | 6:61618bf71a08 | 45 | CurrentState= green; |
Hendrikvg | 6:61618bf71a08 | 46 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 47 | } |
Hendrikvg | 6:61618bf71a08 | 48 | if (Command == 'b') { |
Hendrikvg | 6:61618bf71a08 | 49 | R.stop(); |
Hendrikvg | 6:61618bf71a08 | 50 | pc.printf("The LED has been red for %f seconds!\n\r", R.read()); |
Hendrikvg | 6:61618bf71a08 | 51 | CurrentState= blue; |
Hendrikvg | 6:61618bf71a08 | 52 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 53 | } |
Hendrikvg | 6:61618bf71a08 | 54 | else {} |
Hendrikvg | 6:61618bf71a08 | 55 | break; |
Hendrikvg | 6:61618bf71a08 | 56 | case green: |
Hendrikvg | 6:61618bf71a08 | 57 | if (StateChanged) { |
Hendrikvg | 6:61618bf71a08 | 58 | pc.printf("Initiating turning LED green\n\r"); |
Hendrikvg | 6:61618bf71a08 | 59 | StateChanged= false; |
Hendrikvg | 6:61618bf71a08 | 60 | ledr = 1; |
Hendrikvg | 6:61618bf71a08 | 61 | ledg = 0; |
Hendrikvg | 6:61618bf71a08 | 62 | ledb = 1; |
Hendrikvg | 6:61618bf71a08 | 63 | G.start(); |
Hendrikvg | 6:61618bf71a08 | 64 | pc.printf("LED is now green!\n\r"); |
Hendrikvg | 6:61618bf71a08 | 65 | } |
Hendrikvg | 6:61618bf71a08 | 66 | if (Command == 'r') { |
Hendrikvg | 6:61618bf71a08 | 67 | G.stop(); |
Hendrikvg | 6:61618bf71a08 | 68 | pc.printf("The LED has been green for %f seconds!\n\r", G.read()); |
Hendrikvg | 6:61618bf71a08 | 69 | CurrentState= red; |
Hendrikvg | 6:61618bf71a08 | 70 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 71 | } |
Hendrikvg | 6:61618bf71a08 | 72 | if (Command == 'b') { |
Hendrikvg | 6:61618bf71a08 | 73 | G.stop(); |
Hendrikvg | 6:61618bf71a08 | 74 | pc.printf("The LED has been green for %f seconds!\n\r", G.read()); |
Hendrikvg | 6:61618bf71a08 | 75 | CurrentState= blue; |
Hendrikvg | 6:61618bf71a08 | 76 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 77 | } |
Hendrikvg | 6:61618bf71a08 | 78 | else {} |
Hendrikvg | 6:61618bf71a08 | 79 | break; |
Hendrikvg | 6:61618bf71a08 | 80 | case blue: |
Hendrikvg | 6:61618bf71a08 | 81 | if (StateChanged) { |
Hendrikvg | 6:61618bf71a08 | 82 | pc.printf("Initiating turning LED blue\n\r"); |
Hendrikvg | 6:61618bf71a08 | 83 | StateChanged= false; |
Hendrikvg | 6:61618bf71a08 | 84 | ledr = 1; |
Hendrikvg | 6:61618bf71a08 | 85 | ledg = 1; |
Hendrikvg | 6:61618bf71a08 | 86 | ledb = 0; |
Hendrikvg | 6:61618bf71a08 | 87 | B.start(); |
Hendrikvg | 6:61618bf71a08 | 88 | pc.printf("LED is now blue!\n\r"); |
Hendrikvg | 6:61618bf71a08 | 89 | } |
Hendrikvg | 6:61618bf71a08 | 90 | if (Command == 'r') { |
Hendrikvg | 6:61618bf71a08 | 91 | B.stop(); |
Hendrikvg | 6:61618bf71a08 | 92 | pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); |
Hendrikvg | 6:61618bf71a08 | 93 | CurrentState= red; |
Hendrikvg | 6:61618bf71a08 | 94 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 95 | } |
Hendrikvg | 6:61618bf71a08 | 96 | if (Command == 'g') { |
Hendrikvg | 6:61618bf71a08 | 97 | B.stop(); |
Hendrikvg | 6:61618bf71a08 | 98 | pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); |
Hendrikvg | 6:61618bf71a08 | 99 | CurrentState= green; |
Hendrikvg | 6:61618bf71a08 | 100 | StateChanged= true; |
Hendrikvg | 6:61618bf71a08 | 101 | } |
Hendrikvg | 6:61618bf71a08 | 102 | else {} |
Hendrikvg | 6:61618bf71a08 | 103 | break; |
Hendrikvg | 6:61618bf71a08 | 104 | default: |
Hendrikvg | 6:61618bf71a08 | 105 | pc.printf("Unknown or unimplemented state reached!\n"); |
Hendrikvg | 6:61618bf71a08 | 106 | } |
Hendrikvg | 6:61618bf71a08 | 107 | } |
RobertoO | 0:67c50348f842 | 108 | int main() |
RobertoO | 0:67c50348f842 | 109 | { |
RobertoO | 0:67c50348f842 | 110 | pc.baud(115200); |
Hendrikvg | 6:61618bf71a08 | 111 | RE.attach(ES,3); |
Hendrikvg | 6:61618bf71a08 | 112 | while(true) { |
Hendrikvg | 6:61618bf71a08 | 113 | CheckCommandFromTerminal(); |
Hendrikvg | 6:61618bf71a08 | 114 | StateMachine(); |
RobertoO | 0:67c50348f842 | 115 | } |
Hendrikvg | 2:d9b0ebf3fcca | 116 | } |