EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
Farshad
Date:
Tue Dec 22 03:46:12 2015 +0000
Revision:
10:d37cd13dd529
Parent:
8:ed66e7ef8243
Child:
12:cf8af0b4e0d2
Added command to turn the red dot on and off. Also use the button to push and hold to turn the red dot off.

Who changed what in which revision?

UserRevisionLine numberNew 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 10:d37cd13dd529 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 10:d37cd13dd529 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 10:d37cd13dd529 50 #define PB_DEBUNCE_TIME 100
Farshad 10:d37cd13dd529 51 #define PB_HOLD_TIME 1000 // ms- Holding the push button longer than this will turn the laser dot off if it is on
Farshad 7:8a23a257b66a 52
Farshad 7:8a23a257b66a 53
Farshad 7:8a23a257b66a 54 #undef NORDIC // is board nordic DK?
Farshad 5:207d4b6dface 55
Farshad 6:09cdafc3ffeb 56 #ifdef NORDIC
Farshad 5:207d4b6dface 57 DigitalOut connectionLed(p21);
Farshad 5:207d4b6dface 58 DigitalOut triggerLed(p22);
Farshad 10:d37cd13dd529 59 Serial serial(p13, p17); // tx, rx === NOTE tx port pin needs to be wired and verified (for nordic DK)
Farshad 10:d37cd13dd529 60 Laser laser(serial);
Farshad 10:d37cd13dd529 61 InterruptIn triggerButton(p18); // Button 2
Farshad 5:207d4b6dface 62 #else
Farshad 7:8a23a257b66a 63 DigitalOut connectionLed(p23);
Farshad 7:8a23a257b66a 64 Serial serial(p27, p26); // tx, rx === for adafruit BLE UART board (small blue board with red BLE module)
Farshad 7:8a23a257b66a 65 Laser laser(serial);
Farshad 8:ed66e7ef8243 66 InterruptIn triggerButton(p22);
Farshad 5:207d4b6dface 67 #endif
Farshad 0:d58d1cdf43a9 68
Farshad 8:ed66e7ef8243 69 BLEDevice ble;
Farshad 8:ed66e7ef8243 70 UARTService *uartServicePtr;
Farshad 8:ed66e7ef8243 71 BLEHelper* bleHelper;
Farshad 8:ed66e7ef8243 72 static uint8_t isConnected = 0;
Farshad 10:d37cd13dd529 73 Timer timer;
Farshad 8:ed66e7ef8243 74
Farshad 7:8a23a257b66a 75 const static char DEVICE_NAME[] = "MCS_LRF";
Farshad 0:d58d1cdf43a9 76 const static char MANUFACTURER[] = "MCS";
Farshad 0:d58d1cdf43a9 77 const static char MODEL[] = "Model 1";
Farshad 0:d58d1cdf43a9 78 const static char SERIAL_NO[] = "SN 1234";
Farshad 0:d58d1cdf43a9 79 const static char HARDWARE_REV[] = "hw-rev 1";
Farshad 0:d58d1cdf43a9 80 const static char FIRMWARE_REV[] = "fw-rev 1";
Farshad 0:d58d1cdf43a9 81 const static char SOFTWARE_REV[] = "soft-rev 1";
Farshad 0:d58d1cdf43a9 82
Farshad 2:79a9dad8bc5e 83 // these values must macth definitions in the XML file accompanying this device
Farshad 7:8a23a257b66a 84 const static uint16_t distanceCmd = 0x0001;
Farshad 7:8a23a257b66a 85 const static uint16_t triggerCmd = 0x0002;
Farshad 10:d37cd13dd529 86 const static uint16_t redDotCmd = 0x0003;
Farshad 2:79a9dad8bc5e 87
Farshad 2:79a9dad8bc5e 88 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Farshad 0:d58d1cdf43a9 89 {
Farshad 0:d58d1cdf43a9 90 DEBUG("Disconnected!\n\r");
Farshad 0:d58d1cdf43a9 91 DEBUG("Restarting the advertising process\n\r");
Farshad 0:d58d1cdf43a9 92 ble.startAdvertising();
Farshad 2:79a9dad8bc5e 93 isConnected = 0;
Farshad 5:207d4b6dface 94 connectionLed = isConnected;
Farshad 2:79a9dad8bc5e 95 }
Farshad 2:79a9dad8bc5e 96
Farshad 2:79a9dad8bc5e 97 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
Farshad 2:79a9dad8bc5e 98 {
Farshad 2:79a9dad8bc5e 99 DEBUG("Connected!\n\r");
Farshad 2:79a9dad8bc5e 100 isConnected = 1;
Farshad 5:207d4b6dface 101 connectionLed = isConnected;
Farshad 0:d58d1cdf43a9 102 }
Farshad 0:d58d1cdf43a9 103
Farshad 6:09cdafc3ffeb 104 static void processData(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 105 {
Farshad 6:09cdafc3ffeb 106 if(params->len >= 2) {
Farshad 6:09cdafc3ffeb 107 uint16_t command = params->data[0] + (params->data[1] << 8);
Farshad 6:09cdafc3ffeb 108 bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK;
Farshad 6:09cdafc3ffeb 109 DEBUG("command: %d \r\n", command);
Farshad 6:09cdafc3ffeb 110
Farshad 6:09cdafc3ffeb 111 switch(command & ~SET_PARAM_CMD_MASK) {
Farshad 7:8a23a257b66a 112 case distanceCmd:
Farshad 6:09cdafc3ffeb 113 if(!isSetCmd && params->len == 2) {
Farshad 6:09cdafc3ffeb 114 // form the reply to send
Farshad 7:8a23a257b66a 115 DEBUG("CMD is GET distance\n\r");
Farshad 8:ed66e7ef8243 116 laser.triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 117 }
Farshad 6:09cdafc3ffeb 118 break;
Farshad 8:ed66e7ef8243 119
Farshad 7:8a23a257b66a 120 // TODO not needed really- can just use the distance command
Farshad 8:ed66e7ef8243 121 case triggerCmd:
Farshad 6:09cdafc3ffeb 122 if(isSetCmd && params->len == 3) {
Farshad 7:8a23a257b66a 123 // form the reply to send
Farshad 7:8a23a257b66a 124 DEBUG("CMD is SET trigger\n\r");
Farshad 8:ed66e7ef8243 125 laser.triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 126 }
Farshad 6:09cdafc3ffeb 127 break;
Farshad 6:09cdafc3ffeb 128
Farshad 10:d37cd13dd529 129 case redDotCmd:
Farshad 10:d37cd13dd529 130 if(isSetCmd && params->len == 3) {
Farshad 10:d37cd13dd529 131 DEBUG("CMD is SET redDot\n\r");
Farshad 10:d37cd13dd529 132 laser.setRedDot(params->data[2]);
Farshad 10:d37cd13dd529 133 }
Farshad 10:d37cd13dd529 134 break;
Farshad 10:d37cd13dd529 135
Farshad 6:09cdafc3ffeb 136 default:
Farshad 6:09cdafc3ffeb 137 break;
Farshad 6:09cdafc3ffeb 138 }
Farshad 6:09cdafc3ffeb 139 }
Farshad 6:09cdafc3ffeb 140 }
Farshad 6:09cdafc3ffeb 141
Farshad 6:09cdafc3ffeb 142 void onDataWritten(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 143 {
Farshad 6:09cdafc3ffeb 144 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
Farshad 6:09cdafc3ffeb 145 uint16_t bytesRead = params->len;
Farshad 6:09cdafc3ffeb 146 DEBUG("received %u bytes\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 147 for(int i = 0; i < bytesRead; i++) {
Farshad 6:09cdafc3ffeb 148 DEBUG("0x%X ", params->data[i]);
Farshad 6:09cdafc3ffeb 149 }
Farshad 6:09cdafc3ffeb 150 DEBUG("\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 151
Farshad 6:09cdafc3ffeb 152 // echo?
Farshad 6:09cdafc3ffeb 153 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
Farshad 6:09cdafc3ffeb 154
Farshad 6:09cdafc3ffeb 155 processData(params);
Farshad 6:09cdafc3ffeb 156 }
Farshad 6:09cdafc3ffeb 157 }
Farshad 6:09cdafc3ffeb 158
Farshad 3:de77a4ebbf21 159 // this is an ISR, so do not spend too much time here and be careful with printing debug info
Farshad 2:79a9dad8bc5e 160 void readerCallback()
Farshad 8:ed66e7ef8243 161 {
Farshad 8:ed66e7ef8243 162 //if(serial.readable()) {
Farshad 8:ed66e7ef8243 163 // laser.processRxData(serial.getc());
Farshad 7:8a23a257b66a 164 // }
Farshad 2:79a9dad8bc5e 165 }
Farshad 2:79a9dad8bc5e 166
Farshad 10:d37cd13dd529 167 /* This method is called when a distance measurement is ready to be sent to the client.
Farshad 10:d37cd13dd529 168 send distance measurement to the connected BLE client */
Farshad 8:ed66e7ef8243 169 void distanceCallcack(float distance, float elapsedTime)
Farshad 8:ed66e7ef8243 170 {
Farshad 8:ed66e7ef8243 171 uint8_t buf[10];
Farshad 8:ed66e7ef8243 172 uint16_t arrayLen = 2;
Farshad 8:ed66e7ef8243 173 memcpy(&buf[0], &arrayLen, sizeof(uint16_t));
Farshad 8:ed66e7ef8243 174 memcpy(&buf[2], &distance, sizeof(float));
Farshad 8:ed66e7ef8243 175 memcpy(&buf[6], &elapsedTime, sizeof(float));
Farshad 8:ed66e7ef8243 176 bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf));
Farshad 8:ed66e7ef8243 177 }
Farshad 8:ed66e7ef8243 178
Farshad 10:d37cd13dd529 179 /* processor for the hardware trigger button */
Farshad 10:d37cd13dd529 180 void triggerFall()
Farshad 10:d37cd13dd529 181 {
Farshad 10:d37cd13dd529 182 timer.reset();
Farshad 10:d37cd13dd529 183 timer.start();
Farshad 10:d37cd13dd529 184 }
Farshad 10:d37cd13dd529 185
Farshad 10:d37cd13dd529 186 /* interrupt processor for when the button is released. If it has been pushed and held, turn the red dot off on release,
Farshad 10:d37cd13dd529 187 otherwise debunce and make a measurement */
Farshad 10:d37cd13dd529 188 void triggerRise()
Farshad 10:d37cd13dd529 189 {
Farshad 10:d37cd13dd529 190 int elapsed = timer.read_ms();
Farshad 10:d37cd13dd529 191 timer.stop();
Farshad 10:d37cd13dd529 192 if(elapsed > PB_HOLD_TIME) {
Farshad 10:d37cd13dd529 193 laser.setRedDot(0);
Farshad 10:d37cd13dd529 194 } else if(elapsed > PB_DEBUNCE_TIME) {
Farshad 10:d37cd13dd529 195 laser.triggerDistanceMeasurement();
Farshad 10:d37cd13dd529 196 }
Farshad 8:ed66e7ef8243 197 }
Farshad 8:ed66e7ef8243 198
Farshad 0:d58d1cdf43a9 199 int main(void)
Farshad 0:d58d1cdf43a9 200 {
Farshad 2:79a9dad8bc5e 201 // default state is unknown
Farshad 5:207d4b6dface 202 connectionLed = 0;
Farshad 5:207d4b6dface 203
Farshad 0:d58d1cdf43a9 204 DEBUG("Initialising the nRF51822\n\r");
Farshad 0:d58d1cdf43a9 205 ble.init();
Farshad 0:d58d1cdf43a9 206
Farshad 0:d58d1cdf43a9 207 ble.onDisconnection(disconnectionCallback);
Farshad 2:79a9dad8bc5e 208 ble.onConnection(connectionCallback);
Farshad 0:d58d1cdf43a9 209 ble.onDataWritten(onDataWritten);
Farshad 0:d58d1cdf43a9 210
Farshad 0:d58d1cdf43a9 211 /* setup advertising */
Farshad 0:d58d1cdf43a9 212 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Farshad 0:d58d1cdf43a9 213 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Farshad 0:d58d1cdf43a9 214 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
Farshad 0:d58d1cdf43a9 215 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Farshad 0:d58d1cdf43a9 216 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
Farshad 1:e18634cb382a 217 (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE));
Farshad 2:79a9dad8bc5e 218 ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
Farshad 0:d58d1cdf43a9 219 ble.startAdvertising();
Farshad 0:d58d1cdf43a9 220
Farshad 0:d58d1cdf43a9 221 /* Setup uart service */
Farshad 0:d58d1cdf43a9 222 UARTService uartService(ble);
Farshad 0:d58d1cdf43a9 223 uartServicePtr = &uartService;
Farshad 0:d58d1cdf43a9 224
Farshad 0:d58d1cdf43a9 225 /* Setup auxiliary service. */
Farshad 0:d58d1cdf43a9 226 DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);
Farshad 0:d58d1cdf43a9 227
Farshad 7:8a23a257b66a 228 /* Setup bleHelper */
Farshad 7:8a23a257b66a 229 BLEHelper helper(&ble, uartServicePtr);
Farshad 7:8a23a257b66a 230 bleHelper = &helper;
Farshad 8:ed66e7ef8243 231
Farshad 8:ed66e7ef8243 232 // setup serial port to LRF
Farshad 7:8a23a257b66a 233 serial.baud(READER_BAUD_RATE);
Farshad 8:ed66e7ef8243 234 // serial.attach(&readerCallback);
Farshad 8:ed66e7ef8243 235
Farshad 10:d37cd13dd529 236 // processors for the trigger button
Farshad 10:d37cd13dd529 237 triggerButton.fall(&triggerFall);
Farshad 10:d37cd13dd529 238 triggerButton.rise(&triggerRise);
Farshad 10:d37cd13dd529 239
Farshad 10:d37cd13dd529 240 // setup laser
Farshad 7:8a23a257b66a 241 laser.enableMeasurement(true);
Farshad 8:ed66e7ef8243 242 laser.setDistaceCallback(&distanceCallcack);
Farshad 5:207d4b6dface 243
Farshad 0:d58d1cdf43a9 244 while (true) {
Farshad 0:d58d1cdf43a9 245 ble.waitForEvent();
Farshad 0:d58d1cdf43a9 246 }
Farshad 8:ed66e7ef8243 247 }