football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Sat Jan 16 17:31:27 2016 +0000
Revision:
59:297133497153
Parent:
54:2878d62714d6
Parent:
58:fb2198ceb412
Child:
60:d1fad57e8bfb
Merge

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 31:a6110950f385 20 * *** Now using mbed-src again to modify startup code to use full 32k RAM on QFAC part 20160104 ALS
AntonLS 31:a6110950f385 21 * while still using RBL BLE Nano as a target:
AntonLS 31:a6110950f385 22 * Copied files nRF51822.sct and startup_nRF51822.s
AntonLS 31:a6110950f385 23 * from targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_ARM_STD/TARGET_MCU_NORDIC_32K
AntonLS 31:a6110950f385 24 * to targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_ARM_STD/TARGET_MCU_NORDIC_16K
AntonLS 31:a6110950f385 25 * (Stomping the 16k settings w/ 32k settings.)
AntonLS 31:a6110950f385 26 *
AntonLS 13:28332f65d14b 27 * Changed
AntonLS 13:28332f65d14b 28 * nRF51822/nordic/pstorage_platform.h PSTORAGE_MIN_BLOCK_SIZE changed from 0x010 to 4.
AntonLS 0:28ca4562fe1a 29 */
AntonLS 0:28ca4562fe1a 30
AntonLS 0:28ca4562fe1a 31 /* mbed Microcontroller Library
AntonLS 0:28ca4562fe1a 32 * Copyright (c) 2006-2013 ARM Limited
AntonLS 0:28ca4562fe1a 33 *
AntonLS 0:28ca4562fe1a 34 * Licensed under the Apache License, Version 2.0 (the "License");
AntonLS 0:28ca4562fe1a 35 * you may not use this file except in compliance with the License.
AntonLS 0:28ca4562fe1a 36 * You may obtain a copy of the License at
AntonLS 0:28ca4562fe1a 37 *
AntonLS 0:28ca4562fe1a 38 * http://www.apache.org/licenses/LICENSE-2.0
AntonLS 0:28ca4562fe1a 39 *
AntonLS 0:28ca4562fe1a 40 * Unless required by applicable law or agreed to in writing, software
AntonLS 0:28ca4562fe1a 41 * distributed under the License is distributed on an "AS IS" BASIS,
AntonLS 0:28ca4562fe1a 42 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AntonLS 0:28ca4562fe1a 43 * See the License for the specific language governing permissions and
AntonLS 0:28ca4562fe1a 44 * limitations under the License.
AntonLS 0:28ca4562fe1a 45 */
elmbed 18:affef3a7db2a 46 #include <cstdarg>
elmbed 18:affef3a7db2a 47 #include <cstdio>
AntonLS 0:28ca4562fe1a 48 #include "mbed.h"
AntonLS 12:6d313d575f84 49 // #include "rtos.h"
AntonLS 0:28ca4562fe1a 50 #include "BLEDevice.h"
AntonLS 0:28ca4562fe1a 51
AntonLS 0:28ca4562fe1a 52 #include "DFUService.h"
AntonLS 0:28ca4562fe1a 53 #include "UARTService.h"
AntonLS 0:28ca4562fe1a 54 #include "DeviceInformationService.h"
AntonLS 19:afcbb425b3cf 55 #include "BatteryService.h"
AntonLS 0:28ca4562fe1a 56
AntonLS 0:28ca4562fe1a 57 #include "MTSSerialFlowControl.h"
AntonLS 5:1b9734e68327 58 #include "PhoneAppIO.h"
AntonLS 0:28ca4562fe1a 59
elmbed 23:26f27c462976 60 #include "types.h"
elmbed 18:affef3a7db2a 61 #include "TA.h"
elmbed 18:affef3a7db2a 62
elmbed 23:26f27c462976 63 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
AntonLS 0:28ca4562fe1a 64 * it will have an impact on code-size and power consumption. */
AntonLS 0:28ca4562fe1a 65
AntonLS 11:d3aa5fca2330 66 #define LOOPBACK_MODE 0 // Loopback mode
AntonLS 4:17b8edf264c3 67
AntonLS 0:28ca4562fe1a 68 #if NEED_CONSOLE_OUTPUT
AntonLS 0:28ca4562fe1a 69 #define DEBUG(...) { printf(__VA_ARGS__); }
AntonLS 0:28ca4562fe1a 70 #else
AntonLS 0:28ca4562fe1a 71 #define DEBUG(...) /* nothing */
AntonLS 0:28ca4562fe1a 72 #endif /* #if NEED_CONSOLE_OUTPUT */
AntonLS 0:28ca4562fe1a 73
AntonLS 46:28c29ef61276 74 #ifdef MASTER
AntonLS 46:28c29ef61276 75 static bool is_master = true;
AntonLS 46:28c29ef61276 76 #else
AntonLS 46:28c29ef61276 77 static bool is_master = false;
AntonLS 46:28c29ef61276 78 #endif
AntonLS 46:28c29ef61276 79
elmbed 17:d8b901d791fd 80 void loop();
elmbed 17:d8b901d791fd 81 void setup();
elmbed 17:d8b901d791fd 82 void getRadioInput(char *ibuffer, int size);
elmbed 58:fb2198ceb412 83 void init_datastore();
elmbed 17:d8b901d791fd 84
AntonLS 19:afcbb425b3cf 85 #define BLENANO 0 // BLE Nano vs. TA New Baseboard (rev Aug-Nov 2015.)
AntonLS 19:afcbb425b3cf 86
AntonLS 19:afcbb425b3cf 87 #if BLENANO
AntonLS 19:afcbb425b3cf 88 #define SERTX USBTX
AntonLS 19:afcbb425b3cf 89 #define SERRX USBRX
AntonLS 19:afcbb425b3cf 90 #else // Same RTS pin used by all (P0_08) and unused CTS (P0_10)
AntonLS 19:afcbb425b3cf 91 #define SERTX P0_18
AntonLS 19:afcbb425b3cf 92 #define SERRX P0_17
AntonLS 19:afcbb425b3cf 93 #endif
AntonLS 19:afcbb425b3cf 94
elmbed 18:affef3a7db2a 95 static Timer tmr;
elmbed 17:d8b901d791fd 96
elmbed 17:d8b901d791fd 97 unsigned long millis()
elmbed 17:d8b901d791fd 98 {
elmbed 17:d8b901d791fd 99 return tmr.read_ms();
elmbed 17:d8b901d791fd 100 }
elmbed 17:d8b901d791fd 101
elmbed 17:d8b901d791fd 102 unsigned long micros()
elmbed 17:d8b901d791fd 103 {
AntonLS 19:afcbb425b3cf 104 return tmr.read_us();
elmbed 17:d8b901d791fd 105 }
elmbed 17:d8b901d791fd 106
AntonLS 0:28ca4562fe1a 107 extern "C"
AntonLS 0:28ca4562fe1a 108 {
AntonLS 13:28332f65d14b 109 #include "softdevice_handler.h" // Attempt to get pstorage enqueued cmd callbacks.
AntonLS 13:28332f65d14b 110 #include "app_timer.h"
AntonLS 13:28332f65d14b 111 #include "pstorage.h"
AntonLS 13:28332f65d14b 112
AntonLS 16:3c873f2c8a27 113 // void My_UART0_IRQHandler();
AntonLS 12:6d313d575f84 114
AntonLS 0:28ca4562fe1a 115 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 116 }
AntonLS 0:28ca4562fe1a 117
AntonLS 21:32f022efcc09 118 #if BLENANO
AntonLS 21:32f022efcc09 119 #define ADC_IN_BATT P0_6
AntonLS 22:f58d4b441aba 120 #define CHARGING_IN P0_29
AntonLS 21:32f022efcc09 121 #else
AntonLS 21:32f022efcc09 122 #define ADC_IN_BATT P0_4
AntonLS 22:f58d4b441aba 123 #define CHARGING_IN P0_2
AntonLS 21:32f022efcc09 124 #endif
AntonLS 21:32f022efcc09 125 AnalogIn batt( ADC_IN_BATT );
AntonLS 21:32f022efcc09 126
AntonLS 12:6d313d575f84 127 // DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 19:afcbb425b3cf 128 DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO
AntonLS 12:6d313d575f84 129
AntonLS 12:6d313d575f84 130 // Check if we should swap serial Rx/Tx for early rev of "Little Brain"
AntonLS 19:afcbb425b3cf 131 DigitalIn trSwp( P0_30, PullUp );
AntonLS 12:6d313d575f84 132
AntonLS 12:6d313d575f84 133 // Wait to settle.
AntonLS 12:6d313d575f84 134 int foo = (wait( 0.1 ), 0);
AntonLS 12:6d313d575f84 135
AntonLS 14:76fb883a3cb8 136 // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins.
AntonLS 20:d6c6099b60be 137 static DigitalOut led0( P0_19, 1 ); // TA New Baseboard LED High=On ("STATUS") (OK on Nano: LED Low=On)
AntonLS 19:afcbb425b3cf 138 static DigitalOut led1( P0_21, 0 ); // TA New Baseboard High=On (OK on Nano: NC)
AntonLS 19:afcbb425b3cf 139 DigitalOut led2( P0_22, 1 ); // TA New Baseboard High=On (OK on Nano: NC)
AntonLS 19:afcbb425b3cf 140
AntonLS 19:afcbb425b3cf 141 // DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On (OK on Nano: Alt RxD)
AntonLS 14:76fb883a3cb8 142 // DigitalOut led1( (trSwp ? P0_4 : P0_3), 0 ); // TA Baseboard High=On (And don't use P0_4 on Nano)
AntonLS 14:76fb883a3cb8 143
AntonLS 30:c60b0d52b067 144 // DigitalOut buzz( P0_20, 1 ); // TA New Baseboard Low=On (OK on Nano: NC)
AntonLS 30:c60b0d52b067 145
AntonLS 30:c60b0d52b067 146 int tickTock = 0; // Counter used by periodicCallback().
AntonLS 30:c60b0d52b067 147
AntonLS 30:c60b0d52b067 148 class ChgChg : public InterruptIn
AntonLS 30:c60b0d52b067 149 {
AntonLS 30:c60b0d52b067 150 public:
AntonLS 30:c60b0d52b067 151 ChgChg( PinName pin, PinMode pull=PullUp ) : InterruptIn( pin )
AntonLS 30:c60b0d52b067 152 {
AntonLS 30:c60b0d52b067 153 mode( pull ); // Set pull mode
AntonLS 30:c60b0d52b067 154 fall( this, &ChgChg::chargeStart ); // Attach ISR for fall
AntonLS 30:c60b0d52b067 155 rise( this, &ChgChg::chargeStop ); // Attach ISR for rise
AntonLS 30:c60b0d52b067 156
AntonLS 30:c60b0d52b067 157 led0 = !read();
AntonLS 30:c60b0d52b067 158 }
AntonLS 30:c60b0d52b067 159 void chargeStart()
AntonLS 30:c60b0d52b067 160 {
AntonLS 30:c60b0d52b067 161 led0 = 1;
AntonLS 30:c60b0d52b067 162 tickTock = 0; // Trigger batt level update.
AntonLS 30:c60b0d52b067 163 }
AntonLS 30:c60b0d52b067 164 void chargeStop()
AntonLS 30:c60b0d52b067 165 {
AntonLS 30:c60b0d52b067 166 led0 = 0;
AntonLS 30:c60b0d52b067 167 tickTock = 0; // Trigger batt level update.
AntonLS 30:c60b0d52b067 168 }
AntonLS 30:c60b0d52b067 169 };
AntonLS 30:c60b0d52b067 170 ChgChg notcharge( CHARGING_IN, PullUp );
AntonLS 30:c60b0d52b067 171
AntonLS 19:afcbb425b3cf 172
AntonLS 13:28332f65d14b 173 // App timer, app scheduler, and pstorage are already setup by bootloader.
AntonLS 13:28332f65d14b 174
elmbed 18:affef3a7db2a 175 static BLEDevice ble;
AntonLS 19:afcbb425b3cf 176
AntonLS 12:6d313d575f84 177
AntonLS 1:0ba687d4196f 178 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 179 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 11:d3aa5fca2330 180 // But a version of serial_init() erroneously enabled the uart before the setting of those,
AntonLS 11:d3aa5fca2330 181 // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 11:d3aa5fca2330 182 // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase)
AntonLS 7:205ef63d311a 183 // to not use serial_init() in serial_api.c, so flow control is setup correctly *
AntonLS 7:205ef63d311a 184 // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default *
AntonLS 15:b86c4b798aa1 185 // [* We can't change the uart interrupt vector, so we comment-out the handler in
AntonLS 15:b86c4b798aa1 186 // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work.] - No need now.
AntonLS 15:b86c4b798aa1 187 // * Now using $Sub$$UART0_IRQHandler to override UART0_IRQHandler without needing to use lib source.
AntonLS 15:b86c4b798aa1 188 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might have worked--No need.
AntonLS 15:b86c4b798aa1 189 //
AntonLS 6:ef758ac3c928 190 // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its
AntonLS 11:d3aa5fca2330 191 // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay.
AntonLS 1:0ba687d4196f 192 //
AntonLS 19:afcbb425b3cf 193 // mts::MTSSerialFlowControl pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
AntonLS 19:afcbb425b3cf 194 //mts::MTSSerial pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), 128, 64, RTS_PIN_NUMBER, NC ); // 256, 1280 // 256, 2560
AntonLS 0:28ca4562fe1a 195
elmbed 58:fb2198ceb412 196 Serial pc(SERTX,SERRX);
AntonLS 0:28ca4562fe1a 197 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 198
AntonLS 0:28ca4562fe1a 199
AntonLS 5:1b9734e68327 200 char deviceName[6]; // "TAF00";
AntonLS 5:1b9734e68327 201 Gap::address_t macAddr;
AntonLS 5:1b9734e68327 202 Gap::addr_type_t *pAdType;
AntonLS 19:afcbb425b3cf 203 static const uint16_t uuid16_list[] = { GattService::UUID_DEVICE_INFORMATION_SERVICE,
AntonLS 19:afcbb425b3cf 204 GattService::UUID_BATTERY_SERVICE };
AntonLS 20:d6c6099b60be 205 static uint8_t batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff,
AntonLS 19:afcbb425b3cf 206 GattService::UUID_BATTERY_SERVICE >> 8,
AntonLS 22:f58d4b441aba 207 99, // Batt level
AntonLS 19:afcbb425b3cf 208 'T', 'X' }; // Custom ID trick
AntonLS 19:afcbb425b3cf 209
AntonLS 20:d6c6099b60be 210 UARTService *uartServicePtr;
AntonLS 49:626e84ce5e52 211 PhoneAppIO *phoneP;
AntonLS 20:d6c6099b60be 212 BatteryService *battServiceP;
AntonLS 5:1b9734e68327 213
elmbed 18:affef3a7db2a 214 // Buffer for holding data from the phone
elmbed 18:affef3a7db2a 215 // to the device
AntonLS 45:1eb335c00cb2 216 static char phoneToDev[MAX_LEN] = {0};
AntonLS 0:28ca4562fe1a 217
elmbed 18:affef3a7db2a 218 extern TA ta;
elmbed 18:affef3a7db2a 219
elmbed 18:affef3a7db2a 220 extern void radio_init();
elmbed 44:4ad6133987ed 221 extern void radio_loop(int mac);
elmbed 18:affef3a7db2a 222
AntonLS 22:f58d4b441aba 223 void setAdvData()
AntonLS 22:f58d4b441aba 224 {
AntonLS 22:f58d4b441aba 225 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED |
AntonLS 22:f58d4b441aba 226 GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
AntonLS 22:f58d4b441aba 227 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 22:f58d4b441aba 228
AntonLS 22:f58d4b441aba 229 // Get MAC addr so we can create a device name using it.
AntonLS 22:f58d4b441aba 230 ble.getAddress( pAdType, macAddr );
elmbed 28:8e74ddc4f70f 231
AntonLS 46:28c29ef61276 232 sprintf( deviceName, "%c%02X%02X", (is_master?'T':'S'), macAddr[1], macAddr[0] );
AntonLS 22:f58d4b441aba 233
AntonLS 22:f58d4b441aba 234 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 22:f58d4b441aba 235 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 22:f58d4b441aba 236
AntonLS 22:f58d4b441aba 237 // Moved to scan response packet to give more room in AD packet...
AntonLS 22:f58d4b441aba 238 // ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 239 // (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 22:f58d4b441aba 240
AntonLS 22:f58d4b441aba 241 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 242 (uint8_t *)uuid16_list, sizeof( uuid16_list ) );
AntonLS 22:f58d4b441aba 243 ble.accumulateAdvertisingPayload( GapAdvertisingData::SERVICE_DATA,
AntonLS 30:c60b0d52b067 244 (uint8_t *)batt_and_id, sizeof( batt_and_id ) ); // Batt lev + "TX"
AntonLS 22:f58d4b441aba 245
AntonLS 22:f58d4b441aba 246 ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 22:f58d4b441aba 247 (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) );
AntonLS 22:f58d4b441aba 248 }
AntonLS 22:f58d4b441aba 249
AntonLS 22:f58d4b441aba 250 void updateBatt( uint8_t pct )
AntonLS 22:f58d4b441aba 251 {
AntonLS 22:f58d4b441aba 252 static uint8_t prev;
AntonLS 22:f58d4b441aba 253 static bool wasCharging;
AntonLS 22:f58d4b441aba 254
AntonLS 22:f58d4b441aba 255 if( notcharge )
AntonLS 22:f58d4b441aba 256 {
AntonLS 30:c60b0d52b067 257 if( wasCharging ) ta.beep_off(); // Alarm Clock TMP TODO remove once hardware is fixed.
AntonLS 30:c60b0d52b067 258
AntonLS 22:f58d4b441aba 259 if( pct > 100 ) pct = 100;
AntonLS 22:f58d4b441aba 260
AntonLS 31:a6110950f385 261 if( wasCharging )
AntonLS 31:a6110950f385 262 {
AntonLS 31:a6110950f385 263 pct = 102; // Show 102% at charger unplug.
AntonLS 51:fabe54861f1b 264 tickTock = 50; // Cause another normal update in 5s (60 -50 = 10 half s).
AntonLS 31:a6110950f385 265
AntonLS 31:a6110950f385 266 } else
AntonLS 31:a6110950f385 267 {
AntonLS 31:a6110950f385 268 // Use if we update quite often.
AntonLS 31:a6110950f385 269 // if( abs( (int)prev -(int)pct ) < 3 ) return; // Don't register change of less than 3%.
AntonLS 31:a6110950f385 270 }
AntonLS 22:f58d4b441aba 271
AntonLS 22:f58d4b441aba 272 prev = pct;
AntonLS 22:f58d4b441aba 273 wasCharging = false;
AntonLS 22:f58d4b441aba 274
AntonLS 30:c60b0d52b067 275 } else // Charging
AntonLS 22:f58d4b441aba 276 {
AntonLS 30:c60b0d52b067 277 // Plugged-in seems to add ~23% to level, so checking for 99% is like 76% unplugged.
AntonLS 30:c60b0d52b067 278 // But I had a failure with a smaller gap, so reduced another 8%.
AntonLS 30:c60b0d52b067 279 if( pct >= 91 ) ta.beep( 15000 ); // Alarm Clock TMP TODO remove once hardware is fixed.
AntonLS 30:c60b0d52b067 280
AntonLS 31:a6110950f385 281 if( !wasCharging )
AntonLS 31:a6110950f385 282 pct = 101; // Show 101% at start of charging.
AntonLS 22:f58d4b441aba 283 wasCharging = true; // Force show when done charging.
AntonLS 22:f58d4b441aba 284 }
AntonLS 22:f58d4b441aba 285
AntonLS 54:2878d62714d6 286 if( pct <= 10 ) ta.batteryLow = true;
AntonLS 54:2878d62714d6 287
AntonLS 22:f58d4b441aba 288 batt_and_id[2] = pct;
AntonLS 22:f58d4b441aba 289 ble.clearAdvertisingPayload();
AntonLS 22:f58d4b441aba 290 setAdvData();
AntonLS 22:f58d4b441aba 291 ble.setAdvertisingPayload();
AntonLS 22:f58d4b441aba 292
AntonLS 22:f58d4b441aba 293 if( NULL != battServiceP ) battServiceP->updateBatteryLevel( pct );
AntonLS 22:f58d4b441aba 294 }
AntonLS 22:f58d4b441aba 295
AntonLS 22:f58d4b441aba 296 #define BAT_TOP 793 // 3250 // 52000
AntonLS 22:f58d4b441aba 297 #define BAT_BOT 684 // 2800 // 44800
AntonLS 22:f58d4b441aba 298 uint8_t getBattLevel()
AntonLS 22:f58d4b441aba 299 {
AntonLS 22:f58d4b441aba 300 int battI = (int)(batt * 1000.f);
AntonLS 22:f58d4b441aba 301 // Normalize to usable range...
AntonLS 22:f58d4b441aba 302 // Note: These levels should also give 0% if powered by programmer.
AntonLS 22:f58d4b441aba 303 if( battI > BAT_TOP ) battI = BAT_TOP; // Fully charged reading ~3.85V
AntonLS 22:f58d4b441aba 304 if( battI < BAT_BOT ) battI = BAT_BOT; // Device failing reading ~3.32V
AntonLS 22:f58d4b441aba 305 battI-= BAT_BOT;
AntonLS 22:f58d4b441aba 306 return (uint8_t)(battI*100 / (BAT_TOP-BAT_BOT));
AntonLS 22:f58d4b441aba 307 }
AntonLS 22:f58d4b441aba 308
elmbed 18:affef3a7db2a 309 // True when connected to a phone
AntonLS 11:d3aa5fca2330 310 bool connected = false;
AntonLS 5:1b9734e68327 311
AntonLS 11:d3aa5fca2330 312 void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType,
AntonLS 11:d3aa5fca2330 313 const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams )
AntonLS 11:d3aa5fca2330 314 {
AntonLS 11:d3aa5fca2330 315 connected = true;
AntonLS 19:afcbb425b3cf 316
AntonLS 19:afcbb425b3cf 317 // DEBUG( "Connected!\n\r" );
AntonLS 31:a6110950f385 318
AntonLS 31:a6110950f385 319 // char dummy;
AntonLS 31:a6110950f385 320 // writeToPhone( "Hi. Curr stack bot: 0x%08X\r\n", &dummy );
AntonLS 11:d3aa5fca2330 321 }
AntonLS 5:1b9734e68327 322
AntonLS 0:28ca4562fe1a 323 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 324 {
AntonLS 11:d3aa5fca2330 325 connected = false;
AntonLS 11:d3aa5fca2330 326
AntonLS 22:f58d4b441aba 327 updateBatt( getBattLevel() );
AntonLS 22:f58d4b441aba 328
AntonLS 19:afcbb425b3cf 329 // DEBUG( "Disconnected!\n\r" );
AntonLS 19:afcbb425b3cf 330 // DEBUG( "Restarting the advertising process\n\r" );
AntonLS 46:28c29ef61276 331
AntonLS 0:28ca4562fe1a 332 ble.startAdvertising();
AntonLS 46:28c29ef61276 333
elmbed 18:affef3a7db2a 334 ta.post_color(0);
AntonLS 0:28ca4562fe1a 335 }
AntonLS 0:28ca4562fe1a 336
AntonLS 5:1b9734e68327 337 bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
AntonLS 5:1b9734e68327 338 {
AntonLS 11:d3aa5fca2330 339 ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead );
AntonLS 5:1b9734e68327 340
AntonLS 5:1b9734e68327 341 if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
AntonLS 5:1b9734e68327 342 (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
AntonLS 7:205ef63d311a 343 {
AntonLS 19:afcbb425b3cf 344 // pcfc.printf( "\r\nBLE %d! ", err );
AntonLS 7:205ef63d311a 345
AntonLS 7:205ef63d311a 346 } else if ( err == BLE_STACK_BUSY )
AntonLS 7:205ef63d311a 347 {
AntonLS 11:d3aa5fca2330 348 // Common error when pumping data.
AntonLS 7:205ef63d311a 349 }
AntonLS 5:1b9734e68327 350
AntonLS 5:1b9734e68327 351 return (err != BLE_ERROR_NONE);
AntonLS 5:1b9734e68327 352 }
AntonLS 5:1b9734e68327 353
elmbed 18:affef3a7db2a 354 extern "C" void writeToPhone(char *format, ...)
elmbed 18:affef3a7db2a 355 {
AntonLS 49:626e84ce5e52 356 if( NULL == phoneP ) return;
AntonLS 49:626e84ce5e52 357
elmbed 18:affef3a7db2a 358 va_list arg;
AntonLS 48:4c9551c826e9 359 va_start( arg, format );
elmbed 18:affef3a7db2a 360
AntonLS 19:afcbb425b3cf 361 /**/phoneP->vprintf( format, arg );
AntonLS 19:afcbb425b3cf 362
AntonLS 19:afcbb425b3cf 363 /**/// Also write same to serial port... TODO
AntonLS 19:afcbb425b3cf 364 /**/ // pcfc.vprintf( format, arg );
AntonLS 19:afcbb425b3cf 365
elmbed 18:affef3a7db2a 366 va_end(arg);
elmbed 18:affef3a7db2a 367 }
elmbed 18:affef3a7db2a 368
AntonLS 49:626e84ce5e52 369 /**/ // Byte bits to ASCII.
AntonLS 19:afcbb425b3cf 370 extern "C" void writeBitsToPhone( uint8_t byte, uint8_t minbits )
AntonLS 19:afcbb425b3cf 371 {
AntonLS 19:afcbb425b3cf 372 static char ascbits[9] = { 0 };
AntonLS 19:afcbb425b3cf 373 int pos = 0;
AntonLS 19:afcbb425b3cf 374 if( 0 == minbits ) minbits = 1;
AntonLS 19:afcbb425b3cf 375
AntonLS 19:afcbb425b3cf 376 uint16_t ibyt = byte & 0xFF;
AntonLS 19:afcbb425b3cf 377 bool leading0 = true;
AntonLS 19:afcbb425b3cf 378
AntonLS 19:afcbb425b3cf 379 for( short b=7; b >= 0; b-- )
AntonLS 19:afcbb425b3cf 380 {
AntonLS 19:afcbb425b3cf 381 ibyt<<=1;
AntonLS 19:afcbb425b3cf 382 if( ibyt & 0x100 )
AntonLS 19:afcbb425b3cf 383 {
AntonLS 19:afcbb425b3cf 384 leading0 = false;
AntonLS 19:afcbb425b3cf 385 ascbits[pos++] = '1';
AntonLS 19:afcbb425b3cf 386
AntonLS 19:afcbb425b3cf 387 } else if( !leading0 || (b < minbits) )
AntonLS 19:afcbb425b3cf 388 {
AntonLS 19:afcbb425b3cf 389 ascbits[pos++] = '0';
AntonLS 19:afcbb425b3cf 390 }
AntonLS 19:afcbb425b3cf 391 }
AntonLS 19:afcbb425b3cf 392 ascbits[pos] = '\0';
AntonLS 19:afcbb425b3cf 393
AntonLS 19:afcbb425b3cf 394 writeToPhone( "%s", ascbits );
AntonLS 19:afcbb425b3cf 395 }
AntonLS 19:afcbb425b3cf 396
AntonLS 0:28ca4562fe1a 397 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 398 {
AntonLS 11:d3aa5fca2330 399 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 400 {
AntonLS 11:d3aa5fca2330 401 uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload
elmbed 18:affef3a7db2a 402
AntonLS 11:d3aa5fca2330 403 if( 0 != bytesRead )
AntonLS 11:d3aa5fca2330 404 {
AntonLS 19:afcbb425b3cf 405 /*
AntonLS 19:afcbb425b3cf 406 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 19:afcbb425b3cf 407
AntonLS 19:afcbb425b3cf 408 // Also write to serial port...
AntonLS 19:afcbb425b3cf 409 // pcfc.printf( "From app: " );
AntonLS 19:afcbb425b3cf 410 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 19:afcbb425b3cf 411 // pcfc.printf( "\r\n" );
AntonLS 19:afcbb425b3cf 412 */
AntonLS 19:afcbb425b3cf 413
AntonLS 11:d3aa5fca2330 414 return;
AntonLS 11:d3aa5fca2330 415 }
AntonLS 0:28ca4562fe1a 416 }
AntonLS 0:28ca4562fe1a 417 }
AntonLS 0:28ca4562fe1a 418
AntonLS 0:28ca4562fe1a 419 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 420 {
AntonLS 0:28ca4562fe1a 421 }
AntonLS 0:28ca4562fe1a 422
AntonLS 11:d3aa5fca2330 423 void toPhoneChk( void )
AntonLS 0:28ca4562fe1a 424 {
AntonLS 19:afcbb425b3cf 425 if( phoneP != NULL )
AntonLS 19:afcbb425b3cf 426 {
AntonLS 19:afcbb425b3cf 427 /*
AntonLS 19:afcbb425b3cf 428 char ch;
AntonLS 19:afcbb425b3cf 429 // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars.
AntonLS 19:afcbb425b3cf 430 for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ )
AntonLS 19:afcbb425b3cf 431 {
AntonLS 19:afcbb425b3cf 432 /// For from-serial straight to-phone.
AntonLS 19:afcbb425b3cf 433 /// if( 0 > phoneP->putchar( ch ) )
AntonLS 19:afcbb425b3cf 434 /// {
AntonLS 19:afcbb425b3cf 435 /// pcfc.printf( " * " );
AntonLS 19:afcbb425b3cf 436 /// break;
AntonLS 19:afcbb425b3cf 437 /// }
AntonLS 3:388e441be8df 438
AntonLS 19:afcbb425b3cf 439 // For from-serial inject-to-dev as-if from phone.
AntonLS 19:afcbb425b3cf 440 phoneP->injectHandleRead( &ch, 1 );
AntonLS 19:afcbb425b3cf 441
AntonLS 19:afcbb425b3cf 442 if( (cnt >= 20) && ('\n' == ch) ) break;
AntonLS 19:afcbb425b3cf 443 }
AntonLS 19:afcbb425b3cf 444 */
AntonLS 19:afcbb425b3cf 445 // Write to outgoing characteristic if anything is pending.
AntonLS 19:afcbb425b3cf 446 if( 0 != phoneP->maybeHandleWrite() )
AntonLS 19:afcbb425b3cf 447 {
AntonLS 19:afcbb425b3cf 448 // pcfc.printf( "ToPhoneHandler \r\n" );
AntonLS 19:afcbb425b3cf 449 }
AntonLS 19:afcbb425b3cf 450 }
AntonLS 0:28ca4562fe1a 451 }
elmbed 28:8e74ddc4f70f 452
AntonLS 13:28332f65d14b 453 void periodicCallback( void )
AntonLS 13:28332f65d14b 454 {
AntonLS 51:fabe54861f1b 455 static unsigned long prevMillis = millis() +125;
AntonLS 51:fabe54861f1b 456 static int callCnt = 0;
AntonLS 51:fabe54861f1b 457
AntonLS 51:fabe54861f1b 458 unsigned long elapsedMillis = millis() -prevMillis;
AntonLS 51:fabe54861f1b 459
AntonLS 51:fabe54861f1b 460 if( elapsedMillis >= 125 )
AntonLS 49:626e84ce5e52 461 {
AntonLS 51:fabe54861f1b 462 do
AntonLS 51:fabe54861f1b 463 {
AntonLS 51:fabe54861f1b 464 prevMillis = millis();
AntonLS 51:fabe54861f1b 465
AntonLS 51:fabe54861f1b 466 // if( is_master && ((callCnt % 3) == 0) ) writeToPhone( "Rnd: %x\r\n", rndHW() );
AntonLS 51:fabe54861f1b 467
AntonLS 51:fabe54861f1b 468 if( (callCnt % 4) != 0 ) break;
AntonLS 37:8f15b14d6994 469
AntonLS 30:c60b0d52b067 470 #if BLENANO
AntonLS 51:fabe54861f1b 471 led0 = !led0;
AntonLS 30:c60b0d52b067 472 #endif
AntonLS 51:fabe54861f1b 473 led1 = !led1;
AntonLS 51:fabe54861f1b 474 led2 = !led2;
AntonLS 51:fabe54861f1b 475 // rts = !rts;
AntonLS 20:d6c6099b60be 476
AntonLS 51:fabe54861f1b 477 // Check battery level every 30s.
AntonLS 51:fabe54861f1b 478 if( 0 == (tickTock % 60 ) ) updateBatt( getBattLevel() );
AntonLS 51:fabe54861f1b 479
AntonLS 51:fabe54861f1b 480 tickTock++;
AntonLS 30:c60b0d52b067 481
AntonLS 51:fabe54861f1b 482 } while( false );
AntonLS 49:626e84ce5e52 483
AntonLS 51:fabe54861f1b 484 callCnt++;
AntonLS 51:fabe54861f1b 485 }
AntonLS 13:28332f65d14b 486 }
AntonLS 13:28332f65d14b 487
AntonLS 12:6d313d575f84 488 /*
AntonLS 12:6d313d575f84 489 void led_thread( void const *args )
AntonLS 12:6d313d575f84 490 {
AntonLS 12:6d313d575f84 491 while( true )
AntonLS 12:6d313d575f84 492 {
AntonLS 12:6d313d575f84 493 led0 = !led0;
AntonLS 12:6d313d575f84 494 led1 = !led1;
AntonLS 12:6d313d575f84 495 Thread::wait( 1000 );
AntonLS 12:6d313d575f84 496 }
AntonLS 12:6d313d575f84 497 }
AntonLS 12:6d313d575f84 498 */
AntonLS 12:6d313d575f84 499
AntonLS 13:28332f65d14b 500
AntonLS 0:28ca4562fe1a 501 int main( void )
AntonLS 0:28ca4562fe1a 502 {
AntonLS 0:28ca4562fe1a 503 Ticker ticker;
AntonLS 51:fabe54861f1b 504 ////// ticker.attach( periodicCallback, 0.125 /** 0.2 **/ /** 0.5 **/ /* 1 */ );
AntonLS 19:afcbb425b3cf 505
elmbed 58:fb2198ceb412 506 pc.baud( 57600 );
AntonLS 2:fe1566cdb6e7 507
AntonLS 0:28ca4562fe1a 508 ble.init();
elmbed 18:affef3a7db2a 509
AntonLS 11:d3aa5fca2330 510 ble.onConnection( connectionCallback );
AntonLS 0:28ca4562fe1a 511 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 512 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 513 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 514
AntonLS 0:28ca4562fe1a 515 /* setup advertising */
AntonLS 20:d6c6099b60be 516 setAdvData();
AntonLS 5:1b9734e68327 517
AntonLS 48:4c9551c826e9 518 srnd( rndHW() ); // Seed the sw RNG w/ the hw.
AntonLS 48:4c9551c826e9 519
AntonLS 0:28ca4562fe1a 520 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 19:afcbb425b3cf 521 BatteryService battService( ble );
AntonLS 20:d6c6099b60be 522 battServiceP = &battService;
AntonLS 21:32f022efcc09 523 updateBatt( getBattLevel() );
AntonLS 0:28ca4562fe1a 524
AntonLS 19:afcbb425b3cf 525 /* Enable over-the-air firmware updates. Instantiating DFUService introduces a
AntonLS 0:28ca4562fe1a 526 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 527 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 528 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 529
AntonLS 19:afcbb425b3cf 530 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 531 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 532
AntonLS 49:626e84ce5e52 533 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( is_master ? 132 : 132 ) );
AntonLS 46:28c29ef61276 534
AntonLS 21:32f022efcc09 535 ble.startAdvertising();
AntonLS 21:32f022efcc09 536
AntonLS 19:afcbb425b3cf 537 PhoneAppIO *phone = new PhoneAppIO( ble,
AntonLS 19:afcbb425b3cf 538 uartServicePtr->getRXCharacteristicHandle(),
AntonLS 19:afcbb425b3cf 539 uartServicePtr->getTXCharacteristicHandle() );
AntonLS 19:afcbb425b3cf 540
elmbed 18:affef3a7db2a 541 phone->loopbackMode = LOOPBACK_MODE;
elmbed 18:affef3a7db2a 542 phoneP = phone;
elmbed 18:affef3a7db2a 543
elmbed 17:d8b901d791fd 544 setup();
elmbed 23:26f27c462976 545 radio_init();
AntonLS 11:d3aa5fca2330 546 tmr.start();
elmbed 58:fb2198ceb412 547 init_datastore();
elmbed 18:affef3a7db2a 548
elmbed 44:4ad6133987ed 549 int mac_addr = ((int)macAddr[0]);
elmbed 44:4ad6133987ed 550 mac_addr |= ((int)macAddr[1] << 8);
AntonLS 51:fabe54861f1b 551
AntonLS 51:fabe54861f1b 552 unsigned long lastMillis = millis();
AntonLS 51:fabe54861f1b 553 unsigned long currMillis = lastMillis +25;
elmbed 44:4ad6133987ed 554
elmbed 58:fb2198ceb412 555 int counter = 0;
elmbed 59:297133497153 556
AntonLS 11:d3aa5fca2330 557 // Main Loop
elmbed 58:fb2198ceb412 558 while( true )
AntonLS 0:28ca4562fe1a 559 {
AntonLS 51:fabe54861f1b 560 currMillis = millis();
elmbed 59:297133497153 561
elmbed 59:297133497153 562 if( currMillis -lastMillis < 25 )
elmbed 59:297133497153 563 {
elmbed 59:297133497153 564 continue;
elmbed 59:297133497153 565 }
elmbed 59:297133497153 566
AntonLS 51:fabe54861f1b 567 lastMillis = currMillis;
AntonLS 51:fabe54861f1b 568
AntonLS 51:fabe54861f1b 569 periodicCallback();
AntonLS 19:afcbb425b3cf 570
elmbed 59:297133497153 571 toPhoneChk();
AntonLS 19:afcbb425b3cf 572
AntonLS 46:28c29ef61276 573 #ifdef MASTER
elmbed 59:297133497153 574 int bytes = MIN( MAX_LEN, phoneP->readable() );
AntonLS 46:28c29ef61276 575 getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
elmbed 23:26f27c462976 576 #endif
AntonLS 46:28c29ef61276 577
elmbed 17:d8b901d791fd 578 loop();
elmbed 44:4ad6133987ed 579 radio_loop(mac_addr);
AntonLS 0:28ca4562fe1a 580 }
AntonLS 0:28ca4562fe1a 581 }
AntonLS 0:28ca4562fe1a 582
AntonLS 15:b86c4b798aa1 583 /**@brief Function for error handling, which is called when an error has occurred.
AntonLS 15:b86c4b798aa1 584 *
AntonLS 15:b86c4b798aa1 585 * @warning This handler is an example only and does not fit a final product. You need to analyze
AntonLS 15:b86c4b798aa1 586 * how your product is supposed to react in case of error.
AntonLS 15:b86c4b798aa1 587 *
AntonLS 15:b86c4b798aa1 588 * @param[in] error_code Error code supplied to the handler.
AntonLS 15:b86c4b798aa1 589 * @param[in] line_num Line number where the handler is called.
AntonLS 15:b86c4b798aa1 590 * @param[in] p_file_name Pointer to the file name.
AntonLS 15:b86c4b798aa1 591 */
AntonLS 15:b86c4b798aa1 592 void $Sub$$app_error_handler( uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name )
AntonLS 15:b86c4b798aa1 593 {
AntonLS 15:b86c4b798aa1 594 // nrf_gpio_pin_set( ASSERT_LED_PIN_NO );
elmbed 18:affef3a7db2a 595 //led1 = 1;
AntonLS 15:b86c4b798aa1 596
AntonLS 15:b86c4b798aa1 597 // This call can be used for debug purposes during application development.
AntonLS 15:b86c4b798aa1 598 // @note CAUTION: Activating this code will write the stack to flash on an error.
AntonLS 15:b86c4b798aa1 599 // This function should NOT be used in a final product.
AntonLS 15:b86c4b798aa1 600 // It is intended STRICTLY for development/debugging purposes.
AntonLS 15:b86c4b798aa1 601 // The flash write will happen EVEN if the radio is active, thus interrupting
AntonLS 15:b86c4b798aa1 602 // any communication.
AntonLS 15:b86c4b798aa1 603 // Use with care. Uncomment the line below to use.
AntonLS 15:b86c4b798aa1 604 // ble_debug_assert_handler(error_code, line_num, p_file_name);
AntonLS 15:b86c4b798aa1 605
AntonLS 15:b86c4b798aa1 606 // On assert, the system can only recover with a reset.
AntonLS 15:b86c4b798aa1 607 NVIC_SystemReset();
AntonLS 15:b86c4b798aa1 608 }
AntonLS 15:b86c4b798aa1 609
AntonLS 49:626e84ce5e52 610 uint32_t rndHW()
AntonLS 48:4c9551c826e9 611 {
AntonLS 48:4c9551c826e9 612 uint32_t rndVal;
AntonLS 48:4c9551c826e9 613 uint8_t bytes_available;
AntonLS 48:4c9551c826e9 614 for(;;)
AntonLS 48:4c9551c826e9 615 {
AntonLS 48:4c9551c826e9 616 sd_rand_application_bytes_available_get( &bytes_available );
AntonLS 48:4c9551c826e9 617 if( bytes_available >= 4 )
AntonLS 48:4c9551c826e9 618 {
AntonLS 48:4c9551c826e9 619 if( NRF_SUCCESS == sd_rand_application_vector_get( (uint8_t *)&rndVal, 4 ) )
AntonLS 48:4c9551c826e9 620 break;
AntonLS 48:4c9551c826e9 621 }
AntonLS 48:4c9551c826e9 622 }
AntonLS 48:4c9551c826e9 623
AntonLS 48:4c9551c826e9 624 return rndVal;
AntonLS 48:4c9551c826e9 625 }
AntonLS 48:4c9551c826e9 626
AntonLS 48:4c9551c826e9 627 static uint32_t rndZ = 0xCAFE, rndW = 0xF00D;
AntonLS 48:4c9551c826e9 628 // Seed for rnd()
AntonLS 49:626e84ce5e52 629 void srnd( uint32_t seed )
AntonLS 48:4c9551c826e9 630 {
AntonLS 48:4c9551c826e9 631 rndZ = 0xCAFE;
AntonLS 48:4c9551c826e9 632 rndW = seed;
AntonLS 48:4c9551c826e9 633 }
AntonLS 48:4c9551c826e9 634
AntonLS 48:4c9551c826e9 635 // Simple RNG -- Allows cross-platform compat if we want to run
AntonLS 48:4c9551c826e9 636 // an apples-to-apples freeform competition by using same seed.
AntonLS 49:626e84ce5e52 637 uint32_t rnd()
AntonLS 48:4c9551c826e9 638 {
AntonLS 48:4c9551c826e9 639 rndZ = 36969 * (rndZ & 0xffff) + (rndZ >>16);
AntonLS 48:4c9551c826e9 640 rndW = 18000 * (rndW & 0xffff) + (rndW >>16);
AntonLS 48:4c9551c826e9 641
AntonLS 48:4c9551c826e9 642 return ((rndZ <<16) + rndW);
AntonLS 48:4c9551c826e9 643 }
AntonLS 48:4c9551c826e9 644
AntonLS 0:28ca4562fe1a 645 /* EOF */