workss

Dependencies:   mbed BLE_API nRF51822 VL53L0X

Committer:
vazbyte
Date:
Thu Mar 07 19:09:08 2019 +0000
Revision:
26:793d65b08afb
Parent:
25:0a0805c118c0
Child:
27:903ec28ea7a0
workingx2

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"
vazbyte 25:0a0805c118c0 5 #include "ble/services/BatteryService.h"
vazbyte 25:0a0805c118c0 6 #include "ble/services/DeviceInformationService.h"
mbedAustin 1:94152e7d8b5c 7
vazbyte 24:931eeb8a70fc 8 #define range1_addr (0x56)
vazbyte 24:931eeb8a70fc 9 #define range2_addr (0x60)
vazbyte 24:931eeb8a70fc 10 #define range1_XSHUT p15
vazbyte 24:931eeb8a70fc 11 #define range2_XSHUT p16
vazbyte 24:931eeb8a70fc 12 #define VL53L0_I2C_SDA p30
vazbyte 24:931eeb8a70fc 13 #define VL53L0_I2C_SCL p7
vazbyte 24:931eeb8a70fc 14
vazbyte 24:931eeb8a70fc 15 Serial pc(USBTX, USBRX);
vazbyte 24:931eeb8a70fc 16 static DevI2C devI2c(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
vazbyte 24:931eeb8a70fc 17 DigitalOut led1(LED1);
vazbyte 24:931eeb8a70fc 18 DigitalOut led2(LED2);
vazbyte 24:931eeb8a70fc 19 DigitalOut led(LED3, 1);
mbedAustin 9:b33f42191584 20 uint16_t customServiceUUID = 0xA000;
mbedAustin 13:62b1d32745ac 21 uint16_t readCharUUID = 0xA001;
mbedAustin 9:b33f42191584 22 uint16_t writeCharUUID = 0xA002;
mbedAustin 1:94152e7d8b5c 23
vazbyte 24:931eeb8a70fc 24 static DigitalOut shutdown1_pin(range1_XSHUT);
vazbyte 24:931eeb8a70fc 25 static VL53L0X range1(&devI2c, &shutdown1_pin, NC);
vazbyte 24:931eeb8a70fc 26 static DigitalOut shutdown2_pin(range2_XSHUT);
vazbyte 24:931eeb8a70fc 27 static VL53L0X range2(&devI2c, &shutdown2_pin, NC);
vazbyte 24:931eeb8a70fc 28
vazbyte 24:931eeb8a70fc 29 uint32_t distance1;
vazbyte 24:931eeb8a70fc 30 uint32_t distance2;
vazbyte 24:931eeb8a70fc 31 int status1;
vazbyte 24:931eeb8a70fc 32 int status2;
vazbyte 24:931eeb8a70fc 33
vazbyte 23:52e8e05df60c 34 const static char DEVICE_NAME[] = "OCCUPY-CRICHTON-ST"; // change this
vazbyte 25:0a0805c118c0 35 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; //Custom UUID, FFFF is reserved for development
vazbyte 25:0a0805c118c0 36
vazbyte 25:0a0805c118c0 37 HeartRateService *hrService;
vazbyte 25:0a0805c118c0 38 DeviceInformationService *deviceInfo;
vazbyte 26:793d65b08afb 39 uint8_t hrmCounter = 0;
mbedAustin 2:e84c13abc479 40
andresag 19:477567297aac 41 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 42 {
andresag 22:406127954d1f 43 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 44 }
mbedAustin 0:cd5b6733aeb1 45
andresag 22:406127954d1f 46 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 47 {
andresag 22:406127954d1f 48 BLE &ble = params->ble;
andresag 22:406127954d1f 49 ble_error_t error = params->error;
andresag 22:406127954d1f 50
andresag 22:406127954d1f 51 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 52 return;
andresag 22:406127954d1f 53 }
mbedAustin 0:cd5b6733aeb1 54
andresag 19:477567297aac 55 ble.gap().onDisconnection(disconnectionCallback);
vazbyte 25:0a0805c118c0 56 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbedAustin 2:e84c13abc479 57
andresag 19:477567297aac 58 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 59 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
andresag 19:477567297aac 60 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
andresag 19:477567297aac 61 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
andresag 19:477567297aac 62 ble.gap().setAdvertisingInterval(100); // 100ms.
vazbyte 25:0a0805c118c0 63 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
mbedAustin 2:e84c13abc479 64
andresag 19:477567297aac 65 ble.gap().startAdvertising();
andresag 22:406127954d1f 66 }
vazbyte 23:52e8e05df60c 67 void wakeup_event_cb() {
vazbyte 23:52e8e05df60c 68 led != led;
vazbyte 24:931eeb8a70fc 69
vazbyte 24:931eeb8a70fc 70 status1 = range1.get_distance(&distance1);
vazbyte 24:931eeb8a70fc 71 status2 = range2.get_distance(&distance2);
vazbyte 26:793d65b08afb 72
vazbyte 24:931eeb8a70fc 73 if (status1 == VL53L0X_ERROR_NONE) {
vazbyte 24:931eeb8a70fc 74 printf("Range1 [mm]: %6ld\r\n", distance1);
vazbyte 24:931eeb8a70fc 75 if (distance1 > 40 && distance1 < 2200) {
vazbyte 24:931eeb8a70fc 76 led1 = 0;
vazbyte 26:793d65b08afb 77 hrmCounter++;
vazbyte 24:931eeb8a70fc 78 }
vazbyte 24:931eeb8a70fc 79 else {
vazbyte 24:931eeb8a70fc 80 led1 = 1;
vazbyte 24:931eeb8a70fc 81 }
vazbyte 24:931eeb8a70fc 82 } else {
vazbyte 24:931eeb8a70fc 83 printf("Range1 [mm]: --\r\n");
vazbyte 24:931eeb8a70fc 84 led1 = 1;
vazbyte 24:931eeb8a70fc 85 }
vazbyte 24:931eeb8a70fc 86 if (status2 == VL53L0X_ERROR_NONE) {
vazbyte 24:931eeb8a70fc 87 printf("Range2 [mm]: %6ld\r\n", distance2);
vazbyte 24:931eeb8a70fc 88 if (distance2 > 40 && distance2 < 2200) {
vazbyte 24:931eeb8a70fc 89 led2 = 0;
vazbyte 26:793d65b08afb 90 hrmCounter++;
vazbyte 24:931eeb8a70fc 91 }
vazbyte 24:931eeb8a70fc 92 } else {
vazbyte 24:931eeb8a70fc 93 printf("Range2 [mm]: --\r\n");
vazbyte 24:931eeb8a70fc 94 led2 = 1;
vazbyte 24:931eeb8a70fc 95 }
vazbyte 26:793d65b08afb 96 hrService->updateHeartRate(hrmCounter);
vazbyte 23:52e8e05df60c 97 }
andresag 19:477567297aac 98
andresag 22:406127954d1f 99 int main(void)
andresag 22:406127954d1f 100 {
vazbyte 24:931eeb8a70fc 101 range1.init_sensor(range1_addr);
vazbyte 24:931eeb8a70fc 102 range2.init_sensor(range2_addr);
vazbyte 24:931eeb8a70fc 103
andresag 22:406127954d1f 104 printf("\n\r********* Starting Main Loop *********\n\r");
vazbyte 24:931eeb8a70fc 105
andresag 22:406127954d1f 106 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 107 ble.init(bleInitComplete);
andresag 22:406127954d1f 108
vazbyte 23:52e8e05df60c 109 Ticker ticker;
vazbyte 24:931eeb8a70fc 110
vazbyte 23:52e8e05df60c 111 ticker.attach(wakeup_event_cb, 0.3);
vazbyte 24:931eeb8a70fc 112
vazbyte 23:52e8e05df60c 113 while (ble.hasInitialized()) {
vazbyte 24:931eeb8a70fc 114 ble.waitForEvent();
mbedAustin 2:e84c13abc479 115 }
andresag 20:fcc752d401ec 116 }