workss
Dependencies: mbed BLE_API nRF51822 VL53L0X
main.cpp@25:0a0805c118c0, 2019-03-07 (annotated)
- Committer:
- vazbyte
- Date:
- Thu Mar 07 19:01:23 2019 +0000
- Revision:
- 25:0a0805c118c0
- Parent:
- 24:931eeb8a70fc
- Child:
- 26:793d65b08afb
works!
Who changed what in which revision?
User | Revision | Line number | New 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 | 25:0a0805c118c0 | 39 | uint8_t hrmCounter = 100; // init HRM to 100bps |
mbedAustin | 1:94152e7d8b5c | 40 | |
andresag | 22:406127954d1f | 41 | /* Set Up custom Characteristics */ |
mbedAustin | 9:b33f42191584 | 42 | static uint8_t readValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 43 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
mbedAustin | 2:e84c13abc479 | 44 | |
mbedAustin | 9:b33f42191584 | 45 | static uint8_t writeValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 46 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
mbedAustin | 2:e84c13abc479 | 47 | |
andresag | 22:406127954d1f | 48 | /* Set up custom service */ |
mbedAustin | 8:60ede963dfe2 | 49 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
mbedAustin | 9:b33f42191584 | 50 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
mbedAustin | 2:e84c13abc479 | 51 | |
andresag | 19:477567297aac | 52 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
mbedAustin | 1:94152e7d8b5c | 53 | { |
andresag | 22:406127954d1f | 54 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
mbedAustin | 1:94152e7d8b5c | 55 | } |
mbedAustin | 0:cd5b6733aeb1 | 56 | |
melmon | 18:7d89c4fba362 | 57 | void writeCharCallback(const GattWriteCallbackParams *params) |
mbedAustin | 3:0fb60f81f693 | 58 | { |
andresag | 22:406127954d1f | 59 | /* Check to see what characteristic was written, by handle */ |
melmon | 18:7d89c4fba362 | 60 | if(params->handle == writeChar.getValueHandle()) { |
andresag | 22:406127954d1f | 61 | /* toggle LED if only 1 byte is written */ |
mbedAustin | 8:60ede963dfe2 | 62 | if(params->len == 1) { |
mbedAustin | 8:60ede963dfe2 | 63 | led = params->data[0]; |
andresag | 19:477567297aac | 64 | (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle |
mbedAustin | 8:60ede963dfe2 | 65 | } |
andresag | 22:406127954d1f | 66 | /* Print the data if more than 1 byte is written */ |
mbedAustin | 8:60ede963dfe2 | 67 | else { |
andresag | 19:477567297aac | 68 | printf("Data received: length = %d, data = 0x",params->len); |
mbedAustin | 8:60ede963dfe2 | 69 | for(int x=0; x < params->len; x++) { |
andresag | 19:477567297aac | 70 | printf("%x", params->data[x]); |
mbedAustin | 8:60ede963dfe2 | 71 | } |
andresag | 19:477567297aac | 72 | printf("\n\r"); |
mbedAustin | 8:60ede963dfe2 | 73 | } |
andresag | 22:406127954d1f | 74 | /* Update the readChar with the value of writeChar */ |
andresag | 22:406127954d1f | 75 | BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); |
mbedAustin | 8:60ede963dfe2 | 76 | } |
mbedAustin | 3:0fb60f81f693 | 77 | } |
vazbyte | 24:931eeb8a70fc | 78 | |
andresag | 22:406127954d1f | 79 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 22:406127954d1f | 80 | { |
andresag | 22:406127954d1f | 81 | BLE &ble = params->ble; |
andresag | 22:406127954d1f | 82 | ble_error_t error = params->error; |
andresag | 22:406127954d1f | 83 | |
andresag | 22:406127954d1f | 84 | if (error != BLE_ERROR_NONE) { |
andresag | 22:406127954d1f | 85 | return; |
andresag | 22:406127954d1f | 86 | } |
mbedAustin | 0:cd5b6733aeb1 | 87 | |
andresag | 19:477567297aac | 88 | ble.gap().onDisconnection(disconnectionCallback); |
vazbyte | 25:0a0805c118c0 | 89 | hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
mbedAustin | 2:e84c13abc479 | 90 | |
andresag | 19:477567297aac | 91 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
andresag | 19:477567297aac | 92 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
andresag | 19:477567297aac | 93 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
andresag | 19:477567297aac | 94 | 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 | 95 | ble.gap().setAdvertisingInterval(100); // 100ms. |
vazbyte | 25:0a0805c118c0 | 96 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
mbedAustin | 2:e84c13abc479 | 97 | |
mbedAustin | 13:62b1d32745ac | 98 | ble.addService(customService); |
andresag | 19:477567297aac | 99 | ble.gap().startAdvertising(); |
andresag | 22:406127954d1f | 100 | } |
vazbyte | 23:52e8e05df60c | 101 | void wakeup_event_cb() { |
vazbyte | 23:52e8e05df60c | 102 | led != led; |
vazbyte | 24:931eeb8a70fc | 103 | |
vazbyte | 24:931eeb8a70fc | 104 | status1 = range1.get_distance(&distance1); |
vazbyte | 24:931eeb8a70fc | 105 | status2 = range2.get_distance(&distance2); |
vazbyte | 24:931eeb8a70fc | 106 | if (status1 == VL53L0X_ERROR_NONE) { |
vazbyte | 24:931eeb8a70fc | 107 | printf("Range1 [mm]: %6ld\r\n", distance1); |
vazbyte | 24:931eeb8a70fc | 108 | if (distance1 > 40 && distance1 < 2200) { |
vazbyte | 24:931eeb8a70fc | 109 | led1 = 0; |
vazbyte | 24:931eeb8a70fc | 110 | } |
vazbyte | 24:931eeb8a70fc | 111 | else { |
vazbyte | 24:931eeb8a70fc | 112 | led1 = 1; |
vazbyte | 24:931eeb8a70fc | 113 | } |
vazbyte | 24:931eeb8a70fc | 114 | } else { |
vazbyte | 24:931eeb8a70fc | 115 | printf("Range1 [mm]: --\r\n"); |
vazbyte | 24:931eeb8a70fc | 116 | led1 = 1; |
vazbyte | 24:931eeb8a70fc | 117 | } |
vazbyte | 24:931eeb8a70fc | 118 | if (status2 == VL53L0X_ERROR_NONE) { |
vazbyte | 24:931eeb8a70fc | 119 | printf("Range2 [mm]: %6ld\r\n", distance2); |
vazbyte | 24:931eeb8a70fc | 120 | if (distance2 > 40 && distance2 < 2200) { |
vazbyte | 24:931eeb8a70fc | 121 | led2 = 0; |
vazbyte | 24:931eeb8a70fc | 122 | } |
vazbyte | 24:931eeb8a70fc | 123 | } else { |
vazbyte | 24:931eeb8a70fc | 124 | printf("Range2 [mm]: --\r\n"); |
vazbyte | 24:931eeb8a70fc | 125 | led2 = 1; |
vazbyte | 24:931eeb8a70fc | 126 | } |
vazbyte | 24:931eeb8a70fc | 127 | // wait(0.2); |
vazbyte | 23:52e8e05df60c | 128 | } |
andresag | 19:477567297aac | 129 | |
andresag | 22:406127954d1f | 130 | int main(void) |
andresag | 22:406127954d1f | 131 | { |
vazbyte | 24:931eeb8a70fc | 132 | range1.init_sensor(range1_addr); |
vazbyte | 24:931eeb8a70fc | 133 | range2.init_sensor(range2_addr); |
vazbyte | 24:931eeb8a70fc | 134 | |
andresag | 22:406127954d1f | 135 | printf("\n\r********* Starting Main Loop *********\n\r"); |
vazbyte | 24:931eeb8a70fc | 136 | |
andresag | 22:406127954d1f | 137 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
andresag | 22:406127954d1f | 138 | ble.init(bleInitComplete); |
andresag | 22:406127954d1f | 139 | |
vazbyte | 23:52e8e05df60c | 140 | Ticker ticker; |
vazbyte | 24:931eeb8a70fc | 141 | |
vazbyte | 23:52e8e05df60c | 142 | ticker.attach(wakeup_event_cb, 0.3); |
vazbyte | 24:931eeb8a70fc | 143 | |
vazbyte | 23:52e8e05df60c | 144 | while (ble.hasInitialized()) { |
vazbyte | 24:931eeb8a70fc | 145 | ble.waitForEvent(); |
mbedAustin | 2:e84c13abc479 | 146 | } |
andresag | 20:fcc752d401ec | 147 | } |