EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
main.cpp@8:ed66e7ef8243, 2015-12-17 (annotated)
- Committer:
- Farshad
- Date:
- Thu Dec 17 01:04:59 2015 +0000
- Revision:
- 8:ed66e7ef8243
- Parent:
- 7:8a23a257b66a
- Child:
- 10:d37cd13dd529
Added hardware trigger and tidied up unused code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Farshad | 0:d58d1cdf43a9 | 1 | /* mbed Microcontroller Library |
Farshad | 0:d58d1cdf43a9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Farshad | 0:d58d1cdf43a9 | 3 | * |
Farshad | 0:d58d1cdf43a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Farshad | 0:d58d1cdf43a9 | 5 | * you may not use this file except in compliance with the License. |
Farshad | 0:d58d1cdf43a9 | 6 | * You may obtain a copy of the License at |
Farshad | 0:d58d1cdf43a9 | 7 | * |
Farshad | 0:d58d1cdf43a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Farshad | 0:d58d1cdf43a9 | 9 | * |
Farshad | 0:d58d1cdf43a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
Farshad | 0:d58d1cdf43a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Farshad | 0:d58d1cdf43a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Farshad | 0:d58d1cdf43a9 | 13 | * See the License for the specific language governing permissions and |
Farshad | 0:d58d1cdf43a9 | 14 | * limitations under the License. |
Farshad | 0:d58d1cdf43a9 | 15 | */ |
Farshad | 0:d58d1cdf43a9 | 16 | |
Farshad | 0:d58d1cdf43a9 | 17 | #include "mbed.h" |
Farshad | 5:207d4b6dface | 18 | #include "Serial.h" |
Farshad | 0:d58d1cdf43a9 | 19 | #include "BLE.h" |
Farshad | 0:d58d1cdf43a9 | 20 | #include "DeviceInformationService.h" |
Farshad | 0:d58d1cdf43a9 | 21 | #include "UARTService.h" |
Farshad | 7:8a23a257b66a | 22 | #include "bleHelper.h" |
Farshad | 8:ed66e7ef8243 | 23 | #include "laser.h" |
Farshad | 6:09cdafc3ffeb | 24 | |
Farshad | 7:8a23a257b66a | 25 | |
Farshad | 6:09cdafc3ffeb | 26 | #undef BLE_DEBUG_OUTPUT |
Farshad | 0:d58d1cdf43a9 | 27 | |
Farshad | 8:ed66e7ef8243 | 28 | /* Set this if you need debug messages on the console; |
Farshad | 0:d58d1cdf43a9 | 29 | * it will have an impact on code-size and power consumption. */ |
Farshad | 8:ed66e7ef8243 | 30 | #define NEED_CONSOLE_OUTPUT 0 |
Farshad | 0:d58d1cdf43a9 | 31 | |
Farshad | 8:ed66e7ef8243 | 32 | // only used for parsing tag data- will not work if parse function is called from within serial interrupt |
Farshad | 8:ed66e7ef8243 | 33 | #define NEED_PARSE_TRACE 0 |
Farshad | 2:79a9dad8bc5e | 34 | |
Farshad | 2:79a9dad8bc5e | 35 | Serial pc(USBTX, USBRX); |
Farshad | 0:d58d1cdf43a9 | 36 | #if NEED_CONSOLE_OUTPUT |
Farshad | 0:d58d1cdf43a9 | 37 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Farshad | 0:d58d1cdf43a9 | 38 | #else |
Farshad | 0:d58d1cdf43a9 | 39 | #define DEBUG(...) /* nothing */ |
Farshad | 0:d58d1cdf43a9 | 40 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Farshad | 0:d58d1cdf43a9 | 41 | |
Farshad | 5:207d4b6dface | 42 | #if NEED_PARSE_TRACE |
Farshad | 2:79a9dad8bc5e | 43 | #define TRACE(...) { pc.printf(__VA_ARGS__); } |
Farshad | 2:79a9dad8bc5e | 44 | #else |
Farshad | 2:79a9dad8bc5e | 45 | #define TRACE(...) |
Farshad | 2:79a9dad8bc5e | 46 | #endif /* #if NEED_TRACE */ |
Farshad | 2:79a9dad8bc5e | 47 | |
Farshad | 7:8a23a257b66a | 48 | #define SET_PARAM_CMD_MASK 0x8000 // commands with MSB set to 0 are to get the parameter and MSB of 1 to set the parameter |
Farshad | 7:8a23a257b66a | 49 | #define READER_BAUD_RATE 115200 |
Farshad | 7:8a23a257b66a | 50 | |
Farshad | 7:8a23a257b66a | 51 | |
Farshad | 7:8a23a257b66a | 52 | #undef NORDIC // is board nordic DK? |
Farshad | 5:207d4b6dface | 53 | |
Farshad | 6:09cdafc3ffeb | 54 | #ifdef NORDIC |
Farshad | 5:207d4b6dface | 55 | DigitalOut connectionLed(p21); |
Farshad | 5:207d4b6dface | 56 | DigitalOut triggerLed(p22); |
Farshad | 5:207d4b6dface | 57 | DigitalOut shutdown(p20); // for nordic DK |
Farshad | 5:207d4b6dface | 58 | Serial reader(p13, p17); // tx, rx === NOTE tx port pin needs to be wired and verified (for nordic DK) |
Farshad | 5:207d4b6dface | 59 | #else |
Farshad | 7:8a23a257b66a | 60 | DigitalOut connectionLed(p23); |
Farshad | 7:8a23a257b66a | 61 | Serial serial(p27, p26); // tx, rx === for adafruit BLE UART board (small blue board with red BLE module) |
Farshad | 7:8a23a257b66a | 62 | Laser laser(serial); |
Farshad | 8:ed66e7ef8243 | 63 | InterruptIn triggerButton(p22); |
Farshad | 5:207d4b6dface | 64 | #endif |
Farshad | 0:d58d1cdf43a9 | 65 | |
Farshad | 8:ed66e7ef8243 | 66 | BLEDevice ble; |
Farshad | 8:ed66e7ef8243 | 67 | UARTService *uartServicePtr; |
Farshad | 8:ed66e7ef8243 | 68 | BLEHelper* bleHelper; |
Farshad | 8:ed66e7ef8243 | 69 | static uint8_t isConnected = 0; |
Farshad | 8:ed66e7ef8243 | 70 | |
Farshad | 7:8a23a257b66a | 71 | const static char DEVICE_NAME[] = "MCS_LRF"; |
Farshad | 0:d58d1cdf43a9 | 72 | const static char MANUFACTURER[] = "MCS"; |
Farshad | 0:d58d1cdf43a9 | 73 | const static char MODEL[] = "Model 1"; |
Farshad | 0:d58d1cdf43a9 | 74 | const static char SERIAL_NO[] = "SN 1234"; |
Farshad | 0:d58d1cdf43a9 | 75 | const static char HARDWARE_REV[] = "hw-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 76 | const static char FIRMWARE_REV[] = "fw-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 77 | const static char SOFTWARE_REV[] = "soft-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 78 | |
Farshad | 2:79a9dad8bc5e | 79 | // these values must macth definitions in the XML file accompanying this device |
Farshad | 7:8a23a257b66a | 80 | const static uint16_t distanceCmd = 0x0001; |
Farshad | 7:8a23a257b66a | 81 | const static uint16_t triggerCmd = 0x0002; |
Farshad | 2:79a9dad8bc5e | 82 | |
Farshad | 2:79a9dad8bc5e | 83 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Farshad | 0:d58d1cdf43a9 | 84 | { |
Farshad | 0:d58d1cdf43a9 | 85 | DEBUG("Disconnected!\n\r"); |
Farshad | 0:d58d1cdf43a9 | 86 | DEBUG("Restarting the advertising process\n\r"); |
Farshad | 0:d58d1cdf43a9 | 87 | ble.startAdvertising(); |
Farshad | 2:79a9dad8bc5e | 88 | isConnected = 0; |
Farshad | 5:207d4b6dface | 89 | connectionLed = isConnected; |
Farshad | 2:79a9dad8bc5e | 90 | } |
Farshad | 2:79a9dad8bc5e | 91 | |
Farshad | 2:79a9dad8bc5e | 92 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
Farshad | 2:79a9dad8bc5e | 93 | { |
Farshad | 2:79a9dad8bc5e | 94 | DEBUG("Connected!\n\r"); |
Farshad | 2:79a9dad8bc5e | 95 | isConnected = 1; |
Farshad | 5:207d4b6dface | 96 | connectionLed = isConnected; |
Farshad | 0:d58d1cdf43a9 | 97 | } |
Farshad | 0:d58d1cdf43a9 | 98 | |
Farshad | 6:09cdafc3ffeb | 99 | static void processData(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 100 | { |
Farshad | 6:09cdafc3ffeb | 101 | if(params->len >= 2) { |
Farshad | 6:09cdafc3ffeb | 102 | uint16_t command = params->data[0] + (params->data[1] << 8); |
Farshad | 6:09cdafc3ffeb | 103 | bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK; |
Farshad | 6:09cdafc3ffeb | 104 | DEBUG("command: %d \r\n", command); |
Farshad | 6:09cdafc3ffeb | 105 | |
Farshad | 6:09cdafc3ffeb | 106 | switch(command & ~SET_PARAM_CMD_MASK) { |
Farshad | 7:8a23a257b66a | 107 | case distanceCmd: |
Farshad | 6:09cdafc3ffeb | 108 | if(!isSetCmd && params->len == 2) { |
Farshad | 6:09cdafc3ffeb | 109 | // form the reply to send |
Farshad | 7:8a23a257b66a | 110 | DEBUG("CMD is GET distance\n\r"); |
Farshad | 8:ed66e7ef8243 | 111 | laser.triggerDistanceMeasurement(); |
Farshad | 6:09cdafc3ffeb | 112 | } |
Farshad | 6:09cdafc3ffeb | 113 | break; |
Farshad | 8:ed66e7ef8243 | 114 | |
Farshad | 7:8a23a257b66a | 115 | // TODO not needed really- can just use the distance command |
Farshad | 8:ed66e7ef8243 | 116 | case triggerCmd: |
Farshad | 6:09cdafc3ffeb | 117 | if(isSetCmd && params->len == 3) { |
Farshad | 7:8a23a257b66a | 118 | // form the reply to send |
Farshad | 7:8a23a257b66a | 119 | DEBUG("CMD is SET trigger\n\r"); |
Farshad | 8:ed66e7ef8243 | 120 | laser.triggerDistanceMeasurement(); |
Farshad | 6:09cdafc3ffeb | 121 | } |
Farshad | 6:09cdafc3ffeb | 122 | break; |
Farshad | 6:09cdafc3ffeb | 123 | |
Farshad | 6:09cdafc3ffeb | 124 | default: |
Farshad | 6:09cdafc3ffeb | 125 | break; |
Farshad | 6:09cdafc3ffeb | 126 | } |
Farshad | 6:09cdafc3ffeb | 127 | } |
Farshad | 6:09cdafc3ffeb | 128 | } |
Farshad | 6:09cdafc3ffeb | 129 | |
Farshad | 6:09cdafc3ffeb | 130 | void onDataWritten(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 131 | { |
Farshad | 6:09cdafc3ffeb | 132 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
Farshad | 6:09cdafc3ffeb | 133 | uint16_t bytesRead = params->len; |
Farshad | 6:09cdafc3ffeb | 134 | DEBUG("received %u bytes\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 135 | for(int i = 0; i < bytesRead; i++) { |
Farshad | 6:09cdafc3ffeb | 136 | DEBUG("0x%X ", params->data[i]); |
Farshad | 6:09cdafc3ffeb | 137 | } |
Farshad | 6:09cdafc3ffeb | 138 | DEBUG("\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 139 | |
Farshad | 6:09cdafc3ffeb | 140 | // echo? |
Farshad | 6:09cdafc3ffeb | 141 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
Farshad | 6:09cdafc3ffeb | 142 | |
Farshad | 6:09cdafc3ffeb | 143 | processData(params); |
Farshad | 6:09cdafc3ffeb | 144 | } |
Farshad | 6:09cdafc3ffeb | 145 | } |
Farshad | 6:09cdafc3ffeb | 146 | |
Farshad | 6:09cdafc3ffeb | 147 | |
Farshad | 0:d58d1cdf43a9 | 148 | void periodicCallback(void) |
Farshad | 0:d58d1cdf43a9 | 149 | { |
Farshad | 0:d58d1cdf43a9 | 150 | } |
Farshad | 0:d58d1cdf43a9 | 151 | |
Farshad | 3:de77a4ebbf21 | 152 | // this is an ISR, so do not spend too much time here and be careful with printing debug info |
Farshad | 2:79a9dad8bc5e | 153 | void readerCallback() |
Farshad | 8:ed66e7ef8243 | 154 | { |
Farshad | 8:ed66e7ef8243 | 155 | //if(serial.readable()) { |
Farshad | 8:ed66e7ef8243 | 156 | // laser.processRxData(serial.getc()); |
Farshad | 7:8a23a257b66a | 157 | // } |
Farshad | 2:79a9dad8bc5e | 158 | } |
Farshad | 2:79a9dad8bc5e | 159 | |
Farshad | 8:ed66e7ef8243 | 160 | /* send distance measurement to the connected BLE client */ |
Farshad | 8:ed66e7ef8243 | 161 | void distanceCallcack(float distance, float elapsedTime) |
Farshad | 8:ed66e7ef8243 | 162 | { |
Farshad | 8:ed66e7ef8243 | 163 | uint8_t buf[10]; |
Farshad | 8:ed66e7ef8243 | 164 | uint16_t arrayLen = 2; |
Farshad | 8:ed66e7ef8243 | 165 | memcpy(&buf[0], &arrayLen, sizeof(uint16_t)); |
Farshad | 8:ed66e7ef8243 | 166 | memcpy(&buf[2], &distance, sizeof(float)); |
Farshad | 8:ed66e7ef8243 | 167 | memcpy(&buf[6], &elapsedTime, sizeof(float)); |
Farshad | 8:ed66e7ef8243 | 168 | bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf)); |
Farshad | 8:ed66e7ef8243 | 169 | } |
Farshad | 8:ed66e7ef8243 | 170 | |
Farshad | 8:ed66e7ef8243 | 171 | /* processor for the hardware trigger button */ |
Farshad | 8:ed66e7ef8243 | 172 | void trigger(){ |
Farshad | 8:ed66e7ef8243 | 173 | laser.triggerDistanceMeasurement(); |
Farshad | 8:ed66e7ef8243 | 174 | } |
Farshad | 8:ed66e7ef8243 | 175 | |
Farshad | 0:d58d1cdf43a9 | 176 | int main(void) |
Farshad | 0:d58d1cdf43a9 | 177 | { |
Farshad | 2:79a9dad8bc5e | 178 | // default state is unknown |
Farshad | 5:207d4b6dface | 179 | connectionLed = 0; |
Farshad | 5:207d4b6dface | 180 | |
Farshad | 0:d58d1cdf43a9 | 181 | DEBUG("Initialising the nRF51822\n\r"); |
Farshad | 0:d58d1cdf43a9 | 182 | ble.init(); |
Farshad | 0:d58d1cdf43a9 | 183 | |
Farshad | 0:d58d1cdf43a9 | 184 | ble.onDisconnection(disconnectionCallback); |
Farshad | 2:79a9dad8bc5e | 185 | ble.onConnection(connectionCallback); |
Farshad | 0:d58d1cdf43a9 | 186 | ble.onDataWritten(onDataWritten); |
Farshad | 0:d58d1cdf43a9 | 187 | |
Farshad | 0:d58d1cdf43a9 | 188 | /* setup advertising */ |
Farshad | 0:d58d1cdf43a9 | 189 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Farshad | 0:d58d1cdf43a9 | 190 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Farshad | 0:d58d1cdf43a9 | 191 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
Farshad | 0:d58d1cdf43a9 | 192 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Farshad | 0:d58d1cdf43a9 | 193 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
Farshad | 1:e18634cb382a | 194 | (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE)); |
Farshad | 2:79a9dad8bc5e | 195 | ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); |
Farshad | 0:d58d1cdf43a9 | 196 | ble.startAdvertising(); |
Farshad | 0:d58d1cdf43a9 | 197 | |
Farshad | 0:d58d1cdf43a9 | 198 | /* Setup uart service */ |
Farshad | 0:d58d1cdf43a9 | 199 | UARTService uartService(ble); |
Farshad | 0:d58d1cdf43a9 | 200 | uartServicePtr = &uartService; |
Farshad | 0:d58d1cdf43a9 | 201 | |
Farshad | 0:d58d1cdf43a9 | 202 | /* Setup auxiliary service. */ |
Farshad | 0:d58d1cdf43a9 | 203 | DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV); |
Farshad | 0:d58d1cdf43a9 | 204 | |
Farshad | 7:8a23a257b66a | 205 | /* Setup bleHelper */ |
Farshad | 7:8a23a257b66a | 206 | BLEHelper helper(&ble, uartServicePtr); |
Farshad | 7:8a23a257b66a | 207 | bleHelper = &helper; |
Farshad | 8:ed66e7ef8243 | 208 | |
Farshad | 8:ed66e7ef8243 | 209 | // setup serial port to LRF |
Farshad | 7:8a23a257b66a | 210 | serial.baud(READER_BAUD_RATE); |
Farshad | 8:ed66e7ef8243 | 211 | // serial.attach(&readerCallback); |
Farshad | 6:09cdafc3ffeb | 212 | |
Farshad | 8:ed66e7ef8243 | 213 | // processor for the trigger button |
Farshad | 8:ed66e7ef8243 | 214 | triggerButton.fall(&trigger); |
Farshad | 8:ed66e7ef8243 | 215 | |
Farshad | 7:8a23a257b66a | 216 | laser.enableMeasurement(true); |
Farshad | 8:ed66e7ef8243 | 217 | laser.setDistaceCallback(&distanceCallcack); |
Farshad | 5:207d4b6dface | 218 | |
Farshad | 0:d58d1cdf43a9 | 219 | while (true) { |
Farshad | 0:d58d1cdf43a9 | 220 | ble.waitForEvent(); |
Farshad | 0:d58d1cdf43a9 | 221 | } |
Farshad | 8:ed66e7ef8243 | 222 | } |