![](/media/cache/profiles/profile_pic_for_github_less_than_1MB.jpg.50x50_q85.jpg)
This is example code that can get you started with building your own IR vision robot that communicates over LoRa
Dependencies: Adafruit-MotorShield Adafruit-PWM-Servo-Driver Adafruit_GFX BufferedSerial MAX17055_EZconfig NEO-6m-GPS SX1276GenericLib USBDeviceHT max32630fthr max77650_charger_sample
Fork of MAX326xxFTHR_LoRa_Example_test by
Diff: SX1276GenericPingPong/GenericPingPong.cpp
- Revision:
- 22:abca9d17d13d
- Parent:
- 19:9f035b9e65ec
- Child:
- 23:f74a50977593
--- a/SX1276GenericPingPong/GenericPingPong.cpp Thu Jun 21 16:59:46 2018 +0000 +++ b/SX1276GenericPingPong/GenericPingPong.cpp Fri Jul 13 18:22:53 2018 +0000 @@ -97,7 +97,7 @@ DigitalOut *led3; -int SX1276PingPong(void) +int SX1276PingPongSetup(void) { dprintf("TEST" ); #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) @@ -125,9 +125,6 @@ #endif - uint8_t i; - bool isMaster = true; - dprintf("SX1276 Ping Pong Demo Application" ); dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); @@ -217,153 +214,157 @@ Radio->Rx( RX_TIMEOUT_VALUE ); - - while( 1 ) - { -#ifdef TARGET_STM32L4 - // WatchDogUpdate(); +} + +int SX1276PingPong(void) +{ + uint8_t i; +#if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway + bool isMaster = true; +#elif defined(TARGET_MAX32620FTHR) // Client Device: Robot + bool isMaster = false; #endif - - switch( State ) + + switch( State ) + { + case RX: +// *led3 = 0; + if( isMaster == true ) { - case RX: - *led3 = 0; - if( isMaster == true ) + if( BufferSize > 0 ) { - if( BufferSize > 0 ) + if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) { - if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) +// *led = !*led; + dprintf( "...Pong" ); + // Send the next PING frame + memcpy(Buffer, PingMsg, sizeof(PingMsg)); + // We fill the buffer with numbers for the payload + for( i = sizeof(PingMsg); i < BufferSize; i++ ) { - *led = !*led; - dprintf( "...Pong" ); - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PingMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PingMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + Buffer[i] = i - sizeof(PingMsg); } - else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { // A master already exists then become a slave - dprintf( "...Ping" ); - *led = !*led; - isMaster = false; - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + wait_ms( 10 ); + Radio->Send( Buffer, BufferSize ); + } +/* else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) + { // A master already exists then become a slave + dprintf( "...Ping" ); +// *led = !*led; + isMaster = false; + // Send the next PONG frame + memcpy(Buffer, PongMsg, sizeof(PongMsg)); + // We fill the buffer with numbers for the payload + for( i = sizeof(PongMsg); i < BufferSize; i++ ) + { + Buffer[i] = i - sizeof(PongMsg); } - 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( 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 + } + else + { + if( BufferSize > 0 ) { - if( BufferSize > 0 ) - { - if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { - *led = !*led; - dprintf( "...Ping" ); - // Send the reply to the PING string - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - 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: - *led3 = 1; - if( isMaster == true ) - { - dprintf("Ping..." ); - } - else - { - dprintf("Pong..." ); - } - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case RX_TIMEOUT: - if( isMaster == true ) - { - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = sizeof(PingMsg); i < BufferSize; i++ ) + if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) { - Buffer[i] = i - sizeof(PingMsg); +// *led = !*led; + dprintf( "...Ping" ); + // Send the reply to the PING string + memcpy(Buffer, PongMsg, sizeof(PongMsg)); + // We fill the buffer with numbers for the payload + for( i = sizeof(PongMsg); i < BufferSize; i++ ) + { + Buffer[i] = i - sizeof(PongMsg); + } + wait_ms( 10 ); + Radio->Send( Buffer, BufferSize ); } - 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 + } + State = LOWPOWER; + break; + case TX: +// *led3 = 1; + if( isMaster == true ) + { + dprintf("Ping..." ); + } + else + { + dprintf("Pong..." ); + } + Radio->Rx( RX_TIMEOUT_VALUE ); + State = LOWPOWER; + break; + case RX_TIMEOUT: + if( isMaster == true ) + { + // Send the next PING frame + memcpy(Buffer, PingMsg, sizeof(PingMsg)); + for( i = sizeof(PingMsg); i < BufferSize; i++ ) { - 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 - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + Buffer[i] = i - sizeof(PingMsg); } - else + 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 + memcpy(Buffer, PingMsg, sizeof(PingMsg)); + for( i = 4; i < BufferSize; i++ ) { - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + Buffer[i] = i - 4; } - State = LOWPOWER; - break; - case TX_TIMEOUT: - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case LOWPOWER: - sleep(); - break; - default: - State = LOWPOWER; - break; - } - } + wait_ms( 10 ); + Radio->Send( Buffer, BufferSize ); + } + else + { + // Send the next PONG frame + memcpy(Buffer, PongMsg, sizeof(PongMsg)); + for( i = sizeof(PongMsg); i < BufferSize; i++ ) + { + Buffer[i] = i - sizeof(PongMsg); + } + wait_ms( 10 ); + Radio->Send( Buffer, BufferSize ); + } + State = LOWPOWER; + break; + case TX_TIMEOUT: + Radio->Rx( RX_TIMEOUT_VALUE ); + State = LOWPOWER; + break; + case LOWPOWER: + sleep(); + break; + default: + State = LOWPOWER; + break; + } } void OnTxDone(void *radio, void *userThisPtr, void *userData) @@ -387,7 +388,7 @@ void OnTxTimeout(void *radio, void *userThisPtr, void *userData) { - *led3 = 0; +// *led3 = 0; Radio->Sleep( ); State = TX_TIMEOUT; if(DEBUG_MESSAGE) @@ -396,7 +397,7 @@ void OnRxTimeout(void *radio, void *userThisPtr, void *userData) { - *led3 = 0; +// *led3 = 0; Radio->Sleep( ); Buffer[BufferSize-1] = 0; State = RX_TIMEOUT;