football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
67:5650f461722a
Parent:
66:18c214707b0c
Child:
70:bd4b1e19a0c6
--- a/main.cpp	Tue Jan 19 07:25:15 2016 +0000
+++ b/main.cpp	Thu Feb 11 17:47:28 2016 +0000
@@ -60,17 +60,18 @@
 #include "types.h"
 #include "TA.h"
 #include "DataStore.hh"
+// #include "Radio.hh"
 
-#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
 
 #define LOOPBACK_MODE       0  // Loopback mode
 
-#if NEED_CONSOLE_OUTPUT
-#define DEBUG(...) { printf(__VA_ARGS__); }
-#else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+// #if NEED_CONSOLE_OUTPUT
+// #define DEBUG(...) { printf(__VA_ARGS__); }
+// #else
+// #define DEBUG(...) /* nothing */
+// #endif /* #if NEED_CONSOLE_OUTPUT */
 
 void loop();
 void setup();
@@ -210,7 +211,7 @@
 
 extern TA ta;
 
-extern void radio_init();
+//// extern void radio_init();
 extern void radio_loop(int mac);
 
 #ifdef MASTER
@@ -274,8 +275,9 @@
     } 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.
+          //  But I had a failure with a smaller gap, so reduced another 8%.  Also: Prevent alarm initially.
+          if( millis() > 5000 && /* pct < 100 && TMP to prevent alarm on disconnected battery */
+              pct >= 91 )  ta.beep( 15000 );  // Alarm Clock TMP TODO remove once hardware is fixed.
 
           if( !wasCharging )
               pct     = 101;   // Show 101% at start of charging.
@@ -482,7 +484,8 @@
     }
 }
 
-#define LOOPMS  5 /* 25 */
+#define LOOPUS  1250
+#define LOOPMS  (LOOPUS/1000) /* 2 */ /* 5 */ /* 25 */
 
 int main( void )
 {
@@ -520,6 +523,7 @@
     DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
     BatteryService battService( ble );
     battServiceP = &battService;
+
     updateBatt( getBattLevel() );
 
     /* Enable over-the-air firmware updates. Instantiating DFUService introduces a
@@ -542,32 +546,54 @@
     phoneP = phone;
     
     setup();
-    radio_init();
+///    radio_init();  //// Moved back to TA::initialize()
     tmr.start();
     
     int mac_addr = ((int)macAddr[0]);
     mac_addr    |= ((int)macAddr[1] << 8);
 
+    unsigned long lastMicros = micros();
     unsigned long lastMillis = millis();
+    unsigned long currMicros = lastMicros +LOOPUS;
     unsigned long currMillis = lastMillis +LOOPMS;
 
+    //// Test stuff...
+    unsigned long countLoop     = 0L;
+    unsigned long overage       = 0L;
+    ////
+
     // Main Loop
     while( true )  // for( uint32_t loop=1; ;loop++ )
     {
 //        ble.waitForEvent();
 
+        currMicros = micros();
         currMillis = millis();
-        
-        if( currMillis -lastMillis < LOOPMS )  
+
+        if( currMicros -lastMicros < LOOPUS )
         {
             continue;
         }
 
+        //// Test stuff...
+        overage += ((currMicros -lastMicros) -LOOPUS);
+        countLoop++;
+        if( (countLoop * LOOPUS) >= 30000000 )
+        {
+              if( Dbg )  writeToPhone( "Loop avg over: %.2fus\r", (float)overage / (float)countLoop );
+              // writeToPhone( "RSSI: %d\r", get_rssi() );
+//              read_all_regs();
+              countLoop = 0L;
+              overage   = 0L;
+        }
+        ////
+
+        lastMicros = currMicros;
         lastMillis = currMillis;
 
         periodicCallback();
 
-        toPhoneChk();  // Write any pending data to phone.  
+        toPhoneChk();  // Write any pending data to phone.
 
         if( is_master || Dbg )
         {
@@ -576,7 +602,7 @@
         }
 
         loop();
-        radio_loop(mac_addr);
+        radio_loop( mac_addr );
     }
 }