Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Files at this revision

API Documentation at this revision

Comitter:
AntonLS
Date:
Mon Jan 11 16:34:46 2016 +0000
Parent:
44:4ad6133987ed
Parent:
45:1eb335c00cb2
Child:
47:c66d2995f957
Commit message:
Freeform improvements, etc.

Changed in this revision

TA.cpp Show annotated file Show diff for this revision Revisions of this file
TA.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
proto_code.cpp Show annotated file Show diff for this revision Revisions of this file
types.h Show annotated file Show diff for this revision Revisions of this file
--- a/TA.cpp	Sat Jan 09 21:46:34 2016 +0000
+++ b/TA.cpp	Mon Jan 11 16:34:46 2016 +0000
@@ -163,7 +163,7 @@
         //neo->setPixel(i, rgb);
     } 
     
-    buzzPin = 1;
+    /// buzzPin = 1;
 }
 
 void TA::mask_color(uint32_t rgb)
@@ -176,11 +176,14 @@
   //enable_1 = (mask & 0x01)?1:0;
   //enable_2 = (mask & 0x02)?1:0;
   //enable_3 = (mask & 0x04)?1:0;
-  
-  for (int i = 0; i <= leds_per_strip; ++i)
+
+  if( mask & TOUCHLIGHTS )  // Even though we now only use one sensor, we still have DARK ALERT.
+    for (int i = 0; i <= leds_per_strip; ++i)
     {
           neopixel_set_color_and_show(&m_strip, i, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
-    } 
+    }
+  else
+    neopixel_clear(&m_strip);
 }
 
 void TA::beep(uint16_t ms)
@@ -222,7 +225,7 @@
     pulsing = true;
     pulse_start = millis();
     pulse_period = period;
-    pulse_on = on_time;
+    pulse_on = (on_time < 126 ? 126 : on_time);
     pulse_duration = ms;   
 }
 
@@ -272,16 +275,11 @@
   return false;
 }
 
-void TA::resetTouchIfStuck()
+void TA::resetTouch()
 {
-  if(tripped())
-  {
     cap_enable = 1;
     wait_ms(100);
     cap_enable = 0;
-    writeToPhone("TCP\r\n");
-    //Serial.println(F("toggled cap sense power"));
-  }
 }
 
 void TA::spin(void)
@@ -306,7 +304,12 @@
         last_touch = millis();  
   }
 
-  resetTouchIfStuck();
+  if(tripped())
+  {
+    resetTouch();
+    writeToPhone("TCP\r\n");
+    //Serial.println(F("toggled cap sense power"));
+  }
 
   if(powering_up2)
   {
@@ -358,6 +361,7 @@
     }
   }
 
+#if 1  // Foo
 /*
   if(!waiting_for_ack && radio_receive_complete())
   {
@@ -473,11 +477,15 @@
       }
     }
   }
+#endif  // Foo
 }
 
 bool TA::activated(void)
 {
-    return (buttons() & mask)?true:false; 
+    return  ((buttons() & mask) ||
+             /* Default to enabling activation on disabled--Except if mask has DIS_ON_DARK set */
+             (!(mask & TOUCHLIGHTS) != !!(mask & DIS_ON_DARK))
+            ) ? true : false; 
 }
 
 bool TA::tripped(void)
--- a/TA.h	Sat Jan 09 21:46:34 2016 +0000
+++ b/TA.h	Mon Jan 11 16:34:46 2016 +0000
@@ -7,10 +7,14 @@
 #include "neopixel.h"
 
 #define DATA_SIZE 24
+
+#define MAX_LEN   24  // buffer input commands up to this length
+
 #define SILENT 0x20
 
-#define TOUCHLIGHTS  0x7
+#define TOUCHLIGHTS  0x7                                      /* We'll use bit 2 for the power button */
 #define DEFTOUCHMASK (TOUCHLIGHTS & (5 | 2 /* test/bott */))  /* Note:  No longer using "bottom" touch sensor/lights */
+#define DIS_ON_DARK 0x40                                      /* Used for disabling touch sensor(s) when in dark mode */
 
 struct Message 
 {
@@ -124,7 +128,7 @@
 bool recieve(Message *m);
 void spin(void);
 bool activated(void);
-void resetTouchIfStuck();
+void resetTouch();
 bool tripped(void);
 uint8_t buttons(void);
 void setMask(uint8_t the_mask);
--- a/main.cpp	Sat Jan 09 21:46:34 2016 +0000
+++ b/main.cpp	Mon Jan 11 16:34:46 2016 +0000
@@ -71,6 +71,12 @@
 #define DEBUG(...) /* nothing */
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
+#ifdef MASTER
+static bool is_master = true;
+#else
+static bool is_master = false;
+#endif
+
 void loop();
 void setup();
 void getRadioInput(char *ibuffer, int size);
@@ -205,8 +211,7 @@
 
 // Buffer for holding data from the phone
 // to the device
-#define PHTODEV_BUF_LEN    30  /**/
-static char phoneToDev[PHTODEV_BUF_LEN] = {0};
+static char phoneToDev[MAX_LEN] = {0};
 
 extern TA ta;
 
@@ -222,11 +227,7 @@
     // Get MAC addr so we can create a device name using it.
     ble.getAddress( pAdType, macAddr );
     
-#ifdef MASTER
-    sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
-#else
-    sprintf( deviceName, "S%02X%02X", macAddr[1], macAddr[0] );
-#endif
+    sprintf( deviceName, "%c%02X%02X", (is_master?'T':'S'), macAddr[1], macAddr[0] );
 
     ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
                                       (const uint8_t *)deviceName, strlen(deviceName) );
@@ -258,7 +259,7 @@
         if( wasCharging )
         {
             pct      = 102;   // Show 102% at charger unplug.
-            tickTock = 25;    // Cause another normal update in 5 seconds.
+            tickTock = /* 25 */ 50;    // Cause another normal update in 5s (10 half s).
 
         } else
           {
@@ -323,9 +324,9 @@
 
     // DEBUG( "Disconnected!\n\r" );
     // DEBUG( "Restarting the advertising process\n\r" );
-// #ifdef MASTER
+
     ble.startAdvertising();
-// #endif
+
     ta.post_color(0);
 }
 
@@ -569,7 +570,7 @@
 void periodicCallback( void )
 {
     static int callCnt;
-    if( (callCnt++ % 5) != 0 )  return;
+    if( (callCnt++ % /* 5 */ 4 ) != 0 )  return;
     callCnt = 1;
 
 #if BLENANO
@@ -580,7 +581,7 @@
 //    rts  = !rts;
 
     // Check battery level every 30s.
-    if( 0 == (tickTock % 30) )  updateBatt( getBattLevel() );
+    if( 0 == (tickTock % /* 30 */ 60 ) )  updateBatt( getBattLevel() );
 
     tickTock++;
 }
@@ -601,7 +602,7 @@
 int main( void )
 {
     Ticker ticker;
-    ticker.attach( periodicCallback, 0.2 /** 0.5 **/ /* 1 */ );
+    ticker.attach( periodicCallback, 0.125 /** 0.2 **/ /** 0.5 **/ /* 1 */ );
 
   /*
     // Thread thread( led_thread );
@@ -681,10 +682,9 @@
     UARTService uartService( ble );
     uartServicePtr = &uartService;
 
-    ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 132 ) );
-// #ifdef MASTER
+    ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( is_master ? 132 : 5000 ) );
+
     ble.startAdvertising();
-// #endif
 
     PhoneAppIO *phone = new PhoneAppIO( ble,
                                         uartServicePtr->getRXCharacteristicHandle(),
@@ -715,11 +715,11 @@
         app_sched_execute();  // Attempt to get pstorage enqueued cmd callbacks.
       */
 
-/**/    int bytes = MIN( PHTODEV_BUF_LEN, phoneP->readable() );
-/**/
-#ifdef MASTER    
-    getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
+#ifdef MASTER
+/**/    int bytes = MIN( MAX_LEN, phoneP->readable() );
+        getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) );
 #endif
+
         loop();
         radio_loop(mac_addr);
     }
--- a/proto_code.cpp	Sat Jan 09 21:46:34 2016 +0000
+++ b/proto_code.cpp	Mon Jan 11 16:34:46 2016 +0000
@@ -10,7 +10,6 @@
 #define ENABLE_3 p8
 
 #define DEBUG_BAUD  57600 //57600
-#define MAX_LEN          24  // buffer input commands up to this length
 #define NUM_CONES         3
 #define STATIONS         20  // max length of a pattern
 #define SEQUENCES         9  // number of patterns to store
@@ -18,10 +17,10 @@
 #define TRILAT_CONE      99
 
 #define DEBOUNCE_MS 100
-#define LIGHTS TOUCHLIGHTS
-#define FAKEOUT 0x08
-#define FAIL_QUICK 0x10
-#define SILENT 0x20
+#define LIGHTS      TOUCHLIGHTS
+#define FAKEOUT     0x08
+#define FAIL_QUICK  0x10
+#define SILENT      0x20
 #define GRACE_PERIOD 3000
 
 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need //////////////////////DEBUG messages on the console;
@@ -182,7 +181,7 @@
 static void slave_setup()
 {
     ta.initialize(NODE_ID);
-    ta.beep(250);
+///    ta.beep(250);
 }
 #endif
 
@@ -206,6 +205,8 @@
     static State_t last_state = IDLE;
     static int counter = 0;
   
+    ta.spin();
+
     if(last_state != state)
     {
         if(state == ACTIVE_TARGET)
@@ -217,11 +218,8 @@
         if(state == SUCCESS)
             writeToPhone("State is SUCCESS\r\n");
     }
-     
     last_state = state;
 
-    ta.resetTouchIfStuck();
-  
     char message = 'n';
     timer = millis() - start;
   
@@ -255,17 +253,15 @@
   
   if(message == 'x')
   {
-    //a3 = 0;
-    wait_ms(100);
-    //a3 = 1;
+      ta.resetTouch();
   }
   
   if(message == 'f')
   { // Fail
      //serial.printf("Fail!\n");
      writeToPhone("FAIL\r\n");
-     ta.post_color(fail_colour);
-     //ta.pulse(25,200,3000,0xFF0000);
+///  ta.post_color(fail_colour);
+     ta.pulse(25,200,3000,fail_colour);
      state = FAIL;
   }
   
@@ -282,8 +278,8 @@
     start = millis();
     warning = false;
     penalty = false;
-    ta.post_color(active_colour);
-    //ta.pulse(50,750,0L,0x00FF00);
+/// ta.post_color(active_colour);
+    ta.pulse(50,750,~0L,active_colour);
     state = ACTIVE_TARGET;
     //serial.printf("fakeout, fail_quick\n");
     //serial.printf("%d",fakeout);
@@ -329,7 +325,7 @@
         m->command = 'd';
         m->cone = CONTROL_CONE;
         ta.send(m);
-        ta.post_color(success_colour);
+///     ta.post_color(success_colour);
         ta.pulse_off();
         state = IDLE;
         //serial.printf("Done!\n");
@@ -339,16 +335,16 @@
         writeToPhone("PEN\r\n");
         //serial.printf("Penalty!\n");
         penalty = true;
-        //ta.pulse(350,600,~0L,0xFF00FF);
-        ta.post_color(pen_colour);
+        ta.pulse(350,600,~0L,pen_colour);
+///     ta.post_color(pen_colour);
       }
       else if(timer > ((timeout*3)/4) && !warning)
       {
             writeToPhone("WARN\r\n");
         //serial.printf("Warning!\n");
         warning = true;
-        //ta.pulse(350,750,~0L,0xFFFF00);
-        ta.post_color(warn_colour);
+        ta.pulse(350,750,~0L,warn_colour);
+///     ta.post_color(warn_colour);
       }
       break;
     case IDLE:
@@ -408,7 +404,7 @@
       
       mask = 0x07;
       ta.setMask(mask & LIGHTS);
-      //timeout = ~0;
+      timeout = ~0;
       
       return;
   }
@@ -566,16 +562,16 @@
       
       warning = false;
       penalty = false;
-      ta.post_color(active_colour);
-      //ta.pulse(50,750,~0L,0x00FF00);
-      //if(!(mask & SILENT))ta.pulse(50,750,~0L,0c00FF00);
+///   ta.post_color(active_colour);
+      ta.pulse(50,750,~0L,active_colour);
+      //if(!(mask & SILENT))ta.pulse(50,750,~0L,active_colour);
     }
     if(timer >= timeout)
     {
       if(!penalty)
       {
-        ta.post_color(pen_colour);
-        //ta.pulse(50,325,~0L,0xFF00FF);
+///     ta.post_color(pen_colour);
+        ta.pulse(50,325,~0L,pen_colour);
         
         penalty = true;
       }
@@ -584,8 +580,8 @@
     else if(timer > ((timeout*3)/4) && !warning)
     {
       warning = true;
-      ta.post_color(warn_colour);
-      //ta.pulse(50,750,~0L,0xFFFF00);
+///   ta.post_color(warn_colour);
+      ta.pulse(50,750,~0L,warn_colour);
     }
 
     if(ta.activated() || ((timer >= timeout) && fakeout))
@@ -621,8 +617,8 @@
       DEBUG("\n");
       DEBUG("Timeout\n");
       //ta.post_color();
-      //ta.pulse(25,200,3000,0xFF0000);
-      ta.post_color(fail_colour);
+      ta.pulse(25,200,3000,fail_colour);
+///   ta.post_color(fail_colour);
       
       failCones(); 
       //ta.fail();
@@ -668,9 +664,10 @@
   int buffer_counter = 0;
   static char parameter = '_';
   static char buffer[MAX_LEN + 1];
+  static char trashArea[MAX_LEN + 1];
   int value = 0;
   char *endp = NULL;
-  
+
   // listen for commands coming over bluetooth
   while (buffer_counter < size)
   {
@@ -845,15 +842,15 @@
   case 'B':  // After setting bits, echo back station data.
     l = (uint8_t)value;
     masks[station-1] = l;
-    writeToPhone( "%c%02d;C%02d;T%05d;B%02x\r\n", (1==station) ? 'P'                 : 'S',
+    writeToPhone( "%c%02u;C%02u;T%05u;B%02x\r\n", (1==station) ? 'P'                 : 'S',
                                                   (1==station) ?  active_sequence +1 : station,
                                                   cones[station-1], times[station-1], l );
     break;
   case 'q':
     state_p = IDLE_P;
     new_state = true; // force state reporting, even if we're already in IDLE
-    //ta.pulse_off();
-    //clearCones();
+    ta.pulse_off();  /// //
+    clearCones();    /// //
     break;
   case 'r':
     //Serial.println(F(""));
@@ -861,12 +858,12 @@
     writeToPhone("Pattern is %d:\r\n", active_sequence+1);  // PROTOCOL optimization
     for(int i=0; i<STATIONS; i++){
       // writeToPhone("Station %d: cone %d, ", i+1, cones[i]);
-      writeToPhone("S%02d,C%02d", i+1, cones[i]);           // PROTOCOL optimization
+      writeToPhone("S%02u,C%02u", i+1, cones[i]);           // PROTOCOL optimization
       split = times[i];
       // printMsAsSeconds(split);               // Removed for PROTOCOL optimization
       //Serial.print(F("s timeout, lights: "));
       // writeToPhone("s timeout, config bits: ");
-      writeToPhone(",T%05d", split);                        // PROTOCOL optimization
+      writeToPhone(",T%05u", split);                        // PROTOCOL optimization
       l = masks[i];
       /*
       if(l<0b10000000)
@@ -928,9 +925,7 @@
     break;
    case 'x':
      resetSensors();
-     //digitalWrite(A3, LOW);
-     //delay(100);
-     //digitalWrite(A3, HIGH);
+     ta.resetTouch();
      break;
      
    case 'z':