LoRa Protocol Ricezione

Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Revision:
15:a152246cf3be
Parent:
14:ab6eecd44b7a
--- a/main.cpp	Mon Apr 24 09:29:13 2017 +0000
+++ b/main.cpp	Fri Jul 21 17:46:16 2017 +0000
@@ -3,6 +3,7 @@
 #include "sx1272-hal.h"
 #include "debug.h"
 
+
 /* Set this flag to '1' to display debug messages on the console */
 #define DEBUG_MESSAGE   1
 
@@ -55,6 +56,9 @@
 DigitalOut led( LED1 );
 #endif
 
+Timer timer;
+    
+
 /*
  *  Global variables declarations
  */
@@ -86,8 +90,9 @@
  */
 SX1272MB2xAS Radio( NULL );
 
-const uint8_t PingMsg[] = "PING";
-const uint8_t PongMsg[] = "PONG";
+const uint8_t PingMsg[] = "LOR3";
+const uint8_t PongMs2[] = "LOR1";
+const uint8_t PongMs3[] = "LOR2";
 
 uint16_t BufferSize = BUFFER_SIZE;
 uint8_t Buffer[BUFFER_SIZE];
@@ -95,10 +100,14 @@
 int16_t RssiValue = 0.0;
 int8_t SnrValue = 0.0;
 
+int32_t TimerValue=0;
+int32_t TimeStampNew=0;
+int32_t TimeStampOld=0;
+
 int main( void ) 
 {
     uint8_t i;
-    bool isMaster = true;
+  
 
     debug( "\n\n\r     SX1272 Ping Pong Demo Application \n\n\r" );
 
@@ -162,148 +171,41 @@
     led = 0;
 
     Radio.Rx( RX_TIMEOUT_VALUE );
+    
+    timer.start();
 
     while( 1 )
     {
-        switch( State )
-        {
-        case RX:
-            if( isMaster == true )
-            {
-                if( BufferSize > 0 )
-                {
-                    if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
-                    {
-                        led = !led;
-                        debug( "...Pong\r\n" );
-                        // Send the next PING frame
-                        strcpy( ( char* )Buffer, ( char* )PingMsg );
-                        // We fill the buffer with numbers for the payload
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        wait_ms( 10 );
-                        Radio.Send( Buffer, BufferSize );
-                    }
-                    else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
-                    { // A master already exists then become a slave
-                        debug( "...Ping\r\n" );
-                        led = !led;
-                        isMaster = false;
-                        // Send the next PONG frame
-                        strcpy( ( char* )Buffer, ( char* )PongMsg );
-                        // We fill the buffer with numbers for the payload
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        wait_ms( 10 );
-                        Radio.Send( Buffer, BufferSize );
-                    }
-                    else // valid reception but neither a PING or a PONG message
-                    {    // Set device as master ans start again
-                        isMaster = true;
-                        Radio.Rx( RX_TIMEOUT_VALUE );
-                    }
-                }
-            }
-            else
-            {
-                if( BufferSize > 0 )
-                {
-                    if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
+      switch(State){
+         case RX:
+                    //timer.stop();
+  
+                    //timer.reset();
+                    //timer.start();
+                    if( strncmp( ( const char* )Buffer, ( const char* )PongMs2, 4 ) == 0 )
                     {
-                        led = !led;
-                        debug( "...Ping\r\n" );
-                        // Send the reply to the PING string
-                        strcpy( ( char* )Buffer, ( char* )PongMsg );
-                        // We fill the buffer with numbers for the payload
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        wait_ms( 10 );
-                        Radio.Send( Buffer, BufferSize );
-                    }
-                    else // valid reception but not a PING as expected
-                    {    // Set device as master and start again
-                        isMaster = true;
-                        Radio.Rx( RX_TIMEOUT_VALUE );
-                    }
-                }
-            }
-            State = LOWPOWER;
-            break;
-        case TX:
-            led = !led;
-            if( isMaster == true )  
-            {
-                debug( "Ping...\r\n" );
-            }
-            else
-            {
-                debug( "Pong...\r\n" );
-            }
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
-            break;
-        case RX_TIMEOUT:
-            if( isMaster == true )
-            {
-                // Send the next PING frame
-                strcpy( ( char* )Buffer, ( char* )PingMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );
-                Radio.Send( Buffer, BufferSize );
-            }
-            else
-            {
-                Radio.Rx( RX_TIMEOUT_VALUE );
-            }
-            State = LOWPOWER;
-            break;
-        case RX_ERROR:
-            // We have received a Packet with a CRC error, send reply as if packet was correct
-            if( isMaster == true )
-            {
-                // Send the next PING frame
-                strcpy( ( char* )Buffer, ( char* )PingMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );
-                Radio.Send( Buffer, BufferSize );
-            }
-            else
-            {
-                // Send the next PONG frame
-                strcpy( ( char* )Buffer, ( char* )PongMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );
-                Radio.Send( Buffer, BufferSize );
-            }
-            State = LOWPOWER;
-            break;
-        case TX_TIMEOUT:
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
-            break;
-        case LOWPOWER:
-            break;
-        default:
-            State = LOWPOWER;
-            break;
-        }
-    }
-}
+                    TimeStampNew=timer.read_ms();
+                    TimerValue=TimeStampNew-TimeStampOld;
+                    TimeStampOld=TimeStampNew;
+                    
+                    led = !led;
+                    debug( "I am LOR2\r\n" );
+                    printf("Data received from...%s\r\n",Buffer);
+                    printf("TimeStamp %d\n\r",TimeStampNew);
+                    printf("Time %d\n\r",TimerValue);
+                    Radio.Sleep( );
+                    Buffer[0] = 'A';
+                    }//end if1
+                    wait_ms(10);    
+                    Radio.Rx( RX_TIMEOUT_VALUE );//SETTA LA RADIO IN MODALITA' RICEZIONE
+          
+          break;
+          
+          default:
+          break;      
+                };
+    }//end while
+}//end main
 
 void OnTxDone( void )
 {