Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed BLE_API nRF51822 VL53L0X
main.cpp
00001 #include "mbed.h" 00002 #include "ble/BLE.h" 00003 #include "mbed.h" 00004 #include "VL53L0X.h" 00005 00006 #define range1_addr (0x56) 00007 #define range2_addr (0x60) 00008 #define range1_XSHUT p15 00009 #define range2_XSHUT p16 00010 #define VL53L0_I2C_SDA p30 00011 #define VL53L0_I2C_SCL p7 00012 00013 Serial pc(USBTX, USBRX); 00014 static DevI2C devI2c(VL53L0_I2C_SDA, VL53L0_I2C_SCL); 00015 DigitalOut led1(LED1); 00016 DigitalOut led2(LED2); 00017 DigitalOut led(LED3, 1); 00018 uint16_t customServiceUUID = 0xA000; 00019 uint16_t readCharUUID = 0xA001; 00020 uint16_t writeCharUUID = 0xA002; 00021 00022 static DigitalOut shutdown1_pin(range1_XSHUT); 00023 static VL53L0X range1(&devI2c, &shutdown1_pin, NC); 00024 static DigitalOut shutdown2_pin(range2_XSHUT); 00025 static VL53L0X range2(&devI2c, &shutdown2_pin, NC); 00026 00027 uint32_t distance1; 00028 uint32_t distance2; 00029 int status1; 00030 int status2; 00031 00032 const static char DEVICE_NAME[] = "OCCUPY-CRICHTON-ST"; // change this 00033 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development 00034 00035 /* Set Up custom Characteristics */ 00036 static uint8_t readValue[10] = {0}; 00037 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); 00038 00039 static uint8_t writeValue[10] = {0}; 00040 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); 00041 00042 /* Set up custom service */ 00043 GattCharacteristic *characteristics[] = {&readChar, &writeChar}; 00044 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00045 00046 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) 00047 { 00048 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); 00049 } 00050 00051 void writeCharCallback(const GattWriteCallbackParams *params) 00052 { 00053 /* Check to see what characteristic was written, by handle */ 00054 if(params->handle == writeChar.getValueHandle()) { 00055 /* toggle LED if only 1 byte is written */ 00056 if(params->len == 1) { 00057 led = params->data[0]; 00058 (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle 00059 } 00060 /* Print the data if more than 1 byte is written */ 00061 else { 00062 printf("Data received: length = %d, data = 0x",params->len); 00063 for(int x=0; x < params->len; x++) { 00064 printf("%x", params->data[x]); 00065 } 00066 printf("\n\r"); 00067 } 00068 /* Update the readChar with the value of writeChar */ 00069 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); 00070 } 00071 } 00072 00073 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00074 { 00075 BLE &ble = params->ble; 00076 ble_error_t error = params->error; 00077 00078 if (error != BLE_ERROR_NONE) { 00079 return; 00080 } 00081 00082 ble.gap().onDisconnection(disconnectionCallback); 00083 ble.gattServer().onDataWritten(writeCharCallback); 00084 00085 /* Setup advertising */ 00086 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT 00087 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type 00088 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name 00089 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet 00090 ble.gap().setAdvertisingInterval(100); // 100ms. 00091 00092 /* Add our custom service */ 00093 ble.addService(customService); 00094 00095 /* Start advertising */ 00096 ble.gap().startAdvertising(); 00097 } 00098 void wakeup_event_cb() { 00099 led != led; 00100 00101 status1 = range1.get_distance(&distance1); 00102 status2 = range2.get_distance(&distance2); 00103 if (status1 == VL53L0X_ERROR_NONE) { 00104 printf("Range1 [mm]: %6ld\r\n", distance1); 00105 if (distance1 > 40 && distance1 < 2200) { 00106 led1 = 0; 00107 } 00108 else { 00109 led1 = 1; 00110 } 00111 } else { 00112 printf("Range1 [mm]: --\r\n"); 00113 led1 = 1; 00114 } 00115 if (status2 == VL53L0X_ERROR_NONE) { 00116 printf("Range2 [mm]: %6ld\r\n", distance2); 00117 if (distance2 > 40 && distance2 < 2200) { 00118 led2 = 0; 00119 } 00120 } else { 00121 printf("Range2 [mm]: --\r\n"); 00122 led2 = 1; 00123 } 00124 // wait(0.2); 00125 } 00126 00127 int main(void) 00128 { 00129 range1.init_sensor(range1_addr); 00130 range2.init_sensor(range2_addr); 00131 00132 printf("\n\r********* Starting Main Loop *********\n\r"); 00133 00134 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00135 ble.init(bleInitComplete); 00136 00137 Ticker ticker; 00138 00139 ticker.attach(wakeup_event_cb, 0.3); 00140 00141 while (ble.hasInitialized()) { 00142 ble.waitForEvent(); 00143 } 00144 }
Generated on Sun Jul 17 2022 07:55:06 by
1.7.2