Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
Diff: main.cpp
- Revision:
- 22:f58d4b441aba
- Parent:
- 21:32f022efcc09
- Child:
- 24:ff54dec20f55
--- a/main.cpp Fri Dec 04 14:02:05 2015 +0000 +++ b/main.cpp Tue Dec 08 12:05:18 2015 +0000 @@ -103,11 +103,13 @@ #if BLENANO #define ADC_IN_BATT P0_6 +#define CHARGING_IN P0_29 #else #define ADC_IN_BATT P0_4 -DigitalIn notcharge( P0_2, PullUp ); +#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 @@ -161,7 +163,7 @@ GattService::UUID_BATTERY_SERVICE }; static uint8_t batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff, GattService::UUID_BATTERY_SERVICE >> 8, - 99, // Batt level TODO + 99, // Batt level 'T', 'X' }; // Custom ID trick UARTService *uartServicePtr; @@ -182,6 +184,74 @@ extern void radio_init(); extern void radio_loop(); +void setAdvData() +{ + ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | + GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); + ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); + + // Get MAC addr so we can create a device name using it. + ble.getAddress( pAdType, macAddr ); + sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); + + ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, + (const uint8_t *)deviceName, strlen(deviceName) ); + +// Moved to scan response packet to give more room in AD packet... +// ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, +// (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) ); + + 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" + + ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, + (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) ); +} + +void updateBatt( uint8_t pct ) +{ + static uint8_t prev; + static bool wasCharging; + + if( notcharge ) + { + if( pct > 100 ) pct = 100; + + // Use if we update quite often. +// if( !wasCharging && (abs( (int)prev -(int)pct ) < 3) ) return; // Don't register change of less than 3%. + + prev = pct; + wasCharging = false; + + } else + { + pct = 101; // Show 101% if charging. + wasCharging = true; // Force show when done charging. + } + + batt_and_id[2] = pct; + ble.clearAdvertisingPayload(); + setAdvData(); + ble.setAdvertisingPayload(); + + if( NULL != battServiceP ) battServiceP->updateBatteryLevel( pct ); +} + +#define BAT_TOP 793 // 3250 // 52000 +#define BAT_BOT 684 // 2800 // 44800 +uint8_t getBattLevel() +{ + int battI = (int)(batt * 1000.f); + // Normalize to usable range... + // Note: These levels should also give 0% if powered by programmer. + if( battI > BAT_TOP ) battI = BAT_TOP; // Fully charged reading ~3.85V + if( battI < BAT_BOT ) battI = BAT_BOT; // Device failing reading ~3.32V + battI-= BAT_BOT; + return (uint8_t)(battI*100 / (BAT_TOP-BAT_BOT)); +} + // True when connected to a phone bool connected = false; @@ -197,6 +267,8 @@ { connected = false; + updateBatt( getBattLevel() ); + // DEBUG( "Disconnected!\n\r" ); // DEBUG( "Restarting the advertising process\n\r" ); ble.startAdvertising(); @@ -477,63 +549,6 @@ } */ -void setAdvData() -{ - ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | - GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); - ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); - - // Get MAC addr so we can create a device name using it. - ble.getAddress( pAdType, macAddr ); - sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); - - ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, - (const uint8_t *)deviceName, strlen(deviceName) ); - -// Moved to scan response packet to give more room in AD packet... -// ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, -// (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) ); - - 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" - - ble.accumulateScanResponse( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)UARTServiceUUID_reversed, sizeof( UARTServiceUUID_reversed ) ); -} - -void updateBatt( uint8_t pct ) -{ - static uint8_t prev; - static bool wasCharging; - - if( notcharge ) - { - if( pct > 100 ) pct = 100; - if( !wasCharging && (abs( (int)prev -(int)pct ) < 3) ) return; // Don't register change of less than 3%. - prev = pct; - wasCharging = false; - - } else - { - pct = 101; // Show 101% if charging. - wasCharging = true; // Force show when done charging. - } - - batt_and_id[2] = pct; - ble.clearAdvertisingPayload(); - setAdvData(); - ble.setAdvertisingPayload(); - - if( NULL != battServiceP ) battServiceP->updateBatteryLevel( pct ); -} - -uint8_t getBattLevel() -{ - return (uint8_t)(batt * 125.f); -} - void periodicCallback( void ) { static int foo; @@ -543,9 +558,11 @@ #endif led1 = !led1; led2 = !led2; -// buzz = (foo & 2)>>1; // rts = !rts; +// if( 0 == (foo % 60) ){ led0 = 0; buzz = 0; } +// if( 2 == (foo % 60) ){ led0 = 1; buzz = 1; } + // Check battery level every 30s. if( 0 == (foo % 60) ) updateBatt( getBattLevel() ); }