embedded software for tortuga bike
Dependencies: BLE_API BikeControl DataLogging X_NUCLEO_IDB0XA1 _24LCXXX mbed
main.cpp@1:ffdec767aa55, 2016-07-18 (annotated)
- Committer:
- ptuytsch
- Date:
- Mon Jul 18 09:33:08 2016 +0000
- Revision:
- 1:ffdec767aa55
- Parent:
- 0:0803cdf04c32
- Child:
- 2:7eb0bb74a8b6
At working stage with eeprom and BLE combined, adding a start to the bikeControl library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 0:0803cdf04c32 | 1 | #include "mbed.h" |
ptuytsch | 0:0803cdf04c32 | 2 | #include "BikeData.h" |
ptuytsch | 0:0803cdf04c32 | 3 | #include "BatteryState.h" |
ptuytsch | 0:0803cdf04c32 | 4 | #include "ble/BLE.h" |
ptuytsch | 0:0803cdf04c32 | 5 | #include "ble/services/BikeService.h" |
ptuytsch | 0:0803cdf04c32 | 6 | #include "ble/services/BikeBatteryService.h" |
ptuytsch | 0:0803cdf04c32 | 7 | |
ptuytsch | 0:0803cdf04c32 | 8 | #define trailerBatteryPin PB_0 |
ptuytsch | 0:0803cdf04c32 | 9 | #define bikeBatteryPin PC_5 |
ptuytsch | 0:0803cdf04c32 | 10 | #define auxiliaryBatteryPin PC_4 |
ptuytsch | 0:0803cdf04c32 | 11 | |
ptuytsch | 0:0803cdf04c32 | 12 | /*************************************************************************** |
ptuytsch | 0:0803cdf04c32 | 13 | VARIABLES |
ptuytsch | 0:0803cdf04c32 | 14 | ****************************************************************************/ |
ptuytsch | 0:0803cdf04c32 | 15 | |
ptuytsch | 0:0803cdf04c32 | 16 | |
ptuytsch | 1:ffdec767aa55 | 17 | //LIGHT |
ptuytsch | 1:ffdec767aa55 | 18 | DigitalOut lightFront(PB_14); |
ptuytsch | 1:ffdec767aa55 | 19 | DigitalOut lightBack(PA_11); |
ptuytsch | 1:ffdec767aa55 | 20 | DigitalOut lightLeft(PC_6); |
ptuytsch | 1:ffdec767aa55 | 21 | DigitalOut lightRight(PC_8); |
ptuytsch | 1:ffdec767aa55 | 22 | |
ptuytsch | 1:ffdec767aa55 | 23 | |
ptuytsch | 0:0803cdf04c32 | 24 | |
ptuytsch | 0:0803cdf04c32 | 25 | //3 battery states |
ptuytsch | 1:ffdec767aa55 | 26 | static BatteryState *trailerBattery; |
ptuytsch | 1:ffdec767aa55 | 27 | static BatteryState *bikeBattery; |
ptuytsch | 1:ffdec767aa55 | 28 | static BatteryState *auxiliaryBattery; |
ptuytsch | 0:0803cdf04c32 | 29 | |
ptuytsch | 0:0803cdf04c32 | 30 | //BLE VARIABLES |
ptuytsch | 0:0803cdf04c32 | 31 | static char DEVICE_NAME[] = "TORTUGA"; //Define default name of the BLE device |
ptuytsch | 0:0803cdf04c32 | 32 | static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID, |
ptuytsch | 0:0803cdf04c32 | 33 | //GattService::UUID_DEVICE_INFORMATION_SERVICE, |
ptuytsch | 0:0803cdf04c32 | 34 | BatteryService::BATTERY_SERVICE_UUID, |
ptuytsch | 0:0803cdf04c32 | 35 | }; // List of the Service UUID's that are used. |
ptuytsch | 0:0803cdf04c32 | 36 | |
ptuytsch | 0:0803cdf04c32 | 37 | //Pointers to diferent objects and BLE services. |
ptuytsch | 0:0803cdf04c32 | 38 | static BikeData *bd; |
ptuytsch | 0:0803cdf04c32 | 39 | static BikeService *bikeServicePtr; |
ptuytsch | 0:0803cdf04c32 | 40 | //static DeviceInformationService *DISptr; |
ptuytsch | 0:0803cdf04c32 | 41 | static BatteryService *batSerPtr; |
ptuytsch | 0:0803cdf04c32 | 42 | |
ptuytsch | 0:0803cdf04c32 | 43 | //bool that is set to true each second. |
ptuytsch | 0:0803cdf04c32 | 44 | static bool update = false; |
ptuytsch | 0:0803cdf04c32 | 45 | static bool updateHalf = false; |
ptuytsch | 0:0803cdf04c32 | 46 | |
ptuytsch | 1:ffdec767aa55 | 47 | /*************************************************************************** |
ptuytsch | 1:ffdec767aa55 | 48 | CONTROL FUNCTIONS |
ptuytsch | 1:ffdec767aa55 | 49 | ****************************************************************************/ |
ptuytsch | 1:ffdec767aa55 | 50 | void runTestLight() |
ptuytsch | 1:ffdec767aa55 | 51 | { |
ptuytsch | 1:ffdec767aa55 | 52 | printf("front\n"); |
ptuytsch | 1:ffdec767aa55 | 53 | lightFront =1; |
ptuytsch | 1:ffdec767aa55 | 54 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 55 | lightFront = 0; |
ptuytsch | 1:ffdec767aa55 | 56 | printf("back\n"); |
ptuytsch | 1:ffdec767aa55 | 57 | lightBack = 1; |
ptuytsch | 1:ffdec767aa55 | 58 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 59 | lightBack = 0; |
ptuytsch | 1:ffdec767aa55 | 60 | printf("left\n"); |
ptuytsch | 1:ffdec767aa55 | 61 | lightLeft = 1; |
ptuytsch | 1:ffdec767aa55 | 62 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 63 | lightLeft = 0; |
ptuytsch | 1:ffdec767aa55 | 64 | printf("right\n"); |
ptuytsch | 1:ffdec767aa55 | 65 | lightRight = 1; |
ptuytsch | 1:ffdec767aa55 | 66 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 67 | lightRight = 0; |
ptuytsch | 1:ffdec767aa55 | 68 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 69 | } |
ptuytsch | 0:0803cdf04c32 | 70 | |
ptuytsch | 0:0803cdf04c32 | 71 | /*************************************************************************** |
ptuytsch | 0:0803cdf04c32 | 72 | BLE FUNCTIONS |
ptuytsch | 0:0803cdf04c32 | 73 | ****************************************************************************/ |
ptuytsch | 0:0803cdf04c32 | 74 | |
ptuytsch | 0:0803cdf04c32 | 75 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
ptuytsch | 0:0803cdf04c32 | 76 | { |
ptuytsch | 0:0803cdf04c32 | 77 | //restart advertising when disconnected |
ptuytsch | 0:0803cdf04c32 | 78 | BLE::Instance().gap().startAdvertising(); |
ptuytsch | 0:0803cdf04c32 | 79 | } |
ptuytsch | 0:0803cdf04c32 | 80 | |
ptuytsch | 0:0803cdf04c32 | 81 | void periodicCallback(void) |
ptuytsch | 0:0803cdf04c32 | 82 | { |
ptuytsch | 0:0803cdf04c32 | 83 | //Set update high to send new data over BLE |
ptuytsch | 0:0803cdf04c32 | 84 | update = true; |
ptuytsch | 0:0803cdf04c32 | 85 | } |
ptuytsch | 0:0803cdf04c32 | 86 | |
ptuytsch | 0:0803cdf04c32 | 87 | |
ptuytsch | 0:0803cdf04c32 | 88 | //This function is called when the ble initialization process has failled |
ptuytsch | 0:0803cdf04c32 | 89 | void onBleInitError(BLE &ble, ble_error_t error) |
ptuytsch | 0:0803cdf04c32 | 90 | { |
ptuytsch | 0:0803cdf04c32 | 91 | /* Initialization error handling should go here */ |
ptuytsch | 0:0803cdf04c32 | 92 | printf("initialization error!\n"); |
ptuytsch | 0:0803cdf04c32 | 93 | } |
ptuytsch | 0:0803cdf04c32 | 94 | |
ptuytsch | 0:0803cdf04c32 | 95 | |
ptuytsch | 0:0803cdf04c32 | 96 | //Callback triggered when the ble initialization process has finished |
ptuytsch | 0:0803cdf04c32 | 97 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ptuytsch | 0:0803cdf04c32 | 98 | { |
ptuytsch | 0:0803cdf04c32 | 99 | BLE& ble = params->ble; |
ptuytsch | 0:0803cdf04c32 | 100 | ble_error_t error = params->error; |
ptuytsch | 0:0803cdf04c32 | 101 | |
ptuytsch | 0:0803cdf04c32 | 102 | if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ |
ptuytsch | 0:0803cdf04c32 | 103 | printf("BLE error\n"); |
ptuytsch | 0:0803cdf04c32 | 104 | onBleInitError(ble, error); |
ptuytsch | 0:0803cdf04c32 | 105 | return; |
ptuytsch | 0:0803cdf04c32 | 106 | } |
ptuytsch | 0:0803cdf04c32 | 107 | |
ptuytsch | 0:0803cdf04c32 | 108 | /* Ensure that it is the default instance of BLE */ |
ptuytsch | 0:0803cdf04c32 | 109 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
ptuytsch | 0:0803cdf04c32 | 110 | return; |
ptuytsch | 0:0803cdf04c32 | 111 | } |
ptuytsch | 0:0803cdf04c32 | 112 | |
ptuytsch | 0:0803cdf04c32 | 113 | //set the disconnect function |
ptuytsch | 0:0803cdf04c32 | 114 | ble.gap().onDisconnection(disconnectionCallback); |
ptuytsch | 0:0803cdf04c32 | 115 | printf("BLE Disconnect callback set\n"); |
ptuytsch | 0:0803cdf04c32 | 116 | |
ptuytsch | 0:0803cdf04c32 | 117 | /* Setup primary services */ |
ptuytsch | 0:0803cdf04c32 | 118 | //DISptr = new DeviceInformationService(ble, "The Opportunity Factory", "1.0", "PJTMN06", "1.0", "0.9", "0.9"); |
ptuytsch | 0:0803cdf04c32 | 119 | bikeServicePtr = new BikeService(ble,bd); |
ptuytsch | 1:ffdec767aa55 | 120 | batSerPtr = new BatteryService(ble, |
ptuytsch | 1:ffdec767aa55 | 121 | trailerBattery->getBatteryPercentage(), |
ptuytsch | 1:ffdec767aa55 | 122 | bikeBattery->getBatteryPercentage(), |
ptuytsch | 1:ffdec767aa55 | 123 | auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 124 | |
ptuytsch | 0:0803cdf04c32 | 125 | printf("Services set\n"); |
ptuytsch | 0:0803cdf04c32 | 126 | |
ptuytsch | 0:0803cdf04c32 | 127 | /* setup advertising */ |
ptuytsch | 0:0803cdf04c32 | 128 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);//set apperance |
ptuytsch | 0:0803cdf04c32 | 129 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //different BLE options set |
ptuytsch | 0:0803cdf04c32 | 130 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));//Adding the list of UUID's |
ptuytsch | 0:0803cdf04c32 | 131 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//passing the device name |
ptuytsch | 0:0803cdf04c32 | 132 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, bd->getBikeNameSize());//passing the device name |
ptuytsch | 0:0803cdf04c32 | 133 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);//setting connection type |
ptuytsch | 0:0803cdf04c32 | 134 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
ptuytsch | 0:0803cdf04c32 | 135 | ble.gap().startAdvertising(); |
ptuytsch | 0:0803cdf04c32 | 136 | printf("advertising\n"); |
ptuytsch | 0:0803cdf04c32 | 137 | } |
ptuytsch | 0:0803cdf04c32 | 138 | |
ptuytsch | 0:0803cdf04c32 | 139 | |
ptuytsch | 0:0803cdf04c32 | 140 | int main() { |
ptuytsch | 0:0803cdf04c32 | 141 | |
ptuytsch | 0:0803cdf04c32 | 142 | |
ptuytsch | 0:0803cdf04c32 | 143 | printf("\n======================\n\n"); |
ptuytsch | 0:0803cdf04c32 | 144 | printf("SETUP \n"); |
ptuytsch | 0:0803cdf04c32 | 145 | |
ptuytsch | 0:0803cdf04c32 | 146 | bd = new BikeData(PC_1); // creating BikeData object |
ptuytsch | 0:0803cdf04c32 | 147 | printf("BikeData object created\n"); |
ptuytsch | 0:0803cdf04c32 | 148 | |
ptuytsch | 0:0803cdf04c32 | 149 | |
ptuytsch | 1:ffdec767aa55 | 150 | trailerBattery = new BatteryState(trailerBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 151 | bikeBattery = new BatteryState(bikeBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 152 | auxiliaryBattery = new BatteryState(auxiliaryBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 153 | |
ptuytsch | 1:ffdec767aa55 | 154 | |
ptuytsch | 1:ffdec767aa55 | 155 | printf("run TestLight \n"); |
ptuytsch | 1:ffdec767aa55 | 156 | runTestLight(); |
ptuytsch | 1:ffdec767aa55 | 157 | |
ptuytsch | 0:0803cdf04c32 | 158 | uint8_t size = bd->getBikeNameSize(); |
ptuytsch | 0:0803cdf04c32 | 159 | printf("name length: %i\n", size); |
ptuytsch | 0:0803cdf04c32 | 160 | if (size == 0){ |
ptuytsch | 0:0803cdf04c32 | 161 | printf("Setting Name First Time\n"); |
ptuytsch | 1:ffdec767aa55 | 162 | static char testName[] = "Tortuga"; |
ptuytsch | 0:0803cdf04c32 | 163 | bd -> setBikeName((char *)testName,sizeof(testName)); |
ptuytsch | 0:0803cdf04c32 | 164 | printf("SetFistTimeName\n"); |
ptuytsch | 0:0803cdf04c32 | 165 | //printf( "size: %i\n",bd -> getBikeNameSize()); |
ptuytsch | 0:0803cdf04c32 | 166 | //printf("first character: %c",bd->getBikeName()); |
ptuytsch | 0:0803cdf04c32 | 167 | } |
ptuytsch | 0:0803cdf04c32 | 168 | |
ptuytsch | 0:0803cdf04c32 | 169 | bd->getBikeName(DEVICE_NAME); |
ptuytsch | 0:0803cdf04c32 | 170 | printf("got device name\n"); |
ptuytsch | 0:0803cdf04c32 | 171 | BLE &ble = BLE::Instance(); // creating BLE object |
ptuytsch | 0:0803cdf04c32 | 172 | printf("BLE object created\n"); |
ptuytsch | 0:0803cdf04c32 | 173 | BLE::Instance().init(bleInitComplete); // call init function |
ptuytsch | 0:0803cdf04c32 | 174 | |
ptuytsch | 0:0803cdf04c32 | 175 | while (ble.hasInitialized() == false); // Wait until BLE is initialized |
ptuytsch | 0:0803cdf04c32 | 176 | printf("BLE object intitialized\n"); |
ptuytsch | 0:0803cdf04c32 | 177 | Ticker ticker; |
ptuytsch | 0:0803cdf04c32 | 178 | ticker.attach(periodicCallback, 1); //creating the ticker that wil update each second. |
ptuytsch | 0:0803cdf04c32 | 179 | printf("ticker 1 attached\n"); |
ptuytsch | 0:0803cdf04c32 | 180 | |
ptuytsch | 0:0803cdf04c32 | 181 | printf("START \n"); |
ptuytsch | 0:0803cdf04c32 | 182 | |
ptuytsch | 0:0803cdf04c32 | 183 | |
ptuytsch | 0:0803cdf04c32 | 184 | while(true) |
ptuytsch | 0:0803cdf04c32 | 185 | { |
ptuytsch | 0:0803cdf04c32 | 186 | if (update){ |
ptuytsch | 1:ffdec767aa55 | 187 | printf("--------------\n"); |
ptuytsch | 1:ffdec767aa55 | 188 | float distance = (bd->getDataSet(BikeData::OVERALL))->getDistance(); |
ptuytsch | 1:ffdec767aa55 | 189 | printf("main distance: %f\n", distance); |
ptuytsch | 0:0803cdf04c32 | 190 | float speed = bd->getSpeed(); |
ptuytsch | 1:ffdec767aa55 | 191 | printf("main speed: %f\n", speed); |
ptuytsch | 0:0803cdf04c32 | 192 | |
ptuytsch | 0:0803cdf04c32 | 193 | //printf("update bikeService\n"); |
ptuytsch | 0:0803cdf04c32 | 194 | bikeServicePtr->update(); //update the bikeserice |
ptuytsch | 1:ffdec767aa55 | 195 | //printf("update battery1: %i\n", trailerBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 196 | //batSerPtr->updateBatteryLevel1((++batLevel)%=101); |
ptuytsch | 1:ffdec767aa55 | 197 | //printf("battery level: %i\n", batLevel); |
ptuytsch | 0:0803cdf04c32 | 198 | batSerPtr->updateBatteryLevel1(trailerBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 199 | //printf("update battery2: %i\n", bikeBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 200 | batSerPtr->updateBatteryLevel2(bikeBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 201 | //printf("update battery3: %i\n", auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 202 | batSerPtr->updateBatteryLevel3(auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 203 | update = false; |
ptuytsch | 0:0803cdf04c32 | 204 | } |
ptuytsch | 0:0803cdf04c32 | 205 | ble.waitForEvent(); //wait for an event when idle. |
ptuytsch | 0:0803cdf04c32 | 206 | |
ptuytsch | 0:0803cdf04c32 | 207 | |
ptuytsch | 0:0803cdf04c32 | 208 | } |
ptuytsch | 0:0803cdf04c32 | 209 | } |