EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
Farshad
Date:
Mon Feb 27 01:35:10 2017 +0000
Revision:
15:bc4f8c597c26
Parent:
13:a051da83a849
Child:
16:dc9956bac1a3
Child:
17:229d78f063fb
Firmware version 1.3. Added option for number of samples needed to do a measurement.

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 12:cf8af0b4e0d2 26
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 12:cf8af0b4e0d2 53 #define BATT_VALUE_THRESHOLD 0.5360f // values by experiment 3.6V
Farshad 12:cf8af0b4e0d2 54 //#define BATT_VALUE_HYSTERYSIS 0.0200f // about 0.05 volt
Farshad 12:cf8af0b4e0d2 55
Farshad 12:cf8af0b4e0d2 56 #define ACTIVITY_TIMEOUT_SEC 60
Farshad 7:8a23a257b66a 57
Farshad 7:8a23a257b66a 58 #undef NORDIC // is board nordic DK?
Farshad 5:207d4b6dface 59
Farshad 6:09cdafc3ffeb 60 #ifdef NORDIC
Farshad 5:207d4b6dface 61 DigitalOut connectionLed(p21);
Farshad 5:207d4b6dface 62 DigitalOut triggerLed(p22);
Farshad 10:d37cd13dd529 63 Serial serial(p13, p17); // tx, rx === NOTE tx port pin needs to be wired and verified (for nordic DK)
Farshad 10:d37cd13dd529 64 Laser laser(serial);
Farshad 10:d37cd13dd529 65 InterruptIn triggerButton(p18); // Button 2
Farshad 5:207d4b6dface 66 #else
Farshad 7:8a23a257b66a 67 DigitalOut connectionLed(p23);
Farshad 12:cf8af0b4e0d2 68 DigitalOut redLed(p24);
Farshad 8:ed66e7ef8243 69 InterruptIn triggerButton(p22);
Farshad 12:cf8af0b4e0d2 70 DigitalOut disableLRF(p28);
Farshad 12:cf8af0b4e0d2 71 DigitalOut enableBattVoltSense (p29);
Farshad 12:cf8af0b4e0d2 72 AnalogIn battVoltage (p1);
Farshad 12:cf8af0b4e0d2 73 DigitalOut nReset(p30);
Farshad 12:cf8af0b4e0d2 74 DigitalIn LRF_BOOT(p25, PullNone);
Farshad 12:cf8af0b4e0d2 75 DigitalIn LRF_CAL(p0, PullNone);
Farshad 5:207d4b6dface 76 #endif
Farshad 0:d58d1cdf43a9 77
Farshad 12:cf8af0b4e0d2 78 Serial* serialPtr;
Farshad 12:cf8af0b4e0d2 79 Laser* laserPtr;
Farshad 8:ed66e7ef8243 80 BLEDevice ble;
Farshad 8:ed66e7ef8243 81 UARTService *uartServicePtr;
Farshad 8:ed66e7ef8243 82 BLEHelper* bleHelper;
Farshad 8:ed66e7ef8243 83 static uint8_t isConnected = 0;
Farshad 10:d37cd13dd529 84 Timer timer;
Farshad 12:cf8af0b4e0d2 85 Ticker batteryChecker;
Farshad 12:cf8af0b4e0d2 86 Ticker activityChecker;
Farshad 15:bc4f8c597c26 87
Farshad 8:ed66e7ef8243 88
Farshad 7:8a23a257b66a 89 const static char DEVICE_NAME[] = "MCS_LRF";
Farshad 0:d58d1cdf43a9 90 const static char MANUFACTURER[] = "MCS";
Farshad 12:cf8af0b4e0d2 91 const static char MODEL[] = "Model 2";
Farshad 0:d58d1cdf43a9 92 const static char SERIAL_NO[] = "SN 1234";
Farshad 12:cf8af0b4e0d2 93 const static char HARDWARE_REV[] = "hw-rev 1.1";
Farshad 15:bc4f8c597c26 94 const static char FIRMWARE_REV[] = "fw-rev 1.3";
Farshad 15:bc4f8c597c26 95 const static char SOFTWARE_REV[] = "soft-rev 1.3";
Farshad 0:d58d1cdf43a9 96
Farshad 2:79a9dad8bc5e 97 // these values must macth definitions in the XML file accompanying this device
Farshad 7:8a23a257b66a 98 const static uint16_t distanceCmd = 0x0001;
Farshad 7:8a23a257b66a 99 const static uint16_t triggerCmd = 0x0002;
Farshad 12:cf8af0b4e0d2 100 const static uint16_t redDotCmd = 0x0003;
Farshad 12:cf8af0b4e0d2 101 const static uint16_t resetCmd = 0x0004;
Farshad 15:bc4f8c597c26 102 const static uint16_t nSamplesCmd = 0x0005;
Farshad 12:cf8af0b4e0d2 103
Farshad 12:cf8af0b4e0d2 104
Farshad 12:cf8af0b4e0d2 105 void activityCheckerTask();
Farshad 12:cf8af0b4e0d2 106 void resetActivityCheckerTask();
Farshad 12:cf8af0b4e0d2 107 void turnLaserPowerOff();
Farshad 2:79a9dad8bc5e 108
Farshad 2:79a9dad8bc5e 109 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Farshad 0:d58d1cdf43a9 110 {
Farshad 0:d58d1cdf43a9 111 DEBUG("Disconnected!\n\r");
Farshad 0:d58d1cdf43a9 112 DEBUG("Restarting the advertising process\n\r");
Farshad 0:d58d1cdf43a9 113 ble.startAdvertising();
Farshad 2:79a9dad8bc5e 114 isConnected = 0;
Farshad 5:207d4b6dface 115 connectionLed = isConnected;
Farshad 2:79a9dad8bc5e 116 }
Farshad 2:79a9dad8bc5e 117
Farshad 2:79a9dad8bc5e 118 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
Farshad 2:79a9dad8bc5e 119 {
Farshad 2:79a9dad8bc5e 120 DEBUG("Connected!\n\r");
Farshad 2:79a9dad8bc5e 121 isConnected = 1;
Farshad 5:207d4b6dface 122 connectionLed = isConnected;
Farshad 0:d58d1cdf43a9 123 }
Farshad 0:d58d1cdf43a9 124
Farshad 6:09cdafc3ffeb 125 static void processData(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 126 {
Farshad 6:09cdafc3ffeb 127 if(params->len >= 2) {
Farshad 6:09cdafc3ffeb 128 uint16_t command = params->data[0] + (params->data[1] << 8);
Farshad 6:09cdafc3ffeb 129 bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK;
Farshad 6:09cdafc3ffeb 130 DEBUG("command: %d \r\n", command);
Farshad 6:09cdafc3ffeb 131
Farshad 6:09cdafc3ffeb 132 switch(command & ~SET_PARAM_CMD_MASK) {
Farshad 7:8a23a257b66a 133 case distanceCmd:
Farshad 6:09cdafc3ffeb 134 if(!isSetCmd && params->len == 2) {
Farshad 6:09cdafc3ffeb 135 // form the reply to send
Farshad 7:8a23a257b66a 136 DEBUG("CMD is GET distance\n\r");
Farshad 12:cf8af0b4e0d2 137 laserPtr->triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 138 }
Farshad 6:09cdafc3ffeb 139 break;
Farshad 8:ed66e7ef8243 140
Farshad 7:8a23a257b66a 141 // TODO not needed really- can just use the distance command
Farshad 8:ed66e7ef8243 142 case triggerCmd:
Farshad 6:09cdafc3ffeb 143 if(isSetCmd && params->len == 3) {
Farshad 7:8a23a257b66a 144 // form the reply to send
Farshad 7:8a23a257b66a 145 DEBUG("CMD is SET trigger\n\r");
Farshad 12:cf8af0b4e0d2 146 laserPtr->triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 147 }
Farshad 6:09cdafc3ffeb 148 break;
Farshad 6:09cdafc3ffeb 149
Farshad 10:d37cd13dd529 150 case redDotCmd:
Farshad 10:d37cd13dd529 151 if(isSetCmd && params->len == 3) {
Farshad 10:d37cd13dd529 152 DEBUG("CMD is SET redDot\n\r");
Farshad 12:cf8af0b4e0d2 153 laserPtr->setRedDot(params->data[2]);
Farshad 12:cf8af0b4e0d2 154 }
Farshad 12:cf8af0b4e0d2 155 break;
Farshad 15:bc4f8c597c26 156
Farshad 12:cf8af0b4e0d2 157 case resetCmd:
Farshad 12:cf8af0b4e0d2 158 if(isSetCmd && params->len == 3) {
Farshad 12:cf8af0b4e0d2 159 DEBUG("CMD is reset\n\r");
Farshad 12:cf8af0b4e0d2 160 nReset = 0;
Farshad 12:cf8af0b4e0d2 161 wait_ms(100);
Farshad 12:cf8af0b4e0d2 162 nReset = 1;
Farshad 12:cf8af0b4e0d2 163 wait_ms(1000);
Farshad 12:cf8af0b4e0d2 164 laserPtr->enableMeasurement(true);
Farshad 10:d37cd13dd529 165 }
Farshad 10:d37cd13dd529 166 break;
Farshad 15:bc4f8c597c26 167
Farshad 15:bc4f8c597c26 168 case nSamplesCmd:
Farshad 15:bc4f8c597c26 169 if(isSetCmd && params->len == 4) {
Farshad 15:bc4f8c597c26 170 DEBUG("CMD is nSample\n\r");
Farshad 15:bc4f8c597c26 171 int16_t nSamples = params->data[2] + (params->data[3] << 8);
Farshad 15:bc4f8c597c26 172 laserPtr->setNumberOfSamples(nSamples);
Farshad 15:bc4f8c597c26 173 }
Farshad 15:bc4f8c597c26 174 break;
Farshad 10:d37cd13dd529 175
Farshad 6:09cdafc3ffeb 176 default:
Farshad 6:09cdafc3ffeb 177 break;
Farshad 6:09cdafc3ffeb 178 }
Farshad 6:09cdafc3ffeb 179 }
Farshad 6:09cdafc3ffeb 180 }
Farshad 6:09cdafc3ffeb 181
Farshad 12:cf8af0b4e0d2 182
Farshad 12:cf8af0b4e0d2 183 // Serial driver seems to be buggy and allocate p8 and p10 for flow control
Farshad 12:cf8af0b4e0d2 184 // by default- Need to manually disable flow control since the function for
Farshad 12:cf8af0b4e0d2 185 // disabling Serail.set_flow_control(Serial::Disabled) is not recognised by
Farshad 12:cf8af0b4e0d2 186 // the compiler either! Could be something to do with the target processor??
Farshad 12:cf8af0b4e0d2 187 static void disableFlowControl()
Farshad 12:cf8af0b4e0d2 188 {
Farshad 12:cf8af0b4e0d2 189 uint32_t base = 0x40002000;
Farshad 12:cf8af0b4e0d2 190 uint32_t ctsOffset = 0x510;
Farshad 12:cf8af0b4e0d2 191 uint32_t rtsOffset = 0x508;
Farshad 12:cf8af0b4e0d2 192
Farshad 12:cf8af0b4e0d2 193 uint32_t cts = base + ctsOffset;
Farshad 12:cf8af0b4e0d2 194 uint32_t rts = base + rtsOffset;
Farshad 12:cf8af0b4e0d2 195
Farshad 12:cf8af0b4e0d2 196 uint32_t* pcts = (uint32_t*)cts;
Farshad 12:cf8af0b4e0d2 197 uint32_t* prts = (uint32_t*)rts;
Farshad 12:cf8af0b4e0d2 198
Farshad 12:cf8af0b4e0d2 199 // no pin allocated for CTS and RTS
Farshad 12:cf8af0b4e0d2 200 *pcts = 0xffffffff;
Farshad 12:cf8af0b4e0d2 201 *prts = 0xffffffff;
Farshad 12:cf8af0b4e0d2 202 }
Farshad 12:cf8af0b4e0d2 203
Farshad 12:cf8af0b4e0d2 204 void resetActivityCheckerTask()
Farshad 12:cf8af0b4e0d2 205 {
Farshad 12:cf8af0b4e0d2 206 activityChecker.detach();
Farshad 12:cf8af0b4e0d2 207 activityChecker.attach(activityCheckerTask, ACTIVITY_TIMEOUT_SEC);
Farshad 12:cf8af0b4e0d2 208 }
Farshad 12:cf8af0b4e0d2 209
Farshad 6:09cdafc3ffeb 210 void onDataWritten(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 211 {
Farshad 6:09cdafc3ffeb 212 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
Farshad 6:09cdafc3ffeb 213 uint16_t bytesRead = params->len;
Farshad 6:09cdafc3ffeb 214 DEBUG("received %u bytes\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 215 for(int i = 0; i < bytesRead; i++) {
Farshad 6:09cdafc3ffeb 216 DEBUG("0x%X ", params->data[i]);
Farshad 6:09cdafc3ffeb 217 }
Farshad 6:09cdafc3ffeb 218 DEBUG("\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 219
Farshad 6:09cdafc3ffeb 220 // echo?
Farshad 6:09cdafc3ffeb 221 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
Farshad 6:09cdafc3ffeb 222
Farshad 12:cf8af0b4e0d2 223 // make sure the laser is not powered off due to inactivity
Farshad 12:cf8af0b4e0d2 224 resetActivityCheckerTask();
Farshad 15:bc4f8c597c26 225 laserPtr->turnLaserPowerOn();
Farshad 15:bc4f8c597c26 226
Farshad 6:09cdafc3ffeb 227 processData(params);
Farshad 6:09cdafc3ffeb 228 }
Farshad 6:09cdafc3ffeb 229 }
Farshad 6:09cdafc3ffeb 230
Farshad 3:de77a4ebbf21 231 // this is an ISR, so do not spend too much time here and be careful with printing debug info
Farshad 2:79a9dad8bc5e 232 void readerCallback()
Farshad 8:ed66e7ef8243 233 {
Farshad 8:ed66e7ef8243 234 //if(serial.readable()) {
Farshad 12:cf8af0b4e0d2 235 // laserPtr->processRxData(serial.getc());
Farshad 7:8a23a257b66a 236 // }
Farshad 2:79a9dad8bc5e 237 }
Farshad 2:79a9dad8bc5e 238
Farshad 10:d37cd13dd529 239 /* This method is called when a distance measurement is ready to be sent to the client.
Farshad 10:d37cd13dd529 240 send distance measurement to the connected BLE client */
Farshad 8:ed66e7ef8243 241 void distanceCallcack(float distance, float elapsedTime)
Farshad 8:ed66e7ef8243 242 {
Farshad 8:ed66e7ef8243 243 uint8_t buf[10];
Farshad 8:ed66e7ef8243 244 uint16_t arrayLen = 2;
Farshad 8:ed66e7ef8243 245 memcpy(&buf[0], &arrayLen, sizeof(uint16_t));
Farshad 8:ed66e7ef8243 246 memcpy(&buf[2], &distance, sizeof(float));
Farshad 8:ed66e7ef8243 247 memcpy(&buf[6], &elapsedTime, sizeof(float));
Farshad 8:ed66e7ef8243 248 bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf));
Farshad 8:ed66e7ef8243 249 }
Farshad 8:ed66e7ef8243 250
Farshad 12:cf8af0b4e0d2 251 void batteryCheckerTask(void)
Farshad 12:cf8af0b4e0d2 252 {
Farshad 12:cf8af0b4e0d2 253 enableBattVoltSense = 1;
Farshad 12:cf8af0b4e0d2 254 wait_ms(20); // wait for the circuit to settle
Farshad 12:cf8af0b4e0d2 255 float batt = battVoltage.read();
Farshad 12:cf8af0b4e0d2 256 enableBattVoltSense = 0;
Farshad 12:cf8af0b4e0d2 257
Farshad 15:bc4f8c597c26 258 if(redLed == 0 && batt < BATT_VALUE_THRESHOLD) {
Farshad 12:cf8af0b4e0d2 259 redLed = 1;
Farshad 12:cf8af0b4e0d2 260 }
Farshad 12:cf8af0b4e0d2 261 }
Farshad 12:cf8af0b4e0d2 262
Farshad 12:cf8af0b4e0d2 263
Farshad 12:cf8af0b4e0d2 264
Farshad 10:d37cd13dd529 265 /* processor for the hardware trigger button */
Farshad 10:d37cd13dd529 266 void triggerFall()
Farshad 10:d37cd13dd529 267 {
Farshad 12:cf8af0b4e0d2 268 resetActivityCheckerTask();
Farshad 15:bc4f8c597c26 269 laserPtr->turnLaserPowerOn();
Farshad 15:bc4f8c597c26 270
Farshad 12:cf8af0b4e0d2 271 laserPtr->triggerDistanceMeasurement();
Farshad 12:cf8af0b4e0d2 272
Farshad 10:d37cd13dd529 273 timer.reset();
Farshad 10:d37cd13dd529 274 timer.start();
Farshad 10:d37cd13dd529 275 }
Farshad 10:d37cd13dd529 276
Farshad 10:d37cd13dd529 277 /* 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 278 otherwise debunce and make a measurement */
Farshad 10:d37cd13dd529 279 void triggerRise()
Farshad 10:d37cd13dd529 280 {
Farshad 10:d37cd13dd529 281 int elapsed = timer.read_ms();
Farshad 10:d37cd13dd529 282 timer.stop();
Farshad 10:d37cd13dd529 283 if(elapsed > PB_HOLD_TIME) {
Farshad 15:bc4f8c597c26 284 laserPtr->turnLaserPowerOff();
Farshad 15:bc4f8c597c26 285 }
Farshad 12:cf8af0b4e0d2 286 //else if(elapsed > PB_DEBUNCE_TIME) {
Farshad 12:cf8af0b4e0d2 287 // laserPtr->triggerDistanceMeasurement();
Farshad 12:cf8af0b4e0d2 288 // }
Farshad 12:cf8af0b4e0d2 289 }
Farshad 12:cf8af0b4e0d2 290
Farshad 12:cf8af0b4e0d2 291 void activityCheckerTask()
Farshad 12:cf8af0b4e0d2 292 {
Farshad 12:cf8af0b4e0d2 293 // too long with no activity- turn pwer off from laser to preserve power
Farshad 15:bc4f8c597c26 294 laserPtr->turnLaserPowerOff();
Farshad 8:ed66e7ef8243 295 }
Farshad 8:ed66e7ef8243 296
Farshad 0:d58d1cdf43a9 297 int main(void)
Farshad 0:d58d1cdf43a9 298 {
Farshad 5:207d4b6dface 299 connectionLed = 0;
Farshad 15:bc4f8c597c26 300
Farshad 15:bc4f8c597c26 301 // turn laser on and reset and wait for it to settle
Farshad 12:cf8af0b4e0d2 302 disableLRF = 0;
Farshad 12:cf8af0b4e0d2 303 nReset = 1;
Farshad 12:cf8af0b4e0d2 304 wait_ms(1000);
Farshad 5:207d4b6dface 305
Farshad 0:d58d1cdf43a9 306 DEBUG("Initialising the nRF51822\n\r");
Farshad 0:d58d1cdf43a9 307 ble.init();
Farshad 0:d58d1cdf43a9 308
Farshad 0:d58d1cdf43a9 309 ble.onDisconnection(disconnectionCallback);
Farshad 2:79a9dad8bc5e 310 ble.onConnection(connectionCallback);
Farshad 0:d58d1cdf43a9 311 ble.onDataWritten(onDataWritten);
Farshad 0:d58d1cdf43a9 312
Farshad 0:d58d1cdf43a9 313 /* setup advertising */
Farshad 0:d58d1cdf43a9 314 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Farshad 0:d58d1cdf43a9 315 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Farshad 0:d58d1cdf43a9 316 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
Farshad 0:d58d1cdf43a9 317 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Farshad 0:d58d1cdf43a9 318 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
Farshad 1:e18634cb382a 319 (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE));
Farshad 2:79a9dad8bc5e 320 ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
Farshad 0:d58d1cdf43a9 321 ble.startAdvertising();
Farshad 15:bc4f8c597c26 322
Farshad 12:cf8af0b4e0d2 323 // check battery level now and every minute afterwards
Farshad 12:cf8af0b4e0d2 324 batteryCheckerTask();
Farshad 15:bc4f8c597c26 325 batteryChecker.attach(batteryCheckerTask, 60);
Farshad 15:bc4f8c597c26 326
Farshad 12:cf8af0b4e0d2 327 // check acticity once every 3 minutes
Farshad 12:cf8af0b4e0d2 328 activityChecker.attach(activityCheckerTask, ACTIVITY_TIMEOUT_SEC);
Farshad 0:d58d1cdf43a9 329
Farshad 0:d58d1cdf43a9 330 /* Setup uart service */
Farshad 0:d58d1cdf43a9 331 UARTService uartService(ble);
Farshad 0:d58d1cdf43a9 332 uartServicePtr = &uartService;
Farshad 0:d58d1cdf43a9 333
Farshad 0:d58d1cdf43a9 334 /* Setup auxiliary service. */
Farshad 0:d58d1cdf43a9 335 DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);
Farshad 0:d58d1cdf43a9 336
Farshad 7:8a23a257b66a 337 /* Setup bleHelper */
Farshad 7:8a23a257b66a 338 BLEHelper helper(&ble, uartServicePtr);
Farshad 7:8a23a257b66a 339 bleHelper = &helper;
Farshad 8:ed66e7ef8243 340
Farshad 8:ed66e7ef8243 341 // setup serial port to LRF
Farshad 12:cf8af0b4e0d2 342 serialPtr = new Serial(p27, p26);
Farshad 12:cf8af0b4e0d2 343 disableFlowControl();
Farshad 12:cf8af0b4e0d2 344 serialPtr->baud(READER_BAUD_RATE);
Farshad 8:ed66e7ef8243 345 // serial.attach(&readerCallback);
Farshad 8:ed66e7ef8243 346
Farshad 10:d37cd13dd529 347 // processors for the trigger button
Farshad 10:d37cd13dd529 348 triggerButton.fall(&triggerFall);
Farshad 10:d37cd13dd529 349 triggerButton.rise(&triggerRise);
Farshad 10:d37cd13dd529 350
Farshad 10:d37cd13dd529 351 // setup laser
Farshad 12:cf8af0b4e0d2 352 laserPtr = new Laser(*serialPtr);
Farshad 12:cf8af0b4e0d2 353 laserPtr->enableMeasurement(true);
Farshad 12:cf8af0b4e0d2 354 laserPtr->setDistaceCallback(&distanceCallcack);
Farshad 15:bc4f8c597c26 355 laserPtr->turnLaserPowerOn();
Farshad 5:207d4b6dface 356
Farshad 0:d58d1cdf43a9 357 while (true) {
Farshad 0:d58d1cdf43a9 358 ble.waitForEvent();
Farshad 0:d58d1cdf43a9 359 }
Farshad 8:ed66e7ef8243 360 }