football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Mon Dec 14 14:32:59 2015 +0000
Revision:
24:ff54dec20f55
Parent:
22:f58d4b441aba
Parent:
23:26f27c462976
Child:
28:8e74ddc4f70f
merge with master

Who changed what in which revision?

UserRevisionLine numberNew 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 21:32f022efcc09 104 #if BLENANO
AntonLS 21:32f022efcc09 105 #define ADC_IN_BATT P0_6
AntonLS 22:f58d4b441aba 106 #define CHARGING_IN P0_29
AntonLS 21:32f022efcc09 107 #else
AntonLS 21:32f022efcc09 108 #define ADC_IN_BATT P0_4
AntonLS 22:f58d4b441aba 109 #define CHARGING_IN P0_2
AntonLS 21:32f022efcc09 110 #endif
AntonLS 21:32f022efcc09 111 AnalogIn batt( ADC_IN_BATT );
AntonLS 22:f58d4b441aba 112 DigitalIn notcharge( CHARGING_IN, PullUp );
AntonLS 21:32f022efcc09 113
AntonLS 12:6d313d575f84 114 // DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 19:afcbb425b3cf 115 DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO
AntonLS 12:6d313d575f84 116
AntonLS 12:6d313d575f84 117 // Check if we should swap serial Rx/Tx for early rev of "Little Brain"
AntonLS 19:afcbb425b3cf 118 DigitalIn trSwp( P0_30, PullUp );
AntonLS 12:6d313d575f84 119
AntonLS 12:6d313d575f84 120 // Wait to settle.
AntonLS 12:6d313d575f84 121 int foo = (wait( 0.1 ), 0);
AntonLS 12:6d313d575f84 122
AntonLS 14:76fb883a3cb8 123 // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins.
AntonLS 20:d6c6099b60be 124 static DigitalOut led0( P0_19, 1 ); // TA New Baseboard LED High=On ("STATUS") (OK on Nano: LED Low=On)
AntonLS 19:afcbb425b3cf 125 static DigitalOut led1( P0_21, 0 ); // TA New Baseboard High=On (OK on Nano: NC)
AntonLS 19:afcbb425b3cf 126 DigitalOut led2( P0_22, 1 ); // TA New Baseboard High=On (OK on Nano: NC)
AntonLS 19:afcbb425b3cf 127
AntonLS 19:afcbb425b3cf 128 // DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On (OK on Nano: Alt RxD)
AntonLS 14:76fb883a3cb8 129 // DigitalOut led1( (trSwp ? P0_4 : P0_3), 0 ); // TA Baseboard High=On (And don't use P0_4 on Nano)
AntonLS 14:76fb883a3cb8 130
elmbed 23:26f27c462976 131 //DigitalOut buzz( P0_20, 1 ); // TA New Baseboard Low=On (OK on Nano: NC)
AntonLS 19:afcbb425b3cf 132
AntonLS 13:28332f65d14b 133 // App timer, app scheduler, and pstorage are already setup by bootloader.
AntonLS 13:28332f65d14b 134
elmbed 18:affef3a7db2a 135 static BLEDevice ble;
AntonLS 19:afcbb425b3cf 136
AntonLS 12:6d313d575f84 137
AntonLS 1:0ba687d4196f 138 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 139 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 11:d3aa5fca2330 140 // But a version of serial_init() erroneously enabled the uart before the setting of those,
AntonLS 11:d3aa5fca2330 141 // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 11:d3aa5fca2330 142 // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase)
AntonLS 7:205ef63d311a 143 // to not use serial_init() in serial_api.c, so flow control is setup correctly *
AntonLS 7:205ef63d311a 144 // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default *
AntonLS 15:b86c4b798aa1 145 // [* We can't change the uart interrupt vector, so we comment-out the handler in
AntonLS 15:b86c4b798aa1 146 // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work.] - No need now.
AntonLS 15:b86c4b798aa1 147 // * Now using $Sub$$UART0_IRQHandler to override UART0_IRQHandler without needing to use lib source.
AntonLS 15:b86c4b798aa1 148 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might have worked--No need.
AntonLS 15:b86c4b798aa1 149 //
AntonLS 6:ef758ac3c928 150 // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its
AntonLS 11:d3aa5fca2330 151 // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay.
AntonLS 1:0ba687d4196f 152 //
AntonLS 19:afcbb425b3cf 153 // mts::MTSSerialFlowControl pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
AntonLS 19:afcbb425b3cf 154 //mts::MTSSerial pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), 128, 64, RTS_PIN_NUMBER, NC ); // 256, 1280 // 256, 2560
AntonLS 0:28ca4562fe1a 155
AntonLS 0:28ca4562fe1a 156 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 157
AntonLS 0:28ca4562fe1a 158
AntonLS 5:1b9734e68327 159 char deviceName[6]; // "TAF00";
AntonLS 5:1b9734e68327 160 Gap::address_t macAddr;
AntonLS 5:1b9734e68327 161 Gap::addr_type_t *pAdType;
AntonLS 19:afcbb425b3cf 162 static const uint16_t uuid16_list[] = { GattService::UUID_DEVICE_INFORMATION_SERVICE,
AntonLS 19:afcbb425b3cf 163 GattService::UUID_BATTERY_SERVICE };
AntonLS 20:d6c6099b60be 164 static uint8_t batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff,
AntonLS 19:afcbb425b3cf 165 GattService::UUID_BATTERY_SERVICE >> 8,
AntonLS 22:f58d4b441aba 166 99, // Batt level
AntonLS 19:afcbb425b3cf 167 'T', 'X' }; // Custom ID trick
AntonLS 19:afcbb425b3cf 168
AntonLS 20:d6c6099b60be 169 UARTService *uartServicePtr;
AntonLS 20:d6c6099b60be 170 PhoneAppIO *phoneP;
AntonLS 20:d6c6099b60be 171 BatteryService *battServiceP;
AntonLS 5:1b9734e68327 172
elmbed 18:affef3a7db2a 173 // Buffer for holding data from the phone
elmbed 18:affef3a7db2a 174 // to the device
AntonLS 21:32f022efcc09 175 #define PHTODEV_BUF_LEN 30 /**/
AntonLS 19:afcbb425b3cf 176 /// static char phoneToDev[200] = {0};
AntonLS 19:afcbb425b3cf 177 static char phoneToDev[PHTODEV_BUF_LEN] = {0};
AntonLS 0:28ca4562fe1a 178
AntonLS 19:afcbb425b3cf 179 /// // Current position in the buffer
AntonLS 19:afcbb425b3cf 180 /// static int phoneToDevPos = 0;
AntonLS 0:28ca4562fe1a 181
elmbed 18:affef3a7db2a 182 extern TA ta;
elmbed 18:affef3a7db2a 183
elmbed 18:affef3a7db2a 184 extern void radio_init();
elmbed 18:affef3a7db2a 185 extern void radio_loop();
elmbed 18:affef3a7db2a 186
AntonLS 22:f58d4b441aba 187 void setAdvData()
AntonLS 22:f58d4b441aba 188 {
AntonLS 22:f58d4b441aba 189 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED |
AntonLS 22:f58d4b441aba 190 GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
AntonLS 22:f58d4b441aba 191 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 22:f58d4b441aba 192
AntonLS 22:f58d4b441aba 193 // Get MAC addr so we can create a device name using it.
AntonLS 22:f58d4b441aba 194 ble.getAddress( pAdType, macAddr );
AntonLS 22:f58d4b441aba 195 sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
AntonLS 22:f58d4b441aba 196
AntonLS 22:f58d4b441aba 197 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 22:f58d4b441aba 198 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 22:f58d4b441aba 199
AntonLS 22:f58d4b441aba 200 // Moved to scan response packet to give more room in AD packet...
AntonLS 22:f58d4b441aba 201 // ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 202 // (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 22:f58d4b441aba 203
AntonLS 22:f58d4b441aba 204 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 205 (uint8_t *)uuid16_list, sizeof( uuid16_list ) );
AntonLS 22:f58d4b441aba 206 ble.accumulateAdvertisingPayload( GapAdvertisingData::SERVICE_DATA,
AntonLS 22:f58d4b441aba 207 (uint8_t *)batt_and_id, sizeof( batt_and_id ) ); // Unused batt lev + "TX"
AntonLS 22:f58d4b441aba 208
AntonLS 22:f58d4b441aba 209 ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 210 (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) );
AntonLS 22:f58d4b441aba 211 }
AntonLS 22:f58d4b441aba 212
AntonLS 22:f58d4b441aba 213 void updateBatt( uint8_t pct )
AntonLS 22:f58d4b441aba 214 {
AntonLS 22:f58d4b441aba 215 static uint8_t prev;
AntonLS 22:f58d4b441aba 216 static bool wasCharging;
AntonLS 22:f58d4b441aba 217
AntonLS 22:f58d4b441aba 218 if( notcharge )
AntonLS 22:f58d4b441aba 219 {
AntonLS 22:f58d4b441aba 220 if( pct > 100 ) pct = 100;
AntonLS 22:f58d4b441aba 221
AntonLS 22:f58d4b441aba 222 // Use if we update quite often.
AntonLS 22:f58d4b441aba 223 // if( !wasCharging && (abs( (int)prev -(int)pct ) < 3) ) return; // Don't register change of less than 3%.
AntonLS 22:f58d4b441aba 224
AntonLS 22:f58d4b441aba 225 prev = pct;
AntonLS 22:f58d4b441aba 226 wasCharging = false;
AntonLS 22:f58d4b441aba 227
AntonLS 22:f58d4b441aba 228 } else
AntonLS 22:f58d4b441aba 229 {
AntonLS 22:f58d4b441aba 230 pct = 101; // Show 101% if charging.
AntonLS 22:f58d4b441aba 231 wasCharging = true; // Force show when done charging.
AntonLS 22:f58d4b441aba 232 }
AntonLS 22:f58d4b441aba 233
AntonLS 22:f58d4b441aba 234 batt_and_id[2] = pct;
AntonLS 22:f58d4b441aba 235 ble.clearAdvertisingPayload();
AntonLS 22:f58d4b441aba 236 setAdvData();
AntonLS 22:f58d4b441aba 237 ble.setAdvertisingPayload();
AntonLS 22:f58d4b441aba 238
AntonLS 22:f58d4b441aba 239 if( NULL != battServiceP ) battServiceP->updateBatteryLevel( pct );
AntonLS 22:f58d4b441aba 240 }
AntonLS 22:f58d4b441aba 241
AntonLS 22:f58d4b441aba 242 #define BAT_TOP 793 // 3250 // 52000
AntonLS 22:f58d4b441aba 243 #define BAT_BOT 684 // 2800 // 44800
AntonLS 22:f58d4b441aba 244 uint8_t getBattLevel()
AntonLS 22:f58d4b441aba 245 {
AntonLS 22:f58d4b441aba 246 int battI = (int)(batt * 1000.f);
AntonLS 22:f58d4b441aba 247 // Normalize to usable range...
AntonLS 22:f58d4b441aba 248 // Note: These levels should also give 0% if powered by programmer.
AntonLS 22:f58d4b441aba 249 if( battI > BAT_TOP ) battI = BAT_TOP; // Fully charged reading ~3.85V
AntonLS 22:f58d4b441aba 250 if( battI < BAT_BOT ) battI = BAT_BOT; // Device failing reading ~3.32V
AntonLS 22:f58d4b441aba 251 battI-= BAT_BOT;
AntonLS 22:f58d4b441aba 252 return (uint8_t)(battI*100 / (BAT_TOP-BAT_BOT));
AntonLS 22:f58d4b441aba 253 }
AntonLS 22:f58d4b441aba 254
elmbed 18:affef3a7db2a 255 // True when connected to a phone
AntonLS 11:d3aa5fca2330 256 bool connected = false;
AntonLS 5:1b9734e68327 257
AntonLS 11:d3aa5fca2330 258 void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType,
AntonLS 11:d3aa5fca2330 259 const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams )
AntonLS 11:d3aa5fca2330 260 {
AntonLS 11:d3aa5fca2330 261 connected = true;
AntonLS 19:afcbb425b3cf 262
AntonLS 19:afcbb425b3cf 263 // DEBUG( "Connected!\n\r" );
AntonLS 11:d3aa5fca2330 264 }
AntonLS 5:1b9734e68327 265
AntonLS 0:28ca4562fe1a 266 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 267 {
AntonLS 11:d3aa5fca2330 268 connected = false;
AntonLS 11:d3aa5fca2330 269
AntonLS 22:f58d4b441aba 270 updateBatt( getBattLevel() );
AntonLS 22:f58d4b441aba 271
AntonLS 19:afcbb425b3cf 272 // DEBUG( "Disconnected!\n\r" );
AntonLS 19:afcbb425b3cf 273 // DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 274 ble.startAdvertising();
elmbed 18:affef3a7db2a 275 ta.post_color(0);
AntonLS 0:28ca4562fe1a 276 }
AntonLS 0:28ca4562fe1a 277
AntonLS 5:1b9734e68327 278 bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
AntonLS 5:1b9734e68327 279 {
AntonLS 11:d3aa5fca2330 280 ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead );
AntonLS 5:1b9734e68327 281
AntonLS 5:1b9734e68327 282 if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
AntonLS 5:1b9734e68327 283 (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
AntonLS 7:205ef63d311a 284 {
AntonLS 19:afcbb425b3cf 285 // pcfc.printf( "\r\nBLE %d! ", err );
AntonLS 7:205ef63d311a 286
AntonLS 7:205ef63d311a 287 } else if ( err == BLE_STACK_BUSY )
AntonLS 7:205ef63d311a 288 {
AntonLS 11:d3aa5fca2330 289 // Common error when pumping data.
AntonLS 7:205ef63d311a 290 }
AntonLS 5:1b9734e68327 291
AntonLS 5:1b9734e68327 292 return (err != BLE_ERROR_NONE);
AntonLS 5:1b9734e68327 293 }
AntonLS 5:1b9734e68327 294
AntonLS 19:afcbb425b3cf 295 /// /* Writes the string given to the phone.
AntonLS 19:afcbb425b3cf 296 /// *
AntonLS 19:afcbb425b3cf 297 /// * @param *data - the string to send.
AntonLS 19:afcbb425b3cf 298 /// */
AntonLS 19:afcbb425b3cf 299 /***
elmbed 18:affef3a7db2a 300 static void writeToPhoneImpl(char *data)
elmbed 17:d8b901d791fd 301 {
elmbed 17:d8b901d791fd 302 if (phoneP != NULL)
elmbed 17:d8b901d791fd 303 {
elmbed 18:affef3a7db2a 304 for (int i = 0; i < strlen(data); ++i)
elmbed 18:affef3a7db2a 305 {
elmbed 18:affef3a7db2a 306 phoneP->putchar(data[i]);
elmbed 17:d8b901d791fd 307
elmbed 18:affef3a7db2a 308 // If we don't call maybeHandleWrite all hell breaks loose and
elmbed 18:affef3a7db2a 309 // the app crashes. :(
elmbed 18:affef3a7db2a 310 if (i != 0 && i % 10 == 0)
elmbed 18:affef3a7db2a 311 {
elmbed 18:affef3a7db2a 312 int counter = 0;
elmbed 18:affef3a7db2a 313 while(phoneP->maybeHandleWrite() == 0 && ++counter < 20)
elmbed 18:affef3a7db2a 314 {
elmbed 18:affef3a7db2a 315 wait_us(1);
elmbed 18:affef3a7db2a 316 }
elmbed 18:affef3a7db2a 317 }
elmbed 18:affef3a7db2a 318 }
elmbed 18:affef3a7db2a 319
elmbed 18:affef3a7db2a 320 phoneP->maybeHandleWrite();
elmbed 17:d8b901d791fd 321 }
elmbed 17:d8b901d791fd 322 }
AntonLS 19:afcbb425b3cf 323 ***/
elmbed 17:d8b901d791fd 324
AntonLS 19:afcbb425b3cf 325 /// static char wtp_buff[150] = {0};
elmbed 18:affef3a7db2a 326 extern "C" void writeToPhone(char *format, ...)
elmbed 18:affef3a7db2a 327 {
elmbed 18:affef3a7db2a 328 va_list arg;
elmbed 18:affef3a7db2a 329 va_start(arg, format );
elmbed 18:affef3a7db2a 330
AntonLS 19:afcbb425b3cf 331 /// vsnprintf(wtp_buff, sizeof(wtp_buff), format, arg);
AntonLS 19:afcbb425b3cf 332 /**/phoneP->vprintf( format, arg );
AntonLS 19:afcbb425b3cf 333
AntonLS 19:afcbb425b3cf 334 /// writeToPhoneImpl(wtp_buff);
AntonLS 19:afcbb425b3cf 335
AntonLS 19:afcbb425b3cf 336 /**/// Also write same to serial port... TODO
AntonLS 19:afcbb425b3cf 337 /**/ // pcfc.vprintf( format, arg );
AntonLS 19:afcbb425b3cf 338
elmbed 18:affef3a7db2a 339 va_end(arg);
elmbed 18:affef3a7db2a 340 }
elmbed 18:affef3a7db2a 341
AntonLS 19:afcbb425b3cf 342 /**/ // Byte bits to ASCII - Use until we convert protocol to ASCII-hex.
AntonLS 19:afcbb425b3cf 343 extern "C" void writeBitsToPhone( uint8_t byte, uint8_t minbits )
AntonLS 19:afcbb425b3cf 344 {
AntonLS 19:afcbb425b3cf 345 static char ascbits[9] = { 0 };
AntonLS 19:afcbb425b3cf 346 int pos = 0;
AntonLS 19:afcbb425b3cf 347 if( 0 == minbits ) minbits = 1;
AntonLS 19:afcbb425b3cf 348
AntonLS 19:afcbb425b3cf 349 uint16_t ibyt = byte & 0xFF;
AntonLS 19:afcbb425b3cf 350 bool leading0 = true;
AntonLS 19:afcbb425b3cf 351
AntonLS 19:afcbb425b3cf 352 for( short b=7; b >= 0; b-- )
AntonLS 19:afcbb425b3cf 353 {
AntonLS 19:afcbb425b3cf 354 ibyt<<=1;
AntonLS 19:afcbb425b3cf 355 if( ibyt & 0x100 )
AntonLS 19:afcbb425b3cf 356 {
AntonLS 19:afcbb425b3cf 357 leading0 = false;
AntonLS 19:afcbb425b3cf 358 ascbits[pos++] = '1';
AntonLS 19:afcbb425b3cf 359
AntonLS 19:afcbb425b3cf 360 } else if( !leading0 || (b < minbits) )
AntonLS 19:afcbb425b3cf 361 {
AntonLS 19:afcbb425b3cf 362 ascbits[pos++] = '0';
AntonLS 19:afcbb425b3cf 363 }
AntonLS 19:afcbb425b3cf 364 }
AntonLS 19:afcbb425b3cf 365 ascbits[pos] = '\0';
AntonLS 19:afcbb425b3cf 366
AntonLS 19:afcbb425b3cf 367 writeToPhone( "%s", ascbits );
AntonLS 19:afcbb425b3cf 368 }
AntonLS 19:afcbb425b3cf 369
AntonLS 0:28ca4562fe1a 370 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 371 {
AntonLS 11:d3aa5fca2330 372 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 373 {
AntonLS 11:d3aa5fca2330 374 uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload
elmbed 18:affef3a7db2a 375
AntonLS 11:d3aa5fca2330 376 if( 0 != bytesRead )
AntonLS 11:d3aa5fca2330 377 {
AntonLS 19:afcbb425b3cf 378 /// memcpy(phoneToDev+phoneToDevPos, txPayload, bytesRead);
AntonLS 19:afcbb425b3cf 379 /// phoneToDevPos += bytesRead;
AntonLS 19:afcbb425b3cf 380
AntonLS 19:afcbb425b3cf 381 /*
AntonLS 19:afcbb425b3cf 382 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 19:afcbb425b3cf 383
AntonLS 19:afcbb425b3cf 384 // Also write to serial port...
AntonLS 19:afcbb425b3cf 385 // pcfc.printf( "From app: " );
AntonLS 19:afcbb425b3cf 386 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 19:afcbb425b3cf 387 // pcfc.printf( "\r\n" );
AntonLS 19:afcbb425b3cf 388 */
AntonLS 19:afcbb425b3cf 389
AntonLS 11:d3aa5fca2330 390 return;
AntonLS 11:d3aa5fca2330 391 }
AntonLS 0:28ca4562fe1a 392 }
AntonLS 0:28ca4562fe1a 393 }
AntonLS 0:28ca4562fe1a 394
AntonLS 0:28ca4562fe1a 395 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 396 {
AntonLS 0:28ca4562fe1a 397 }
AntonLS 0:28ca4562fe1a 398
AntonLS 11:d3aa5fca2330 399 void toPhoneChk( void )
AntonLS 0:28ca4562fe1a 400 {
AntonLS 19:afcbb425b3cf 401 if( phoneP != NULL )
AntonLS 19:afcbb425b3cf 402 {
AntonLS 19:afcbb425b3cf 403 /*
AntonLS 19:afcbb425b3cf 404 char ch;
AntonLS 19:afcbb425b3cf 405 // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars.
AntonLS 19:afcbb425b3cf 406 for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ )
AntonLS 19:afcbb425b3cf 407 {
AntonLS 19:afcbb425b3cf 408 /// For from-serial straight to-phone.
AntonLS 19:afcbb425b3cf 409 /// if( 0 > phoneP->putchar( ch ) )
AntonLS 19:afcbb425b3cf 410 /// {
AntonLS 19:afcbb425b3cf 411 /// pcfc.printf( " * " );
AntonLS 19:afcbb425b3cf 412 /// break;
AntonLS 19:afcbb425b3cf 413 /// }
AntonLS 3:388e441be8df 414
AntonLS 19:afcbb425b3cf 415 // For from-serial inject-to-dev as-if from phone.
AntonLS 19:afcbb425b3cf 416 phoneP->injectHandleRead( &ch, 1 );
AntonLS 19:afcbb425b3cf 417
AntonLS 19:afcbb425b3cf 418 if( (cnt >= 20) && ('\n' == ch) ) break;
AntonLS 19:afcbb425b3cf 419 }
AntonLS 19:afcbb425b3cf 420 */
AntonLS 19:afcbb425b3cf 421 // Write to outgoing characteristic if anything is pending.
AntonLS 19:afcbb425b3cf 422 if( 0 != phoneP->maybeHandleWrite() )
AntonLS 19:afcbb425b3cf 423 {
AntonLS 19:afcbb425b3cf 424 // pcfc.printf( "ToPhoneHandler \r\n" );
AntonLS 19:afcbb425b3cf 425 }
AntonLS 19:afcbb425b3cf 426 }
AntonLS 0:28ca4562fe1a 427 }
AntonLS 19:afcbb425b3cf 428 /*
AntonLS 13:28332f65d14b 429 static uint32_t boot_cnt_data = 0;
AntonLS 13:28332f65d14b 430 static uint16_t data_block = 9*20 +75; // Last block -- Use for app-start count.
AntonLS 13:28332f65d14b 431 static uint16_t data_size = 4;
AntonLS 13:28332f65d14b 432 static uint16_t data_offset = 0; // 12 for 16-byte blocks.
AntonLS 13:28332f65d14b 433 static bool data_loaded = false;
AntonLS 13:28332f65d14b 434 static bool data_cleared = false;
AntonLS 13:28332f65d14b 435 static bool data_stored = false;
AntonLS 13:28332f65d14b 436 static pstorage_handle_t pstorage_id;
AntonLS 19:afcbb425b3cf 437
AntonLS 13:28332f65d14b 438 static uint32_t pstorage_read_test()
AntonLS 13:28332f65d14b 439 {
AntonLS 13:28332f65d14b 440 uint32_t err_code;
AntonLS 13:28332f65d14b 441
AntonLS 13:28332f65d14b 442 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 443
AntonLS 13:28332f65d14b 444 do
AntonLS 13:28332f65d14b 445 {
AntonLS 13:28332f65d14b 446 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 447 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 448
AntonLS 13:28332f65d14b 449 err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset );
AntonLS 13:28332f65d14b 450 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 451
AntonLS 13:28332f65d14b 452 } while( 0 );
AntonLS 13:28332f65d14b 453
AntonLS 13:28332f65d14b 454 return err_code;
AntonLS 13:28332f65d14b 455 }
AntonLS 13:28332f65d14b 456 static uint32_t pstorage_clear_test()
AntonLS 13:28332f65d14b 457 {
AntonLS 13:28332f65d14b 458 uint32_t err_code;
AntonLS 13:28332f65d14b 459
AntonLS 13:28332f65d14b 460 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 461
AntonLS 13:28332f65d14b 462 do
AntonLS 13:28332f65d14b 463 {
AntonLS 13:28332f65d14b 464 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 465 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 466
AntonLS 13:28332f65d14b 467 err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE );
AntonLS 13:28332f65d14b 468 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 469
AntonLS 13:28332f65d14b 470 } while( 0 );
AntonLS 13:28332f65d14b 471
AntonLS 13:28332f65d14b 472 return err_code;
AntonLS 13:28332f65d14b 473 }
AntonLS 13:28332f65d14b 474 static uint32_t pstorage_write_test()
AntonLS 13:28332f65d14b 475 {
AntonLS 13:28332f65d14b 476 uint32_t err_code;
AntonLS 13:28332f65d14b 477
AntonLS 13:28332f65d14b 478 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 479
AntonLS 13:28332f65d14b 480 do
AntonLS 13:28332f65d14b 481 {
AntonLS 13:28332f65d14b 482 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 483 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 484
AntonLS 13:28332f65d14b 485 err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset );
AntonLS 13:28332f65d14b 486
AntonLS 13:28332f65d14b 487 } while( 0 );
AntonLS 13:28332f65d14b 488
AntonLS 13:28332f65d14b 489 return err_code;
AntonLS 13:28332f65d14b 490 }
AntonLS 19:afcbb425b3cf 491
AntonLS 13:28332f65d14b 492 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 493 {
AntonLS 13:28332f65d14b 494 switch( op_code )
AntonLS 13:28332f65d14b 495 {
AntonLS 13:28332f65d14b 496 case PSTORAGE_LOAD_OP_CODE:
AntonLS 13:28332f65d14b 497 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 498 {
AntonLS 13:28332f65d14b 499 // Load operation successful.
AntonLS 13:28332f65d14b 500 data_loaded = true; // Flag to signal load is done.
AntonLS 13:28332f65d14b 501
AntonLS 13:28332f65d14b 502 } else
AntonLS 13:28332f65d14b 503 {
AntonLS 13:28332f65d14b 504 // Load operation failed.
elmbed 18:affef3a7db2a 505 //pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 506 }
AntonLS 13:28332f65d14b 507 break;
AntonLS 13:28332f65d14b 508 case PSTORAGE_UPDATE_OP_CODE:
AntonLS 13:28332f65d14b 509 case PSTORAGE_STORE_OP_CODE:
AntonLS 13:28332f65d14b 510 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 511 {
AntonLS 13:28332f65d14b 512 // Store operation successful.
AntonLS 13:28332f65d14b 513 data_stored = true; // Flag to signal store is done.
AntonLS 13:28332f65d14b 514
AntonLS 13:28332f65d14b 515 } else
AntonLS 13:28332f65d14b 516 {
AntonLS 13:28332f65d14b 517 // Store operation failed.
elmbed 18:affef3a7db2a 518 //pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 519 }
AntonLS 13:28332f65d14b 520 // Source memory can now be reused or freed.
AntonLS 13:28332f65d14b 521 break;
AntonLS 13:28332f65d14b 522 case PSTORAGE_CLEAR_OP_CODE:
AntonLS 13:28332f65d14b 523 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 524 {
AntonLS 13:28332f65d14b 525 // Clear operation successful.
AntonLS 13:28332f65d14b 526 data_cleared = true; // Flag to store to the same data area.
AntonLS 13:28332f65d14b 527
AntonLS 13:28332f65d14b 528 } else
AntonLS 13:28332f65d14b 529 {
AntonLS 13:28332f65d14b 530 // Clear operation failed.
elmbed 18:affef3a7db2a 531 //pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 532 }
AntonLS 13:28332f65d14b 533 break;
AntonLS 19:afcbb425b3cf 534 //default:
AntonLS 19:afcbb425b3cf 535 //pcfc.printf( "\r\nWarn: pstorage unknown op: %x error: %x\r\n", op_code, result );
AntonLS 13:28332f65d14b 536 }
AntonLS 13:28332f65d14b 537 }
AntonLS 19:afcbb425b3cf 538
AntonLS 13:28332f65d14b 539 static uint32_t pstorage_setup()
AntonLS 13:28332f65d14b 540 {
AntonLS 13:28332f65d14b 541 pstorage_module_param_t pstorage_param;
AntonLS 13:28332f65d14b 542
AntonLS 13:28332f65d14b 543 // Setup pstorage with 9*20 +76 blocks of 4 bytes each. (or 9*20/4 + 19 for 16 byte blocks)
AntonLS 13:28332f65d14b 544 pstorage_param.block_size = 4; // Recommended to be >= 4 bytes.
AntonLS 13:28332f65d14b 545 pstorage_param.block_count = 9*20 +76; // 9 Sequences x 20 Stations + 76 blocks to fill out to 1k.
AntonLS 13:28332f65d14b 546 pstorage_param.cb = pstorage_cb_handler;
AntonLS 13:28332f65d14b 547
AntonLS 13:28332f65d14b 548 return pstorage_register( &pstorage_param, &pstorage_id );
AntonLS 13:28332f65d14b 549 }
AntonLS 19:afcbb425b3cf 550 */
AntonLS 13:28332f65d14b 551
AntonLS 13:28332f65d14b 552 void periodicCallback( void )
AntonLS 13:28332f65d14b 553 {
AntonLS 19:afcbb425b3cf 554 static int foo;
AntonLS 19:afcbb425b3cf 555 foo++;
elmbed 23:26f27c462976 556 //#if BLENANO
elmbed 18:affef3a7db2a 557 led0 = !led0;
elmbed 23:26f27c462976 558 //#endif
elmbed 18:affef3a7db2a 559 led1 = !led1;
AntonLS 19:afcbb425b3cf 560 led2 = !led2;
AntonLS 13:28332f65d14b 561 // rts = !rts;
AntonLS 20:d6c6099b60be 562
AntonLS 22:f58d4b441aba 563 // if( 0 == (foo % 60) ){ led0 = 0; buzz = 0; }
AntonLS 22:f58d4b441aba 564 // if( 2 == (foo % 60) ){ led0 = 1; buzz = 1; }
AntonLS 22:f58d4b441aba 565
AntonLS 21:32f022efcc09 566 // Check battery level every 30s.
AntonLS 21:32f022efcc09 567 if( 0 == (foo % 60) ) updateBatt( getBattLevel() );
AntonLS 13:28332f65d14b 568 }
AntonLS 13:28332f65d14b 569
AntonLS 12:6d313d575f84 570 /*
AntonLS 12:6d313d575f84 571 void led_thread( void const *args )
AntonLS 12:6d313d575f84 572 {
AntonLS 12:6d313d575f84 573 while( true )
AntonLS 12:6d313d575f84 574 {
AntonLS 12:6d313d575f84 575 led0 = !led0;
AntonLS 12:6d313d575f84 576 led1 = !led1;
AntonLS 12:6d313d575f84 577 Thread::wait( 1000 );
AntonLS 12:6d313d575f84 578 }
AntonLS 12:6d313d575f84 579 }
AntonLS 12:6d313d575f84 580 */
AntonLS 12:6d313d575f84 581
AntonLS 13:28332f65d14b 582
AntonLS 0:28ca4562fe1a 583 int main( void )
AntonLS 0:28ca4562fe1a 584 {
AntonLS 0:28ca4562fe1a 585 Ticker ticker;
AntonLS 19:afcbb425b3cf 586 ticker.attach( periodicCallback, 0.5 /*** 0.1 ***/ /* 1 */ );
AntonLS 19:afcbb425b3cf 587
AntonLS 19:afcbb425b3cf 588 /*
AntonLS 19:afcbb425b3cf 589 // Thread thread( led_thread );
AntonLS 19:afcbb425b3cf 590 */
AntonLS 19:afcbb425b3cf 591
AntonLS 19:afcbb425b3cf 592 /////pcfc.baud( 57600 );
AntonLS 19:afcbb425b3cf 593
AntonLS 19:afcbb425b3cf 594 /*
AntonLS 19:afcbb425b3cf 595 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 19:afcbb425b3cf 596 */
AntonLS 2:fe1566cdb6e7 597
AntonLS 0:28ca4562fe1a 598 ble.init();
elmbed 18:affef3a7db2a 599
AntonLS 11:d3aa5fca2330 600 ble.onConnection( connectionCallback );
AntonLS 0:28ca4562fe1a 601 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 602 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 603 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 604
AntonLS 0:28ca4562fe1a 605 /* setup advertising */
AntonLS 20:d6c6099b60be 606 setAdvData();
AntonLS 5:1b9734e68327 607
AntonLS 19:afcbb425b3cf 608 /////pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName );
AntonLS 19:afcbb425b3cf 609
AntonLS 19:afcbb425b3cf 610 /*
AntonLS 19:afcbb425b3cf 611 #if LOOPBACK_MODE
AntonLS 19:afcbb425b3cf 612 pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
AntonLS 19:afcbb425b3cf 613 #endif
AntonLS 19:afcbb425b3cf 614 */
AntonLS 19:afcbb425b3cf 615
AntonLS 13:28332f65d14b 616 uint32_t p_count;
AntonLS 13:28332f65d14b 617 uint32_t pstorageErr = pstorage_init(); // This needs to be called, even though apparently called in bootloader--Check other stuff.
AntonLS 13:28332f65d14b 618 pstorage_access_status_get( &p_count );
AntonLS 13:28332f65d14b 619
AntonLS 19:afcbb425b3cf 620 /*
AntonLS 19:afcbb425b3cf 621 // pcfc.printf( "\r\nInitial pstorage count: %d\r\n", p_count );
AntonLS 19:afcbb425b3cf 622
AntonLS 19:afcbb425b3cf 623 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr );
AntonLS 19:afcbb425b3cf 624 else
AntonLS 19:afcbb425b3cf 625 {
AntonLS 19:afcbb425b3cf 626 pstorageErr = pstorage_setup();
AntonLS 19:afcbb425b3cf 627 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr );
AntonLS 19:afcbb425b3cf 628 else
AntonLS 19:afcbb425b3cf 629 {
AntonLS 19:afcbb425b3cf 630 pstorageErr = pstorage_read_test();
AntonLS 19:afcbb425b3cf 631 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr );
AntonLS 19:afcbb425b3cf 632 else
AntonLS 19:afcbb425b3cf 633 {
AntonLS 19:afcbb425b3cf 634 // In this test setup, we use the load callback to signal start to clear,
AntonLS 19:afcbb425b3cf 635 // then we use the clear callback to signal start to write.
AntonLS 19:afcbb425b3cf 636 }
AntonLS 19:afcbb425b3cf 637 }
AntonLS 19:afcbb425b3cf 638 }
AntonLS 19:afcbb425b3cf 639 */
AntonLS 19:afcbb425b3cf 640
AntonLS 0:28ca4562fe1a 641 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 19:afcbb425b3cf 642 BatteryService battService( ble );
AntonLS 20:d6c6099b60be 643 battServiceP = &battService;
AntonLS 21:32f022efcc09 644 updateBatt( getBattLevel() );
AntonLS 0:28ca4562fe1a 645
AntonLS 19:afcbb425b3cf 646 /* Enable over-the-air firmware updates. Instantiating DFUService introduces a
AntonLS 0:28ca4562fe1a 647 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 648 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 649 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 650
AntonLS 19:afcbb425b3cf 651 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 652 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 653
AntonLS 21:32f022efcc09 654 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 132 ) );
AntonLS 21:32f022efcc09 655 ble.startAdvertising();
AntonLS 21:32f022efcc09 656
AntonLS 19:afcbb425b3cf 657 PhoneAppIO *phone = new PhoneAppIO( ble,
AntonLS 19:afcbb425b3cf 658 uartServicePtr->getRXCharacteristicHandle(),
AntonLS 19:afcbb425b3cf 659 uartServicePtr->getTXCharacteristicHandle() );
AntonLS 19:afcbb425b3cf 660
elmbed 18:affef3a7db2a 661 phone->loopbackMode = LOOPBACK_MODE;
elmbed 18:affef3a7db2a 662 phoneP = phone;
elmbed 18:affef3a7db2a 663
AntonLS 19:afcbb425b3cf 664 // DigitalOut *buzzPin = new DigitalOut(p20);
AntonLS 19:afcbb425b3cf 665 // *buzzPin = 1;
elmbed 17:d8b901d791fd 666 setup();
elmbed 23:26f27c462976 667 radio_init();
AntonLS 11:d3aa5fca2330 668 tmr.start();
elmbed 18:affef3a7db2a 669
AntonLS 11:d3aa5fca2330 670 // Main Loop
AntonLS 19:afcbb425b3cf 671 while( true ) // for( uint32_t loop=1; ;loop++ )
AntonLS 0:28ca4562fe1a 672 {
AntonLS 0:28ca4562fe1a 673 ble.waitForEvent();
AntonLS 19:afcbb425b3cf 674
AntonLS 19:afcbb425b3cf 675 /**/ toPhoneChk(); // Write any pending data to phone.
AntonLS 19:afcbb425b3cf 676
AntonLS 19:afcbb425b3cf 677 /*
AntonLS 19:afcbb425b3cf 678 while( 0 <= phone.getchar() ); // Eat input.
AntonLS 19:afcbb425b3cf 679
AntonLS 19:afcbb425b3cf 680 // if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() );
AntonLS 19:afcbb425b3cf 681
AntonLS 19:afcbb425b3cf 682 app_sched_execute(); // Attempt to get pstorage enqueued cmd callbacks.
AntonLS 19:afcbb425b3cf 683 */
AntonLS 19:afcbb425b3cf 684
AntonLS 19:afcbb425b3cf 685 /***
elmbed 18:affef3a7db2a 686 if (phoneToDevPos > 0)
elmbed 18:affef3a7db2a 687 {
elmbed 18:affef3a7db2a 688 getRadioInput(phoneToDev, phoneToDevPos );
elmbed 18:affef3a7db2a 689 phoneToDevPos = 0;
elmbed 18:affef3a7db2a 690 }
AntonLS 19:afcbb425b3cf 691 ***/
AntonLS 19:afcbb425b3cf 692
AntonLS 19:afcbb425b3cf 693 /**/ int bytes = MIN( PHTODEV_BUF_LEN, phoneP->readable() );
elmbed 23:26f27c462976 694 /**/
elmbed 23:26f27c462976 695 #ifdef MASTER
elmbed 23:26f27c462976 696 getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
elmbed 23:26f27c462976 697 #endif
elmbed 17:d8b901d791fd 698 loop();
elmbed 18:affef3a7db2a 699 //radio_loop();
AntonLS 19:afcbb425b3cf 700
AntonLS 19:afcbb425b3cf 701 /***
elmbed 18:affef3a7db2a 702 if (connected)
AntonLS 13:28332f65d14b 703 {
elmbed 18:affef3a7db2a 704 phoneP->maybeHandleWrite();
elmbed 18:affef3a7db2a 705 }
AntonLS 13:28332f65d14b 706
elmbed 18:affef3a7db2a 707 while( 0 <= phone->getchar() ); // Eat input.
AntonLS 19:afcbb425b3cf 708 ***/
AntonLS 0:28ca4562fe1a 709 }
AntonLS 0:28ca4562fe1a 710 }
AntonLS 0:28ca4562fe1a 711
AntonLS 15:b86c4b798aa1 712 /**@brief Function for error handling, which is called when an error has occurred.
AntonLS 15:b86c4b798aa1 713 *
AntonLS 15:b86c4b798aa1 714 * @warning This handler is an example only and does not fit a final product. You need to analyze
AntonLS 15:b86c4b798aa1 715 * how your product is supposed to react in case of error.
AntonLS 15:b86c4b798aa1 716 *
AntonLS 15:b86c4b798aa1 717 * @param[in] error_code Error code supplied to the handler.
AntonLS 15:b86c4b798aa1 718 * @param[in] line_num Line number where the handler is called.
AntonLS 15:b86c4b798aa1 719 * @param[in] p_file_name Pointer to the file name.
AntonLS 15:b86c4b798aa1 720 */
AntonLS 15:b86c4b798aa1 721 void $Sub$$app_error_handler( uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name )
AntonLS 15:b86c4b798aa1 722 {
AntonLS 15:b86c4b798aa1 723 // nrf_gpio_pin_set( ASSERT_LED_PIN_NO );
elmbed 18:affef3a7db2a 724 //led1 = 1;
AntonLS 15:b86c4b798aa1 725
AntonLS 15:b86c4b798aa1 726 // This call can be used for debug purposes during application development.
AntonLS 15:b86c4b798aa1 727 // @note CAUTION: Activating this code will write the stack to flash on an error.
AntonLS 15:b86c4b798aa1 728 // This function should NOT be used in a final product.
AntonLS 15:b86c4b798aa1 729 // It is intended STRICTLY for development/debugging purposes.
AntonLS 15:b86c4b798aa1 730 // The flash write will happen EVEN if the radio is active, thus interrupting
AntonLS 15:b86c4b798aa1 731 // any communication.
AntonLS 15:b86c4b798aa1 732 // Use with care. Uncomment the line below to use.
AntonLS 15:b86c4b798aa1 733 // ble_debug_assert_handler(error_code, line_num, p_file_name);
AntonLS 15:b86c4b798aa1 734
AntonLS 15:b86c4b798aa1 735 // On assert, the system can only recover with a reset.
AntonLS 15:b86c4b798aa1 736 NVIC_SystemReset();
AntonLS 15:b86c4b798aa1 737 }
AntonLS 15:b86c4b798aa1 738
AntonLS 0:28ca4562fe1a 739 /* EOF */