|TheBase| / Mbed 2 deprecated Reaktionstester

Dependencies:   C12832_lcd mbed

Files at this revision

API Documentation at this revision

Comitter:
philipp19961
Date:
Wed Oct 29 08:10:05 2014 +0000
Commit message:
Gay

Changed in this revision

C12832_lcd.lib Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 721134a8164c C12832_lcd.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832_lcd.lib	Wed Oct 29 08:10:05 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
diff -r 000000000000 -r 721134a8164c DebouncedIn.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.cpp	Wed Oct 29 08:10:05 2014 +0000
@@ -0,0 +1,93 @@
+#include "DebouncedIn.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+DebouncedIn::DebouncedIn(PinName in) 
+    : _in(in) {    
+        
+    // reset all the flags and counters    
+    _samples = 0;
+    _output = 0;
+    _output_last = 0;
+    _rising_flag = 0;
+    _falling_flag = 0;
+    _state_counter = 0;
+    
+    // Attach ticker
+    _ticker.attach(this, &DebouncedIn::_sample, 0.005);     
+}
+  
+void DebouncedIn::_sample() {
+
+    // take a sample
+    _samples = _samples >> 1; // shift left
+      
+    if (_in) {
+        _samples |= 0x80;
+    }  
+      
+    // examine the sample window, look for steady state
+    if (_samples == 0x00) {
+        _output = 0;
+    } 
+    else if (_samples == 0xFF) {
+        _output = 1;
+    }
+
+
+    // Rising edge detection
+    if ((_output == 1) && (_output_last == 0)) {
+        _rising_flag++;
+        _state_counter = 0;
+    }
+
+    // Falling edge detection
+    else if ((_output == 0) && (_output_last == 1)) {
+        _falling_flag++;
+        _state_counter = 0;
+    }
+    
+    // steady state
+    else {
+        _state_counter++;
+    }
+    
+   // update the output
+    _output_last = _output;
+    
+}
+
+
+
+// return number of rising edges
+int DebouncedIn::rising(void) {
+    int return_value = _rising_flag; 
+    _rising_flag = 0;
+    return(return_value);
+}
+
+// return number of falling edges
+int DebouncedIn::falling(void) {
+    int return_value = _falling_flag; 
+    _falling_flag = 0;
+    return(return_value);
+}
+
+// return number of ticsk we've bene steady for
+int DebouncedIn::steady(void) {
+return(_state_counter);
+}
+
+// return the debounced status
+int DebouncedIn::read(void) {
+    return(_output);
+}
+
+// shorthand for read()
+DebouncedIn::operator int() {
+    return read();
+}
+
+
diff -r 000000000000 -r 721134a8164c DebouncedIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Wed Oct 29 08:10:05 2014 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+
+    class DebouncedIn {
+        public:      
+             DebouncedIn(PinName in);
+
+             int read (void);
+             operator int();
+              
+             int rising(void);
+             int falling(void);
+             int steady(void);
+              
+        private :    
+               // objects
+               DigitalIn _in;    
+               Ticker _ticker;
+
+               // function to take a sample, and update flags
+               void _sample(void);
+
+               // counters and flags
+               int _samples;
+               int _output;
+               int _output_last;
+               int _rising_flag;
+               int _falling_flag;
+               int _state_counter;
+
+    };
+    
\ No newline at end of file
diff -r 000000000000 -r 721134a8164c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 29 08:10:05 2014 +0000
@@ -0,0 +1,133 @@
+#include "mbed.h"
+#include "DebouncedIn.h"
+#include "C12832_lcd.h"
+
+Ticker tick;
+C12832_LCD lcd;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+DebouncedIn button(p12);
+
+volatile unsigned int event=0;
+float initTime = 0;
+bool startreaction = false;
+float reactionTime = 0;
+
+void reactionTick();
+void startTime();
+
+void RandomTime()
+{
+    float min = 0.8 / 6.3;
+    float max = 6.2 / 6.3;
+ 
+    initTime = min + ((float)rand()/RAND_MAX) * (max - min) * 6.3;
+}
+
+void startTimerStart()
+{
+    RandomTime();
+    tick.attach(&startTime, 0.1);
+}
+
+void reactionTimerStart()
+{
+    reactionTime = 0;
+    tick.attach(&reactionTick, 0.001);
+}
+
+void stopTimer()        
+{ 
+    tick.detach();
+}
+
+void ledsEin()   
+{ 
+    led1 = 1;
+    led2 = 1;
+    led3 = 1;
+    led4 = 1;
+}
+
+void ledsAus()   
+{ 
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+}
+
+void startTime()
+{
+    initTime -= 0.1;
+    
+    if(initTime <= 0)
+    {
+        startreaction = true;
+        ledsAus();
+        stopTimer();
+        reactionTimerStart();
+    }
+}
+
+void reactionTick()
+{
+    reactionTime += 0.001;
+}
+
+void doEvent()
+{
+    switch(event)
+    {
+     case 0: 
+        ledsAus();    
+        lcd.cls();
+        lcd.locate(0, 0);
+        lcd.printf("Reaktionstester: ");
+        break; 
+     case 1:
+        ledsEin();
+        lcd.printf("Running: ");
+        startTimerStart();
+        break;
+     case 2: 
+        if(startreaction)
+        {
+            stopTimer();
+            lcd.locate(0, 10);
+            lcd.printf("Zeit: %.3f s", reactionTime);
+        }
+        else
+        {
+            stopTimer();
+            lcd.locate(0, 10);
+            lcd.printf("Zu frueh!!!!");
+        }
+        break;  
+     case 3: 
+        event = 0;
+        ledsAus();    
+        lcd.cls();
+        lcd.locate(0, 0);
+        lcd.printf("Reaktionstester: ");
+        break;
+     default: 
+        break;  
+    }
+}
+
+int main()
+{    
+    doEvent();
+    
+    while(1) {  
+        if (button.rising()) {
+            event++;
+            doEvent();
+        }
+    }
+}
+
diff -r 000000000000 -r 721134a8164c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 29 08:10:05 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cb3d968589d8
\ No newline at end of file