7 years ago.  This question has been closed. Reason: Too broad - no single answer

Can someone explain me this section of the ping-pong code?

I understand that the code follows a master-slave architecture. I am not pretty confident of my understanding from this while loop on how the program decides which node has to be the master and who transmits first. Also, what if I want to change the loop operation to a different sequence? Say the current operation is Ping-Pong. I want to change it to Ping-Ping-Pong.

From what I have observed on the terminal: On a Ping, a transmission is done, on a Pong a reception is achieved. Correct me if I am wrong.

Code:

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( 100 ); 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( 100 ); 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 ); } } }