Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Revision:
15:77a116408c4b
Parent:
14:ab6eecd44b7a
Child:
16:0cf0cb508f19
--- a/main.cpp	Mon Apr 24 09:29:13 2017 +0000
+++ b/main.cpp	Fri Jun 01 09:32:13 2018 +0000
@@ -10,16 +10,17 @@
 #define USE_MODEM_LORA  1
 #define USE_MODEM_FSK   !USE_MODEM_LORA
 
-#define RF_FREQUENCY                                    868000000 // Hz
+#define RF_FREQUENCY                                    850000000 // Hz frequenza centrale  868
 #define TX_OUTPUT_POWER                                 14        // 14 dBm
 
 #if USE_MODEM_LORA == 1
 
-    #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
+    #define LORA_BANDWIDTH                              2         //banda  
+                                                                  // [0: 125 kHz, 
                                                                   //  1: 250 kHz,
                                                                   //  2: 500 kHz,
                                                                   //  3: Reserved]
-    #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
+    #define LORA_SPREADING_FACTOR                       12         // [SF7..SF12]
     #define LORA_CODINGRATE                             1         // [1: 4/5,
                                                                   //  2: 4/6,
                                                                   //  3: 4/7,
@@ -55,6 +56,8 @@
 DigitalOut led( LED1 );
 #endif
 
+#define waitTime     1000                       //Wait Time
+
 /*
  *  Global variables declarations
  */
@@ -86,8 +89,8 @@
  */
 SX1272MB2xAS Radio( NULL );
 
-const uint8_t PingMsg[] = "PING";
-const uint8_t PongMsg[] = "PONG";
+const uint8_t PingMsg[] = "alfa";       //messaggio Ping
+const uint8_t PongMsg[] = "beta";
 
 uint16_t BufferSize = BUFFER_SIZE;
 uint8_t Buffer[BUFFER_SIZE];
@@ -95,10 +98,21 @@
 int16_t RssiValue = 0.0;
 int8_t SnrValue = 0.0;
 
+/************************/
+DigitalIn button1(PC_13);
+Timer t;
+char tempo[5];
+float somma, tmisura;
+int misure=10;
+
+bool oktx=1;
+/************************/
+
+
 int main( void ) 
 {
     uint8_t i;
-    bool isMaster = true;
+    bool isMaster = true;       //parte come master
 
     debug( "\n\n\r     SX1272 Ping Pong Demo Application \n\n\r" );
 
@@ -163,19 +177,69 @@
 
     Radio.Rx( RX_TIMEOUT_VALUE );
 
-    while( 1 )
+    
+
+    while (button1==1)
     {
-        switch( State )
+    }
+    
+    
+   
+    //for(int count=0; count<misure; count++)
+    //{
+        // 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( waitTime );
+        
+        /*while (oktx==0)
+        {}
+        oktx=0;*/
+        
+        debug( "Start TX\r\n" ); 
+        t.start();
+        Radio.Send( Buffer, BufferSize ); //send
+        
+        //debug ("End TX\r\n");
+        
+        //t.stop();
+        wait_ms( 10000 );
+        /*tmisura=t.read();
+        somma+=tmisura;
+        t.reset();
+
+        
+        debug("Tempo impiegato\r\n");  
+        sprintf(tempo, "%f", tmisura);
+        debug(tempo);
+        debug("\r\n\n\n");*/
+        
+        
+    //} 
+    
+    somma=somma/misure;
+    debug("Tempo medio di TX\r\n");
+    sprintf(tempo, "%f", somma);
+    debug(tempo);
+    debug("\r\n\n\n");
+
+        
+       /* switch( State )
         {
         case RX:
             if( isMaster == true )
             {
                 if( BufferSize > 0 )
                 {
-                    if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
-                    {
+                    if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )  //se sono uguali vale 0
+                    {                                                           //ho ricevuto un pong
                         led = !led;
-                        debug( "...Pong\r\n" );
+                        debug( "Master RX...Pong\r\n" );                                 
                         // Send the next PING frame
                         strcpy( ( char* )Buffer, ( char* )PingMsg );
                         // We fill the buffer with numbers for the payload
@@ -183,12 +247,12 @@
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 );
-                        Radio.Send( Buffer, BufferSize );
+                        wait_ms( waitTime );
+                        Radio.Send( Buffer, BufferSize );                       //mando il ping
                     }
                     else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
                     { // A master already exists then become a slave
-                        debug( "...Ping\r\n" );
+                        debug( "Master RX...Ping\r\n" );                                 //ho ricevuto un ping
                         led = !led;
                         isMaster = false;
                         // Send the next PONG frame
@@ -198,24 +262,24 @@
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 );
+                        wait_ms( waitTime );
                         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 );
+                        Radio.Rx( RX_TIMEOUT_VALUE );                       //re non ho ricevuto niente timeout
                     }
                 }
             }
-            else
+            else   //s sono lo slave
             {
                 if( BufferSize > 0 )
                 {
                     if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
                     {
                         led = !led;
-                        debug( "...Ping\r\n" );
+                        debug( "Slave RX...Ping\r\n" );
                         // Send the reply to the PING string
                         strcpy( ( char* )Buffer, ( char* )PongMsg );
                         // We fill the buffer with numbers for the payload
@@ -223,27 +287,27 @@
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 );
-                        Radio.Send( Buffer, BufferSize );
+                        wait_ms( waitTime );
+                        Radio.Send( Buffer, BufferSize );                       //trasmetto un pong
                     }
                     else // valid reception but not a PING as expected
                     {    // Set device as master and start again
-                        isMaster = true;
+                        isMaster = true;                                        //divento master
                         Radio.Rx( RX_TIMEOUT_VALUE );
                     }
                 }
             }
-            State = LOWPOWER;
+            State = LOWPOWER;                                                   //vado nello stato lowpower
             break;
         case TX:
             led = !led;
             if( isMaster == true )  
             {
-                debug( "Ping...\r\n" );
+                debug( "Master TX Ping...\r\n" );
             }
             else
             {
-                debug( "Pong...\r\n" );
+                debug( "Slave TX Pong...\r\n" );
             }
             Radio.Rx( RX_TIMEOUT_VALUE );
             State = LOWPOWER;
@@ -257,7 +321,7 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 );
+                wait_ms( waitTime );
                 Radio.Send( Buffer, BufferSize );
             }
             else
@@ -276,7 +340,7 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 );
+                wait_ms( waitTime );
                 Radio.Send( Buffer, BufferSize );
             }
             else
@@ -287,7 +351,7 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 );
+                wait_ms( waitTime );
                 Radio.Send( Buffer, BufferSize );
             }
             State = LOWPOWER;
@@ -302,14 +366,30 @@
             State = LOWPOWER;
             break;
         }
-    }
+    }*/
 }
 
 void OnTxDone( void )
 {
     Radio.Sleep( );
     State = TX;
+    //oktx=1;
     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
+    t.stop();
+
+    tmisura=t.read();
+    somma+=tmisura;
+    t.reset();
+    
+    debug("Tempo impiegato\r\n");  
+    sprintf(tempo, "%f", tmisura);
+    debug(tempo);
+    debug("\r\n\n\n");
+    
+    wait (10);
+    
+    Radio.Send( Buffer, BufferSize ); //send
+
 }
 
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
@@ -321,6 +401,7 @@
     SnrValue = snr;
     State = RX;
     debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
+    oktx=1;
 }
 
 void OnTxTimeout( void )