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