EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
bmentink
Date:
Tue Mar 06 21:34:31 2018 +0000
Revision:
21:c19355cc5e79
Parent:
20:fa5dfaf624a9
Added following:; 1. ACA modules that control VBUS and ID pins; 2. Turned off laser after powerup

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"
bmentink 21:c19355cc5e79 24 #include "ACA.h"
Farshad 6:09cdafc3ffeb 25
Farshad 7:8a23a257b66a 26
Farshad 12:cf8af0b4e0d2 27
Farshad 0:d58d1cdf43a9 28
Farshad 8:ed66e7ef8243 29 /* Set this if you need debug messages on the console;
Farshad 0:d58d1cdf43a9 30 * it will have an impact on code-size and power consumption. */
Farshad 10:d37cd13dd529 31 #define NEED_CONSOLE_OUTPUT 0
Farshad 0:d58d1cdf43a9 32
Farshad 8:ed66e7ef8243 33 // only used for parsing tag data- will not work if parse function is called from within serial interrupt
Farshad 10:d37cd13dd529 34 #define NEED_PARSE_TRACE 0
Farshad 2:79a9dad8bc5e 35
Farshad 2:79a9dad8bc5e 36 Serial pc(USBTX, USBRX);
Farshad 0:d58d1cdf43a9 37 #if NEED_CONSOLE_OUTPUT
Farshad 0:d58d1cdf43a9 38 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Farshad 0:d58d1cdf43a9 39 #else
Farshad 0:d58d1cdf43a9 40 #define DEBUG(...) /* nothing */
Farshad 0:d58d1cdf43a9 41 #endif /* #if NEED_CONSOLE_OUTPUT */
Farshad 0:d58d1cdf43a9 42
Farshad 5:207d4b6dface 43 #if NEED_PARSE_TRACE
Farshad 2:79a9dad8bc5e 44 #define TRACE(...) { pc.printf(__VA_ARGS__); }
Farshad 2:79a9dad8bc5e 45 #else
Farshad 2:79a9dad8bc5e 46 #define TRACE(...)
Farshad 2:79a9dad8bc5e 47 #endif /* #if NEED_TRACE */
Farshad 2:79a9dad8bc5e 48
Farshad 7:8a23a257b66a 49 #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 50 #define READER_BAUD_RATE 115200
Farshad 10:d37cd13dd529 51 #define PB_DEBUNCE_TIME 100
Farshad 10:d37cd13dd529 52 #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 53
Farshad 12:cf8af0b4e0d2 54 #define BATT_VALUE_THRESHOLD 0.5360f // values by experiment 3.6V
Farshad 12:cf8af0b4e0d2 55 //#define BATT_VALUE_HYSTERYSIS 0.0200f // about 0.05 volt
Farshad 12:cf8af0b4e0d2 56
Farshad 17:229d78f063fb 57 #define ACTIVITY_TIMEOUT_SEC 60 // default value- turn laser off if no measurement for more than this many seconds
Farshad 7:8a23a257b66a 58
Farshad 20:fa5dfaf624a9 59 #undef NORDIC // is board nordic DK?
Farshad 5:207d4b6dface 60
Farshad 6:09cdafc3ffeb 61 #ifdef NORDIC
Farshad 5:207d4b6dface 62 DigitalOut connectionLed(p21);
Farshad 5:207d4b6dface 63 DigitalOut triggerLed(p22);
Farshad 10:d37cd13dd529 64 Serial serial(p13, p17); // tx, rx === NOTE tx port pin needs to be wired and verified (for nordic DK)
Farshad 10:d37cd13dd529 65 Laser laser(serial);
Farshad 10:d37cd13dd529 66 InterruptIn triggerButton(p18); // Button 2
Farshad 5:207d4b6dface 67 #else
Farshad 7:8a23a257b66a 68 DigitalOut connectionLed(p23);
Farshad 18:08184949ab30 69 //PwmOut connectionLed(p23); // good idea using PwmOut but it seems that period can't be set to longer than about 1s
Farshad 12:cf8af0b4e0d2 70 DigitalOut redLed(p24);
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);
bmentink 21:c19355cc5e79 78 // IO For ACA
bmentink 21:c19355cc5e79 79 DigitalOut disableVBUS(p8);
bmentink 21:c19355cc5e79 80 DigitalOut ID_SW1(p7);
bmentink 21:c19355cc5e79 81 DigitalOut ID_SW(p6);
bmentink 21:c19355cc5e79 82 DigitalIn VBUS_IN(p5, PullNone);
Farshad 5:207d4b6dface 83 #endif
Farshad 0:d58d1cdf43a9 84
Farshad 12:cf8af0b4e0d2 85 Serial* serialPtr;
Farshad 12:cf8af0b4e0d2 86 Laser* laserPtr;
Farshad 8:ed66e7ef8243 87 BLEDevice ble;
Farshad 8:ed66e7ef8243 88 UARTService *uartServicePtr;
Farshad 8:ed66e7ef8243 89 BLEHelper* bleHelper;
Farshad 8:ed66e7ef8243 90 static uint8_t isConnected = 0;
Farshad 10:d37cd13dd529 91 Timer timer;
Farshad 12:cf8af0b4e0d2 92 Ticker batteryChecker;
Farshad 12:cf8af0b4e0d2 93 Ticker activityChecker;
Farshad 18:08184949ab30 94 Ticker connectionLedBlinker;
bmentink 21:c19355cc5e79 95 Ticker ACACharger;
Farshad 17:229d78f063fb 96 uint16_t activityTimeout = ACTIVITY_TIMEOUT_SEC;
Farshad 15:bc4f8c597c26 97
Farshad 18:08184949ab30 98 // settings for blinking the connection LED- Blink to save power
Farshad 18:08184949ab30 99 uint16_t connectionLedOffCount = 0;
Farshad 18:08184949ab30 100 #define CONNECTION_LED_OFF_TIME_MS 2000
Farshad 18:08184949ab30 101 #define CONNECTION_LED_ON_TIME_MS 20
Farshad 18:08184949ab30 102 #define CONNECTION_LED_OFF_COUNT (CONNECTION_LED_OFF_TIME_MS / CONNECTION_LED_ON_TIME_MS)
bmentink 21:c19355cc5e79 103 //
Farshad 8:ed66e7ef8243 104
bmentink 21:c19355cc5e79 105 const static char DEVICE_NAME[] = "MCS_EEP_01";
Farshad 0:d58d1cdf43a9 106 const static char MANUFACTURER[] = "MCS";
Farshad 12:cf8af0b4e0d2 107 const static char MODEL[] = "Model 2";
Farshad 0:d58d1cdf43a9 108 const static char SERIAL_NO[] = "SN 1234";
Farshad 12:cf8af0b4e0d2 109 const static char HARDWARE_REV[] = "hw-rev 1.1";
Farshad 18:08184949ab30 110 const static char FIRMWARE_REV[] = "fw-rev 1.5";
Farshad 18:08184949ab30 111 const static char SOFTWARE_REV[] = "soft-rev 1.5";
Farshad 0:d58d1cdf43a9 112
Farshad 2:79a9dad8bc5e 113 // these values must macth definitions in the XML file accompanying this device
Farshad 7:8a23a257b66a 114 const static uint16_t distanceCmd = 0x0001;
Farshad 7:8a23a257b66a 115 const static uint16_t triggerCmd = 0x0002;
Farshad 12:cf8af0b4e0d2 116 const static uint16_t redDotCmd = 0x0003;
Farshad 12:cf8af0b4e0d2 117 const static uint16_t resetCmd = 0x0004;
Farshad 15:bc4f8c597c26 118 const static uint16_t nSamplesCmd = 0x0005;
Farshad 17:229d78f063fb 119 const static uint16_t activityTimeoutCmd = 0x0006;
Farshad 12:cf8af0b4e0d2 120
Farshad 12:cf8af0b4e0d2 121
Farshad 12:cf8af0b4e0d2 122 void activityCheckerTask();
Farshad 12:cf8af0b4e0d2 123 void resetActivityCheckerTask();
Farshad 12:cf8af0b4e0d2 124 void turnLaserPowerOff();
Farshad 2:79a9dad8bc5e 125
Farshad 2:79a9dad8bc5e 126 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Farshad 0:d58d1cdf43a9 127 {
Farshad 0:d58d1cdf43a9 128 DEBUG("Disconnected!\n\r");
Farshad 0:d58d1cdf43a9 129 DEBUG("Restarting the advertising process\n\r");
Farshad 0:d58d1cdf43a9 130 ble.startAdvertising();
Farshad 2:79a9dad8bc5e 131 isConnected = 0;
Farshad 18:08184949ab30 132 // connectionLed = isConnected;
Farshad 18:08184949ab30 133
Farshad 18:08184949ab30 134 // turn off blinking LED
Farshad 18:08184949ab30 135 // connectionLed.pulsewidth_ms(0);
Farshad 2:79a9dad8bc5e 136 }
Farshad 2:79a9dad8bc5e 137
Farshad 2:79a9dad8bc5e 138 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
Farshad 2:79a9dad8bc5e 139 {
Farshad 2:79a9dad8bc5e 140 DEBUG("Connected!\n\r");
Farshad 2:79a9dad8bc5e 141 isConnected = 1;
Farshad 18:08184949ab30 142 //connectionLed = isConnected;
Farshad 18:08184949ab30 143 // connectionLed.pulsewidth_ms(25);
Farshad 0:d58d1cdf43a9 144 }
Farshad 0:d58d1cdf43a9 145
Farshad 19:b576835175e2 146 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
Farshad 19:b576835175e2 147 {
Farshad 19:b576835175e2 148 printf("Input passKey: ");
Farshad 19:b576835175e2 149 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
Farshad 19:b576835175e2 150 printf("%c ", passkey[i]);
Farshad 19:b576835175e2 151 }
Farshad 19:b576835175e2 152 printf("\r\n");
Farshad 19:b576835175e2 153 }
Farshad 19:b576835175e2 154
Farshad 19:b576835175e2 155 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
Farshad 19:b576835175e2 156 {
Farshad 19:b576835175e2 157 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
Farshad 19:b576835175e2 158 printf("Security success\r\n");
Farshad 19:b576835175e2 159 } else {
Farshad 19:b576835175e2 160 printf("Security failed\r\n");
Farshad 19:b576835175e2 161 }
Farshad 19:b576835175e2 162 }
Farshad 19:b576835175e2 163
Farshad 6:09cdafc3ffeb 164 static void processData(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 165 {
Farshad 6:09cdafc3ffeb 166 if(params->len >= 2) {
Farshad 6:09cdafc3ffeb 167 uint16_t command = params->data[0] + (params->data[1] << 8);
Farshad 6:09cdafc3ffeb 168 bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK;
Farshad 6:09cdafc3ffeb 169 DEBUG("command: %d \r\n", command);
Farshad 6:09cdafc3ffeb 170
Farshad 6:09cdafc3ffeb 171 switch(command & ~SET_PARAM_CMD_MASK) {
Farshad 7:8a23a257b66a 172 case distanceCmd:
Farshad 6:09cdafc3ffeb 173 if(!isSetCmd && params->len == 2) {
Farshad 6:09cdafc3ffeb 174 // form the reply to send
Farshad 7:8a23a257b66a 175 DEBUG("CMD is GET distance\n\r");
Farshad 12:cf8af0b4e0d2 176 laserPtr->triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 177 }
Farshad 6:09cdafc3ffeb 178 break;
Farshad 8:ed66e7ef8243 179
Farshad 7:8a23a257b66a 180 // TODO not needed really- can just use the distance command
Farshad 8:ed66e7ef8243 181 case triggerCmd:
Farshad 6:09cdafc3ffeb 182 if(isSetCmd && params->len == 3) {
Farshad 7:8a23a257b66a 183 // form the reply to send
Farshad 7:8a23a257b66a 184 DEBUG("CMD is SET trigger\n\r");
Farshad 12:cf8af0b4e0d2 185 laserPtr->triggerDistanceMeasurement();
Farshad 6:09cdafc3ffeb 186 }
Farshad 6:09cdafc3ffeb 187 break;
Farshad 6:09cdafc3ffeb 188
Farshad 10:d37cd13dd529 189 case redDotCmd:
Farshad 10:d37cd13dd529 190 if(isSetCmd && params->len == 3) {
Farshad 10:d37cd13dd529 191 DEBUG("CMD is SET redDot\n\r");
Farshad 12:cf8af0b4e0d2 192 laserPtr->setRedDot(params->data[2]);
Farshad 12:cf8af0b4e0d2 193 }
Farshad 12:cf8af0b4e0d2 194 break;
Farshad 15:bc4f8c597c26 195
Farshad 12:cf8af0b4e0d2 196 case resetCmd:
Farshad 12:cf8af0b4e0d2 197 if(isSetCmd && params->len == 3) {
Farshad 12:cf8af0b4e0d2 198 DEBUG("CMD is reset\n\r");
Farshad 12:cf8af0b4e0d2 199 nReset = 0;
Farshad 12:cf8af0b4e0d2 200 wait_ms(100);
Farshad 12:cf8af0b4e0d2 201 nReset = 1;
Farshad 12:cf8af0b4e0d2 202 wait_ms(1000);
Farshad 12:cf8af0b4e0d2 203 laserPtr->enableMeasurement(true);
Farshad 10:d37cd13dd529 204 }
Farshad 10:d37cd13dd529 205 break;
Farshad 15:bc4f8c597c26 206
Farshad 15:bc4f8c597c26 207 case nSamplesCmd:
Farshad 15:bc4f8c597c26 208 if(isSetCmd && params->len == 4) {
Farshad 15:bc4f8c597c26 209 DEBUG("CMD is nSample\n\r");
Farshad 15:bc4f8c597c26 210 int16_t nSamples = params->data[2] + (params->data[3] << 8);
Farshad 15:bc4f8c597c26 211 laserPtr->setNumberOfSamples(nSamples);
Farshad 15:bc4f8c597c26 212 }
Farshad 15:bc4f8c597c26 213 break;
Farshad 17:229d78f063fb 214
Farshad 17:229d78f063fb 215 case activityTimeoutCmd:
Farshad 17:229d78f063fb 216 if(isSetCmd && params->len == 4) {
Farshad 17:229d78f063fb 217 DEBUG("CMD is nSample\n\r");
Farshad 17:229d78f063fb 218 activityTimeout = params->data[2] + (params->data[3] << 8);
Farshad 17:229d78f063fb 219 }
Farshad 17:229d78f063fb 220 break;
Farshad 10:d37cd13dd529 221
Farshad 6:09cdafc3ffeb 222 default:
Farshad 6:09cdafc3ffeb 223 break;
Farshad 6:09cdafc3ffeb 224 }
Farshad 6:09cdafc3ffeb 225 }
Farshad 6:09cdafc3ffeb 226 }
Farshad 6:09cdafc3ffeb 227
Farshad 12:cf8af0b4e0d2 228
Farshad 12:cf8af0b4e0d2 229 // Serial driver seems to be buggy and allocate p8 and p10 for flow control
Farshad 12:cf8af0b4e0d2 230 // by default- Need to manually disable flow control since the function for
Farshad 12:cf8af0b4e0d2 231 // disabling Serail.set_flow_control(Serial::Disabled) is not recognised by
Farshad 12:cf8af0b4e0d2 232 // the compiler either! Could be something to do with the target processor??
Farshad 12:cf8af0b4e0d2 233 static void disableFlowControl()
Farshad 12:cf8af0b4e0d2 234 {
Farshad 12:cf8af0b4e0d2 235 uint32_t base = 0x40002000;
Farshad 12:cf8af0b4e0d2 236 uint32_t ctsOffset = 0x510;
Farshad 12:cf8af0b4e0d2 237 uint32_t rtsOffset = 0x508;
Farshad 12:cf8af0b4e0d2 238
Farshad 12:cf8af0b4e0d2 239 uint32_t cts = base + ctsOffset;
Farshad 12:cf8af0b4e0d2 240 uint32_t rts = base + rtsOffset;
Farshad 12:cf8af0b4e0d2 241
Farshad 12:cf8af0b4e0d2 242 uint32_t* pcts = (uint32_t*)cts;
Farshad 12:cf8af0b4e0d2 243 uint32_t* prts = (uint32_t*)rts;
Farshad 12:cf8af0b4e0d2 244
Farshad 12:cf8af0b4e0d2 245 // no pin allocated for CTS and RTS
Farshad 12:cf8af0b4e0d2 246 *pcts = 0xffffffff;
Farshad 12:cf8af0b4e0d2 247 *prts = 0xffffffff;
Farshad 12:cf8af0b4e0d2 248 }
Farshad 12:cf8af0b4e0d2 249
Farshad 12:cf8af0b4e0d2 250 void resetActivityCheckerTask()
Farshad 12:cf8af0b4e0d2 251 {
Farshad 12:cf8af0b4e0d2 252 activityChecker.detach();
Farshad 17:229d78f063fb 253 activityChecker.attach(activityCheckerTask, activityTimeout);
Farshad 12:cf8af0b4e0d2 254 }
Farshad 12:cf8af0b4e0d2 255
Farshad 6:09cdafc3ffeb 256 void onDataWritten(const GattWriteCallbackParams *params)
Farshad 6:09cdafc3ffeb 257 {
Farshad 6:09cdafc3ffeb 258 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
Farshad 6:09cdafc3ffeb 259 uint16_t bytesRead = params->len;
Farshad 6:09cdafc3ffeb 260 DEBUG("received %u bytes\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 261 for(int i = 0; i < bytesRead; i++) {
Farshad 6:09cdafc3ffeb 262 DEBUG("0x%X ", params->data[i]);
Farshad 6:09cdafc3ffeb 263 }
Farshad 6:09cdafc3ffeb 264 DEBUG("\n\r", bytesRead);
Farshad 6:09cdafc3ffeb 265
Farshad 6:09cdafc3ffeb 266 // echo?
Farshad 6:09cdafc3ffeb 267 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
Farshad 6:09cdafc3ffeb 268
Farshad 12:cf8af0b4e0d2 269 // make sure the laser is not powered off due to inactivity
Farshad 12:cf8af0b4e0d2 270 resetActivityCheckerTask();
Farshad 15:bc4f8c597c26 271 laserPtr->turnLaserPowerOn();
Farshad 15:bc4f8c597c26 272
Farshad 6:09cdafc3ffeb 273 processData(params);
Farshad 6:09cdafc3ffeb 274 }
Farshad 6:09cdafc3ffeb 275 }
Farshad 6:09cdafc3ffeb 276
Farshad 3:de77a4ebbf21 277 // this is an ISR, so do not spend too much time here and be careful with printing debug info
Farshad 2:79a9dad8bc5e 278 void readerCallback()
Farshad 8:ed66e7ef8243 279 {
Farshad 8:ed66e7ef8243 280 //if(serial.readable()) {
Farshad 12:cf8af0b4e0d2 281 // laserPtr->processRxData(serial.getc());
Farshad 7:8a23a257b66a 282 // }
Farshad 2:79a9dad8bc5e 283 }
Farshad 2:79a9dad8bc5e 284
Farshad 10:d37cd13dd529 285 /* This method is called when a distance measurement is ready to be sent to the client.
Farshad 10:d37cd13dd529 286 send distance measurement to the connected BLE client */
Farshad 8:ed66e7ef8243 287 void distanceCallcack(float distance, float elapsedTime)
Farshad 8:ed66e7ef8243 288 {
Farshad 8:ed66e7ef8243 289 uint8_t buf[10];
Farshad 8:ed66e7ef8243 290 uint16_t arrayLen = 2;
Farshad 8:ed66e7ef8243 291 memcpy(&buf[0], &arrayLen, sizeof(uint16_t));
Farshad 8:ed66e7ef8243 292 memcpy(&buf[2], &distance, sizeof(float));
Farshad 8:ed66e7ef8243 293 memcpy(&buf[6], &elapsedTime, sizeof(float));
Farshad 8:ed66e7ef8243 294 bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf));
Farshad 8:ed66e7ef8243 295 }
Farshad 8:ed66e7ef8243 296
Farshad 17:229d78f063fb 297 void notifyActivityTimeout(){
Farshad 17:229d78f063fb 298 uint8_t buf[1];
Farshad 17:229d78f063fb 299 bleHelper->sendPacketOverBLE(activityTimeoutCmd, buf, 0);
Farshad 17:229d78f063fb 300 }
Farshad 18:08184949ab30 301
Farshad 18:08184949ab30 302 void connectionLedBlinkerTask()
Farshad 18:08184949ab30 303 {
Farshad 18:08184949ab30 304 if(isConnected) {
Farshad 18:08184949ab30 305 if(connectionLed) {
Farshad 18:08184949ab30 306 connectionLed = 0;
Farshad 18:08184949ab30 307 connectionLedOffCount = 0;
Farshad 18:08184949ab30 308 } else if(++connectionLedOffCount > CONNECTION_LED_OFF_COUNT) {
Farshad 18:08184949ab30 309 connectionLed = 1;
Farshad 18:08184949ab30 310 }
Farshad 18:08184949ab30 311 } else {
Farshad 18:08184949ab30 312 connectionLed = 0;
Farshad 18:08184949ab30 313 }
Farshad 18:08184949ab30 314 }
Farshad 17:229d78f063fb 315
Farshad 17:229d78f063fb 316
Farshad 12:cf8af0b4e0d2 317 void batteryCheckerTask(void)
Farshad 12:cf8af0b4e0d2 318 {
Farshad 12:cf8af0b4e0d2 319 enableBattVoltSense = 1;
Farshad 12:cf8af0b4e0d2 320 wait_ms(20); // wait for the circuit to settle
Farshad 12:cf8af0b4e0d2 321 float batt = battVoltage.read();
Farshad 12:cf8af0b4e0d2 322 enableBattVoltSense = 0;
Farshad 12:cf8af0b4e0d2 323
Farshad 15:bc4f8c597c26 324 if(redLed == 0 && batt < BATT_VALUE_THRESHOLD) {
Farshad 12:cf8af0b4e0d2 325 redLed = 1;
Farshad 12:cf8af0b4e0d2 326 }
Farshad 12:cf8af0b4e0d2 327 }
Farshad 12:cf8af0b4e0d2 328
Farshad 12:cf8af0b4e0d2 329
bmentink 21:c19355cc5e79 330 /* processor for the hardware trigger button */
Farshad 10:d37cd13dd529 331 void triggerFall()
Farshad 10:d37cd13dd529 332 {
Farshad 12:cf8af0b4e0d2 333 resetActivityCheckerTask();
Farshad 15:bc4f8c597c26 334 laserPtr->turnLaserPowerOn();
Farshad 15:bc4f8c597c26 335
Farshad 12:cf8af0b4e0d2 336 laserPtr->triggerDistanceMeasurement();
Farshad 12:cf8af0b4e0d2 337
Farshad 10:d37cd13dd529 338 timer.reset();
Farshad 10:d37cd13dd529 339 timer.start();
Farshad 10:d37cd13dd529 340 }
Farshad 10:d37cd13dd529 341
Farshad 17:229d78f063fb 342 void turnLaserPowerOff()
Farshad 17:229d78f063fb 343 {
Farshad 17:229d78f063fb 344 laserPtr->turnLaserPowerOff();
Farshad 17:229d78f063fb 345 notifyActivityTimeout();
Farshad 17:229d78f063fb 346 }
Farshad 17:229d78f063fb 347
Farshad 10:d37cd13dd529 348 /* 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 349 otherwise debunce and make a measurement */
Farshad 10:d37cd13dd529 350 void triggerRise()
Farshad 10:d37cd13dd529 351 {
Farshad 10:d37cd13dd529 352 int elapsed = timer.read_ms();
Farshad 10:d37cd13dd529 353 timer.stop();
Farshad 10:d37cd13dd529 354 if(elapsed > PB_HOLD_TIME) {
Farshad 17:229d78f063fb 355 turnLaserPowerOff();
Farshad 15:bc4f8c597c26 356 }
Farshad 12:cf8af0b4e0d2 357 //else if(elapsed > PB_DEBUNCE_TIME) {
Farshad 12:cf8af0b4e0d2 358 // laserPtr->triggerDistanceMeasurement();
Farshad 12:cf8af0b4e0d2 359 // }
Farshad 12:cf8af0b4e0d2 360 }
Farshad 12:cf8af0b4e0d2 361
Farshad 12:cf8af0b4e0d2 362 void activityCheckerTask()
Farshad 12:cf8af0b4e0d2 363 {
Farshad 12:cf8af0b4e0d2 364 // too long with no activity- turn pwer off from laser to preserve power
Farshad 17:229d78f063fb 365 turnLaserPowerOff();
Farshad 8:ed66e7ef8243 366 }
Farshad 8:ed66e7ef8243 367
Farshad 0:d58d1cdf43a9 368 int main(void)
Farshad 0:d58d1cdf43a9 369 {
Farshad 5:207d4b6dface 370 connectionLed = 0;
Farshad 15:bc4f8c597c26 371
Farshad 15:bc4f8c597c26 372 // turn laser on and reset and wait for it to settle
Farshad 12:cf8af0b4e0d2 373 disableLRF = 0;
Farshad 12:cf8af0b4e0d2 374 nReset = 1;
Farshad 12:cf8af0b4e0d2 375 wait_ms(1000);
Farshad 5:207d4b6dface 376
Farshad 0:d58d1cdf43a9 377 DEBUG("Initialising the nRF51822\n\r");
Farshad 0:d58d1cdf43a9 378 ble.init();
Farshad 19:b576835175e2 379
Farshad 19:b576835175e2 380 /* Initialize BLE security */
Farshad 19:b576835175e2 381 bool enableBonding = true;
Farshad 19:b576835175e2 382 bool requireMITM = false;
Farshad 19:b576835175e2 383 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE);
Farshad 0:d58d1cdf43a9 384
Farshad 0:d58d1cdf43a9 385 ble.onDisconnection(disconnectionCallback);
Farshad 2:79a9dad8bc5e 386 ble.onConnection(connectionCallback);
Farshad 0:d58d1cdf43a9 387 ble.onDataWritten(onDataWritten);
Farshad 19:b576835175e2 388 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
Farshad 19:b576835175e2 389 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
Farshad 0:d58d1cdf43a9 390
Farshad 0:d58d1cdf43a9 391 /* setup advertising */
Farshad 0:d58d1cdf43a9 392 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Farshad 0:d58d1cdf43a9 393 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Farshad 0:d58d1cdf43a9 394 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
Farshad 0:d58d1cdf43a9 395 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Farshad 0:d58d1cdf43a9 396 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
Farshad 1:e18634cb382a 397 (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE));
Farshad 2:79a9dad8bc5e 398 ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
Farshad 0:d58d1cdf43a9 399 ble.startAdvertising();
Farshad 15:bc4f8c597c26 400
Farshad 12:cf8af0b4e0d2 401 // check battery level now and every minute afterwards
Farshad 12:cf8af0b4e0d2 402 batteryCheckerTask();
Farshad 15:bc4f8c597c26 403 batteryChecker.attach(batteryCheckerTask, 60);
Farshad 15:bc4f8c597c26 404
Farshad 12:cf8af0b4e0d2 405 // check acticity once every 3 minutes
Farshad 17:229d78f063fb 406 activityChecker.attach(activityCheckerTask, activityTimeout);
Farshad 0:d58d1cdf43a9 407
Farshad 0:d58d1cdf43a9 408 /* Setup uart service */
Farshad 0:d58d1cdf43a9 409 UARTService uartService(ble);
Farshad 0:d58d1cdf43a9 410 uartServicePtr = &uartService;
Farshad 0:d58d1cdf43a9 411
Farshad 0:d58d1cdf43a9 412 /* Setup auxiliary service. */
Farshad 0:d58d1cdf43a9 413 DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);
Farshad 0:d58d1cdf43a9 414
Farshad 7:8a23a257b66a 415 /* Setup bleHelper */
Farshad 7:8a23a257b66a 416 BLEHelper helper(&ble, uartServicePtr);
Farshad 7:8a23a257b66a 417 bleHelper = &helper;
Farshad 8:ed66e7ef8243 418
Farshad 8:ed66e7ef8243 419 // setup serial port to LRF
Farshad 12:cf8af0b4e0d2 420 serialPtr = new Serial(p27, p26);
Farshad 12:cf8af0b4e0d2 421 disableFlowControl();
Farshad 12:cf8af0b4e0d2 422 serialPtr->baud(READER_BAUD_RATE);
Farshad 8:ed66e7ef8243 423 // serial.attach(&readerCallback);
Farshad 8:ed66e7ef8243 424
Farshad 10:d37cd13dd529 425 // processors for the trigger button
Farshad 10:d37cd13dd529 426 triggerButton.fall(&triggerFall);
Farshad 10:d37cd13dd529 427 triggerButton.rise(&triggerRise);
Farshad 10:d37cd13dd529 428
Farshad 10:d37cd13dd529 429 // setup laser
Farshad 12:cf8af0b4e0d2 430 laserPtr = new Laser(*serialPtr);
Farshad 12:cf8af0b4e0d2 431 laserPtr->enableMeasurement(true);
Farshad 12:cf8af0b4e0d2 432 laserPtr->setDistaceCallback(&distanceCallcack);
Farshad 15:bc4f8c597c26 433 laserPtr->turnLaserPowerOn();
bmentink 21:c19355cc5e79 434 laserPtr->setRedDot(false); // turn off dot initially
Farshad 18:08184949ab30 435
Farshad 18:08184949ab30 436 // setup blinking connectionLed
Farshad 18:08184949ab30 437 // connectionLed.period(2.0f);
Farshad 18:08184949ab30 438 connectionLedBlinker.attach(connectionLedBlinkerTask, CONNECTION_LED_ON_TIME_MS / 1000.0f);
bmentink 21:c19355cc5e79 439
bmentink 21:c19355cc5e79 440 // Setup task to implement ACA Charger mode on phone.
bmentink 21:c19355cc5e79 441 ACACharger.attach(ACAChargerTask, ACA_CHARGER_TASK_TIME_MS / 1000.0f);
Farshad 5:207d4b6dface 442
Farshad 0:d58d1cdf43a9 443 while (true) {
Farshad 0:d58d1cdf43a9 444 ble.waitForEvent();
Farshad 0:d58d1cdf43a9 445 }
Farshad 8:ed66e7ef8243 446 }