Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
main.cpp@23:26f27c462976, 2015-12-14 (annotated)
- Committer:
- elmbed
- Date:
- Mon Dec 14 14:24:10 2015 +0000
- Revision:
- 23:26f27c462976
- Parent:
- 19:afcbb425b3cf
- Child:
- 24:ff54dec20f55
Radio sort of works but still has many bugs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AntonLS | 0:28ca4562fe1a | 1 | /* |
AntonLS | 0:28ca4562fe1a | 2 | * TA test |
AntonLS | 0:28ca4562fe1a | 3 | * |
AntonLS | 19:afcbb425b3cf | 4 | * Updated for New TA Baseboard (rev Aug-Nov 2015.) |
AntonLS | 19:afcbb425b3cf | 5 | * |
AntonLS | 11:d3aa5fca2330 | 6 | * TODO maybe have a mode where the serial port I/O can be swapped, |
AntonLS | 11:d3aa5fca2330 | 7 | * such that what the nRF generates is sent out the serial port, |
AntonLS | 11:d3aa5fca2330 | 8 | * and what comes in the serial port goes into the nRF. |
AntonLS | 11:d3aa5fca2330 | 9 | * Maybe could use the now-unused CTS pin for that. |
AntonLS | 13:28332f65d14b | 10 | * |
AntonLS | 13:28332f65d14b | 11 | * Using |
AntonLS | 13:28332f65d14b | 12 | * rev 327 of BLE_API |
AntonLS | 13:28332f65d14b | 13 | * rev 102 of nRF51822 w/ modification--See below |
AntonLS | 15:b86c4b798aa1 | 14 | * rev 97 of mbed lib |
AntonLS | 15:b86c4b798aa1 | 15 | * corresponding to: |
AntonLS | 15:b86c4b798aa1 | 16 | * rev 493 of mbed-src w/ modification to targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c |
AntonLS | 19:afcbb425b3cf | 17 | * so things compile properly.* |
AntonLS | 19:afcbb425b3cf | 18 | * * Now using $Sub$$UART0_IRQHandler to override UART0_IRQHandler without needing to use lib source. |
AntonLS | 13:28332f65d14b | 19 | * |
AntonLS | 13:28332f65d14b | 20 | * Changed |
AntonLS | 13:28332f65d14b | 21 | * nRF51822/nordic/pstorage_platform.h PSTORAGE_MIN_BLOCK_SIZE changed from 0x010 to 4. |
AntonLS | 0:28ca4562fe1a | 22 | */ |
AntonLS | 0:28ca4562fe1a | 23 | |
AntonLS | 0:28ca4562fe1a | 24 | /* mbed Microcontroller Library |
AntonLS | 0:28ca4562fe1a | 25 | * Copyright (c) 2006-2013 ARM Limited |
AntonLS | 0:28ca4562fe1a | 26 | * |
AntonLS | 0:28ca4562fe1a | 27 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AntonLS | 0:28ca4562fe1a | 28 | * you may not use this file except in compliance with the License. |
AntonLS | 0:28ca4562fe1a | 29 | * You may obtain a copy of the License at |
AntonLS | 0:28ca4562fe1a | 30 | * |
AntonLS | 0:28ca4562fe1a | 31 | * http://www.apache.org/licenses/LICENSE-2.0 |
AntonLS | 0:28ca4562fe1a | 32 | * |
AntonLS | 0:28ca4562fe1a | 33 | * Unless required by applicable law or agreed to in writing, software |
AntonLS | 0:28ca4562fe1a | 34 | * distributed under the License is distributed on an "AS IS" BASIS, |
AntonLS | 0:28ca4562fe1a | 35 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AntonLS | 0:28ca4562fe1a | 36 | * See the License for the specific language governing permissions and |
AntonLS | 0:28ca4562fe1a | 37 | * limitations under the License. |
AntonLS | 0:28ca4562fe1a | 38 | */ |
elmbed | 18:affef3a7db2a | 39 | #include <cstdarg> |
elmbed | 18:affef3a7db2a | 40 | #include <cstdio> |
AntonLS | 0:28ca4562fe1a | 41 | #include "mbed.h" |
AntonLS | 12:6d313d575f84 | 42 | // #include "rtos.h" |
AntonLS | 0:28ca4562fe1a | 43 | #include "BLEDevice.h" |
AntonLS | 0:28ca4562fe1a | 44 | |
AntonLS | 0:28ca4562fe1a | 45 | #include "DFUService.h" |
AntonLS | 0:28ca4562fe1a | 46 | #include "UARTService.h" |
AntonLS | 0:28ca4562fe1a | 47 | #include "DeviceInformationService.h" |
AntonLS | 19:afcbb425b3cf | 48 | #include "BatteryService.h" |
AntonLS | 0:28ca4562fe1a | 49 | |
AntonLS | 0:28ca4562fe1a | 50 | #include "MTSSerialFlowControl.h" |
AntonLS | 5:1b9734e68327 | 51 | #include "PhoneAppIO.h" |
AntonLS | 0:28ca4562fe1a | 52 | |
elmbed | 23:26f27c462976 | 53 | #include "types.h" |
elmbed | 18:affef3a7db2a | 54 | #include "TA.h" |
elmbed | 18:affef3a7db2a | 55 | |
elmbed | 23:26f27c462976 | 56 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
AntonLS | 0:28ca4562fe1a | 57 | * it will have an impact on code-size and power consumption. */ |
AntonLS | 0:28ca4562fe1a | 58 | |
AntonLS | 11:d3aa5fca2330 | 59 | #define LOOPBACK_MODE 0 // Loopback mode |
AntonLS | 4:17b8edf264c3 | 60 | |
AntonLS | 0:28ca4562fe1a | 61 | #if NEED_CONSOLE_OUTPUT |
AntonLS | 0:28ca4562fe1a | 62 | #define DEBUG(...) { printf(__VA_ARGS__); } |
AntonLS | 0:28ca4562fe1a | 63 | #else |
AntonLS | 0:28ca4562fe1a | 64 | #define DEBUG(...) /* nothing */ |
AntonLS | 0:28ca4562fe1a | 65 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
AntonLS | 0:28ca4562fe1a | 66 | |
elmbed | 17:d8b901d791fd | 67 | void loop(); |
elmbed | 17:d8b901d791fd | 68 | void setup(); |
elmbed | 17:d8b901d791fd | 69 | void getRadioInput(char *ibuffer, int size); |
elmbed | 17:d8b901d791fd | 70 | |
AntonLS | 19:afcbb425b3cf | 71 | #define BLENANO 0 // BLE Nano vs. TA New Baseboard (rev Aug-Nov 2015.) |
AntonLS | 19:afcbb425b3cf | 72 | |
AntonLS | 19:afcbb425b3cf | 73 | #if BLENANO |
AntonLS | 19:afcbb425b3cf | 74 | #define SERTX USBTX |
AntonLS | 19:afcbb425b3cf | 75 | #define SERRX USBRX |
AntonLS | 19:afcbb425b3cf | 76 | #else // Same RTS pin used by all (P0_08) and unused CTS (P0_10) |
AntonLS | 19:afcbb425b3cf | 77 | #define SERTX P0_18 |
AntonLS | 19:afcbb425b3cf | 78 | #define SERRX P0_17 |
AntonLS | 19:afcbb425b3cf | 79 | #endif |
AntonLS | 19:afcbb425b3cf | 80 | |
elmbed | 18:affef3a7db2a | 81 | static Timer tmr; |
elmbed | 17:d8b901d791fd | 82 | |
elmbed | 17:d8b901d791fd | 83 | unsigned long millis() |
elmbed | 17:d8b901d791fd | 84 | { |
elmbed | 17:d8b901d791fd | 85 | return tmr.read_ms(); |
elmbed | 17:d8b901d791fd | 86 | } |
elmbed | 17:d8b901d791fd | 87 | |
elmbed | 17:d8b901d791fd | 88 | unsigned long micros() |
elmbed | 17:d8b901d791fd | 89 | { |
AntonLS | 19:afcbb425b3cf | 90 | return tmr.read_us(); |
elmbed | 17:d8b901d791fd | 91 | } |
elmbed | 17:d8b901d791fd | 92 | |
AntonLS | 0:28ca4562fe1a | 93 | extern "C" |
AntonLS | 0:28ca4562fe1a | 94 | { |
AntonLS | 13:28332f65d14b | 95 | #include "softdevice_handler.h" // Attempt to get pstorage enqueued cmd callbacks. |
AntonLS | 13:28332f65d14b | 96 | #include "app_timer.h" |
AntonLS | 13:28332f65d14b | 97 | #include "pstorage.h" |
AntonLS | 13:28332f65d14b | 98 | |
AntonLS | 16:3c873f2c8a27 | 99 | // void My_UART0_IRQHandler(); |
AntonLS | 12:6d313d575f84 | 100 | |
AntonLS | 0:28ca4562fe1a | 101 | void pin_mode( PinName, PinMode ); |
AntonLS | 0:28ca4562fe1a | 102 | } |
AntonLS | 0:28ca4562fe1a | 103 | |
AntonLS | 12:6d313d575f84 | 104 | // DigitalOut rts( RTS_PIN_NUMBER ); |
AntonLS | 19:afcbb425b3cf | 105 | DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO |
AntonLS | 12:6d313d575f84 | 106 | |
AntonLS | 12:6d313d575f84 | 107 | // Check if we should swap serial Rx/Tx for early rev of "Little Brain" |
AntonLS | 19:afcbb425b3cf | 108 | DigitalIn trSwp( P0_30, PullUp ); |
AntonLS | 12:6d313d575f84 | 109 | |
AntonLS | 12:6d313d575f84 | 110 | // Wait to settle. |
AntonLS | 12:6d313d575f84 | 111 | int foo = (wait( 0.1 ), 0); |
AntonLS | 12:6d313d575f84 | 112 | |
AntonLS | 14:76fb883a3cb8 | 113 | // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins. |
AntonLS | 19:afcbb425b3cf | 114 | static DigitalOut led0( P0_19, 1 ); // TA New Baseboard LED High=On / Buzzer Low=En ("STATUS") (OK on Nano: LED Low=On) |
AntonLS | 19:afcbb425b3cf | 115 | // Note: TA New Baseboard LED off only if Buzzer is sounding. |
AntonLS | 19:afcbb425b3cf | 116 | static DigitalOut led1( P0_21, 0 ); // TA New Baseboard High=On (OK on Nano: NC) |
AntonLS | 19:afcbb425b3cf | 117 | DigitalOut led2( P0_22, 1 ); // TA New Baseboard High=On (OK on Nano: NC) |
AntonLS | 19:afcbb425b3cf | 118 | |
AntonLS | 19:afcbb425b3cf | 119 | // DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On (OK on Nano: Alt RxD) |
AntonLS | 14:76fb883a3cb8 | 120 | // DigitalOut led1( (trSwp ? P0_4 : P0_3), 0 ); // TA Baseboard High=On (And don't use P0_4 on Nano) |
AntonLS | 14:76fb883a3cb8 | 121 | |
elmbed | 23:26f27c462976 | 122 | //DigitalOut buzz( P0_20, 1 ); // TA New Baseboard Low=On (OK on Nano: NC) |
AntonLS | 19:afcbb425b3cf | 123 | |
AntonLS | 13:28332f65d14b | 124 | // App timer, app scheduler, and pstorage are already setup by bootloader. |
AntonLS | 13:28332f65d14b | 125 | |
elmbed | 18:affef3a7db2a | 126 | static BLEDevice ble; |
AntonLS | 19:afcbb425b3cf | 127 | |
AntonLS | 12:6d313d575f84 | 128 | |
AntonLS | 1:0ba687d4196f | 129 | // Note: From the datasheet: |
AntonLS | 1:0ba687d4196f | 130 | // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled. |
AntonLS | 11:d3aa5fca2330 | 131 | // But a version of serial_init() erroneously enabled the uart before the setting of those, |
AntonLS | 11:d3aa5fca2330 | 132 | // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH! |
AntonLS | 11:d3aa5fca2330 | 133 | // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase) |
AntonLS | 7:205ef63d311a | 134 | // to not use serial_init() in serial_api.c, so flow control is setup correctly * |
AntonLS | 7:205ef63d311a | 135 | // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default * |
AntonLS | 15:b86c4b798aa1 | 136 | // [* We can't change the uart interrupt vector, so we comment-out the handler in |
AntonLS | 15:b86c4b798aa1 | 137 | // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work.] - No need now. |
AntonLS | 15:b86c4b798aa1 | 138 | // * Now using $Sub$$UART0_IRQHandler to override UART0_IRQHandler without needing to use lib source. |
AntonLS | 15:b86c4b798aa1 | 139 | // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might have worked--No need. |
AntonLS | 15:b86c4b798aa1 | 140 | // |
AntonLS | 6:ef758ac3c928 | 141 | // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its |
AntonLS | 11:d3aa5fca2330 | 142 | // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay. |
AntonLS | 1:0ba687d4196f | 143 | // |
AntonLS | 19:afcbb425b3cf | 144 | // mts::MTSSerialFlowControl pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 ); |
AntonLS | 19:afcbb425b3cf | 145 | //mts::MTSSerial pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), 128, 64, RTS_PIN_NUMBER, NC ); // 256, 1280 // 256, 2560 |
AntonLS | 0:28ca4562fe1a | 146 | |
AntonLS | 0:28ca4562fe1a | 147 | uint8_t txPayload[TXRX_BUF_LEN] = { 0 }; |
AntonLS | 0:28ca4562fe1a | 148 | |
AntonLS | 0:28ca4562fe1a | 149 | |
AntonLS | 5:1b9734e68327 | 150 | char deviceName[6]; // "TAF00"; |
AntonLS | 5:1b9734e68327 | 151 | Gap::address_t macAddr; |
AntonLS | 5:1b9734e68327 | 152 | Gap::addr_type_t *pAdType; |
AntonLS | 19:afcbb425b3cf | 153 | static const uint16_t uuid16_list[] = { GattService::UUID_DEVICE_INFORMATION_SERVICE, |
AntonLS | 19:afcbb425b3cf | 154 | GattService::UUID_BATTERY_SERVICE }; |
AntonLS | 19:afcbb425b3cf | 155 | static const uint8_t batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff, |
AntonLS | 19:afcbb425b3cf | 156 | GattService::UUID_BATTERY_SERVICE >> 8, |
AntonLS | 19:afcbb425b3cf | 157 | 99, // Batt level TODO |
AntonLS | 19:afcbb425b3cf | 158 | 'T', 'X' }; // Custom ID trick |
AntonLS | 19:afcbb425b3cf | 159 | |
AntonLS | 19:afcbb425b3cf | 160 | UARTService *uartServicePtr; |
AntonLS | 19:afcbb425b3cf | 161 | PhoneAppIO *phoneP; |
AntonLS | 5:1b9734e68327 | 162 | |
elmbed | 18:affef3a7db2a | 163 | // Buffer for holding data from the phone |
elmbed | 18:affef3a7db2a | 164 | // to the device |
AntonLS | 19:afcbb425b3cf | 165 | #define PHTODEV_BUF_LEN 200 /**/ |
AntonLS | 19:afcbb425b3cf | 166 | /// static char phoneToDev[200] = {0}; |
AntonLS | 19:afcbb425b3cf | 167 | static char phoneToDev[PHTODEV_BUF_LEN] = {0}; |
AntonLS | 0:28ca4562fe1a | 168 | |
AntonLS | 19:afcbb425b3cf | 169 | /// // Current position in the buffer |
AntonLS | 19:afcbb425b3cf | 170 | /// static int phoneToDevPos = 0; |
AntonLS | 0:28ca4562fe1a | 171 | |
elmbed | 18:affef3a7db2a | 172 | extern TA ta; |
elmbed | 18:affef3a7db2a | 173 | |
elmbed | 18:affef3a7db2a | 174 | extern void radio_init(); |
elmbed | 18:affef3a7db2a | 175 | extern void radio_loop(); |
elmbed | 18:affef3a7db2a | 176 | |
elmbed | 18:affef3a7db2a | 177 | // True when connected to a phone |
AntonLS | 11:d3aa5fca2330 | 178 | bool connected = false; |
AntonLS | 5:1b9734e68327 | 179 | |
AntonLS | 11:d3aa5fca2330 | 180 | void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType, |
AntonLS | 11:d3aa5fca2330 | 181 | const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams ) |
AntonLS | 11:d3aa5fca2330 | 182 | { |
AntonLS | 11:d3aa5fca2330 | 183 | connected = true; |
AntonLS | 19:afcbb425b3cf | 184 | |
AntonLS | 19:afcbb425b3cf | 185 | // DEBUG( "Connected!\n\r" ); |
AntonLS | 11:d3aa5fca2330 | 186 | } |
AntonLS | 5:1b9734e68327 | 187 | |
AntonLS | 0:28ca4562fe1a | 188 | void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ) |
AntonLS | 0:28ca4562fe1a | 189 | { |
AntonLS | 11:d3aa5fca2330 | 190 | connected = false; |
AntonLS | 11:d3aa5fca2330 | 191 | |
AntonLS | 19:afcbb425b3cf | 192 | // DEBUG( "Disconnected!\n\r" ); |
AntonLS | 19:afcbb425b3cf | 193 | // DEBUG( "Restarting the advertising process\n\r" ); |
AntonLS | 0:28ca4562fe1a | 194 | ble.startAdvertising(); |
elmbed | 18:affef3a7db2a | 195 | ta.post_color(0); |
AntonLS | 0:28ca4562fe1a | 196 | } |
AntonLS | 0:28ca4562fe1a | 197 | |
AntonLS | 5:1b9734e68327 | 198 | bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead ) |
AntonLS | 5:1b9734e68327 | 199 | { |
AntonLS | 11:d3aa5fca2330 | 200 | ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead ); |
AntonLS | 5:1b9734e68327 | 201 | |
AntonLS | 5:1b9734e68327 | 202 | if( (err == BLE_ERROR_BUFFER_OVERFLOW) || |
AntonLS | 5:1b9734e68327 | 203 | (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) ) |
AntonLS | 7:205ef63d311a | 204 | { |
AntonLS | 19:afcbb425b3cf | 205 | // pcfc.printf( "\r\nBLE %d! ", err ); |
AntonLS | 7:205ef63d311a | 206 | |
AntonLS | 7:205ef63d311a | 207 | } else if ( err == BLE_STACK_BUSY ) |
AntonLS | 7:205ef63d311a | 208 | { |
AntonLS | 11:d3aa5fca2330 | 209 | // Common error when pumping data. |
AntonLS | 7:205ef63d311a | 210 | } |
AntonLS | 5:1b9734e68327 | 211 | |
AntonLS | 5:1b9734e68327 | 212 | return (err != BLE_ERROR_NONE); |
AntonLS | 5:1b9734e68327 | 213 | } |
AntonLS | 5:1b9734e68327 | 214 | |
AntonLS | 19:afcbb425b3cf | 215 | /// /* Writes the string given to the phone. |
AntonLS | 19:afcbb425b3cf | 216 | /// * |
AntonLS | 19:afcbb425b3cf | 217 | /// * @param *data - the string to send. |
AntonLS | 19:afcbb425b3cf | 218 | /// */ |
AntonLS | 19:afcbb425b3cf | 219 | /*** |
elmbed | 18:affef3a7db2a | 220 | static void writeToPhoneImpl(char *data) |
elmbed | 17:d8b901d791fd | 221 | { |
elmbed | 17:d8b901d791fd | 222 | if (phoneP != NULL) |
elmbed | 17:d8b901d791fd | 223 | { |
elmbed | 18:affef3a7db2a | 224 | for (int i = 0; i < strlen(data); ++i) |
elmbed | 18:affef3a7db2a | 225 | { |
elmbed | 18:affef3a7db2a | 226 | phoneP->putchar(data[i]); |
elmbed | 17:d8b901d791fd | 227 | |
elmbed | 18:affef3a7db2a | 228 | // If we don't call maybeHandleWrite all hell breaks loose and |
elmbed | 18:affef3a7db2a | 229 | // the app crashes. :( |
elmbed | 18:affef3a7db2a | 230 | if (i != 0 && i % 10 == 0) |
elmbed | 18:affef3a7db2a | 231 | { |
elmbed | 18:affef3a7db2a | 232 | int counter = 0; |
elmbed | 18:affef3a7db2a | 233 | while(phoneP->maybeHandleWrite() == 0 && ++counter < 20) |
elmbed | 18:affef3a7db2a | 234 | { |
elmbed | 18:affef3a7db2a | 235 | wait_us(1); |
elmbed | 18:affef3a7db2a | 236 | } |
elmbed | 18:affef3a7db2a | 237 | } |
elmbed | 18:affef3a7db2a | 238 | } |
elmbed | 18:affef3a7db2a | 239 | |
elmbed | 18:affef3a7db2a | 240 | phoneP->maybeHandleWrite(); |
elmbed | 17:d8b901d791fd | 241 | } |
elmbed | 17:d8b901d791fd | 242 | } |
AntonLS | 19:afcbb425b3cf | 243 | ***/ |
elmbed | 17:d8b901d791fd | 244 | |
AntonLS | 19:afcbb425b3cf | 245 | /// static char wtp_buff[150] = {0}; |
elmbed | 18:affef3a7db2a | 246 | extern "C" void writeToPhone(char *format, ...) |
elmbed | 18:affef3a7db2a | 247 | { |
elmbed | 18:affef3a7db2a | 248 | va_list arg; |
elmbed | 18:affef3a7db2a | 249 | va_start(arg, format ); |
elmbed | 18:affef3a7db2a | 250 | |
AntonLS | 19:afcbb425b3cf | 251 | /// vsnprintf(wtp_buff, sizeof(wtp_buff), format, arg); |
AntonLS | 19:afcbb425b3cf | 252 | /**/phoneP->vprintf( format, arg ); |
AntonLS | 19:afcbb425b3cf | 253 | |
AntonLS | 19:afcbb425b3cf | 254 | /// writeToPhoneImpl(wtp_buff); |
AntonLS | 19:afcbb425b3cf | 255 | |
AntonLS | 19:afcbb425b3cf | 256 | /**/// Also write same to serial port... TODO |
AntonLS | 19:afcbb425b3cf | 257 | /**/ // pcfc.vprintf( format, arg ); |
AntonLS | 19:afcbb425b3cf | 258 | |
elmbed | 18:affef3a7db2a | 259 | va_end(arg); |
elmbed | 18:affef3a7db2a | 260 | } |
elmbed | 18:affef3a7db2a | 261 | |
AntonLS | 19:afcbb425b3cf | 262 | /**/ // Byte bits to ASCII - Use until we convert protocol to ASCII-hex. |
AntonLS | 19:afcbb425b3cf | 263 | extern "C" void writeBitsToPhone( uint8_t byte, uint8_t minbits ) |
AntonLS | 19:afcbb425b3cf | 264 | { |
AntonLS | 19:afcbb425b3cf | 265 | static char ascbits[9] = { 0 }; |
AntonLS | 19:afcbb425b3cf | 266 | int pos = 0; |
AntonLS | 19:afcbb425b3cf | 267 | if( 0 == minbits ) minbits = 1; |
AntonLS | 19:afcbb425b3cf | 268 | |
AntonLS | 19:afcbb425b3cf | 269 | uint16_t ibyt = byte & 0xFF; |
AntonLS | 19:afcbb425b3cf | 270 | bool leading0 = true; |
AntonLS | 19:afcbb425b3cf | 271 | |
AntonLS | 19:afcbb425b3cf | 272 | for( short b=7; b >= 0; b-- ) |
AntonLS | 19:afcbb425b3cf | 273 | { |
AntonLS | 19:afcbb425b3cf | 274 | ibyt<<=1; |
AntonLS | 19:afcbb425b3cf | 275 | if( ibyt & 0x100 ) |
AntonLS | 19:afcbb425b3cf | 276 | { |
AntonLS | 19:afcbb425b3cf | 277 | leading0 = false; |
AntonLS | 19:afcbb425b3cf | 278 | ascbits[pos++] = '1'; |
AntonLS | 19:afcbb425b3cf | 279 | |
AntonLS | 19:afcbb425b3cf | 280 | } else if( !leading0 || (b < minbits) ) |
AntonLS | 19:afcbb425b3cf | 281 | { |
AntonLS | 19:afcbb425b3cf | 282 | ascbits[pos++] = '0'; |
AntonLS | 19:afcbb425b3cf | 283 | } |
AntonLS | 19:afcbb425b3cf | 284 | } |
AntonLS | 19:afcbb425b3cf | 285 | ascbits[pos] = '\0'; |
AntonLS | 19:afcbb425b3cf | 286 | |
AntonLS | 19:afcbb425b3cf | 287 | writeToPhone( "%s", ascbits ); |
AntonLS | 19:afcbb425b3cf | 288 | } |
AntonLS | 19:afcbb425b3cf | 289 | |
AntonLS | 0:28ca4562fe1a | 290 | void onDataWritten( const GattCharacteristicWriteCBParams *params ) |
AntonLS | 0:28ca4562fe1a | 291 | { |
AntonLS | 11:d3aa5fca2330 | 292 | if( phoneP != NULL ) |
AntonLS | 0:28ca4562fe1a | 293 | { |
AntonLS | 11:d3aa5fca2330 | 294 | uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload |
elmbed | 18:affef3a7db2a | 295 | |
AntonLS | 11:d3aa5fca2330 | 296 | if( 0 != bytesRead ) |
AntonLS | 11:d3aa5fca2330 | 297 | { |
AntonLS | 19:afcbb425b3cf | 298 | /// memcpy(phoneToDev+phoneToDevPos, txPayload, bytesRead); |
AntonLS | 19:afcbb425b3cf | 299 | /// phoneToDevPos += bytesRead; |
AntonLS | 19:afcbb425b3cf | 300 | |
AntonLS | 19:afcbb425b3cf | 301 | /* |
AntonLS | 19:afcbb425b3cf | 302 | DEBUG( "received %u bytes\n\r", bytesRead ); |
AntonLS | 19:afcbb425b3cf | 303 | |
AntonLS | 19:afcbb425b3cf | 304 | // Also write to serial port... |
AntonLS | 19:afcbb425b3cf | 305 | // pcfc.printf( "From app: " ); |
AntonLS | 19:afcbb425b3cf | 306 | pcfc.write( (char *)txPayload, bytesRead ); |
AntonLS | 19:afcbb425b3cf | 307 | // pcfc.printf( "\r\n" ); |
AntonLS | 19:afcbb425b3cf | 308 | */ |
AntonLS | 19:afcbb425b3cf | 309 | |
AntonLS | 11:d3aa5fca2330 | 310 | return; |
AntonLS | 11:d3aa5fca2330 | 311 | } |
AntonLS | 0:28ca4562fe1a | 312 | } |
AntonLS | 0:28ca4562fe1a | 313 | } |
AntonLS | 0:28ca4562fe1a | 314 | |
AntonLS | 0:28ca4562fe1a | 315 | void onDataSent( unsigned count ) |
AntonLS | 0:28ca4562fe1a | 316 | { |
AntonLS | 0:28ca4562fe1a | 317 | } |
AntonLS | 0:28ca4562fe1a | 318 | |
AntonLS | 11:d3aa5fca2330 | 319 | void toPhoneChk( void ) |
AntonLS | 0:28ca4562fe1a | 320 | { |
AntonLS | 19:afcbb425b3cf | 321 | if( phoneP != NULL ) |
AntonLS | 19:afcbb425b3cf | 322 | { |
AntonLS | 19:afcbb425b3cf | 323 | /* |
AntonLS | 19:afcbb425b3cf | 324 | char ch; |
AntonLS | 19:afcbb425b3cf | 325 | // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars. |
AntonLS | 19:afcbb425b3cf | 326 | for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ ) |
AntonLS | 19:afcbb425b3cf | 327 | { |
AntonLS | 19:afcbb425b3cf | 328 | /// For from-serial straight to-phone. |
AntonLS | 19:afcbb425b3cf | 329 | /// if( 0 > phoneP->putchar( ch ) ) |
AntonLS | 19:afcbb425b3cf | 330 | /// { |
AntonLS | 19:afcbb425b3cf | 331 | /// pcfc.printf( " * " ); |
AntonLS | 19:afcbb425b3cf | 332 | /// break; |
AntonLS | 19:afcbb425b3cf | 333 | /// } |
AntonLS | 3:388e441be8df | 334 | |
AntonLS | 19:afcbb425b3cf | 335 | // For from-serial inject-to-dev as-if from phone. |
AntonLS | 19:afcbb425b3cf | 336 | phoneP->injectHandleRead( &ch, 1 ); |
AntonLS | 19:afcbb425b3cf | 337 | |
AntonLS | 19:afcbb425b3cf | 338 | if( (cnt >= 20) && ('\n' == ch) ) break; |
AntonLS | 19:afcbb425b3cf | 339 | } |
AntonLS | 19:afcbb425b3cf | 340 | */ |
AntonLS | 19:afcbb425b3cf | 341 | // Write to outgoing characteristic if anything is pending. |
AntonLS | 19:afcbb425b3cf | 342 | if( 0 != phoneP->maybeHandleWrite() ) |
AntonLS | 19:afcbb425b3cf | 343 | { |
AntonLS | 19:afcbb425b3cf | 344 | // pcfc.printf( "ToPhoneHandler \r\n" ); |
AntonLS | 19:afcbb425b3cf | 345 | } |
AntonLS | 19:afcbb425b3cf | 346 | } |
AntonLS | 0:28ca4562fe1a | 347 | } |
AntonLS | 19:afcbb425b3cf | 348 | /* |
AntonLS | 13:28332f65d14b | 349 | static uint32_t boot_cnt_data = 0; |
AntonLS | 13:28332f65d14b | 350 | static uint16_t data_block = 9*20 +75; // Last block -- Use for app-start count. |
AntonLS | 13:28332f65d14b | 351 | static uint16_t data_size = 4; |
AntonLS | 13:28332f65d14b | 352 | static uint16_t data_offset = 0; // 12 for 16-byte blocks. |
AntonLS | 13:28332f65d14b | 353 | static bool data_loaded = false; |
AntonLS | 13:28332f65d14b | 354 | static bool data_cleared = false; |
AntonLS | 13:28332f65d14b | 355 | static bool data_stored = false; |
AntonLS | 13:28332f65d14b | 356 | static pstorage_handle_t pstorage_id; |
AntonLS | 19:afcbb425b3cf | 357 | |
AntonLS | 13:28332f65d14b | 358 | static uint32_t pstorage_read_test() |
AntonLS | 13:28332f65d14b | 359 | { |
AntonLS | 13:28332f65d14b | 360 | uint32_t err_code; |
AntonLS | 13:28332f65d14b | 361 | |
AntonLS | 13:28332f65d14b | 362 | pstorage_handle_t p_block_id; |
AntonLS | 13:28332f65d14b | 363 | |
AntonLS | 13:28332f65d14b | 364 | do |
AntonLS | 13:28332f65d14b | 365 | { |
AntonLS | 13:28332f65d14b | 366 | err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); |
AntonLS | 13:28332f65d14b | 367 | if( NRF_SUCCESS != err_code ) break; |
AntonLS | 13:28332f65d14b | 368 | |
AntonLS | 13:28332f65d14b | 369 | err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset ); |
AntonLS | 13:28332f65d14b | 370 | if( NRF_SUCCESS != err_code ) break; |
AntonLS | 13:28332f65d14b | 371 | |
AntonLS | 13:28332f65d14b | 372 | } while( 0 ); |
AntonLS | 13:28332f65d14b | 373 | |
AntonLS | 13:28332f65d14b | 374 | return err_code; |
AntonLS | 13:28332f65d14b | 375 | } |
AntonLS | 13:28332f65d14b | 376 | static uint32_t pstorage_clear_test() |
AntonLS | 13:28332f65d14b | 377 | { |
AntonLS | 13:28332f65d14b | 378 | uint32_t err_code; |
AntonLS | 13:28332f65d14b | 379 | |
AntonLS | 13:28332f65d14b | 380 | pstorage_handle_t p_block_id; |
AntonLS | 13:28332f65d14b | 381 | |
AntonLS | 13:28332f65d14b | 382 | do |
AntonLS | 13:28332f65d14b | 383 | { |
AntonLS | 13:28332f65d14b | 384 | err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); |
AntonLS | 13:28332f65d14b | 385 | if( NRF_SUCCESS != err_code ) break; |
AntonLS | 13:28332f65d14b | 386 | |
AntonLS | 13:28332f65d14b | 387 | err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE ); |
AntonLS | 13:28332f65d14b | 388 | if( NRF_SUCCESS != err_code ) break; |
AntonLS | 13:28332f65d14b | 389 | |
AntonLS | 13:28332f65d14b | 390 | } while( 0 ); |
AntonLS | 13:28332f65d14b | 391 | |
AntonLS | 13:28332f65d14b | 392 | return err_code; |
AntonLS | 13:28332f65d14b | 393 | } |
AntonLS | 13:28332f65d14b | 394 | static uint32_t pstorage_write_test() |
AntonLS | 13:28332f65d14b | 395 | { |
AntonLS | 13:28332f65d14b | 396 | uint32_t err_code; |
AntonLS | 13:28332f65d14b | 397 | |
AntonLS | 13:28332f65d14b | 398 | pstorage_handle_t p_block_id; |
AntonLS | 13:28332f65d14b | 399 | |
AntonLS | 13:28332f65d14b | 400 | do |
AntonLS | 13:28332f65d14b | 401 | { |
AntonLS | 13:28332f65d14b | 402 | err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); |
AntonLS | 13:28332f65d14b | 403 | if( NRF_SUCCESS != err_code ) break; |
AntonLS | 13:28332f65d14b | 404 | |
AntonLS | 13:28332f65d14b | 405 | err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset ); |
AntonLS | 13:28332f65d14b | 406 | |
AntonLS | 13:28332f65d14b | 407 | } while( 0 ); |
AntonLS | 13:28332f65d14b | 408 | |
AntonLS | 13:28332f65d14b | 409 | return err_code; |
AntonLS | 13:28332f65d14b | 410 | } |
AntonLS | 19:afcbb425b3cf | 411 | |
AntonLS | 13:28332f65d14b | 412 | static void pstorage_cb_handler( pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len ) |
AntonLS | 13:28332f65d14b | 413 | { |
AntonLS | 13:28332f65d14b | 414 | switch( op_code ) |
AntonLS | 13:28332f65d14b | 415 | { |
AntonLS | 13:28332f65d14b | 416 | case PSTORAGE_LOAD_OP_CODE: |
AntonLS | 13:28332f65d14b | 417 | if( NRF_SUCCESS == result ) |
AntonLS | 13:28332f65d14b | 418 | { |
AntonLS | 13:28332f65d14b | 419 | // Load operation successful. |
AntonLS | 13:28332f65d14b | 420 | data_loaded = true; // Flag to signal load is done. |
AntonLS | 13:28332f65d14b | 421 | |
AntonLS | 13:28332f65d14b | 422 | } else |
AntonLS | 13:28332f65d14b | 423 | { |
AntonLS | 13:28332f65d14b | 424 | // Load operation failed. |
elmbed | 18:affef3a7db2a | 425 | //pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result ); |
AntonLS | 13:28332f65d14b | 426 | } |
AntonLS | 13:28332f65d14b | 427 | break; |
AntonLS | 13:28332f65d14b | 428 | case PSTORAGE_UPDATE_OP_CODE: |
AntonLS | 13:28332f65d14b | 429 | case PSTORAGE_STORE_OP_CODE: |
AntonLS | 13:28332f65d14b | 430 | if( NRF_SUCCESS == result ) |
AntonLS | 13:28332f65d14b | 431 | { |
AntonLS | 13:28332f65d14b | 432 | // Store operation successful. |
AntonLS | 13:28332f65d14b | 433 | data_stored = true; // Flag to signal store is done. |
AntonLS | 13:28332f65d14b | 434 | |
AntonLS | 13:28332f65d14b | 435 | } else |
AntonLS | 13:28332f65d14b | 436 | { |
AntonLS | 13:28332f65d14b | 437 | // Store operation failed. |
elmbed | 18:affef3a7db2a | 438 | //pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result ); |
AntonLS | 13:28332f65d14b | 439 | } |
AntonLS | 13:28332f65d14b | 440 | // Source memory can now be reused or freed. |
AntonLS | 13:28332f65d14b | 441 | break; |
AntonLS | 13:28332f65d14b | 442 | case PSTORAGE_CLEAR_OP_CODE: |
AntonLS | 13:28332f65d14b | 443 | if( NRF_SUCCESS == result ) |
AntonLS | 13:28332f65d14b | 444 | { |
AntonLS | 13:28332f65d14b | 445 | // Clear operation successful. |
AntonLS | 13:28332f65d14b | 446 | data_cleared = true; // Flag to store to the same data area. |
AntonLS | 13:28332f65d14b | 447 | |
AntonLS | 13:28332f65d14b | 448 | } else |
AntonLS | 13:28332f65d14b | 449 | { |
AntonLS | 13:28332f65d14b | 450 | // Clear operation failed. |
elmbed | 18:affef3a7db2a | 451 | //pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result ); |
AntonLS | 13:28332f65d14b | 452 | } |
AntonLS | 13:28332f65d14b | 453 | break; |
AntonLS | 19:afcbb425b3cf | 454 | //default: |
AntonLS | 19:afcbb425b3cf | 455 | //pcfc.printf( "\r\nWarn: pstorage unknown op: %x error: %x\r\n", op_code, result ); |
AntonLS | 13:28332f65d14b | 456 | } |
AntonLS | 13:28332f65d14b | 457 | } |
AntonLS | 19:afcbb425b3cf | 458 | |
AntonLS | 13:28332f65d14b | 459 | static uint32_t pstorage_setup() |
AntonLS | 13:28332f65d14b | 460 | { |
AntonLS | 13:28332f65d14b | 461 | pstorage_module_param_t pstorage_param; |
AntonLS | 13:28332f65d14b | 462 | |
AntonLS | 13:28332f65d14b | 463 | // Setup pstorage with 9*20 +76 blocks of 4 bytes each. (or 9*20/4 + 19 for 16 byte blocks) |
AntonLS | 13:28332f65d14b | 464 | pstorage_param.block_size = 4; // Recommended to be >= 4 bytes. |
AntonLS | 13:28332f65d14b | 465 | pstorage_param.block_count = 9*20 +76; // 9 Sequences x 20 Stations + 76 blocks to fill out to 1k. |
AntonLS | 13:28332f65d14b | 466 | pstorage_param.cb = pstorage_cb_handler; |
AntonLS | 13:28332f65d14b | 467 | |
AntonLS | 13:28332f65d14b | 468 | return pstorage_register( &pstorage_param, &pstorage_id ); |
AntonLS | 13:28332f65d14b | 469 | } |
AntonLS | 19:afcbb425b3cf | 470 | */ |
AntonLS | 13:28332f65d14b | 471 | |
AntonLS | 13:28332f65d14b | 472 | void periodicCallback( void ) |
AntonLS | 13:28332f65d14b | 473 | { |
AntonLS | 19:afcbb425b3cf | 474 | static int foo; |
AntonLS | 19:afcbb425b3cf | 475 | foo++; |
elmbed | 23:26f27c462976 | 476 | //#if BLENANO |
elmbed | 18:affef3a7db2a | 477 | led0 = !led0; |
elmbed | 23:26f27c462976 | 478 | //#endif |
elmbed | 18:affef3a7db2a | 479 | led1 = !led1; |
AntonLS | 19:afcbb425b3cf | 480 | led2 = !led2; |
AntonLS | 19:afcbb425b3cf | 481 | // buzz = (foo & 2)>>1; |
AntonLS | 13:28332f65d14b | 482 | // rts = !rts; |
AntonLS | 13:28332f65d14b | 483 | } |
AntonLS | 13:28332f65d14b | 484 | |
AntonLS | 12:6d313d575f84 | 485 | /* |
AntonLS | 12:6d313d575f84 | 486 | void led_thread( void const *args ) |
AntonLS | 12:6d313d575f84 | 487 | { |
AntonLS | 12:6d313d575f84 | 488 | while( true ) |
AntonLS | 12:6d313d575f84 | 489 | { |
AntonLS | 12:6d313d575f84 | 490 | led0 = !led0; |
AntonLS | 12:6d313d575f84 | 491 | led1 = !led1; |
AntonLS | 12:6d313d575f84 | 492 | Thread::wait( 1000 ); |
AntonLS | 12:6d313d575f84 | 493 | } |
AntonLS | 12:6d313d575f84 | 494 | } |
AntonLS | 12:6d313d575f84 | 495 | */ |
AntonLS | 12:6d313d575f84 | 496 | |
AntonLS | 13:28332f65d14b | 497 | |
AntonLS | 0:28ca4562fe1a | 498 | int main( void ) |
AntonLS | 0:28ca4562fe1a | 499 | { |
AntonLS | 0:28ca4562fe1a | 500 | Ticker ticker; |
AntonLS | 19:afcbb425b3cf | 501 | ticker.attach( periodicCallback, 0.5 /*** 0.1 ***/ /* 1 */ ); |
AntonLS | 19:afcbb425b3cf | 502 | |
AntonLS | 19:afcbb425b3cf | 503 | /* |
AntonLS | 19:afcbb425b3cf | 504 | // Thread thread( led_thread ); |
AntonLS | 19:afcbb425b3cf | 505 | */ |
AntonLS | 19:afcbb425b3cf | 506 | |
AntonLS | 19:afcbb425b3cf | 507 | /////pcfc.baud( 57600 ); |
AntonLS | 19:afcbb425b3cf | 508 | |
AntonLS | 19:afcbb425b3cf | 509 | /* |
AntonLS | 19:afcbb425b3cf | 510 | DEBUG( "Initialising the nRF51822\n\r" ); |
AntonLS | 19:afcbb425b3cf | 511 | */ |
AntonLS | 2:fe1566cdb6e7 | 512 | |
AntonLS | 0:28ca4562fe1a | 513 | ble.init(); |
elmbed | 18:affef3a7db2a | 514 | |
AntonLS | 11:d3aa5fca2330 | 515 | ble.onConnection( connectionCallback ); |
AntonLS | 0:28ca4562fe1a | 516 | ble.onDisconnection( disconnectionCallback ); |
AntonLS | 0:28ca4562fe1a | 517 | ble.onDataWritten( onDataWritten ); |
AntonLS | 0:28ca4562fe1a | 518 | ble.onDataSent( onDataSent ); |
AntonLS | 0:28ca4562fe1a | 519 | |
AntonLS | 0:28ca4562fe1a | 520 | /* setup advertising */ |
AntonLS | 12:6d313d575f84 | 521 | ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | |
AntonLS | 12:6d313d575f84 | 522 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); |
elmbed | 18:affef3a7db2a | 523 | |
AntonLS | 0:28ca4562fe1a | 524 | ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); |
AntonLS | 5:1b9734e68327 | 525 | |
AntonLS | 5:1b9734e68327 | 526 | // Get MAC addr so we can create a device name using it. |
AntonLS | 5:1b9734e68327 | 527 | ble.getAddress( pAdType, macAddr ); |
AntonLS | 5:1b9734e68327 | 528 | sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); |
AntonLS | 5:1b9734e68327 | 529 | |
AntonLS | 19:afcbb425b3cf | 530 | /////pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName ); |
AntonLS | 19:afcbb425b3cf | 531 | |
AntonLS | 19:afcbb425b3cf | 532 | /* |
AntonLS | 19:afcbb425b3cf | 533 | #if LOOPBACK_MODE |
AntonLS | 19:afcbb425b3cf | 534 | pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" ); |
AntonLS | 19:afcbb425b3cf | 535 | #endif |
AntonLS | 19:afcbb425b3cf | 536 | */ |
AntonLS | 19:afcbb425b3cf | 537 | |
AntonLS | 13:28332f65d14b | 538 | uint32_t p_count; |
AntonLS | 13:28332f65d14b | 539 | uint32_t pstorageErr = pstorage_init(); // This needs to be called, even though apparently called in bootloader--Check other stuff. |
AntonLS | 13:28332f65d14b | 540 | pstorage_access_status_get( &p_count ); |
AntonLS | 13:28332f65d14b | 541 | |
AntonLS | 19:afcbb425b3cf | 542 | /* |
AntonLS | 19:afcbb425b3cf | 543 | // pcfc.printf( "\r\nInitial pstorage count: %d\r\n", p_count ); |
AntonLS | 19:afcbb425b3cf | 544 | |
AntonLS | 19:afcbb425b3cf | 545 | if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); |
AntonLS | 19:afcbb425b3cf | 546 | else |
AntonLS | 19:afcbb425b3cf | 547 | { |
AntonLS | 19:afcbb425b3cf | 548 | pstorageErr = pstorage_setup(); |
AntonLS | 19:afcbb425b3cf | 549 | if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); |
AntonLS | 19:afcbb425b3cf | 550 | else |
AntonLS | 19:afcbb425b3cf | 551 | { |
AntonLS | 19:afcbb425b3cf | 552 | pstorageErr = pstorage_read_test(); |
AntonLS | 19:afcbb425b3cf | 553 | if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); |
AntonLS | 19:afcbb425b3cf | 554 | else |
AntonLS | 19:afcbb425b3cf | 555 | { |
AntonLS | 19:afcbb425b3cf | 556 | // In this test setup, we use the load callback to signal start to clear, |
AntonLS | 19:afcbb425b3cf | 557 | // then we use the clear callback to signal start to write. |
AntonLS | 19:afcbb425b3cf | 558 | } |
AntonLS | 19:afcbb425b3cf | 559 | } |
AntonLS | 19:afcbb425b3cf | 560 | } |
AntonLS | 19:afcbb425b3cf | 561 | */ |
AntonLS | 19:afcbb425b3cf | 562 | |
AntonLS | 0:28ca4562fe1a | 563 | ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, |
AntonLS | 0:28ca4562fe1a | 564 | (const uint8_t *)deviceName, strlen(deviceName) ); |
AntonLS | 19:afcbb425b3cf | 565 | |
AntonLS | 19:afcbb425b3cf | 566 | // Moved to scan response packet to give more room in AD packet... |
AntonLS | 19:afcbb425b3cf | 567 | // ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, |
AntonLS | 19:afcbb425b3cf | 568 | // (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) ); |
AntonLS | 0:28ca4562fe1a | 569 | |
AntonLS | 19:afcbb425b3cf | 570 | ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
AntonLS | 19:afcbb425b3cf | 571 | (uint8_t *)uuid16_list, sizeof( uuid16_list ) ); |
AntonLS | 19:afcbb425b3cf | 572 | ble.accumulateAdvertisingPayload( GapAdvertisingData::SERVICE_DATA, |
AntonLS | 19:afcbb425b3cf | 573 | (uint8_t *)batt_and_id, sizeof( batt_and_id ) ); // Unused batt lev + "TX" |
AntonLS | 19:afcbb425b3cf | 574 | |
AntonLS | 19:afcbb425b3cf | 575 | ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, |
AntonLS | 19:afcbb425b3cf | 576 | (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) ); |
AntonLS | 0:28ca4562fe1a | 577 | |
AntonLS | 11:d3aa5fca2330 | 578 | ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 200 ) ); |
AntonLS | 0:28ca4562fe1a | 579 | ble.startAdvertising(); |
AntonLS | 0:28ca4562fe1a | 580 | |
AntonLS | 0:28ca4562fe1a | 581 | DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" ); |
AntonLS | 19:afcbb425b3cf | 582 | BatteryService battService( ble ); |
AntonLS | 0:28ca4562fe1a | 583 | |
AntonLS | 19:afcbb425b3cf | 584 | /* Enable over-the-air firmware updates. Instantiating DFUService introduces a |
AntonLS | 0:28ca4562fe1a | 585 | * control characteristic which can be used to trigger the application to |
AntonLS | 0:28ca4562fe1a | 586 | * handover control to a resident bootloader. */ |
AntonLS | 0:28ca4562fe1a | 587 | DFUService dfu( ble ); |
AntonLS | 0:28ca4562fe1a | 588 | |
AntonLS | 19:afcbb425b3cf | 589 | UARTService uartService( ble ); |
AntonLS | 0:28ca4562fe1a | 590 | uartServicePtr = &uartService; |
AntonLS | 0:28ca4562fe1a | 591 | |
AntonLS | 19:afcbb425b3cf | 592 | PhoneAppIO *phone = new PhoneAppIO( ble, |
AntonLS | 19:afcbb425b3cf | 593 | uartServicePtr->getRXCharacteristicHandle(), |
AntonLS | 19:afcbb425b3cf | 594 | uartServicePtr->getTXCharacteristicHandle() ); |
AntonLS | 19:afcbb425b3cf | 595 | |
elmbed | 18:affef3a7db2a | 596 | phone->loopbackMode = LOOPBACK_MODE; |
elmbed | 18:affef3a7db2a | 597 | phoneP = phone; |
elmbed | 18:affef3a7db2a | 598 | |
AntonLS | 19:afcbb425b3cf | 599 | // DigitalOut *buzzPin = new DigitalOut(p20); |
AntonLS | 19:afcbb425b3cf | 600 | // *buzzPin = 1; |
elmbed | 17:d8b901d791fd | 601 | setup(); |
elmbed | 23:26f27c462976 | 602 | radio_init(); |
AntonLS | 11:d3aa5fca2330 | 603 | tmr.start(); |
elmbed | 18:affef3a7db2a | 604 | |
AntonLS | 11:d3aa5fca2330 | 605 | // Main Loop |
AntonLS | 19:afcbb425b3cf | 606 | while( true ) // for( uint32_t loop=1; ;loop++ ) |
AntonLS | 0:28ca4562fe1a | 607 | { |
AntonLS | 0:28ca4562fe1a | 608 | ble.waitForEvent(); |
AntonLS | 19:afcbb425b3cf | 609 | |
AntonLS | 19:afcbb425b3cf | 610 | /**/ toPhoneChk(); // Write any pending data to phone. |
AntonLS | 19:afcbb425b3cf | 611 | |
AntonLS | 19:afcbb425b3cf | 612 | /* |
AntonLS | 19:afcbb425b3cf | 613 | while( 0 <= phone.getchar() ); // Eat input. |
AntonLS | 19:afcbb425b3cf | 614 | |
AntonLS | 19:afcbb425b3cf | 615 | // if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() ); |
AntonLS | 19:afcbb425b3cf | 616 | |
AntonLS | 19:afcbb425b3cf | 617 | app_sched_execute(); // Attempt to get pstorage enqueued cmd callbacks. |
AntonLS | 19:afcbb425b3cf | 618 | */ |
AntonLS | 19:afcbb425b3cf | 619 | |
AntonLS | 19:afcbb425b3cf | 620 | /*** |
elmbed | 18:affef3a7db2a | 621 | if (phoneToDevPos > 0) |
elmbed | 18:affef3a7db2a | 622 | { |
elmbed | 18:affef3a7db2a | 623 | getRadioInput(phoneToDev, phoneToDevPos ); |
elmbed | 18:affef3a7db2a | 624 | phoneToDevPos = 0; |
elmbed | 18:affef3a7db2a | 625 | } |
AntonLS | 19:afcbb425b3cf | 626 | ***/ |
AntonLS | 19:afcbb425b3cf | 627 | |
AntonLS | 19:afcbb425b3cf | 628 | /**/ int bytes = MIN( PHTODEV_BUF_LEN, phoneP->readable() ); |
elmbed | 23:26f27c462976 | 629 | /**/ |
elmbed | 23:26f27c462976 | 630 | #ifdef MASTER |
elmbed | 23:26f27c462976 | 631 | getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) ); |
elmbed | 23:26f27c462976 | 632 | #endif |
elmbed | 17:d8b901d791fd | 633 | loop(); |
elmbed | 18:affef3a7db2a | 634 | //radio_loop(); |
AntonLS | 19:afcbb425b3cf | 635 | |
AntonLS | 19:afcbb425b3cf | 636 | /*** |
elmbed | 18:affef3a7db2a | 637 | if (connected) |
AntonLS | 13:28332f65d14b | 638 | { |
elmbed | 18:affef3a7db2a | 639 | phoneP->maybeHandleWrite(); |
elmbed | 18:affef3a7db2a | 640 | } |
AntonLS | 13:28332f65d14b | 641 | |
elmbed | 18:affef3a7db2a | 642 | while( 0 <= phone->getchar() ); // Eat input. |
AntonLS | 19:afcbb425b3cf | 643 | ***/ |
AntonLS | 0:28ca4562fe1a | 644 | } |
AntonLS | 0:28ca4562fe1a | 645 | } |
AntonLS | 0:28ca4562fe1a | 646 | |
AntonLS | 15:b86c4b798aa1 | 647 | /**@brief Function for error handling, which is called when an error has occurred. |
AntonLS | 15:b86c4b798aa1 | 648 | * |
AntonLS | 15:b86c4b798aa1 | 649 | * @warning This handler is an example only and does not fit a final product. You need to analyze |
AntonLS | 15:b86c4b798aa1 | 650 | * how your product is supposed to react in case of error. |
AntonLS | 15:b86c4b798aa1 | 651 | * |
AntonLS | 15:b86c4b798aa1 | 652 | * @param[in] error_code Error code supplied to the handler. |
AntonLS | 15:b86c4b798aa1 | 653 | * @param[in] line_num Line number where the handler is called. |
AntonLS | 15:b86c4b798aa1 | 654 | * @param[in] p_file_name Pointer to the file name. |
AntonLS | 15:b86c4b798aa1 | 655 | */ |
AntonLS | 15:b86c4b798aa1 | 656 | void $Sub$$app_error_handler( uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name ) |
AntonLS | 15:b86c4b798aa1 | 657 | { |
AntonLS | 15:b86c4b798aa1 | 658 | // nrf_gpio_pin_set( ASSERT_LED_PIN_NO ); |
elmbed | 18:affef3a7db2a | 659 | //led1 = 1; |
AntonLS | 15:b86c4b798aa1 | 660 | |
AntonLS | 15:b86c4b798aa1 | 661 | // This call can be used for debug purposes during application development. |
AntonLS | 15:b86c4b798aa1 | 662 | // @note CAUTION: Activating this code will write the stack to flash on an error. |
AntonLS | 15:b86c4b798aa1 | 663 | // This function should NOT be used in a final product. |
AntonLS | 15:b86c4b798aa1 | 664 | // It is intended STRICTLY for development/debugging purposes. |
AntonLS | 15:b86c4b798aa1 | 665 | // The flash write will happen EVEN if the radio is active, thus interrupting |
AntonLS | 15:b86c4b798aa1 | 666 | // any communication. |
AntonLS | 15:b86c4b798aa1 | 667 | // Use with care. Uncomment the line below to use. |
AntonLS | 15:b86c4b798aa1 | 668 | // ble_debug_assert_handler(error_code, line_num, p_file_name); |
AntonLS | 15:b86c4b798aa1 | 669 | |
AntonLS | 15:b86c4b798aa1 | 670 | // On assert, the system can only recover with a reset. |
AntonLS | 15:b86c4b798aa1 | 671 | NVIC_SystemReset(); |
AntonLS | 15:b86c4b798aa1 | 672 | } |
AntonLS | 15:b86c4b798aa1 | 673 | |
AntonLS | 0:28ca4562fe1a | 674 | /* EOF */ |