workss

Dependencies:   mbed BLE_API nRF51822 VL53L0X

Committer:
vazbyte
Date:
Wed Mar 13 15:16:25 2019 +0000
Revision:
28:ca14866eec64
Parent:
27:903ec28ea7a0
Child:
29:6ba8491c1dab
added confirmation of bluetooth connect/disconnect

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:cd5b6733aeb1 1 #include "mbed.h"
andresag 19:477567297aac 2 #include "ble/BLE.h"
vazbyte 24:931eeb8a70fc 3 #include "VL53L0X.h"
vazbyte 25:0a0805c118c0 4 #include "ble/services/HeartRateService.h"
mbedAustin 1:94152e7d8b5c 5
vazbyte 24:931eeb8a70fc 6 #define range1_addr (0x56)
vazbyte 24:931eeb8a70fc 7 #define range2_addr (0x60)
vazbyte 24:931eeb8a70fc 8 #define range1_XSHUT p15
vazbyte 24:931eeb8a70fc 9 #define range2_XSHUT p16
vazbyte 24:931eeb8a70fc 10 #define VL53L0_I2C_SDA p30
vazbyte 24:931eeb8a70fc 11 #define VL53L0_I2C_SCL p7
vazbyte 24:931eeb8a70fc 12
vazbyte 24:931eeb8a70fc 13 Serial pc(USBTX, USBRX);
vazbyte 24:931eeb8a70fc 14 static DevI2C devI2c(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
vazbyte 24:931eeb8a70fc 15 DigitalOut led1(LED1);
vazbyte 24:931eeb8a70fc 16 DigitalOut led2(LED2);
vazbyte 24:931eeb8a70fc 17 DigitalOut led(LED3, 1);
mbedAustin 9:b33f42191584 18 uint16_t customServiceUUID = 0xA000;
mbedAustin 13:62b1d32745ac 19 uint16_t readCharUUID = 0xA001;
mbedAustin 9:b33f42191584 20 uint16_t writeCharUUID = 0xA002;
mbedAustin 1:94152e7d8b5c 21
vazbyte 24:931eeb8a70fc 22 static DigitalOut shutdown1_pin(range1_XSHUT);
vazbyte 24:931eeb8a70fc 23 static VL53L0X range1(&devI2c, &shutdown1_pin, NC);
vazbyte 24:931eeb8a70fc 24 static DigitalOut shutdown2_pin(range2_XSHUT);
vazbyte 24:931eeb8a70fc 25 static VL53L0X range2(&devI2c, &shutdown2_pin, NC);
vazbyte 24:931eeb8a70fc 26
vazbyte 24:931eeb8a70fc 27 uint32_t distance1;
vazbyte 24:931eeb8a70fc 28 uint32_t distance2;
vazbyte 27:903ec28ea7a0 29 int dist1;
vazbyte 27:903ec28ea7a0 30 int dist2;
vazbyte 24:931eeb8a70fc 31 int status1;
vazbyte 24:931eeb8a70fc 32 int status2;
vazbyte 24:931eeb8a70fc 33
vazbyte 28:ca14866eec64 34 const static char DEVICE_NAME[] = "OCCUPY-CRICHTON-ST";
vazbyte 28:ca14866eec64 35 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
vazbyte 25:0a0805c118c0 36
vazbyte 25:0a0805c118c0 37 HeartRateService *hrService;
vazbyte 26:793d65b08afb 38 uint8_t hrmCounter = 0;
mbedAustin 2:e84c13abc479 39
vazbyte 28:ca14866eec64 40 void connectionCallback(const Gap::ConnectionCallbackParams_t *) {
vazbyte 28:ca14866eec64 41 printf("Bluetooth connected\n");
vazbyte 28:ca14866eec64 42 }
vazbyte 28:ca14866eec64 43
andresag 19:477567297aac 44 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 45 {
vazbyte 28:ca14866eec64 46 printf("Bluetooth disconnected\n");
andresag 22:406127954d1f 47 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 48 }
mbedAustin 0:cd5b6733aeb1 49
andresag 22:406127954d1f 50 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 51 {
andresag 22:406127954d1f 52 BLE &ble = params->ble;
andresag 22:406127954d1f 53 ble_error_t error = params->error;
andresag 22:406127954d1f 54
andresag 22:406127954d1f 55 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 56 return;
andresag 22:406127954d1f 57 }
vazbyte 28:ca14866eec64 58
vazbyte 28:ca14866eec64 59 ble.gap().onConnection(connectionCallback);
andresag 19:477567297aac 60 ble.gap().onDisconnection(disconnectionCallback);
vazbyte 25:0a0805c118c0 61 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbedAustin 2:e84c13abc479 62
vazbyte 28:ca14866eec64 63 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vazbyte 28:ca14866eec64 64 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vazbyte 28:ca14866eec64 65 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
vazbyte 28:ca14866eec64 66 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
vazbyte 25:0a0805c118c0 67 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
mbedAustin 2:e84c13abc479 68
andresag 19:477567297aac 69 ble.gap().startAdvertising();
andresag 22:406127954d1f 70 }
vazbyte 27:903ec28ea7a0 71
vazbyte 27:903ec28ea7a0 72 int format_dist(int distance) {
vazbyte 27:903ec28ea7a0 73 int result;
vazbyte 27:903ec28ea7a0 74
vazbyte 27:903ec28ea7a0 75 if (distance > 1270)
vazbyte 27:903ec28ea7a0 76 result = 127;
vazbyte 27:903ec28ea7a0 77 else
vazbyte 27:903ec28ea7a0 78 result = distance/10;
vazbyte 27:903ec28ea7a0 79
vazbyte 27:903ec28ea7a0 80 return result;
vazbyte 27:903ec28ea7a0 81 }
vazbyte 27:903ec28ea7a0 82
vazbyte 23:52e8e05df60c 83 void wakeup_event_cb() {
vazbyte 23:52e8e05df60c 84 led != led;
vazbyte 24:931eeb8a70fc 85
vazbyte 24:931eeb8a70fc 86 status1 = range1.get_distance(&distance1);
vazbyte 24:931eeb8a70fc 87 status2 = range2.get_distance(&distance2);
vazbyte 26:793d65b08afb 88
vazbyte 27:903ec28ea7a0 89 dist1 = format_dist(distance1);
vazbyte 27:903ec28ea7a0 90 dist2 = format_dist(distance2);
vazbyte 28:ca14866eec64 91
vazbyte 24:931eeb8a70fc 92 if (status1 == VL53L0X_ERROR_NONE) {
vazbyte 27:903ec28ea7a0 93 printf("Range1 [mm]: %6ld\r\n", dist1);
vazbyte 28:ca14866eec64 94
vazbyte 27:903ec28ea7a0 95 if (dist1 > 4 && dist1 < 220) {
vazbyte 24:931eeb8a70fc 96 led1 = 0;
vazbyte 27:903ec28ea7a0 97 hrmCounter = dist1;
vazbyte 27:903ec28ea7a0 98 hrService->updateHeartRate(hrmCounter);
vazbyte 24:931eeb8a70fc 99 }
vazbyte 24:931eeb8a70fc 100 else {
vazbyte 24:931eeb8a70fc 101 led1 = 1;
vazbyte 24:931eeb8a70fc 102 }
vazbyte 24:931eeb8a70fc 103 } else {
vazbyte 24:931eeb8a70fc 104 printf("Range1 [mm]: --\r\n");
vazbyte 24:931eeb8a70fc 105 led1 = 1;
vazbyte 24:931eeb8a70fc 106 }
vazbyte 24:931eeb8a70fc 107 if (status2 == VL53L0X_ERROR_NONE) {
vazbyte 27:903ec28ea7a0 108 printf("Range2 [mm]: %6ld\r\n", dist2);
vazbyte 27:903ec28ea7a0 109 if (dist2 > 4 && dist2 < 220) {
vazbyte 27:903ec28ea7a0 110 led2 = 0;
vazbyte 27:903ec28ea7a0 111 hrmCounter = dist2;
vazbyte 27:903ec28ea7a0 112 hrService->updateHeartRate(hrmCounter);
vazbyte 27:903ec28ea7a0 113 }
vazbyte 24:931eeb8a70fc 114 } else {
vazbyte 24:931eeb8a70fc 115 printf("Range2 [mm]: --\r\n");
vazbyte 24:931eeb8a70fc 116 led2 = 1;
vazbyte 24:931eeb8a70fc 117 }
vazbyte 23:52e8e05df60c 118 }
andresag 19:477567297aac 119
andresag 22:406127954d1f 120 int main(void)
andresag 22:406127954d1f 121 {
vazbyte 24:931eeb8a70fc 122 range1.init_sensor(range1_addr);
vazbyte 24:931eeb8a70fc 123 range2.init_sensor(range2_addr);
vazbyte 24:931eeb8a70fc 124
andresag 22:406127954d1f 125 printf("\n\r********* Starting Main Loop *********\n\r");
vazbyte 24:931eeb8a70fc 126
andresag 22:406127954d1f 127 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 128 ble.init(bleInitComplete);
andresag 22:406127954d1f 129
vazbyte 23:52e8e05df60c 130 Ticker ticker;
vazbyte 23:52e8e05df60c 131 ticker.attach(wakeup_event_cb, 0.3);
vazbyte 24:931eeb8a70fc 132
vazbyte 23:52e8e05df60c 133 while (ble.hasInitialized()) {
vazbyte 24:931eeb8a70fc 134 ble.waitForEvent();
mbedAustin 2:e84c13abc479 135 }
andresag 20:fcc752d401ec 136 }