embedded software for tortuga bike

Dependencies:   BLE_API BikeControl DataLogging X_NUCLEO_IDB0XA1 _24LCXXX mbed

Committer:
ptuytsch
Date:
Fri Jul 15 12:49:59 2016 +0000
Revision:
0:0803cdf04c32
Child:
1:ffdec767aa55
back at working point with BLE on tortuga itself with eeprom working

Who changed what in which revision?

UserRevisionLine numberNew 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 0:0803cdf04c32 17
ptuytsch 0:0803cdf04c32 18 //3 battery states
ptuytsch 0:0803cdf04c32 19 BatteryState *trailerBattery = new BatteryState(trailerBatteryPin,BatteryState::Battery48V);
ptuytsch 0:0803cdf04c32 20 BatteryState *bikeBattery = new BatteryState(bikeBatteryPin,BatteryState::Battery48V);
ptuytsch 0:0803cdf04c32 21 BatteryState *auxiliaryBattery = new BatteryState(auxiliaryBatteryPin,BatteryState::Battery48V);
ptuytsch 0:0803cdf04c32 22
ptuytsch 0:0803cdf04c32 23 //BLE VARIABLES
ptuytsch 0:0803cdf04c32 24 static char DEVICE_NAME[] = "TORTUGA"; //Define default name of the BLE device
ptuytsch 0:0803cdf04c32 25 static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID,
ptuytsch 0:0803cdf04c32 26 //GattService::UUID_DEVICE_INFORMATION_SERVICE,
ptuytsch 0:0803cdf04c32 27 BatteryService::BATTERY_SERVICE_UUID,
ptuytsch 0:0803cdf04c32 28 }; // List of the Service UUID's that are used.
ptuytsch 0:0803cdf04c32 29
ptuytsch 0:0803cdf04c32 30 //Pointers to diferent objects and BLE services.
ptuytsch 0:0803cdf04c32 31 static BikeData *bd;
ptuytsch 0:0803cdf04c32 32 static BikeService *bikeServicePtr;
ptuytsch 0:0803cdf04c32 33 //static DeviceInformationService *DISptr;
ptuytsch 0:0803cdf04c32 34 static BatteryService *batSerPtr;
ptuytsch 0:0803cdf04c32 35
ptuytsch 0:0803cdf04c32 36 //bool that is set to true each second.
ptuytsch 0:0803cdf04c32 37 static bool update = false;
ptuytsch 0:0803cdf04c32 38 static bool updateHalf = false;
ptuytsch 0:0803cdf04c32 39
ptuytsch 0:0803cdf04c32 40
ptuytsch 0:0803cdf04c32 41 /***************************************************************************
ptuytsch 0:0803cdf04c32 42 BLE FUNCTIONS
ptuytsch 0:0803cdf04c32 43 ****************************************************************************/
ptuytsch 0:0803cdf04c32 44
ptuytsch 0:0803cdf04c32 45 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ptuytsch 0:0803cdf04c32 46 {
ptuytsch 0:0803cdf04c32 47 //restart advertising when disconnected
ptuytsch 0:0803cdf04c32 48 BLE::Instance().gap().startAdvertising();
ptuytsch 0:0803cdf04c32 49 }
ptuytsch 0:0803cdf04c32 50
ptuytsch 0:0803cdf04c32 51 void periodicCallback(void)
ptuytsch 0:0803cdf04c32 52 {
ptuytsch 0:0803cdf04c32 53 //Set update high to send new data over BLE
ptuytsch 0:0803cdf04c32 54 update = true;
ptuytsch 0:0803cdf04c32 55 }
ptuytsch 0:0803cdf04c32 56
ptuytsch 0:0803cdf04c32 57
ptuytsch 0:0803cdf04c32 58 //This function is called when the ble initialization process has failled
ptuytsch 0:0803cdf04c32 59 void onBleInitError(BLE &ble, ble_error_t error)
ptuytsch 0:0803cdf04c32 60 {
ptuytsch 0:0803cdf04c32 61 /* Initialization error handling should go here */
ptuytsch 0:0803cdf04c32 62 printf("initialization error!\n");
ptuytsch 0:0803cdf04c32 63 }
ptuytsch 0:0803cdf04c32 64
ptuytsch 0:0803cdf04c32 65
ptuytsch 0:0803cdf04c32 66 //Callback triggered when the ble initialization process has finished
ptuytsch 0:0803cdf04c32 67 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ptuytsch 0:0803cdf04c32 68 {
ptuytsch 0:0803cdf04c32 69 BLE& ble = params->ble;
ptuytsch 0:0803cdf04c32 70 ble_error_t error = params->error;
ptuytsch 0:0803cdf04c32 71
ptuytsch 0:0803cdf04c32 72 if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */
ptuytsch 0:0803cdf04c32 73 printf("BLE error\n");
ptuytsch 0:0803cdf04c32 74 onBleInitError(ble, error);
ptuytsch 0:0803cdf04c32 75 return;
ptuytsch 0:0803cdf04c32 76 }
ptuytsch 0:0803cdf04c32 77
ptuytsch 0:0803cdf04c32 78 /* Ensure that it is the default instance of BLE */
ptuytsch 0:0803cdf04c32 79 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
ptuytsch 0:0803cdf04c32 80 return;
ptuytsch 0:0803cdf04c32 81 }
ptuytsch 0:0803cdf04c32 82
ptuytsch 0:0803cdf04c32 83 //set the disconnect function
ptuytsch 0:0803cdf04c32 84 ble.gap().onDisconnection(disconnectionCallback);
ptuytsch 0:0803cdf04c32 85 printf("BLE Disconnect callback set\n");
ptuytsch 0:0803cdf04c32 86
ptuytsch 0:0803cdf04c32 87 /* Setup primary services */
ptuytsch 0:0803cdf04c32 88 //DISptr = new DeviceInformationService(ble, "The Opportunity Factory", "1.0", "PJTMN06", "1.0", "0.9", "0.9");
ptuytsch 0:0803cdf04c32 89 batSerPtr = new BatteryService(ble);
ptuytsch 0:0803cdf04c32 90 bikeServicePtr = new BikeService(ble,bd);
ptuytsch 0:0803cdf04c32 91 printf("Services set\n");
ptuytsch 0:0803cdf04c32 92
ptuytsch 0:0803cdf04c32 93 /* setup advertising */
ptuytsch 0:0803cdf04c32 94 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);//set apperance
ptuytsch 0:0803cdf04c32 95 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //different BLE options set
ptuytsch 0:0803cdf04c32 96 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 97 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//passing the device name
ptuytsch 0:0803cdf04c32 98 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, bd->getBikeNameSize());//passing the device name
ptuytsch 0:0803cdf04c32 99 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);//setting connection type
ptuytsch 0:0803cdf04c32 100 ble.gap().setAdvertisingInterval(100); /* 1000ms. */
ptuytsch 0:0803cdf04c32 101 ble.gap().startAdvertising();
ptuytsch 0:0803cdf04c32 102 printf("advertising\n");
ptuytsch 0:0803cdf04c32 103 }
ptuytsch 0:0803cdf04c32 104
ptuytsch 0:0803cdf04c32 105
ptuytsch 0:0803cdf04c32 106 int main() {
ptuytsch 0:0803cdf04c32 107
ptuytsch 0:0803cdf04c32 108
ptuytsch 0:0803cdf04c32 109 printf("\n======================\n\n");
ptuytsch 0:0803cdf04c32 110 printf("SETUP \n");
ptuytsch 0:0803cdf04c32 111
ptuytsch 0:0803cdf04c32 112 bd = new BikeData(PC_1); // creating BikeData object
ptuytsch 0:0803cdf04c32 113 printf("BikeData object created\n");
ptuytsch 0:0803cdf04c32 114
ptuytsch 0:0803cdf04c32 115
ptuytsch 0:0803cdf04c32 116 uint8_t size = bd->getBikeNameSize();
ptuytsch 0:0803cdf04c32 117 printf("name length: %i\n", size);
ptuytsch 0:0803cdf04c32 118 if (size == 0){
ptuytsch 0:0803cdf04c32 119 printf("Setting Name First Time\n");
ptuytsch 0:0803cdf04c32 120 static char testName[] = "TortugaPJT06";
ptuytsch 0:0803cdf04c32 121 bd -> setBikeName((char *)testName,sizeof(testName));
ptuytsch 0:0803cdf04c32 122 printf("SetFistTimeName\n");
ptuytsch 0:0803cdf04c32 123 //printf( "size: %i\n",bd -> getBikeNameSize());
ptuytsch 0:0803cdf04c32 124 //printf("first character: %c",bd->getBikeName());
ptuytsch 0:0803cdf04c32 125 }
ptuytsch 0:0803cdf04c32 126
ptuytsch 0:0803cdf04c32 127 bd->getBikeName(DEVICE_NAME);
ptuytsch 0:0803cdf04c32 128 printf("got device name\n");
ptuytsch 0:0803cdf04c32 129 BLE &ble = BLE::Instance(); // creating BLE object
ptuytsch 0:0803cdf04c32 130 printf("BLE object created\n");
ptuytsch 0:0803cdf04c32 131 BLE::Instance().init(bleInitComplete); // call init function
ptuytsch 0:0803cdf04c32 132
ptuytsch 0:0803cdf04c32 133 while (ble.hasInitialized() == false); // Wait until BLE is initialized
ptuytsch 0:0803cdf04c32 134 printf("BLE object intitialized\n");
ptuytsch 0:0803cdf04c32 135 Ticker ticker;
ptuytsch 0:0803cdf04c32 136 ticker.attach(periodicCallback, 1); //creating the ticker that wil update each second.
ptuytsch 0:0803cdf04c32 137 printf("ticker 1 attached\n");
ptuytsch 0:0803cdf04c32 138
ptuytsch 0:0803cdf04c32 139 printf("START \n");
ptuytsch 0:0803cdf04c32 140
ptuytsch 0:0803cdf04c32 141
ptuytsch 0:0803cdf04c32 142 while(true)
ptuytsch 0:0803cdf04c32 143 {
ptuytsch 0:0803cdf04c32 144 if (update){
ptuytsch 0:0803cdf04c32 145 float speed = bd->getSpeed();
ptuytsch 0:0803cdf04c32 146 printf("speed: %f\n", speed);
ptuytsch 0:0803cdf04c32 147
ptuytsch 0:0803cdf04c32 148 //printf("update bikeService\n");
ptuytsch 0:0803cdf04c32 149 bikeServicePtr->update(); //update the bikeserice
ptuytsch 0:0803cdf04c32 150 //printf("update battery1\n");
ptuytsch 0:0803cdf04c32 151 batSerPtr->updateBatteryLevel1(trailerBattery->getBatteryPercentage());
ptuytsch 0:0803cdf04c32 152 //printf("update battery2\n");
ptuytsch 0:0803cdf04c32 153 batSerPtr->updateBatteryLevel2(bikeBattery->getBatteryPercentage());
ptuytsch 0:0803cdf04c32 154 //printf("update battery3\n");
ptuytsch 0:0803cdf04c32 155 batSerPtr->updateBatteryLevel3(auxiliaryBattery->getBatteryPercentage());
ptuytsch 0:0803cdf04c32 156 update = false;
ptuytsch 0:0803cdf04c32 157 }
ptuytsch 0:0803cdf04c32 158 ble.waitForEvent(); //wait for an event when idle.
ptuytsch 0:0803cdf04c32 159
ptuytsch 0:0803cdf04c32 160
ptuytsch 0:0803cdf04c32 161 }
ptuytsch 0:0803cdf04c32 162
ptuytsch 0:0803cdf04c32 163 /*printf("\n======================");
ptuytsch 0:0803cdf04c32 164 printf("\n\n");
ptuytsch 0:0803cdf04c32 165
ptuytsch 0:0803cdf04c32 166 Ticker t1;
ptuytsch 0:0803cdf04c32 167 t1.attach(periodicCallback,1);
ptuytsch 0:0803cdf04c32 168
ptuytsch 0:0803cdf04c32 169 for(uint64_t i = 0 ; i <0xFFFFF;i++);*/
ptuytsch 0:0803cdf04c32 170 //uint32_t dummy = 0x0002;
ptuytsch 0:0803cdf04c32 171 /*I2C i2c(PB_9, PB_8);
ptuytsch 0:0803cdf04c32 172 Serial pc(SERIAL_TX, SERIAL_RX);
ptuytsch 0:0803cdf04c32 173 _24LCXXX mem(&i2c);
ptuytsch 0:0803cdf04c32 174
ptuytsch 0:0803cdf04c32 175 printf("\nsizeof dummy: %i\n", sizeof(dummy));
ptuytsch 0:0803cdf04c32 176 printf("Writing: %#x\n", dummy);
ptuytsch 0:0803cdf04c32 177 mem.nbyte_write(0,&dummy,4);
ptuytsch 0:0803cdf04c32 178 dummy+=0x1111;
ptuytsch 0:0803cdf04c32 179 printf("Writing: %#x\n", dummy);
ptuytsch 0:0803cdf04c32 180 mem.nbyte_write(4,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 181 dummy+=0x1111;
ptuytsch 0:0803cdf04c32 182 printf("Writing: %#x\n", dummy);
ptuytsch 0:0803cdf04c32 183 mem.nbyte_write(8,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 184 dummy+=0x1111;
ptuytsch 0:0803cdf04c32 185 printf("Writing: %#x\n", dummy);
ptuytsch 0:0803cdf04c32 186 mem.nbyte_write(12,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 187 dummy+=0x1111;
ptuytsch 0:0803cdf04c32 188 printf("Writing: %#x\n", dummy);
ptuytsch 0:0803cdf04c32 189 mem.nbyte_write(16,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 190 printf("Done Writing\n");
ptuytsch 0:0803cdf04c32 191
ptuytsch 0:0803cdf04c32 192 for (uint64_t i = 0 ; i<0xFFFFF ; i++); // waiting between reading en writing
ptuytsch 0:0803cdf04c32 193
ptuytsch 0:0803cdf04c32 194 dummy = 0x0000;
ptuytsch 0:0803cdf04c32 195 printf("reading\n");
ptuytsch 0:0803cdf04c32 196 mem.nbyte_read(0,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 197 printf(" res: %#x\n",dummy);
ptuytsch 0:0803cdf04c32 198 dummy = 0x0000;
ptuytsch 0:0803cdf04c32 199 printf("reading\n");
ptuytsch 0:0803cdf04c32 200 mem.nbyte_read(4,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 201 printf(" res: %#x\n",dummy);
ptuytsch 0:0803cdf04c32 202 dummy = 0x0000;
ptuytsch 0:0803cdf04c32 203 printf("reading\n");
ptuytsch 0:0803cdf04c32 204 mem.nbyte_read(8,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 205 printf(" res: %#x\n",dummy);
ptuytsch 0:0803cdf04c32 206 dummy = 0x0000;
ptuytsch 0:0803cdf04c32 207 printf("reading\n");
ptuytsch 0:0803cdf04c32 208 mem.nbyte_read(12,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 209 printf(" res: %#x\n",dummy);
ptuytsch 0:0803cdf04c32 210 dummy = 0x0000;
ptuytsch 0:0803cdf04c32 211 printf("reading\n");
ptuytsch 0:0803cdf04c32 212 mem.nbyte_read(16,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 213 printf(" res: %#x\n",dummy);
ptuytsch 0:0803cdf04c32 214 printf("Done reading\n");*/
ptuytsch 0:0803cdf04c32 215
ptuytsch 0:0803cdf04c32 216
ptuytsch 0:0803cdf04c32 217
ptuytsch 0:0803cdf04c32 218 /*printf("updating distance in eeprom\n");
ptuytsch 0:0803cdf04c32 219 float d = 1000.2;
ptuytsch 0:0803cdf04c32 220 if(!mem.nbyte_write(0,&d,sizeof(float))){
ptuytsch 0:0803cdf04c32 221 printf("eeprom writing error distance\n");
ptuytsch 0:0803cdf04c32 222 } // update the eeprom values
ptuytsch 0:0803cdf04c32 223 printf("updating time in eeprom\n");
ptuytsch 0:0803cdf04c32 224 uint32_t t = 0x10101010;
ptuytsch 0:0803cdf04c32 225 if(!mem.nbyte_write(4,&t,sizeof(uint32_t))){
ptuytsch 0:0803cdf04c32 226 printf("eeprom writing error time\n");
ptuytsch 0:0803cdf04c32 227 }
ptuytsch 0:0803cdf04c32 228
ptuytsch 0:0803cdf04c32 229
ptuytsch 0:0803cdf04c32 230
ptuytsch 0:0803cdf04c32 231
ptuytsch 0:0803cdf04c32 232 bd = new BikeData(PC_0);
ptuytsch 0:0803cdf04c32 233
ptuytsch 0:0803cdf04c32 234 for (int i = 9 ; i<16 ; i++){
ptuytsch 0:0803cdf04c32 235 char dummy;
ptuytsch 0:0803cdf04c32 236 mem.nbyte_read(i,&dummy,sizeof(dummy));
ptuytsch 0:0803cdf04c32 237 printf("char: %c\n", dummy);
ptuytsch 0:0803cdf04c32 238 }
ptuytsch 0:0803cdf04c32 239
ptuytsch 0:0803cdf04c32 240
ptuytsch 0:0803cdf04c32 241 while(1){
ptuytsch 0:0803cdf04c32 242
ptuytsch 0:0803cdf04c32 243
ptuytsch 0:0803cdf04c32 244
ptuytsch 0:0803cdf04c32 245 if (update){
ptuytsch 0:0803cdf04c32 246 //printf("u");
ptuytsch 0:0803cdf04c32 247 //printf("update\n");
ptuytsch 0:0803cdf04c32 248 float distance = bd->getDataSet(BikeData::OVERALL)->getDistance();
ptuytsch 0:0803cdf04c32 249 printf("bikedata overall distance: %f\n",distance);
ptuytsch 0:0803cdf04c32 250 update = false;
ptuytsch 0:0803cdf04c32 251 }
ptuytsch 0:0803cdf04c32 252 } */
ptuytsch 0:0803cdf04c32 253 }