Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
Diff: main.cpp
- Revision:
- 30:c60b0d52b067
- Parent:
- 28:8e74ddc4f70f
- Child:
- 31:a6110950f385
--- a/main.cpp Sun Jan 03 20:05:15 2016 +0000 +++ b/main.cpp Mon Jan 04 16:33:39 2016 +0000 @@ -109,7 +109,6 @@ #define CHARGING_IN P0_2 #endif AnalogIn batt( ADC_IN_BATT ); -DigitalIn notcharge( CHARGING_IN, PullUp ); // DigitalOut rts( RTS_PIN_NUMBER ); DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO @@ -128,7 +127,34 @@ // DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On (OK on Nano: Alt RxD) // DigitalOut led1( (trSwp ? P0_4 : P0_3), 0 ); // TA Baseboard High=On (And don't use P0_4 on Nano) -//DigitalOut buzz( P0_20, 1 ); // TA New Baseboard Low=On (OK on Nano: NC) +// DigitalOut buzz( P0_20, 1 ); // TA New Baseboard Low=On (OK on Nano: NC) + +int tickTock = 0; // Counter used by periodicCallback(). + +class ChgChg : public InterruptIn +{ + public: + ChgChg( PinName pin, PinMode pull=PullUp ) : InterruptIn( pin ) + { + mode( pull ); // Set pull mode + fall( this, &ChgChg::chargeStart ); // Attach ISR for fall + rise( this, &ChgChg::chargeStop ); // Attach ISR for rise + + led0 = !read(); + } + void chargeStart() + { + led0 = 1; + tickTock = 0; // Trigger batt level update. + } + void chargeStop() + { + led0 = 0; + tickTock = 0; // Trigger batt level update. + } +}; +ChgChg notcharge( CHARGING_IN, PullUp ); + // App timer, app scheduler, and pstorage are already setup by bootloader. @@ -173,12 +199,8 @@ // Buffer for holding data from the phone // to the device #define PHTODEV_BUF_LEN 30 /**/ -/// static char phoneToDev[200] = {0}; static char phoneToDev[PHTODEV_BUF_LEN] = {0}; -/// // Current position in the buffer -/// static int phoneToDevPos = 0; - extern TA ta; extern void radio_init(); @@ -193,11 +215,11 @@ // Get MAC addr so we can create a device name using it. ble.getAddress( pAdType, macAddr ); - #ifdef MASTER - sprintf( deviceName, "M%02X%02X", macAddr[1], macAddr[0] ); - #else +#ifdef MASTER + sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); +#else sprintf( deviceName, "S%02X%02X", macAddr[1], macAddr[0] ); - #endif +#endif ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName) ); @@ -209,7 +231,7 @@ ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof( uuid16_list ) ); ble.accumulateAdvertisingPayload( GapAdvertisingData::SERVICE_DATA, - (uint8_t *)batt_and_id, sizeof( batt_and_id ) ); // Unused batt lev + "TX" + (uint8_t *)batt_and_id, sizeof( batt_and_id ) ); // Batt lev + "TX" ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) ); @@ -222,6 +244,8 @@ if( notcharge ) { + if( wasCharging ) ta.beep_off(); // Alarm Clock TMP TODO remove once hardware is fixed. + if( pct > 100 ) pct = 100; // Use if we update quite often. @@ -230,8 +254,12 @@ prev = pct; wasCharging = false; - } else + } else // Charging { + // Plugged-in seems to add ~23% to level, so checking for 99% is like 76% unplugged. + // But I had a failure with a smaller gap, so reduced another 8%. + if( pct >= 91 ) ta.beep( 15000 ); // Alarm Clock TMP TODO remove once hardware is fixed. + pct = 101; // Show 101% if charging. wasCharging = true; // Force show when done charging. } @@ -276,7 +304,9 @@ // DEBUG( "Disconnected!\n\r" ); // DEBUG( "Restarting the advertising process\n\r" ); +// #ifdef MASTER ble.startAdvertising(); +// #endif ta.post_color(0); } @@ -297,47 +327,13 @@ return (err != BLE_ERROR_NONE); } -/// /* Writes the string given to the phone. -/// * -/// * @param *data - the string to send. -/// */ -/*** -static void writeToPhoneImpl(char *data) -{ - if (phoneP != NULL) - { - for (int i = 0; i < strlen(data); ++i) - { - phoneP->putchar(data[i]); - - // If we don't call maybeHandleWrite all hell breaks loose and - // the app crashes. :( - if (i != 0 && i % 10 == 0) - { - int counter = 0; - while(phoneP->maybeHandleWrite() == 0 && ++counter < 20) - { - wait_us(1); - } - } - } - - phoneP->maybeHandleWrite(); - } -} -***/ - -/// static char wtp_buff[150] = {0}; extern "C" void writeToPhone(char *format, ...) { va_list arg; va_start(arg, format ); -/// vsnprintf(wtp_buff, sizeof(wtp_buff), format, arg); /**/phoneP->vprintf( format, arg ); -/// writeToPhoneImpl(wtp_buff); - /**/// Also write same to serial port... TODO /**/ // pcfc.vprintf( format, arg ); @@ -380,9 +376,6 @@ if( 0 != bytesRead ) { -/// memcpy(phoneToDev+phoneToDevPos, txPayload, bytesRead); -/// phoneToDevPos += bytesRead; - /* DEBUG( "received %u bytes\n\r", bytesRead ); @@ -556,20 +549,20 @@ void periodicCallback( void ) { - static int foo; - foo++; -//#if BLENANO +#if BLENANO led0 = !led0; -//#endif +#endif led1 = !led1; led2 = !led2; // rts = !rts; -// if( 0 == (foo % 60) ){ led0 = 0; buzz = 0; } -// if( 2 == (foo % 60) ){ led0 = 1; buzz = 1; } +// if( 0 == (tickTock % 60) ){ led0 = 0; buzz = 0; } +// if( 2 == (tickTock % 60) ){ led0 = 1; buzz = 1; } // Check battery level every 30s. - if( 0 == (foo % 60) ) updateBatt( getBattLevel() ); + if( 0 == (tickTock % 60) ) updateBatt( getBattLevel() ); + + tickTock++; } /* @@ -627,17 +620,26 @@ if( NRF_SUCCESS != pstorageErr ) { - writeToPhone( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); - } - else + // pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); + /// writeToPhone( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); + + } else { pstorageErr = pstorage_setup(); - if( NRF_SUCCESS != pstorageErr ) writeToPhone( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); - else + if( NRF_SUCCESS != pstorageErr ) + { + // pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); + /// writeToPhone( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); + + } else { pstorageErr = pstorage_read_test(); - if( NRF_SUCCESS != pstorageErr ) writeToPhone( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); - else + if( NRF_SUCCESS != pstorageErr ) + { + // pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); + /// writeToPhone( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); + + } else { // In this test setup, we use the load callback to signal start to clear, // then we use the clear callback to signal start to write. @@ -660,7 +662,9 @@ uartServicePtr = &uartService; ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 132 ) ); +// #ifdef MASTER ble.startAdvertising(); +// #endif PhoneAppIO *phone = new PhoneAppIO( ble, uartServicePtr->getRXCharacteristicHandle(), @@ -669,8 +673,6 @@ phone->loopbackMode = LOOPBACK_MODE; phoneP = phone; -// DigitalOut *buzzPin = new DigitalOut(p20); -// *buzzPin = 1; setup(); radio_init(); tmr.start(); @@ -690,14 +692,6 @@ app_sched_execute(); // Attempt to get pstorage enqueued cmd callbacks. */ -/*** - if (phoneToDevPos > 0) - { - getRadioInput(phoneToDev, phoneToDevPos ); - phoneToDevPos = 0; - } -***/ - /**/ int bytes = MIN( PHTODEV_BUF_LEN, phoneP->readable() ); /**/ #ifdef MASTER @@ -705,15 +699,6 @@ #endif loop(); //radio_loop(); - -/*** - if (connected) - { - phoneP->maybeHandleWrite(); - } - - while( 0 <= phone->getchar() ); // Eat input. -***/ } }