Firmware for Keewi v1 electronic board
Dependencies: BLE_API mbed nRF51822
Fork of Keewi_v1 by
Diff: main.cpp
- Revision:
- 2:8ebada2e4924
- Parent:
- 1:e60b7762dd79
- Child:
- 3:6e08445f54f3
--- a/main.cpp Fri Jul 18 10:42:51 2014 +0000 +++ b/main.cpp Fri Jul 18 16:54:16 2014 +0000 @@ -109,12 +109,19 @@ static uint16_t cscCumulativeCrankRevolutions = 0; static uint16_t cscLastCrankEventTime = 0; // Unit is second & has a resolution of 1/1024s. static uint8_t csc_mes_flags = (cscCrankRevolutionDataSPresent << 1) + cscWheelRevolutionDataSPresent; -static uint8_t csc_mes[] = {csc_mes_flags, cscCumulativeWheelRevolutions & 0x000000FF, cscCumulativeWheelRevolutions & 0x0000FF00, cscCumulativeWheelRevolutions & 0x00FF0000, cscCumulativeWheelRevolutions & 0xFF000000, cscLastWheelEventTime & 0x00FF, cscLastWheelEventTime & 0xFF00, cscCumulativeCrankRevolutions & 0x00FF, cscCumulativeCrankRevolutions & 0xFF00, cscLastCrankEventTime & 0x00FF, cscLastCrankEventTime & 0xFF00 }; -//static uint8_t csc_mes[5] = { (cscCrankRevolutionDataSPresent << 1) + cscWheelRevolutionDataSPresent, cscCumulativeWheelRevolutions & 0x000000FF, cscLastWheelEventTime & 0x00FF, cscCumulativeCrankRevolutions & 0x00FF, cscLastCrankEventTime & 0x00FF }; - +static uint8_t csc_mes[] = { csc_mes_flags, + cscCumulativeWheelRevolutions & 0x000000FF , + cscCumulativeWheelRevolutions & 0x0000FF00 , + cscCumulativeWheelRevolutions & 0x00FF0000 , + cscCumulativeWheelRevolutions & 0xFF000000 , + cscLastWheelEventTime & 0x00FF , + cscLastWheelEventTime & 0xFF00 , + cscCumulativeCrankRevolutions & 0x00FF , + cscCumulativeCrankRevolutions & 0xFF00 , + cscLastCrankEventTime & 0x00FF , + cscLastCrankEventTime & 0xFF00 }; GattCharacteristic cscMeasurement(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR, csc_mes, sizeof(csc_mes), sizeof(csc_mes), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - static bool cscWheelRevolutionDataSupported = true; static bool cscWCrankRevolutionDataSupported = true; static bool cscMultipleSensorLocationsSupported = false; @@ -122,27 +129,93 @@ static uint8_t csc_feat[2] = { 0x00, csc_feat_flags }; GattCharacteristic cscFeature(GattCharacteristic::UUID_CSC_FEATURE_CHAR, csc_feat, sizeof(csc_feat), sizeof(csc_feat), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); - -static const uint8_t location = 0x09; /* Front Hub */ +/* Sensor location */ +enum BLE_SENSOR_LOCATION_LIST { /**< possible location for the CSC sensor. */ + BLE_SENSOR_LOCATION_OTHER , + BLE_SENSOR_LOCATION_TOP_OF_SHOE , + BLE_SENSOR_LOCATION_IN_SHOE , + BLE_SENSOR_LOCATION_HIP , + BLE_SENSOR_LOCATION_FRONT_WHEEL , + BLE_SENSOR_LOCATION_LEFT_CRANK , + BLE_SENSOR_LOCATION_RIGHT_CRANK , + BLE_SENSOR_LOCATION_LEFT_PEDAL , + BLE_SENSOR_LOCATION_RIGHT_PEDAL , + BLE_SENSOR_LOCATION_FRONT_HUB , + BLE_SENSOR_LOCATION_REAR_DROPOUT, + BLE_SENSOR_LOCATION_CHAINSTAY , + BLE_SENSOR_LOCATION_REAR_WHEEL , + BLE_SENSOR_LOCATION_REAR_HUB , + BLE_SENSOR_LOCATION_CHEST +}; +static uint8_t keewi_v1_location_possibilities[] = { /**< possible location for Keewi. */ + BLE_SENSOR_LOCATION_OTHER , + BLE_SENSOR_LOCATION_FRONT_WHEEL , + BLE_SENSOR_LOCATION_LEFT_CRANK , + BLE_SENSOR_LOCATION_RIGHT_CRANK , + BLE_SENSOR_LOCATION_FRONT_HUB , + BLE_SENSOR_LOCATION_REAR_WHEEL , + BLE_SENSOR_LOCATION_REAR_HUB +}; +static const uint8_t location = BLE_SENSOR_LOCATION_FRONT_WHEEL; // Keewi v1 speed sensor default location GattCharacteristic cscLocation(GattCharacteristic::UUID_SENSOR_LOCATION, (uint8_t *)&location, sizeof(location), sizeof(location), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); - -/* TODO: implement a Write charac. cf. wheelSizeCallback() + +// SC Control Point +//TODO: implement a Write charac. cf. paramUpdateCallback() GattCharacteristic cscCtrlPoint(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); -*/ -GattCharacteristic *cscChars[] = {&cscMeasurement, &cscFeature, }; //&cscLocation, &cscControlPoint, +GattCharacteristic *cscChars[] = {&cscMeasurement, &cscFeature, &cscLocation, }; // &cscControlPoint, GattService cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, cscChars, sizeof(cscChars) / sizeof(GattCharacteristic *)); -static const uint16_t uuid16_list[] = {GattService::UUID_CYCLING_SPEED_AND_CADENCE}; +/* Custom LBS service (led & button service) + Switch: Front & rear lights + Led: Alarm unlock +*/ +#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), + 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), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); +GattCharacteristic *lbsChars[] = {&lbsSwitches, &lbsIndicators, }; +#define LBS_UUID_SERVICE 0x1523 +GattService lbsService(LBS_UUID_SERVICE, lbsChars, sizeof(lbsChars) / sizeof(GattCharacteristic *)); -void wheelSizeCallback() { - DEBUG("Wheel size request ack!"); +/* Custom UART Service for sending ride data + +*/ +#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). */ + + + +/* List of all available services */ +static const uint16_t uuid16_list[] = { + GattService::UUID_CYCLING_SPEED_AND_CADENCE, + GattService::UUID_BATTERY_SERVICE, + GattService::UUID_CURRENT_TIME_SERVICE, + LBS_UUID_SERVICE, +}; + + +void paramUpdateCallback() { + DEBUG("Param change request ack!"); return; } - + void disconnectionCallback(void) { @@ -159,22 +232,26 @@ led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ if (ble.getGapState().connected) { - DEBUG("Ble connected!"); led1 = 1; - /* Update the CSC measurement */ - cscCumulativeWheelRevolutions+=3; - cscCumulativeCrankRevolutions+=2; - cscLastWheelEventTime++; - cscLastCrankEventTime++; - - // Update the battery level + // Simulate evolution of battery level batteryLevel--; ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel)); + // Simulate evolution of Speed & Cadence + cscCumulativeWheelRevolutions+=5; + cscCumulativeCrankRevolutions+=2; + cscLastWheelEventTime+=1000; + cscLastCrankEventTime+=1000; uint8_t new_csc_mes[] = {csc_mes_flags, cscCumulativeWheelRevolutions & 0x000000FF, cscCumulativeWheelRevolutions & 0x0000FF00, cscCumulativeWheelRevolutions & 0x00FF0000, cscCumulativeWheelRevolutions & 0xFF000000, cscLastWheelEventTime & 0x00FF, cscLastWheelEventTime & 0xFF00, cscCumulativeCrankRevolutions & 0x00FF, cscCumulativeCrankRevolutions & 0xFF00, cscLastCrankEventTime & 0x00FF, cscLastCrankEventTime & 0xFF00 }; + ble.updateCharacteristicValue(cscMeasurement.getHandle(), new_csc_mes, sizeof(new_csc_mes)); - 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)); + + DEBUG("led val = %d - mes val = %d\r\n", csc_feat_flags, csc_mes_flags); + } } @@ -187,7 +264,7 @@ DEBUG("Initialized the nRF51822\n\r"); ble.init(); ble.onDisconnection(disconnectionCallback); - ble.onDataSent(wheelSizeCallback); + ble.onDataSent(paramUpdateCallback); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // @@ -200,17 +277,14 @@ /* Setup services */ ble.addService(cscService); - /*ble.addService(batteryService); + ble.addService(batteryService); //ble.addService(firmwareService); ble.addService(manufacturerService); - ble.addService(modelService); + ble.addService(lbsService); ble.addService(temperatureService); -*/ + DEBUG("Initialized the BLE stack\n\r"); -DEBUG("feat val = %d - mes val = %d", csc_feat_flags, csc_mes_flags); -DEBUG("\r\n"); - while (true) { ble.waitForEvent(); }