ha
Dependencies: mbed
main.cpp@0:51814a1e44cd, 2016-04-21 (annotated)
- Committer:
- vici
- Date:
- Thu Apr 21 15:47:07 2016 +0000
- Revision:
- 0:51814a1e44cd
hallo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vici | 0:51814a1e44cd | 1 | /* Modulname: SCHÖNI VICI CALC 4STATES Version: 1.0 */ |
vici | 0:51814a1e44cd | 2 | /* Funktion: State-Machine-Programm */ |
vici | 0:51814a1e44cd | 3 | /* Erstellt von: SC am: 2013-02-11 */ |
vici | 0:51814a1e44cd | 4 | |
vici | 0:51814a1e44cd | 5 | #include "mbed.h" |
vici | 0:51814a1e44cd | 6 | #include "C12832_lcd.h" |
vici | 0:51814a1e44cd | 7 | |
vici | 0:51814a1e44cd | 8 | bool newEvent =false; |
vici | 0:51814a1e44cd | 9 | uint8_t event=0, state=0; |
vici | 0:51814a1e44cd | 10 | |
vici | 0:51814a1e44cd | 11 | InterruptIn iiJsUp(p15); //alle Funktionen von Joystick initialisieren |
vici | 0:51814a1e44cd | 12 | DigitalIn diJsUp(p15); |
vici | 0:51814a1e44cd | 13 | InterruptIn iiJsDown(p12); |
vici | 0:51814a1e44cd | 14 | DigitalIn diJsDown(p12); |
vici | 0:51814a1e44cd | 15 | InterruptIn iiJsCenter(p14); |
vici | 0:51814a1e44cd | 16 | DigitalIn diJsCenter(p14); |
vici | 0:51814a1e44cd | 17 | InterruptIn iiJsLeft(p13); |
vici | 0:51814a1e44cd | 18 | DigitalIn diJsLeft(p13); |
vici | 0:51814a1e44cd | 19 | InterruptIn iiJsRight(p16); |
vici | 0:51814a1e44cd | 20 | DigitalIn diJsRight(p16); |
vici | 0:51814a1e44cd | 21 | char calcOp = '+'; // calcOp festgelegt als "+" oder "-" |
vici | 0:51814a1e44cd | 22 | bool updateLcd = true; // aktualisierung von LCD |
vici | 0:51814a1e44cd | 23 | int value1, value2, result; // variablen fürs Rechnen festlegen, initialisierung |
vici | 0:51814a1e44cd | 24 | C12832_LCD lcd; |
vici | 0:51814a1e44cd | 25 | |
vici | 0:51814a1e44cd | 26 | BusOut myLeds(LED1, LED2, LED3, LED4); //Ledbar |
vici | 0:51814a1e44cd | 27 | |
vici | 0:51814a1e44cd | 28 | // prototypes //festgelegte Funktionen werden hier abgearbeitet |
vici | 0:51814a1e44cd | 29 | void noFunction(); //keine Funktion |
vici | 0:51814a1e44cd | 30 | void selectAddition(); //Auswahl + |
vici | 0:51814a1e44cd | 31 | void selectSubtraction(); //Auswahl - |
vici | 0:51814a1e44cd | 32 | void CountTerm_1_Up(); //Erste Zahl wird raufgezählt |
vici | 0:51814a1e44cd | 33 | void CountTerm_2_Up(); //Zweite Zahl wird raufgezählt |
vici | 0:51814a1e44cd | 34 | void CountTerm_1_Down(); //Erste Zahl wird runtergezählt |
vici | 0:51814a1e44cd | 35 | void CountTerm_2_Down(); //Zweite Zahl wird runtergezählt |
vici | 0:51814a1e44cd | 36 | void reset(); //rückgesetzt |
vici | 0:51814a1e44cd | 37 | void calculateResult(); //Ergebnis von Zahl1 und Zahl2 |
vici | 0:51814a1e44cd | 38 | |
vici | 0:51814a1e44cd | 39 | /****************************************************/ |
vici | 0:51814a1e44cd | 40 | /* Tabelle fuer die naechsten Zustaende bei Eingabe */ |
vici | 0:51814a1e44cd | 41 | /****************************************************/ |
vici | 0:51814a1e44cd | 42 | uint8_t nextstate_tab[5][4]= |
vici | 0:51814a1e44cd | 43 | // current event/ present state 0 1 2 3 |
vici | 0:51814a1e44cd | 44 | //---------------------- |
vici | 0:51814a1e44cd | 45 | /* Event 0 (Joystick UP) */{{ 1, 1, 1, 3 }, // next |
vici | 0:51814a1e44cd | 46 | /* Event 1 (Joystick DOWN) */ { 1, 1, 1 ,3 }, |
vici | 0:51814a1e44cd | 47 | /* Event 2 (Joystick LEFT) */ { 0, 2, 2 ,0 }, |
vici | 0:51814a1e44cd | 48 | /* Event 3 (Joystick RIGHT) */ { 0, 2, 2 ,0 }, |
vici | 0:51814a1e44cd | 49 | /* Event 4 (Joystick CENTER)*/ { 0, 3, 3 ,3}}; // state |
vici | 0:51814a1e44cd | 50 | // state=nextstate_tab[event][state]; // gehe auf naechsten Zustand |
vici | 0:51814a1e44cd | 51 | |
vici | 0:51814a1e44cd | 52 | /*************************************************************/ |
vici | 0:51814a1e44cd | 53 | /* Tabelle fuer Aktionsroutinen fuer die Zustaende bei Input */ |
vici | 0:51814a1e44cd | 54 | /*************************************************************/ |
vici | 0:51814a1e44cd | 55 | // p r e s e n t s t a t e |
vici | 0:51814a1e44cd | 56 | void (*action[5][4])()= // 0 1 2 3 |
vici | 0:51814a1e44cd | 57 | /* event 0 */ {{ noFunction,selectAddition,selectAddition, noFunction}, |
vici | 0:51814a1e44cd | 58 | /* event 1 */ { noFunction,selectSubtraction,selectSubtraction,noFunction}, |
vici | 0:51814a1e44cd | 59 | /* event 2 */ { CountTerm_1_Up,noFunction,CountTerm_2_Up,reset}, |
vici | 0:51814a1e44cd | 60 | /* event 3 */ { CountTerm_1_Down,noFunction,CountTerm_2_Down,reset}, |
vici | 0:51814a1e44cd | 61 | /* event 4 */ { noFunction,calculateResult,calculateResult,noFunction}}; |
vici | 0:51814a1e44cd | 62 | |
vici | 0:51814a1e44cd | 63 | // action routines //Funktionen erstellen,was darin passiert |
vici | 0:51814a1e44cd | 64 | void noFunction() { |
vici | 0:51814a1e44cd | 65 | } |
vici | 0:51814a1e44cd | 66 | |
vici | 0:51814a1e44cd | 67 | void selectAddition() { |
vici | 0:51814a1e44cd | 68 | calcOp = '+'; |
vici | 0:51814a1e44cd | 69 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 70 | lcd.printf("%d %c ", value1, calcOp); |
vici | 0:51814a1e44cd | 71 | } |
vici | 0:51814a1e44cd | 72 | void selectSubtraction() { |
vici | 0:51814a1e44cd | 73 | calcOp = '-'; |
vici | 0:51814a1e44cd | 74 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 75 | lcd.printf("%d %c ", value1, calcOp); |
vici | 0:51814a1e44cd | 76 | } |
vici | 0:51814a1e44cd | 77 | void CountTerm_1_Up() { |
vici | 0:51814a1e44cd | 78 | value1++; |
vici | 0:51814a1e44cd | 79 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 80 | lcd.printf("%d ", value1); |
vici | 0:51814a1e44cd | 81 | } |
vici | 0:51814a1e44cd | 82 | void CountTerm_1_Down() { |
vici | 0:51814a1e44cd | 83 | value1--; |
vici | 0:51814a1e44cd | 84 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 85 | lcd.printf("%d ", value1); |
vici | 0:51814a1e44cd | 86 | } |
vici | 0:51814a1e44cd | 87 | void CountTerm_2_Up() { |
vici | 0:51814a1e44cd | 88 | value2++; |
vici | 0:51814a1e44cd | 89 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 90 | lcd.printf("%d %c %d ", value1, calcOp, value2); |
vici | 0:51814a1e44cd | 91 | } |
vici | 0:51814a1e44cd | 92 | void CountTerm_2_Down() { |
vici | 0:51814a1e44cd | 93 | value2--; |
vici | 0:51814a1e44cd | 94 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 95 | lcd.printf("%d %c %d ", value1, calcOp, value2); |
vici | 0:51814a1e44cd | 96 | } |
vici | 0:51814a1e44cd | 97 | void reset() { |
vici | 0:51814a1e44cd | 98 | value1 = value2 = 0; |
vici | 0:51814a1e44cd | 99 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 100 | lcd.printf("%d ", value1); |
vici | 0:51814a1e44cd | 101 | } |
vici | 0:51814a1e44cd | 102 | void calculateResult() { |
vici | 0:51814a1e44cd | 103 | if (calcOp == '+') |
vici | 0:51814a1e44cd | 104 | result = value1 + value2; |
vici | 0:51814a1e44cd | 105 | else |
vici | 0:51814a1e44cd | 106 | result = value1 - value2; |
vici | 0:51814a1e44cd | 107 | lcd.locate(0, 20); |
vici | 0:51814a1e44cd | 108 | lcd.printf("%d %c %d = %d ", value1, calcOp, value2, result); |
vici | 0:51814a1e44cd | 109 | } |
vici | 0:51814a1e44cd | 110 | |
vici | 0:51814a1e44cd | 111 | // common functions |
vici | 0:51814a1e44cd | 112 | uint8_t debounce(DigitalIn myIn) |
vici | 0:51814a1e44cd | 113 | { |
vici | 0:51814a1e44cd | 114 | #define LEVEL_CHECKS 16 |
vici | 0:51814a1e44cd | 115 | #define MAX_LOOPS 80 // stoppt das Überprüfen des Prellen nach max. MAX_LOOPS Durchläufen |
vici | 0:51814a1e44cd | 116 | unsigned char port_buffer; |
vici | 0:51814a1e44cd | 117 | unsigned char debounceCounter = 0; |
vici | 0:51814a1e44cd | 118 | uint8_t loopCounter = 0; |
vici | 0:51814a1e44cd | 119 | |
vici | 0:51814a1e44cd | 120 | do |
vici | 0:51814a1e44cd | 121 | { |
vici | 0:51814a1e44cd | 122 | port_buffer = myIn; |
vici | 0:51814a1e44cd | 123 | wait_us(100); |
vici | 0:51814a1e44cd | 124 | loopCounter++; |
vici | 0:51814a1e44cd | 125 | if(myIn == port_buffer) |
vici | 0:51814a1e44cd | 126 | debounceCounter++; // mindestens 'LEVEL_CHECKS' Abtastungen in Folge: gleicher Pegel |
vici | 0:51814a1e44cd | 127 | else |
vici | 0:51814a1e44cd | 128 | debounceCounter = 0; |
vici | 0:51814a1e44cd | 129 | } |
vici | 0:51814a1e44cd | 130 | while ((debounceCounter <= LEVEL_CHECKS) && (loopCounter <= MAX_LOOPS)); |
vici | 0:51814a1e44cd | 131 | return loopCounter; |
vici | 0:51814a1e44cd | 132 | } |
vici | 0:51814a1e44cd | 133 | |
vici | 0:51814a1e44cd | 134 | // ISR |
vici | 0:51814a1e44cd | 135 | void opPlus() { // op +: JsUp=event0 |
vici | 0:51814a1e44cd | 136 | debounce(diJsUp); |
vici | 0:51814a1e44cd | 137 | event=0; |
vici | 0:51814a1e44cd | 138 | newEvent = true; |
vici | 0:51814a1e44cd | 139 | } |
vici | 0:51814a1e44cd | 140 | void opMinus() { // op -: JsDown=event1 |
vici | 0:51814a1e44cd | 141 | debounce(diJsDown); |
vici | 0:51814a1e44cd | 142 | event=1; |
vici | 0:51814a1e44cd | 143 | newEvent = true; |
vici | 0:51814a1e44cd | 144 | } |
vici | 0:51814a1e44cd | 145 | void cntUp() { // value +1: JsLeft=event2 |
vici | 0:51814a1e44cd | 146 | debounce(diJsLeft); |
vici | 0:51814a1e44cd | 147 | event=2; |
vici | 0:51814a1e44cd | 148 | newEvent = true; |
vici | 0:51814a1e44cd | 149 | } |
vici | 0:51814a1e44cd | 150 | void cntDown() { // value -1: JsRight=event3 |
vici | 0:51814a1e44cd | 151 | debounce(diJsRight); |
vici | 0:51814a1e44cd | 152 | event=3; |
vici | 0:51814a1e44cd | 153 | newEvent = true; |
vici | 0:51814a1e44cd | 154 | } |
vici | 0:51814a1e44cd | 155 | void doCalc() { // calculate: JsCenter=event4 |
vici | 0:51814a1e44cd | 156 | debounce(diJsCenter); |
vici | 0:51814a1e44cd | 157 | event=4; |
vici | 0:51814a1e44cd | 158 | newEvent = true; |
vici | 0:51814a1e44cd | 159 | } |
vici | 0:51814a1e44cd | 160 | |
vici | 0:51814a1e44cd | 161 | |
vici | 0:51814a1e44cd | 162 | void init() { //hier wird alles initialisiert |
vici | 0:51814a1e44cd | 163 | myLeds = 0; |
vici | 0:51814a1e44cd | 164 | newEvent = false; |
vici | 0:51814a1e44cd | 165 | state=0; |
vici | 0:51814a1e44cd | 166 | event=0; |
vici | 0:51814a1e44cd | 167 | lcd.cls(); |
vici | 0:51814a1e44cd | 168 | lcd.locate(0,0); |
vici | 0:51814a1e44cd | 169 | iiJsUp.rise(&opPlus); |
vici | 0:51814a1e44cd | 170 | iiJsDown.rise(&opMinus); |
vici | 0:51814a1e44cd | 171 | iiJsLeft.rise(&cntUp); |
vici | 0:51814a1e44cd | 172 | iiJsRight.rise(&cntDown); |
vici | 0:51814a1e44cd | 173 | iiJsCenter.rise(&doCalc); |
vici | 0:51814a1e44cd | 174 | } |
vici | 0:51814a1e44cd | 175 | |
vici | 0:51814a1e44cd | 176 | int main() //Hauptprogramm |
vici | 0:51814a1e44cd | 177 | { |
vici | 0:51814a1e44cd | 178 | init(); |
vici | 0:51814a1e44cd | 179 | lcd.printf("BULME Calculator BV"); |
vici | 0:51814a1e44cd | 180 | lcd.locate(0,10); |
vici | 0:51814a1e44cd | 181 | lcd.printf("Berechung eingeben"); |
vici | 0:51814a1e44cd | 182 | |
vici | 0:51814a1e44cd | 183 | while(1) { |
vici | 0:51814a1e44cd | 184 | if (newEvent) { |
vici | 0:51814a1e44cd | 185 | newEvent=false; |
vici | 0:51814a1e44cd | 186 | (*action[event][state])(); // line 1 for executing sm: run action routine |
vici | 0:51814a1e44cd | 187 | state = nextstate_tab[event][state]; // line 2 for executing sm: run state transition |
vici | 0:51814a1e44cd | 188 | } |
vici | 0:51814a1e44cd | 189 | } |
vici | 0:51814a1e44cd | 190 | return 1; |
vici | 0:51814a1e44cd | 191 | } |