We are making a bluetooth application for a vehicle.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Files at this revision

API Documentation at this revision

Comitter:
bobbaddeley
Date:
Wed Jul 19 14:38:04 2017 +0000
Parent:
29:e7d4922a4620
Commit message:
Getting alarms to work

Changed in this revision

bike_service.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
--- a/bike_service.h	Wed Jul 19 05:03:35 2017 +0000
+++ b/bike_service.h	Wed Jul 19 14:38:04 2017 +0000
@@ -24,17 +24,17 @@
         outputBytes(0),
         timestampBytes(),
         alarmBytes(0),
-        inputs(0x8EC6, inputBytes.getPointer(),
+        input_characteristic(0x8EC6, inputBytes.getPointer(),
                 inputBytes.getNumValueBytes(), inputBytes.getNumValueBytes(),
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        outputs(0x8EC7, outputBytes.getPointer(),
+        output_characteristic(0x8EC7, outputBytes.getPointer(),
                 outputBytes.getNumValueBytes(), outputBytes.getNumValueBytes(),
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        timestamp(0x8EC8, timestampBytes.getPointer(),
+        timestamp_characteristic(0x8EC8, timestampBytes.getPointer(),
                 timestampBytes.getNumValueBytes(), timestampBytes.getNumValueBytes(),
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        log(0x8EC9,0),
-        alarmcode(0x8ECA,alarmBytes.getPointer(),
+        log_characteristic(0x8EC9,0),
+        alarmcode_characteristic(0x8ECA,alarmBytes.getPointer(),
                 alarmBytes.getNumValueBytes(), alarmBytes.getNumValueBytes(),
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE){
         setupService();
@@ -48,7 +48,7 @@
      */
     void updateInputs(uint8_t value) {
         inputBytes.updateValue(value);
-        ble.gattServer().write(inputs.getValueHandle(), inputBytes.getPointer(), inputBytes.getNumValueBytes());
+        ble.gattServer().write(input_characteristic.getValueHandle(), inputBytes.getPointer(), inputBytes.getNumValueBytes());
     }
     
     /**
@@ -59,7 +59,7 @@
      */
     void updateOutputs(uint8_t value) {
         outputBytes.updateValue(value);
-        ble.gattServer().write(outputs.getValueHandle(), outputBytes.getPointer(), 1);
+        ble.gattServer().write(output_characteristic.getValueHandle(), outputBytes.getPointer(), 1);
     }
     
     /**
@@ -70,11 +70,11 @@
      */
     void updateAlarmCode(uint8_t value) {
         alarmBytes.updateAlarm(value);
-        ble.gattServer().write(alarmcode.getValueHandle(), alarmBytes.getPointer(), 1);
+        ble.gattServer().write(alarmcode_characteristic.getValueHandle(), alarmBytes.getPointer(), 1);
     }
     
     void updateTimestamp() {
-        ble.gattServer().write(timestamp.getValueHandle(), timestampBytes.getPointer(), 1);
+        ble.gattServer().write(timestamp_characteristic.getValueHandle(), timestampBytes.getPointer(), 1);
     }
 
     /**
@@ -85,17 +85,17 @@
      *     Information about the characterisitc being updated.
      */
     virtual void onDataWritten(const GattWriteCallbackParams *params) {
-        if (params->handle == outputs.getValueAttribute().getHandle()) {
+        if (params->handle == output_characteristic.getValueAttribute().getHandle()) {
             updateOutputs(*(params->data));
         }
-        else if (params->handle == alarmcode.getValueAttribute().getHandle()) {
+        else if (params->handle == alarmcode_characteristic.getValueAttribute().getHandle()) {
             updateAlarmCode(*(params->data));
         }
     }
 
 protected:
     void setupService(void) {
-        GattCharacteristic *charTable[] = {&inputs, &outputs, &timestamp, &log, &alarmcode};
+        GattCharacteristic *charTable[] = {&input_characteristic, &output_characteristic, &timestamp_characteristic, &log_characteristic, &alarmcode_characteristic};
         GattService         bikeService(0x8EC5, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
         ble.addService(bikeService);
@@ -117,6 +117,10 @@
         uint8_t       *getPointer(void) {
             return valueBytes;
         }
+        
+        uint8_t getValue(){
+            return valueBytes[0];
+        }
 
         const uint8_t *getPointer(void) const {
             return valueBytes;
@@ -202,11 +206,11 @@
     TimestampBytes  timestampBytes;
     AlarmBytes      alarmBytes;
 
-    GattCharacteristic                   inputs;
-    GattCharacteristic                   outputs;
-    GattCharacteristic                   timestamp;
-    GattCharacteristic                   log;
-    GattCharacteristic                   alarmcode;
+    GattCharacteristic                   input_characteristic;
+    GattCharacteristic                   output_characteristic;
+    GattCharacteristic                   timestamp_characteristic;
+    GattCharacteristic                   log_characteristic;
+    GattCharacteristic                   alarmcode_characteristic;
 };
 
 #endif /* #ifndef __BLE_BIKE_SERVICE_H__*/
\ No newline at end of file
--- a/main.cpp	Wed Jul 19 05:03:35 2017 +0000
+++ b/main.cpp	Wed Jul 19 14:38:04 2017 +0000
@@ -48,9 +48,8 @@
 //InterruptIn interruptEventSensor_0(PC_0); // Vibration
 //InterruptIn interruptEventSensor_1(PC_1); // Tilt
 
-unsigned char byteOut = 0; 
-unsigned char prevByteOut = 0;
-unsigned char byteIn = 0x08;
+unsigned char byteIn = 0; 
+unsigned char prevByteIn = 0;
 
 uint8_t inputs = 0x00;
 uint8_t outputs = 0x00;
@@ -85,10 +84,51 @@
 }
 
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
-        //if (params->handle == BikeService.outputs.getValueAttribute().getHandle()) {
-            //BOB Should update the outputs.
-            outputs = *(params->data);
-        //}
+    //if (params->handle == BikeService.output_characteristic.getValueAttribute().getHandle()) {
+        outputs = *(params->data);
+        if (outputs & 0x01){
+            led_0 = 1;
+        }
+        if ((outputs & 0x01) == 0){
+            led_0 = 0;
+        }
+            
+        if (outputs & 0x02){
+            led_1 = 1;
+        }
+        if ((outputs & 0x02) == 0){
+            led_1 = 0;
+        }
+            
+        if (outputs & 0x04){
+            buzzer = 1;
+            }
+        if ((outputs & 0x04) == 0){
+            buzzer = 0;
+        }
+            
+        if (outputs & 0x08){
+            rumblr = 1;
+        }
+        if ((outputs & 0x08) == 0){
+            rumblr = 0;
+        }
+            
+        if (outputs & 0x10){
+            alarm_activated = 1;
+        }
+        if ((outputs & 0x10) == 0){
+            alarm_activated = 0;
+            alarm_on = 0;
+        }
+        
+        if (outputs & 0x20){
+            alarm_on = 1;
+        }
+        if ((outputs & 0x20) == 0){
+            alarm_on = 0;
+        }
+    //}
 }
 
 void alarm() {
@@ -164,62 +204,24 @@
         // check for trigger from periodicCallback()
         if (triggerSensorPolling && ble.getGapState().connected) {
         //if (ble.getGapState().connected) {
-            if (outputs & 0x01){
-                led_0 = 1;
-            }
-            if ((outputs & 0x01) == 0){
-                led_0 = 0;
-            }
-                
-            if (outputs & 0x02){
-                led_1 = 1;
-            }
-            if ((outputs & 0x02) == 0){
-                led_1 = 0;
-            }
-                
-            if (outputs & 0x04){
-                buzzer = 1;
-                }
-            if ((outputs & 0x04) == 0){
-                buzzer = 0;
-            }
-                
-            if (outputs & 0x08){
-                rumblr = 1;
-            }
-            if ((outputs & 0x08) == 0){
-                rumblr = 0;
-            }
-                
-            if (outputs & 0x10){
-                alarm_activated = 1;
-            }
-            if ((outputs & 0x10) == 0){
-                alarm_activated = 0;
-            }
             
-            if (outputs & 0x20){
-                alarm_on = 1;
-            }
-            if ((outputs & 0x20) == 0){
-                alarm_on = 0;
-            }
             
             triggerSensorPolling = false;
-            byteOut = sixButtons & buttonMask;
+            byteIn = sixButtons & buttonMask;
             // update the ble with the inputs
-            bikeService.updateInputs(byteOut);
-            bikeService.updateTimestamp();
-            //if ((byteOut & 0x01) && alarm_activated==1) {
-            //    alarm_on = 1;
-            // }
-            if (byteOut != prevByteOut){
+            //bikeService.updateInputs(byteOut);
+            //bikeService.updateTimestamp();
+            if (((byteIn & 0x01)==0) && alarm_activated==1 && alarm_on==0) {
+                alarm_on = 1;
+                outputs = outputs | 0x20;
+                bikeService.updateOutputs(outputs);
+             }
+            if (byteIn != prevByteIn){
                 // update the ble with the inputs
-                bikeService.updateInputs(byteOut);
+                bikeService.updateInputs(byteIn);
                 bikeService.updateTimestamp();
             }
-            prevByteOut = byteOut;
+            prevByteIn = byteIn;
              
         } else {
             ble.waitForEvent(); // low power wait for event
@@ -244,7 +246,7 @@
     
     Ticker ticker_CallBack;
     //ticker.attach(periodicCallback, 1); // blink LED every second
-    ticker_CallBack.attach(periodicCallback, 0.01);
+    ticker_CallBack.attach(periodicCallback, 0.1);
     
     BLE::Instance().init(bleInitComplete);
 }