LP Long Distance IR Vision Robot

Dependencies:   max77650_charger_sample BufferedSerial SX1276GenericLib Adafruit-MotorShield NEO-6m-GPS MAX17055_EZconfig Adafruit_GFX USBDeviceHT Adafruit-PWM-Servo-Driver

Revision:
24:e8d03912f303
Parent:
23:f74a50977593
Child:
25:1a031add188a
--- a/SX1276GenericPingPong/GenericPingPong.cpp	Wed Jul 18 18:52:30 2018 +0000
+++ b/SX1276GenericPingPong/GenericPingPong.cpp	Wed Jul 18 21:25:17 2018 +0000
@@ -10,6 +10,7 @@
 #include "GenericPingPong.h"
 #include "sx1276-mbed-hal.h"
 #include "main.h"
+#include "global_buffers.h"
 
 #ifdef FEATURE_LORA
 
@@ -53,8 +54,7 @@
 
 #define RX_TIMEOUT_VALUE    3500	// in ms
 
-//#define BUFFER_SIZE       32        // Define the payload size here
-#define BUFFER_SIZE         64        // Define the payload size here
+//#define BUFFER_SIZE       64        // Define the payload size here
 
 /*
  *  Global variables declarations
@@ -91,8 +91,17 @@
 const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
 const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
 
-uint16_t BufferSize = BUFFER_SIZE;
-uint8_t *Buffer;
+/* determine the appropriate buffer sizes */
+#if   defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway
+    uint16_t BufferSizeTx = PAYLOAD_BUFFER_SIZE_MASTER_TO_SLAVE;
+	uint16_t BufferSizeRx = PAYLOAD_BUFFER_SIZE_SLAVE_TO_MASTER;
+#elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot
+	uint16_t BufferSizeTx = PAYLOAD_BUFFER_SIZE_SLAVE_TO_MASTER;
+	uint16_t BufferSizeRx = PAYLOAD_BUFFER_SIZE_MASTER_TO_SLAVE;
+#endif
+
+uint8_t *BufferTx;
+uint8_t *BufferRx;
 
 DigitalOut *led3;
 
@@ -110,7 +119,8 @@
     led3 = led;
 #endif
     
-    Buffer = new  uint8_t[BUFFER_SIZE];
+    BufferTx = new  uint8_t[BufferSizeTx];
+    BufferRx = new  uint8_t[BufferSizeRx];
     *led3 = 1;
 
 #ifdef B_L072Z_LRWAN1_LORA
@@ -231,21 +241,21 @@
 //        	*led3 = 0;
         if( isMaster == true )
         {
-            if( BufferSize > 0 )
+            if( BufferSizeRx > 0 )
             {
-                if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 )
+                if( memcmp(BufferRx, PongMsg, sizeof(PongMsg)) == 0 )
                 {
 //                        *led = !*led;
                     dprintf( "...Pong" );
                     // Send the next PING frame            
-                    memcpy(Buffer, PingMsg, sizeof(PingMsg));
+                    memcpy(BufferTx, PingMsg, sizeof(PingMsg));
                     // We fill the buffer with numbers for the payload 
-                    for( i = sizeof(PingMsg); i < BufferSize; i++ )
+                    for( i = sizeof(PingMsg); i < BufferSizeTx; i++ )
                     {
-                        Buffer[i] = i - sizeof(PingMsg);
+                        BufferTx[i] = i - sizeof(PingMsg);
                     }
                     wait_ms( 10 ); 
-                    Radio->Send( Buffer, BufferSize );
+                    Radio->Send( BufferTx, BufferSizeTx );
                 }
 /*                else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
                 { // A master already exists then become a slave
@@ -265,28 +275,28 @@
 */
                 else // valid reception but neither a PING or a PONG message
                 {    // Set device as master ans start again
-                    isMaster = true;
+                    //isMaster = true;
                     Radio->Rx( RX_TIMEOUT_VALUE );
                 }
             }
         }
         else
         {
-            if( BufferSize > 0 )
+            if( BufferSizeRx > 0 )
             {
-                if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
+                if( memcmp(BufferRx, PingMsg, sizeof(PingMsg)) == 0 )
                 {
 //                        *led = !*led;
                     dprintf( "...Ping" );
                     // Send the reply to the PING string
-                    memcpy(Buffer, PongMsg, sizeof(PongMsg));
+                    memcpy(BufferTx, PongMsg, sizeof(PongMsg));
                     // We fill the buffer with numbers for the payload 
-                    for( i = sizeof(PongMsg); i < BufferSize; i++ )
+                    for( i = sizeof(PongMsg); i < BufferSizeTx; i++ )
                     {
-                        Buffer[i] = i - sizeof(PongMsg);
+                        BufferTx[i] = i - sizeof(PongMsg);
                     }
                     wait_ms( 10 );  
-                    Radio->Send( Buffer, BufferSize );
+                    Radio->Send( BufferTx, BufferSizeTx );
                 }
                 else // valid reception but not a PING as expected
                 {    // Set device as master and start again
@@ -314,13 +324,13 @@
         if( isMaster == true )
         {
             // Send the next PING frame
-            memcpy(Buffer, PingMsg, sizeof(PingMsg));
-            for( i = sizeof(PingMsg); i < BufferSize; i++ )
+            memcpy(BufferTx, PingMsg, sizeof(PingMsg));
+            for( i = sizeof(PingMsg); i < BufferSizeTx; i++ )
             {
-                Buffer[i] = i - sizeof(PingMsg);
+                BufferTx[i] = i - sizeof(PingMsg);
             }
             wait_ms( 10 ); 
-            Radio->Send( Buffer, BufferSize );
+            Radio->Send( BufferTx, BufferSizeTx );
         }
         else
         {
@@ -333,24 +343,24 @@
         if( isMaster == true )
         {
             // Send the next PING frame
-            memcpy(Buffer, PingMsg, sizeof(PingMsg));
-            for( i = 4; i < BufferSize; i++ )
+            memcpy(BufferTx, PingMsg, sizeof(PingMsg));
+            for( i = 4; i < BufferSizeTx; i++ )
             {
-                Buffer[i] = i - 4;
+                BufferTx[i] = i - 4;
             }
             wait_ms( 10 );  
-            Radio->Send( Buffer, BufferSize );
+            Radio->Send( BufferTx, BufferSizeTx );
         }
         else
         {
             // Send the next PONG frame
-            memcpy(Buffer, PongMsg, sizeof(PongMsg));
-            for( i = sizeof(PongMsg); i < BufferSize; i++ )
+            memcpy(BufferTx, PongMsg, sizeof(PongMsg));
+            for( i = sizeof(PongMsg); i < BufferSizeTx; i++ )
             {
-                Buffer[i] = i - sizeof(PongMsg);
+                BufferTx[i] = i - sizeof(PongMsg);
             }
             wait_ms( 10 );  
-            Radio->Send( Buffer, BufferSize );
+            Radio->Send( BufferTx, BufferSizeTx );
         }
         State = LOWPOWER;
         break;
@@ -378,8 +388,8 @@
 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
 {
     Radio->Sleep( );
-    BufferSize = size;
-    memcpy( Buffer, payload, BufferSize );
+    BufferSizeRx = size;
+    memcpy( BufferRx, payload, BufferSizeRx );
     State = RX;
     if (DEBUG_MESSAGE)
         dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
@@ -399,7 +409,7 @@
 {
 //    *led3 = 0;
     Radio->Sleep( );
-    Buffer[BufferSize-1] = 0;
+    BufferRx[BufferSizeRx-1] = 0;
     State = RX_TIMEOUT;
     if (DEBUG_MESSAGE)
         dprintf("> OnRxTimeout");