cycle speed and cadence service

Dependencies:   BLE_API mbed nRF51822

Fork of Bluetooth_Heart_Rate_Monitor_dummy by Icarus Sensors

Revision:
70:957972b63ae1
Parent:
69:2018e8c1d998
Child:
71:832536f63f0b
--- a/main.cpp	Fri May 08 10:09:55 2015 +0000
+++ b/main.cpp	Sun May 24 20:17:25 2015 +0000
@@ -25,7 +25,8 @@
  * interval.*/
 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
 #define TICK_TIME 1.0/1024.0
-#define DEBOUNCE_TIME 30
+#define DEBOUNCE_TIME 50
+#define BAUD_RATE 9600
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
@@ -36,8 +37,8 @@
 static volatile bool updateWheel = false; 
 static volatile bool updateCrank = false;
 
-InterruptIn wheelSwitch(p1);
-InterruptIn crankSwitch(p2);
+InterruptIn wheelSwitch(P0_22);
+InterruptIn crankSwitch(P0_23);
 
 Timer wheelDebounce;
 Timer crankDebounce;
@@ -59,39 +60,68 @@
     counter++;
 }
 
-void incrementWheel() {
-    if (wheelDebounce.read_ms()>DEBOUNCE_TIME){ //only allow toggle if debounce timer has passed 200ms
-        wheelDebounce.reset();       //restart timer when toggle is performed
+void wheelSwitchFall() {
+    int time = wheelDebounce.read_ms();
+    wheelDebounce.stop();
+    wheelDebounce.reset();
+    if (time>DEBOUNCE_TIME){ //only allow toggle if debounce timer has passed 200ms
+        //restart timer when toggle is performed        
+        led1= !led1;
         wheelCounter++;
         wheelEvent=counter;
-        led1= !led1;
         updateWheel=true;
     }
+    
+}
+
+void wheelSwitchRise() {    
+    wheelDebounce.reset();       //restart timer when toggle is performed    
+    wheelDebounce.start();
 }
 
-void incrementCrank() {
-    if (crankDebounce.read_ms()>DEBOUNCE_TIME){ //only allow toggle if debounce timer has passed 200ms
-        crankDebounce.reset();       //restart timer when toggle is performed
+void crankSwithFall() {
+    int time = crankDebounce.read_ms();
+//    printf("rise: %d \r\n",time);
+    crankDebounce.stop();
+    crankDebounce.reset();
+    if (time>DEBOUNCE_TIME){ //only allow toggle if debounce timer has passed 200ms
+//        printf("TICK!!!!!!!!!");        
+//        led1= !led1;       //restart timer when toggle is performed
         crankCounter++;
-        crankEvent=counter;
-        led1= !led1;
+        crankEvent=counter;        
         updateCrank=true;
     }
+    
 }
 
+void crankSwithRise() {
+//    printf("fall: %d \r\n",crankDebounce.read_ms());
+    crankDebounce.reset();
+    crankDebounce.start();
+}
+
+
 
 int main(void)
 {
-    led1 = 1;
+    
+//    Serial s(P0_5, P0_6); //default for nrf51 is p0.09 p0.11
+//    s.baud(BAUD_RATE);
+    
+//    led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, TICK_TIME); 
 
     wheelDebounce.start();
     crankDebounce.start();
-     
-    wheelSwitch.rise(&incrementWheel);
-    crankSwitch.rise(&incrementCrank);
-
+    
+    wheelSwitch.rise(&wheelSwitchRise); 
+    wheelSwitch.fall(&wheelSwitchFall);
+    
+    crankSwitch.rise(&crankSwithRise);
+    crankSwitch.fall(&crankSwithFall);
+    
+    
     ble.init();
     ble.onDisconnection(disconnectionCallback);
 
@@ -110,7 +140,8 @@
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.setAdvertisingInterval(1000);
     ble.startAdvertising();
-
+    
+//    printf("starting\n\r");
     // infinite loop
     while (1) {
         // check for trigger from periodicCallback()
@@ -124,6 +155,9 @@
                 speedService.updateCrank(crankCounter,crankEvent);
                 updateCrank=false;
             }
+            
+//            printf("wheel: %d crank: %d\r\n",wheelCounter,crankCounter);
+            
         } else {
             ble.waitForEvent(); // low power wait for event
         }