Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
69:a3295b74209e
Parent:
68:0c96bb3d73a7
Child:
70:bd4b1e19a0c6
--- a/Radio.cpp	Thu Feb 11 19:28:01 2016 +0000
+++ b/Radio.cpp	Fri Feb 12 21:13:14 2016 +0000
@@ -157,13 +157,20 @@
         payload[6] = (byte)(em.mac >> 16);
         payload[5] = (byte)(em.mac >> 24);
 
-        radio.send( em.m.cone, payload, sizeof(payload), false );  //// NO ACK req.
+        radio_send_raw( em.m.cone, payload, sizeof(payload), false );  //// NO ACK req.
     /// radio.send(em.m.cone, payload, sizeof(payload));
         
         if( Dbg )  writeToPhone("SND: %d %d 0x%x\r\n", em.m.cone, node_id, em.mac);
     }
 }
 
+void radio_send_raw( uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK )  ////
+{
+    radio.send( toAddress, buffer, bufferSize, requestACK );
+
+//    radio_ensure_rx_mode();
+}
+
 void radio_send( Message *m, bool requestACK )  ////
 {
     static byte payload [6] = {0};
@@ -184,8 +191,7 @@
 
     payload[5] = (byte)++TA::msg_id;  //// Msg ID,  was: = (byte)'%';
 
-    radio.send( m->cone, payload, sizeof(payload), requestACK );  ////
-    radio_receive_complete();  //// Prevent radio from going to sleep.
+    radio_send_raw( m->cone, payload, sizeof(payload), requestACK );
 }
 
 void stompage_check()
@@ -204,11 +210,12 @@
 
 bool radio_receive(Message *m)
 {
-   bool retval = false;
+   bool retval         = false;
+   bool ensure_rx_mode = false;
 
    //// For msg id feature...
    static prev_message mPrev;
-   static long    lastMsgAt = millis();
+   static long lastMsgAt = millis();
 
     Message lm;
     extended_message em;
@@ -222,21 +229,16 @@
           if( radio.TARGETID == datastore_node_id() )  //// Check for stompage...
           {
               stompage_check();
-
-              if( radio.ACK_RECEIVED )
-              {
-                  // We've received an ACK between retries.
-                  retval = true;
-                  break;
-              }
           }
           if( !get_crc_ok() )  // CRC error will be rechecked/sent from caller.
           {
               retval = true;
               break;
           }
-          if( radio.DATALEN < 6 )
+          if( (radio.DATALEN < 6) && !radio.ACK_RECEIVED )
           {
+              ensure_rx_mode = true;
+
               RA_DEBUG( "Warn: Short packet\r\n" );
               RA_DEBUG( "Warn: Data len %d\r\n", radio.DATALEN );
               RA_DEBUG( "Warn: Cmd %c, Frm %d\r\n", radio.DATA[0], radio.SENDERID );
@@ -279,27 +281,36 @@
           {
               memcpy(m, &lm, sizeof(Message));
 
-              //// Check if sender missed an ACK (Got duplicate of last msg, so ignore dup.)
-              if( (millis() -lastMsgAt < 100) &&    //// Only consider as dup if w/in 100ms of prev.
-                  (mPrev.m.cone == m->cone)   &&
-                  (mPrev.msgid  == radio.DATA[5]) )
-              {                                     ////  ASS-U-ME [for now] last msg is from same cone.
-                  RA_DEBUG( "Ignored dup cmd\r\n" );
-                  radio_send_ack();
-                  break;
+              if( !radio.ACK_RECEIVED )
+              {
+                //// Check if sender missed an ACK (Got duplicate of last msg, so ignore dup.)
+                if( (millis() -lastMsgAt < 100) &&    //// Only consider as dup if w/in 100ms of prev.
+                    (mPrev.m.cone == m->cone)   &&
+                    (mPrev.msgid  == radio.DATA[5]) )
+                {                                     ////  ASS-U-ME [for now] last msg is from same cone.
+                    RA_DEBUG( "Ignored dup cmd\r\n" );
+                    radio_send_ack();
+                    ensure_rx_mode = true;
+                    break;
+                }
+
+                memcpy( &mPrev.m, m, sizeof( Message ) );  ////
+                mPrev.msgid = radio.DATA[5];               ////
+                lastMsgAt = millis();                      ////
+
+                if( Dbg )  writeToPhone("GM: %d %c %d\r\n", radio.SENDERID, m->command, m->value);
               }
-
-              memcpy( &mPrev.m, m, sizeof( Message ) );  ////
-              mPrev.msgid = radio.DATA[5];               ////
-              lastMsgAt = millis();                      ////
-
-              if( Dbg )  writeToPhone("GM: %d %c %d\r\n", radio.SENDERID, m->command, m->value);
               retval = true;
           }
       }
 
     } while( false );
 
+    if( ensure_rx_mode )
+    {
+        radio_ensure_rx_mode();  // Will stomp/erase payload data.
+    }
+
     if( retval )
     {
         if( get_crc_ok() )
@@ -311,19 +322,35 @@
     return  retval;
 }
 
+void radio_ensure_rx_mode()
+{
+    if( RF69_MODE_RX != radio._mode )
+    {
+        // Either do this or change the setMode in RFM69.cpp at end of sendFrame() to  setMode(RF69_MODE_RX);
+        radio.receiveDone();  //// Prevent radio from going to standby or sleep. (Should never return true here.)
+    }
+}
+
 void radio_send_ack()  ////
 {
     radio.sendACK( "K", 1 );
-    radio_receive_complete();  //// Prevent radio from going to sleep.
+
+//    radio_ensure_rx_mode();
 }
 
 bool radio_ack_received( int cone )
 {
     bool retval = radio.ACKReceived(cone);
 
-    if( retval && (radio.TARGETID == datastore_node_id()) )
+    if( retval )
     {
-        stompage_check();
+        if( radio.TARGETID == datastore_node_id() )
+        {
+            stompage_check();
+        }
+
+        // Note if we ever want to retrieve data from ACK packet: This stomps the packet data.
+//        radio_ensure_rx_mode();
     }
 
     return  retval;