football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
20:d6c6099b60be
Parent:
19:afcbb425b3cf
Child:
21:32f022efcc09
--- a/main.cpp	Tue Dec 01 16:03:15 2015 +0000
+++ b/main.cpp	Fri Dec 04 09:46:49 2015 +0000
@@ -111,8 +111,7 @@
 int foo = (wait( 0.1 ), 0);
 
 // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins.
-static DigitalOut led0( P0_19, 1 );           // TA New Baseboard  LED High=On / Buzzer Low=En ("STATUS")  (OK on Nano: LED Low=On)
-                                              // Note: TA New Baseboard LED off only if Buzzer is sounding.
+static DigitalOut led0( P0_19, 1 );           // TA New Baseboard  LED High=On ("STATUS")  (OK on Nano: LED Low=On)
 static DigitalOut led1( P0_21, 0 );           // TA New Baseboard  High=On  (OK on Nano: NC)
 DigitalOut led2( P0_22, 1 );                  // TA New Baseboard  High=On  (OK on Nano: NC)
 
@@ -152,13 +151,14 @@
 Gap::addr_type_t *pAdType;
 static const uint16_t uuid16_list[] = { GattService::UUID_DEVICE_INFORMATION_SERVICE,
                                         GattService::UUID_BATTERY_SERVICE };
-static const uint8_t  batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff,
+static       uint8_t  batt_and_id[] = { GattService::UUID_BATTERY_SERVICE & 0xff,
                                         GattService::UUID_BATTERY_SERVICE >> 8,
                                         99,          // Batt level TODO
                                         'T', 'X' };  // Custom ID trick
 
-UARTService *uartServicePtr;
-PhoneAppIO  *phoneP;
+UARTService    *uartServicePtr;
+PhoneAppIO     *phoneP;
+BatteryService *battServiceP;
 
 // Buffer for holding data from the phone
 // to the device
@@ -469,6 +469,49 @@
 }
 */
 
+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;
+
+    if( pct > 100 )  pct = 100;
+    if( abs( (int)prev -(int)pct ) < 3 )  return;  // Don't register change of less than 3%.
+
+    batt_and_id[2] = pct;
+    ble.clearAdvertisingPayload();
+    setAdvData();
+    ble.setAdvertisingPayload();
+
+    if( NULL != battServiceP )  battServiceP->updateBatteryLevel( pct );
+
+    prev = pct;
+}
+
 void periodicCallback( void )
 {
     static int foo;
@@ -480,6 +523,9 @@
     led2 = !led2;
 //    buzz = (foo & 2)>>1;
 //    rts  = !rts;
+
+    // Temp dummy battery level data.  TODO
+    if( 8 == (foo & 15) )  updateBatt( (uint8_t)foo & 0x7f );
 }
 
 /*
@@ -518,14 +564,7 @@
     ble.onDataSent( onDataSent );
 
     /* setup advertising */
-    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] );
+    setAdvData();
 
     /////pcfc.printf( "\r\nNano nano!   I am \"%s\"\r\n", deviceName );
 
@@ -560,26 +599,12 @@
       }
     */
 
-    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 ) );
-
-    ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 200 ) );
+    ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 132 ) );
     ble.startAdvertising();
 
     DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
     BatteryService battService( ble );
+    battServiceP = &battService;
 
     /* Enable over-the-air firmware updates. Instantiating DFUService introduces a
      * control characteristic which can be used to trigger the application to