Polytech school project. RICM4 students, see http://air.imag.fr/index.php/Projets-2016-2017-Station_de_pompage_connect%C3%A9e for more information
Fork of SX1276PingPong by
Diff: main.cpp
- Revision:
- 15:0439a7579868
- Parent:
- 14:c7251480feb9
- Child:
- 16:1643ac8ba29f
--- a/main.cpp Wed Feb 17 09:28:29 2016 +0000 +++ b/main.cpp Fri Mar 03 08:07:37 2017 +0000 @@ -4,13 +4,15 @@ #include "debug.h" /* Set this flag to '1' to display debug messages on the console */ -#define DEBUG_MESSAGE 0 +#define DEBUG_MESSAGE 1 + +#define ID_STATION 0x55 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 #define USE_MODEM_FSK !USE_MODEM_LORA -#define RF_FREQUENCY 868000000 // Hz +#define RF_FREQUENCY 865000000 // Hz #define TX_OUTPUT_POWER 14 // 14 dBm #if USE_MODEM_LORA == 1 @@ -86,8 +88,8 @@ */ SX1276MB1xAS Radio( NULL ); -const uint8_t PingMsg[] = "PING"; -const uint8_t PongMsg[] = "PONG"; +//const uint8_t PingMsg[] = "PING"; +//const uint8_t PongMsg[] = "PONG"; uint16_t BufferSize = BUFFER_SIZE; uint8_t Buffer[BUFFER_SIZE]; @@ -98,9 +100,9 @@ int main() { uint8_t i; - bool isMaster = true; + bool isMaster = false; - debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); + debug( "\n\n\r SX1276 Server Demo Application \n\n\r" ); // Initialize Radio driver RadioEvents.TxDone = OnTxDone; @@ -158,10 +160,13 @@ #endif - debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); + debug_if( DEBUG_MESSAGE, "Starting Server loop\r\n" ); led = 0; - + + char PingMsg[] = {0x24, 0x7F, 0xEF}; + char PongMsg[] = {0x24, 0x55, 0xFF}; + Radio.Rx( RX_TIMEOUT_VALUE ); while( 1 ) @@ -169,128 +174,50 @@ switch( State ) { case RX: - if( isMaster == true ) + if( BufferSize > 0 ) { - if( BufferSize > 0 ) + if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 3 ) == 0 ) { - if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) + led = !led; + debug( "...DATA\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 = 3; i < BufferSize; i++ ) { - 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 ); + Buffer[i] = i - 4; } - 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 ); - } + wait_ms( 1000 ); + Radio.Send( Buffer, BufferSize ); } - } - else - { - if( BufferSize > 0 ) - { - if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 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 ); - } - } + 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" ); - } + debug( "ORDRE...\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 ); - } + 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 PONG frame + strcpy( ( char* )Buffer, ( char* )PongMsg ); + for( i = 3; i < BufferSize; i++ ) { - // 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 ); + Buffer[i] = i - 4; } - 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 ); - } + wait_ms( 10 ); + Radio.Send( Buffer, BufferSize ); State = LOWPOWER; break; case TX_TIMEOUT: