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