EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
Farshad
Date:
Mon Oct 17 04:50:33 2016 +0000
Revision:
12:cf8af0b4e0d2
Parent:
10:d37cd13dd529
Child:
13:a051da83a849
Added low battery check and power save after i minute of inactivity. Also added function to reset the laser module.

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