Reaktionstester_01

Dependencies:   C12832 mbed

Committer:
p0ckin3d
Date:
Wed Oct 29 08:21:59 2014 +0000
Revision:
0:23696056294d
Reaktionstester

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p0ckin3d 0:23696056294d 1 #include "mbed.h"
p0ckin3d 0:23696056294d 2 #include "C12832.h"
p0ckin3d 0:23696056294d 3
p0ckin3d 0:23696056294d 4 C12832 lcd(p5, p7, p6, p8, p11);
p0ckin3d 0:23696056294d 5
p0ckin3d 0:23696056294d 6 InterruptIn center(p14);
p0ckin3d 0:23696056294d 7 Timer timer;
p0ckin3d 0:23696056294d 8 Ticker ticker;
p0ckin3d 0:23696056294d 9
p0ckin3d 0:23696056294d 10 DigitalOut green(p24);
p0ckin3d 0:23696056294d 11 DigitalOut red(p23);
p0ckin3d 0:23696056294d 12 AnalogIn seed(p16);
p0ckin3d 0:23696056294d 13
p0ckin3d 0:23696056294d 14 int allOff();
p0ckin3d 0:23696056294d 15 int startTimer();
p0ckin3d 0:23696056294d 16 int startMeasurement();
p0ckin3d 0:23696056294d 17 int finishMeasurement();
p0ckin3d 0:23696056294d 18 void tick();
p0ckin3d 0:23696056294d 19
p0ckin3d 0:23696056294d 20 volatile unsigned int newEvent =0;
p0ckin3d 0:23696056294d 21 volatile unsigned int event=0, state=0;
p0ckin3d 0:23696056294d 22
p0ckin3d 0:23696056294d 23 unsigned int rnd = 0;
p0ckin3d 0:23696056294d 24 int rndCounter = 0;
p0ckin3d 0:23696056294d 25
p0ckin3d 0:23696056294d 26 unsigned char nextstate_tab[1][4]= { 1, 2, 3, 0 };
p0ckin3d 0:23696056294d 27
p0ckin3d 0:23696056294d 28 int(*action[1][4])()= { allOff, startTimer, startMeasurement, finishMeasurement };
p0ckin3d 0:23696056294d 29
p0ckin3d 0:23696056294d 30 int allOff()
p0ckin3d 0:23696056294d 31 {
p0ckin3d 0:23696056294d 32 ticker.detach();
p0ckin3d 0:23696056294d 33 green = 1;
p0ckin3d 0:23696056294d 34 red = 1;
p0ckin3d 0:23696056294d 35 return 0;
p0ckin3d 0:23696056294d 36 }
p0ckin3d 0:23696056294d 37
p0ckin3d 0:23696056294d 38 int startTimer()
p0ckin3d 0:23696056294d 39 {
p0ckin3d 0:23696056294d 40 rnd = rand()%63;
p0ckin3d 0:23696056294d 41
p0ckin3d 0:23696056294d 42 if(rnd < 8)
p0ckin3d 0:23696056294d 43 {
p0ckin3d 0:23696056294d 44 while(rnd < 8)
p0ckin3d 0:23696056294d 45 rnd = rand()%63;
p0ckin3d 0:23696056294d 46 }
p0ckin3d 0:23696056294d 47
p0ckin3d 0:23696056294d 48 green = 0;
p0ckin3d 0:23696056294d 49
p0ckin3d 0:23696056294d 50 ticker.attach(&tick, 0.1);
p0ckin3d 0:23696056294d 51 center.disable_irq();
p0ckin3d 0:23696056294d 52 return 0;
p0ckin3d 0:23696056294d 53 }
p0ckin3d 0:23696056294d 54
p0ckin3d 0:23696056294d 55 int startMeasurement()
p0ckin3d 0:23696056294d 56 {
p0ckin3d 0:23696056294d 57 timer.start();
p0ckin3d 0:23696056294d 58 ticker.detach();
p0ckin3d 0:23696056294d 59 return 0;
p0ckin3d 0:23696056294d 60 }
p0ckin3d 0:23696056294d 61
p0ckin3d 0:23696056294d 62 int finishMeasurement()
p0ckin3d 0:23696056294d 63 {
p0ckin3d 0:23696056294d 64 timer.stop();
p0ckin3d 0:23696056294d 65 rndCounter = 0;
p0ckin3d 0:23696056294d 66 lcd.locate(0, 20);
p0ckin3d 0:23696056294d 67 lcd.printf("Zeit: %f", timer.read());
p0ckin3d 0:23696056294d 68 timer.reset();
p0ckin3d 0:23696056294d 69 newEvent = 1;
p0ckin3d 0:23696056294d 70 return 0;
p0ckin3d 0:23696056294d 71 }
p0ckin3d 0:23696056294d 72
p0ckin3d 0:23696056294d 73 void buttonPressed()
p0ckin3d 0:23696056294d 74 {
p0ckin3d 0:23696056294d 75 newEvent = 1;
p0ckin3d 0:23696056294d 76 }
p0ckin3d 0:23696056294d 77
p0ckin3d 0:23696056294d 78 void tick()
p0ckin3d 0:23696056294d 79 {
p0ckin3d 0:23696056294d 80 rndCounter++;
p0ckin3d 0:23696056294d 81
p0ckin3d 0:23696056294d 82 if(rndCounter == rnd)
p0ckin3d 0:23696056294d 83 {
p0ckin3d 0:23696056294d 84 green = 1;
p0ckin3d 0:23696056294d 85 newEvent = 1;
p0ckin3d 0:23696056294d 86 center.enable_irq();
p0ckin3d 0:23696056294d 87 }
p0ckin3d 0:23696056294d 88 }
p0ckin3d 0:23696056294d 89
p0ckin3d 0:23696056294d 90 int main() {
p0ckin3d 0:23696056294d 91
p0ckin3d 0:23696056294d 92 center.rise(&buttonPressed);
p0ckin3d 0:23696056294d 93
p0ckin3d 0:23696056294d 94 lcd.cls();
p0ckin3d 0:23696056294d 95 lcd.locate(0, 0);
p0ckin3d 0:23696056294d 96 lcd.printf("Reaktionstester");
p0ckin3d 0:23696056294d 97
p0ckin3d 0:23696056294d 98 green = 1;
p0ckin3d 0:23696056294d 99 red = 1;
p0ckin3d 0:23696056294d 100
p0ckin3d 0:23696056294d 101 newEvent = 0;
p0ckin3d 0:23696056294d 102 state=0;
p0ckin3d 0:23696056294d 103 event=0;
p0ckin3d 0:23696056294d 104
p0ckin3d 0:23696056294d 105 srand(seed.read_u16());
p0ckin3d 0:23696056294d 106
p0ckin3d 0:23696056294d 107 while(1) {
p0ckin3d 0:23696056294d 108
p0ckin3d 0:23696056294d 109 if (newEvent)
p0ckin3d 0:23696056294d 110 {
p0ckin3d 0:23696056294d 111 newEvent =0;
p0ckin3d 0:23696056294d 112 (*action[event][state])();
p0ckin3d 0:23696056294d 113 state=nextstate_tab[event][state];
p0ckin3d 0:23696056294d 114 }
p0ckin3d 0:23696056294d 115
p0ckin3d 0:23696056294d 116 }
p0ckin3d 0:23696056294d 117 }