Reaktionstester mit Pointer

Dependencies:   C12832_lcd mbed

Files at this revision

API Documentation at this revision

Comitter:
philipp19961
Date:
Wed Nov 05 07:40:38 2014 +0000
Commit message:
ReaktionsTester mit Pointer like WedlMethode

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832_lcd.lib	Wed Nov 05 07:40:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.cpp	Wed Nov 05 07:40:38 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();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Wed Nov 05 07:40:38 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 05 07:40:38 2014 +0000
@@ -0,0 +1,135 @@
+#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);
+
+int init();
+int ledsOn();
+int auswertung();
+
+int(*action[3])()= {init, ledsOn, auswertung };  
+
+volatile int event=0;
+float initTime = 0;
+bool startreaction;
+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 stopTimer()        
+{ 
+    tick.detach();
+}
+
+void ledsEin()   
+{ 
+    led1 = 1;
+    led2 = 1;
+    led3 = 1;
+    led4 = 1;
+}
+
+void ledsAus()   
+{ 
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+}
+
+int init()
+{
+    startreaction = false;
+    ledsAus();    
+    lcd.cls();
+    lcd.locate(0, 0);
+    lcd.printf("Reaktionstester: ");
+    return 0;    
+}
+
+int ledsOn()
+{
+    ledsEin();
+    lcd.printf("Running: ");
+    startTimerStart();
+    return 0;    
+}
+
+int auswertung()
+{
+    if(startreaction)
+    {
+        stopTimer();
+        lcd.locate(0, 10);
+        lcd.printf("Zeit: %.3f s", reactionTime);
+    }
+    else
+    {
+        stopTimer();
+        lcd.locate(0, 10);
+        lcd.printf("Zu frueh!!!!");
+    }
+    event = -1;
+    return 0;    
+}
+
+void reactionTimerStart()
+{
+    reactionTime = 0;
+    tick.attach(&reactionTick, 0.001);
+}
+
+void startTime()
+{
+    initTime -= 0.1;
+    
+    if(initTime <= 0)
+    {
+        startreaction = true;
+        ledsAus();
+        stopTimer();
+        reactionTimerStart();
+    }
+}
+
+void reactionTick()
+{
+    reactionTime += 0.001;
+}
+
+int main()
+{    
+    (action[event])();
+    
+    while(1) {  
+        if (button.rising()) {
+            event++;
+            (action[event])();
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Nov 05 07:40:38 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file