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