Firmware for Keewi v1 electronic board

Dependencies:   BLE_API mbed nRF51822

Fork of Keewi_v1 by Clément Bertolini

Revision:
3:6e08445f54f3
Parent:
2:8ebada2e4924
--- a/main.cpp	Fri Jul 18 16:54:16 2014 +0000
+++ b/main.cpp	Wed Aug 20 08:59:22 2014 +0000
@@ -169,38 +169,52 @@
 GattCharacteristic *cscChars[] = {&cscMeasurement, &cscFeature, &cscLocation, };     // &cscControlPoint,
 GattService        cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, cscChars, sizeof(cscChars) / sizeof(GattCharacteristic *));
 
-/*  Custom LBS service (led & button service)
+/*  Led & button service (LBS)
     Switch: Front & rear lights
-    Led: Alarm unlock
+    Led: Alarm state
 */
 #define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00}
 #define LBS_UUID_LED_CHAR 0x1525
 #define LBS_UUID_BUTTON_CHAR 0x1524
-static bool alarmState = false;
 static bool rLightState = true;
 static bool fLightState = true;
-static uint8_t lbs_sw_flags = (fLightState << 2) + (rLightState << 1) + alarmState;
-GattCharacteristic lbsSwitches(LBS_UUID_BUTTON_CHAR,
-                               (uint8_t *)&lbs_sw_flags, sizeof(lbs_sw_flags), sizeof(lbs_sw_flags),
+static uint8_t lbs_butt_flags = fLightState + (rLightState << 1);
+GattCharacteristic lbsButtons(LBS_UUID_BUTTON_CHAR,
+                               (uint8_t *)&lbs_butt_flags, sizeof(lbs_butt_flags), sizeof(lbs_butt_flags),
                                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
-GattCharacteristic lbsIndicators(LBS_UUID_LED_CHAR,
-                               (uint8_t *)&location, sizeof(location), sizeof(location),
+static bool alarmState = false;
+static uint8_t lbs_led_flags = alarmState;
+GattCharacteristic lbsLeds(LBS_UUID_LED_CHAR,
+                               (uint8_t *)&lbs_led_flags, sizeof(lbs_led_flags), sizeof(lbs_led_flags),
                                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-GattCharacteristic *lbsChars[] = {&lbsSwitches, &lbsIndicators, };
+GattCharacteristic *lbsChars[] = {&lbsButtons, &lbsLeds, };
 #define LBS_UUID_SERVICE 0x1523
 GattService        lbsService(LBS_UUID_SERVICE, lbsChars, sizeof(lbsChars) / sizeof(GattCharacteristic *));
 
-/* Custom UART Service for sending ride data
-
+/*  Nordic UART Service (nus)
+    TX: sending ride data
+    RX: receiving ack, unlocking alarm
 */
 #define BLE_UUID_NUS_SERVICE            0x0001                       /**< The UUID of the Nordic UART Service. */
 #define BLE_UUID_NUS_TX_CHARACTERISTIC  0x0002                       /**< The UUID of the TX Characteristic. */
 #define BLE_UUID_NUS_RX_CHARACTERISTIC  0x0003                       /**< The UUID of the RX Characteristic. */
 #define BLE_NUS_MAX_DATA_LEN            (GATT_MTU_SIZE_DEFAULT - 3)  /**< Maximum length of data (in bytes) that can be transmitted by the Nordic UART service module to the peer. */
 #define BLE_NUS_MAX_RX_CHAR_LEN         BLE_NUS_MAX_DATA_LEN         /**< Maximum length of the RX Characteristic (in bytes). */
-#define BLE_NUS_MAX_TX_CHAR_LEN         20                           /**< Maximum length of the TX Characteristic (in bytes). */
-
-
+#define BLE_NUS_MAX_TX_CHAR_LEN         9                           /**< Maximum length of the TX Characteristic (in bytes). */
+static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_tx_uuid[]   = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_rx_uuid[]   = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e};
+uint8_t nus_rx_buffer[BLE_NUS_MAX_RX_CHAR_LEN] = {0,};
+GattCharacteristic nusRx(uart_rx_uuid,
+                               (uint8_t *)&nus_rx_buffer, sizeof(nus_rx_buffer), BLE_NUS_MAX_RX_CHAR_LEN,
+                               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+uint8_t nus_tx_buffer[BLE_NUS_MAX_TX_CHAR_LEN] = {0,};
+GattCharacteristic nusTx(uart_tx_uuid,
+                               (uint8_t *)&nus_tx_buffer, sizeof(nus_tx_buffer), BLE_NUS_MAX_TX_CHAR_LEN,
+                               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+GattCharacteristic *nusChars[] = {&nusTx, &nusRx, };
+GattService        nusService(uart_base_uuid, nusChars, sizeof(nusChars) / sizeof(GattCharacteristic *));
 
 /* List of all available services */
 static const uint16_t uuid16_list[] = {
@@ -208,6 +222,7 @@
     GattService::UUID_BATTERY_SERVICE,
     GattService::UUID_CURRENT_TIME_SERVICE,
     LBS_UUID_SERVICE,
+    BLE_UUID_NUS_SERVICE,
 };
 
 
@@ -247,10 +262,11 @@
         ble.updateCharacteristicValue(cscMeasurement.getHandle(), new_csc_mes, sizeof(new_csc_mes));
         
         // Notify alarm & lights status
-        static uint8_t new_lbs_sw_flags = (fLightState << 2) + (rLightState << 1) + alarmState;
-        ble.updateCharacteristicValue(lbsSwitches.getHandle(), new_lbs_sw_flags, sizeof(new_lbs_sw_flags));
+        alarmState ^= 1;
+        uint8_t new_lbs_butt_flags = (uint8_t)alarmState;
+        ble.updateCharacteristicValue(lbsLeds.getHandle(), &new_lbs_butt_flags, sizeof(new_lbs_butt_flags));
         
-        DEBUG("led val = %d - mes val = %d\r\n", csc_feat_flags, csc_mes_flags);
+        DEBUG("led val = %d\r\n", new_lbs_butt_flags);
 
     }
 }
@@ -268,7 +284,10 @@
 
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
+                                        (uint8_t*)uuid16_list, sizeof(uuid16_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                        (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
     ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -281,7 +300,7 @@
     //ble.addService(firmwareService);
     ble.addService(manufacturerService);
     ble.addService(lbsService);
-    ble.addService(temperatureService);
+    ble.addService(nusService);
 
     DEBUG("Initialized the BLE stack\n\r");