EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
Farshad
Date:
Fri Jun 16 05:09:17 2017 +0000
Revision:
17:229d78f063fb
Parent:
15:bc4f8c597c26
Child:
18:08184949ab30
Added notification for when Laser power goes off due to inactivity. Also added command for setting the inactivity timeout.

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