EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
main.cpp@18:08184949ab30, 2017-07-18 (annotated)
- Committer:
- Farshad
- Date:
- Tue Jul 18 05:17:37 2017 +0000
- Revision:
- 18:08184949ab30
- Parent:
- 17:229d78f063fb
- Child:
- 19:b576835175e2
Version 1.5- Some modifications to save power. After each measurement go to redDot only to reduce current consumption from about 250mA to 50mA. Also the connectionLed now blinks every 2 seconds for 20ms to reduce the 4-5mA proportionally.
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 | 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 | 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 | 6:09cdafc3ffeb | 139 | static void processData(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 140 | { |
Farshad | 6:09cdafc3ffeb | 141 | if(params->len >= 2) { |
Farshad | 6:09cdafc3ffeb | 142 | uint16_t command = params->data[0] + (params->data[1] << 8); |
Farshad | 6:09cdafc3ffeb | 143 | bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK; |
Farshad | 6:09cdafc3ffeb | 144 | DEBUG("command: %d \r\n", command); |
Farshad | 6:09cdafc3ffeb | 145 | |
Farshad | 6:09cdafc3ffeb | 146 | switch(command & ~SET_PARAM_CMD_MASK) { |
Farshad | 7:8a23a257b66a | 147 | case distanceCmd: |
Farshad | 6:09cdafc3ffeb | 148 | if(!isSetCmd && params->len == 2) { |
Farshad | 6:09cdafc3ffeb | 149 | // form the reply to send |
Farshad | 7:8a23a257b66a | 150 | DEBUG("CMD is GET distance\n\r"); |
Farshad | 12:cf8af0b4e0d2 | 151 | laserPtr->triggerDistanceMeasurement(); |
Farshad | 6:09cdafc3ffeb | 152 | } |
Farshad | 6:09cdafc3ffeb | 153 | break; |
Farshad | 8:ed66e7ef8243 | 154 | |
Farshad | 7:8a23a257b66a | 155 | // TODO not needed really- can just use the distance command |
Farshad | 8:ed66e7ef8243 | 156 | case triggerCmd: |
Farshad | 6:09cdafc3ffeb | 157 | if(isSetCmd && params->len == 3) { |
Farshad | 7:8a23a257b66a | 158 | // form the reply to send |
Farshad | 7:8a23a257b66a | 159 | DEBUG("CMD is SET trigger\n\r"); |
Farshad | 12:cf8af0b4e0d2 | 160 | laserPtr->triggerDistanceMeasurement(); |
Farshad | 6:09cdafc3ffeb | 161 | } |
Farshad | 6:09cdafc3ffeb | 162 | break; |
Farshad | 6:09cdafc3ffeb | 163 | |
Farshad | 10:d37cd13dd529 | 164 | case redDotCmd: |
Farshad | 10:d37cd13dd529 | 165 | if(isSetCmd && params->len == 3) { |
Farshad | 10:d37cd13dd529 | 166 | DEBUG("CMD is SET redDot\n\r"); |
Farshad | 12:cf8af0b4e0d2 | 167 | laserPtr->setRedDot(params->data[2]); |
Farshad | 12:cf8af0b4e0d2 | 168 | } |
Farshad | 12:cf8af0b4e0d2 | 169 | break; |
Farshad | 15:bc4f8c597c26 | 170 | |
Farshad | 12:cf8af0b4e0d2 | 171 | case resetCmd: |
Farshad | 12:cf8af0b4e0d2 | 172 | if(isSetCmd && params->len == 3) { |
Farshad | 12:cf8af0b4e0d2 | 173 | DEBUG("CMD is reset\n\r"); |
Farshad | 12:cf8af0b4e0d2 | 174 | nReset = 0; |
Farshad | 12:cf8af0b4e0d2 | 175 | wait_ms(100); |
Farshad | 12:cf8af0b4e0d2 | 176 | nReset = 1; |
Farshad | 12:cf8af0b4e0d2 | 177 | wait_ms(1000); |
Farshad | 12:cf8af0b4e0d2 | 178 | laserPtr->enableMeasurement(true); |
Farshad | 10:d37cd13dd529 | 179 | } |
Farshad | 10:d37cd13dd529 | 180 | break; |
Farshad | 15:bc4f8c597c26 | 181 | |
Farshad | 15:bc4f8c597c26 | 182 | case nSamplesCmd: |
Farshad | 15:bc4f8c597c26 | 183 | if(isSetCmd && params->len == 4) { |
Farshad | 15:bc4f8c597c26 | 184 | DEBUG("CMD is nSample\n\r"); |
Farshad | 15:bc4f8c597c26 | 185 | int16_t nSamples = params->data[2] + (params->data[3] << 8); |
Farshad | 15:bc4f8c597c26 | 186 | laserPtr->setNumberOfSamples(nSamples); |
Farshad | 15:bc4f8c597c26 | 187 | } |
Farshad | 15:bc4f8c597c26 | 188 | break; |
Farshad | 17:229d78f063fb | 189 | |
Farshad | 17:229d78f063fb | 190 | case activityTimeoutCmd: |
Farshad | 17:229d78f063fb | 191 | if(isSetCmd && params->len == 4) { |
Farshad | 17:229d78f063fb | 192 | DEBUG("CMD is nSample\n\r"); |
Farshad | 17:229d78f063fb | 193 | activityTimeout = params->data[2] + (params->data[3] << 8); |
Farshad | 17:229d78f063fb | 194 | } |
Farshad | 17:229d78f063fb | 195 | break; |
Farshad | 10:d37cd13dd529 | 196 | |
Farshad | 6:09cdafc3ffeb | 197 | default: |
Farshad | 6:09cdafc3ffeb | 198 | break; |
Farshad | 6:09cdafc3ffeb | 199 | } |
Farshad | 6:09cdafc3ffeb | 200 | } |
Farshad | 6:09cdafc3ffeb | 201 | } |
Farshad | 6:09cdafc3ffeb | 202 | |
Farshad | 12:cf8af0b4e0d2 | 203 | |
Farshad | 12:cf8af0b4e0d2 | 204 | // Serial driver seems to be buggy and allocate p8 and p10 for flow control |
Farshad | 12:cf8af0b4e0d2 | 205 | // by default- Need to manually disable flow control since the function for |
Farshad | 12:cf8af0b4e0d2 | 206 | // disabling Serail.set_flow_control(Serial::Disabled) is not recognised by |
Farshad | 12:cf8af0b4e0d2 | 207 | // the compiler either! Could be something to do with the target processor?? |
Farshad | 12:cf8af0b4e0d2 | 208 | static void disableFlowControl() |
Farshad | 12:cf8af0b4e0d2 | 209 | { |
Farshad | 12:cf8af0b4e0d2 | 210 | uint32_t base = 0x40002000; |
Farshad | 12:cf8af0b4e0d2 | 211 | uint32_t ctsOffset = 0x510; |
Farshad | 12:cf8af0b4e0d2 | 212 | uint32_t rtsOffset = 0x508; |
Farshad | 12:cf8af0b4e0d2 | 213 | |
Farshad | 12:cf8af0b4e0d2 | 214 | uint32_t cts = base + ctsOffset; |
Farshad | 12:cf8af0b4e0d2 | 215 | uint32_t rts = base + rtsOffset; |
Farshad | 12:cf8af0b4e0d2 | 216 | |
Farshad | 12:cf8af0b4e0d2 | 217 | uint32_t* pcts = (uint32_t*)cts; |
Farshad | 12:cf8af0b4e0d2 | 218 | uint32_t* prts = (uint32_t*)rts; |
Farshad | 12:cf8af0b4e0d2 | 219 | |
Farshad | 12:cf8af0b4e0d2 | 220 | // no pin allocated for CTS and RTS |
Farshad | 12:cf8af0b4e0d2 | 221 | *pcts = 0xffffffff; |
Farshad | 12:cf8af0b4e0d2 | 222 | *prts = 0xffffffff; |
Farshad | 12:cf8af0b4e0d2 | 223 | } |
Farshad | 12:cf8af0b4e0d2 | 224 | |
Farshad | 12:cf8af0b4e0d2 | 225 | void resetActivityCheckerTask() |
Farshad | 12:cf8af0b4e0d2 | 226 | { |
Farshad | 12:cf8af0b4e0d2 | 227 | activityChecker.detach(); |
Farshad | 17:229d78f063fb | 228 | activityChecker.attach(activityCheckerTask, activityTimeout); |
Farshad | 12:cf8af0b4e0d2 | 229 | } |
Farshad | 12:cf8af0b4e0d2 | 230 | |
Farshad | 6:09cdafc3ffeb | 231 | void onDataWritten(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 232 | { |
Farshad | 6:09cdafc3ffeb | 233 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
Farshad | 6:09cdafc3ffeb | 234 | uint16_t bytesRead = params->len; |
Farshad | 6:09cdafc3ffeb | 235 | DEBUG("received %u bytes\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 236 | for(int i = 0; i < bytesRead; i++) { |
Farshad | 6:09cdafc3ffeb | 237 | DEBUG("0x%X ", params->data[i]); |
Farshad | 6:09cdafc3ffeb | 238 | } |
Farshad | 6:09cdafc3ffeb | 239 | DEBUG("\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 240 | |
Farshad | 6:09cdafc3ffeb | 241 | // echo? |
Farshad | 6:09cdafc3ffeb | 242 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
Farshad | 6:09cdafc3ffeb | 243 | |
Farshad | 12:cf8af0b4e0d2 | 244 | // make sure the laser is not powered off due to inactivity |
Farshad | 12:cf8af0b4e0d2 | 245 | resetActivityCheckerTask(); |
Farshad | 15:bc4f8c597c26 | 246 | laserPtr->turnLaserPowerOn(); |
Farshad | 15:bc4f8c597c26 | 247 | |
Farshad | 6:09cdafc3ffeb | 248 | processData(params); |
Farshad | 6:09cdafc3ffeb | 249 | } |
Farshad | 6:09cdafc3ffeb | 250 | } |
Farshad | 6:09cdafc3ffeb | 251 | |
Farshad | 3:de77a4ebbf21 | 252 | // this is an ISR, so do not spend too much time here and be careful with printing debug info |
Farshad | 2:79a9dad8bc5e | 253 | void readerCallback() |
Farshad | 8:ed66e7ef8243 | 254 | { |
Farshad | 8:ed66e7ef8243 | 255 | //if(serial.readable()) { |
Farshad | 12:cf8af0b4e0d2 | 256 | // laserPtr->processRxData(serial.getc()); |
Farshad | 7:8a23a257b66a | 257 | // } |
Farshad | 2:79a9dad8bc5e | 258 | } |
Farshad | 2:79a9dad8bc5e | 259 | |
Farshad | 10:d37cd13dd529 | 260 | /* This method is called when a distance measurement is ready to be sent to the client. |
Farshad | 10:d37cd13dd529 | 261 | send distance measurement to the connected BLE client */ |
Farshad | 8:ed66e7ef8243 | 262 | void distanceCallcack(float distance, float elapsedTime) |
Farshad | 8:ed66e7ef8243 | 263 | { |
Farshad | 8:ed66e7ef8243 | 264 | uint8_t buf[10]; |
Farshad | 8:ed66e7ef8243 | 265 | uint16_t arrayLen = 2; |
Farshad | 8:ed66e7ef8243 | 266 | memcpy(&buf[0], &arrayLen, sizeof(uint16_t)); |
Farshad | 8:ed66e7ef8243 | 267 | memcpy(&buf[2], &distance, sizeof(float)); |
Farshad | 8:ed66e7ef8243 | 268 | memcpy(&buf[6], &elapsedTime, sizeof(float)); |
Farshad | 8:ed66e7ef8243 | 269 | bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf)); |
Farshad | 8:ed66e7ef8243 | 270 | } |
Farshad | 8:ed66e7ef8243 | 271 | |
Farshad | 17:229d78f063fb | 272 | void notifyActivityTimeout(){ |
Farshad | 17:229d78f063fb | 273 | uint8_t buf[1]; |
Farshad | 17:229d78f063fb | 274 | bleHelper->sendPacketOverBLE(activityTimeoutCmd, buf, 0); |
Farshad | 17:229d78f063fb | 275 | } |
Farshad | 18:08184949ab30 | 276 | |
Farshad | 18:08184949ab30 | 277 | void connectionLedBlinkerTask() |
Farshad | 18:08184949ab30 | 278 | { |
Farshad | 18:08184949ab30 | 279 | if(isConnected) { |
Farshad | 18:08184949ab30 | 280 | if(connectionLed) { |
Farshad | 18:08184949ab30 | 281 | connectionLed = 0; |
Farshad | 18:08184949ab30 | 282 | connectionLedOffCount = 0; |
Farshad | 18:08184949ab30 | 283 | } else if(++connectionLedOffCount > CONNECTION_LED_OFF_COUNT) { |
Farshad | 18:08184949ab30 | 284 | connectionLed = 1; |
Farshad | 18:08184949ab30 | 285 | } |
Farshad | 18:08184949ab30 | 286 | } else { |
Farshad | 18:08184949ab30 | 287 | connectionLed = 0; |
Farshad | 18:08184949ab30 | 288 | } |
Farshad | 18:08184949ab30 | 289 | } |
Farshad | 17:229d78f063fb | 290 | |
Farshad | 17:229d78f063fb | 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 | 15:bc4f8c597c26 | 299 | if(redLed == 0 && batt < BATT_VALUE_THRESHOLD) { |
Farshad | 12:cf8af0b4e0d2 | 300 | redLed = 1; |
Farshad | 12:cf8af0b4e0d2 | 301 | } |
Farshad | 12:cf8af0b4e0d2 | 302 | } |
Farshad | 12:cf8af0b4e0d2 | 303 | |
Farshad | 12:cf8af0b4e0d2 | 304 | |
Farshad | 12:cf8af0b4e0d2 | 305 | |
Farshad | 10:d37cd13dd529 | 306 | /* processor for the hardware trigger button */ |
Farshad | 10:d37cd13dd529 | 307 | void triggerFall() |
Farshad | 10:d37cd13dd529 | 308 | { |
Farshad | 12:cf8af0b4e0d2 | 309 | resetActivityCheckerTask(); |
Farshad | 15:bc4f8c597c26 | 310 | laserPtr->turnLaserPowerOn(); |
Farshad | 15:bc4f8c597c26 | 311 | |
Farshad | 12:cf8af0b4e0d2 | 312 | laserPtr->triggerDistanceMeasurement(); |
Farshad | 12:cf8af0b4e0d2 | 313 | |
Farshad | 10:d37cd13dd529 | 314 | timer.reset(); |
Farshad | 10:d37cd13dd529 | 315 | timer.start(); |
Farshad | 10:d37cd13dd529 | 316 | } |
Farshad | 10:d37cd13dd529 | 317 | |
Farshad | 17:229d78f063fb | 318 | void turnLaserPowerOff() |
Farshad | 17:229d78f063fb | 319 | { |
Farshad | 17:229d78f063fb | 320 | laserPtr->turnLaserPowerOff(); |
Farshad | 17:229d78f063fb | 321 | notifyActivityTimeout(); |
Farshad | 17:229d78f063fb | 322 | } |
Farshad | 17:229d78f063fb | 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 | 17:229d78f063fb | 331 | turnLaserPowerOff(); |
Farshad | 15:bc4f8c597c26 | 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 | 17:229d78f063fb | 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 | 15:bc4f8c597c26 | 347 | |
Farshad | 15:bc4f8c597c26 | 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 | 15:bc4f8c597c26 | 369 | |
Farshad | 12:cf8af0b4e0d2 | 370 | // check battery level now and every minute afterwards |
Farshad | 12:cf8af0b4e0d2 | 371 | batteryCheckerTask(); |
Farshad | 15:bc4f8c597c26 | 372 | batteryChecker.attach(batteryCheckerTask, 60); |
Farshad | 15:bc4f8c597c26 | 373 | |
Farshad | 12:cf8af0b4e0d2 | 374 | // check acticity once every 3 minutes |
Farshad | 17:229d78f063fb | 375 | activityChecker.attach(activityCheckerTask, activityTimeout); |
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 | 15:bc4f8c597c26 | 402 | laserPtr->turnLaserPowerOn(); |
Farshad | 18:08184949ab30 | 403 | |
Farshad | 18:08184949ab30 | 404 | // setup blinking connectionLed |
Farshad | 18:08184949ab30 | 405 | // connectionLed.period(2.0f); |
Farshad | 18:08184949ab30 | 406 | connectionLedBlinker.attach(connectionLedBlinkerTask, CONNECTION_LED_ON_TIME_MS / 1000.0f); |
Farshad | 5:207d4b6dface | 407 | |
Farshad | 0:d58d1cdf43a9 | 408 | while (true) { |
Farshad | 0:d58d1cdf43a9 | 409 | ble.waitForEvent(); |
Farshad | 0:d58d1cdf43a9 | 410 | } |
Farshad | 8:ed66e7ef8243 | 411 | } |