Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SX1276Lib_modtronix mbed
Fork of SX1276PingPong_inAir by
main.cpp@17:4fcf3c76cab7, 2016-09-14 (annotated)
- Committer:
 - Razorfoot
 - Date:
 - Wed Sep 14 05:47:41 2016 +0000
 - Revision:
 - 17:4fcf3c76cab7
 - Parent:
 - 16:3d22f4dd0311
 
init;
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| GregCr | 0:1ed39951ab7b | 1 | #include "mbed.h" | 
| GregCr | 4:5ece30264cd9 | 2 | #include "main.h" | 
| rba90 | 16:3d22f4dd0311 | 3 | #include "sx1276-inAir.h" | 
| GregCr | 0:1ed39951ab7b | 4 | |
| GregCr | 0:1ed39951ab7b | 5 | /* Set this flag to '1' to display debug messages on the console */ | 
| rba90 | 15:916032fc76c0 | 6 | #define DEBUG_MESSAGE 1 | 
| GregCr | 0:1ed39951ab7b | 7 | |
| GregCr | 0:1ed39951ab7b | 8 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ | 
| GregCr | 5:f2431c4fe3bb | 9 | #define USE_MODEM_LORA 1 | 
| GregCr | 0:1ed39951ab7b | 10 | #define USE_MODEM_FSK !USE_MODEM_LORA | 
| GregCr | 0:1ed39951ab7b | 11 | |
| GregCr | 5:f2431c4fe3bb | 12 | #define RF_FREQUENCY 868000000 // Hz | 
| GregCr | 0:1ed39951ab7b | 13 | #define TX_OUTPUT_POWER 14 // 14 dBm | 
| GregCr | 0:1ed39951ab7b | 14 | |
| GregCr | 0:1ed39951ab7b | 15 | #if USE_MODEM_LORA == 1 | 
| GregCr | 0:1ed39951ab7b | 16 | |
| Razorfoot | 17:4fcf3c76cab7 | 17 | #define LORA_BANDWIDTH 9 // [7: 125 kHz, changed from 2 to 9 | 
| Razorfoot | 17:4fcf3c76cab7 | 18 | // 8: 250 kHz, | 
| Razorfoot | 17:4fcf3c76cab7 | 19 | // 9: 500 kHz, | 
| Razorfoot | 17:4fcf3c76cab7 | 20 | // X: Reserved] | 
| Razorfoot | 17:4fcf3c76cab7 | 21 | #define LORA_SPREADING_FACTOR 12 // [SF7..SF12] | 
| GregCr | 0:1ed39951ab7b | 22 | #define LORA_CODINGRATE 1 // [1: 4/5, | 
| GregCr | 0:1ed39951ab7b | 23 | // 2: 4/6, | 
| GregCr | 0:1ed39951ab7b | 24 | // 3: 4/7, | 
| GregCr | 0:1ed39951ab7b | 25 | // 4: 4/8] | 
| GregCr | 0:1ed39951ab7b | 26 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx | 
| GregCr | 0:1ed39951ab7b | 27 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols | 
| GregCr | 0:1ed39951ab7b | 28 | #define LORA_FIX_LENGTH_PAYLOAD_ON false | 
| GregCr | 3:8b9e2a4df4b5 | 29 | #define LORA_FHSS_ENABLED false | 
| GregCr | 3:8b9e2a4df4b5 | 30 | #define LORA_NB_SYMB_HOP 4 | 
| GregCr | 0:1ed39951ab7b | 31 | #define LORA_IQ_INVERSION_ON false | 
| Razorfoot | 17:4fcf3c76cab7 | 32 | #define LORA_CRC_ENABLED true //changed from false to true | 
| GregCr | 3:8b9e2a4df4b5 | 33 | |
| GregCr | 0:1ed39951ab7b | 34 | #elif USE_MODEM_FSK == 1 | 
| GregCr | 0:1ed39951ab7b | 35 | |
| GregCr | 2:59e108728d71 | 36 | #define FSK_FDEV 25000 // Hz | 
| GregCr | 2:59e108728d71 | 37 | #define FSK_DATARATE 19200 // bps | 
| GregCr | 2:59e108728d71 | 38 | #define FSK_BANDWIDTH 50000 // Hz | 
| GregCr | 2:59e108728d71 | 39 | #define FSK_AFC_BANDWIDTH 83333 // Hz | 
| GregCr | 0:1ed39951ab7b | 40 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx | 
| GregCr | 0:1ed39951ab7b | 41 | #define FSK_FIX_LENGTH_PAYLOAD_ON false | 
| GregCr | 3:8b9e2a4df4b5 | 42 | #define FSK_CRC_ENABLED true | 
| GregCr | 3:8b9e2a4df4b5 | 43 | |
| GregCr | 0:1ed39951ab7b | 44 | #else | 
| GregCr | 0:1ed39951ab7b | 45 | #error "Please define a modem in the compiler options." | 
| GregCr | 0:1ed39951ab7b | 46 | #endif | 
| GregCr | 0:1ed39951ab7b | 47 | |
| GregCr | 7:c1bbd6c56979 | 48 | #define RX_TIMEOUT_VALUE 3500000 // in us | 
| GregCr | 0:1ed39951ab7b | 49 | #define BUFFER_SIZE 32 // Define the payload size here | 
| GregCr | 0:1ed39951ab7b | 50 | |
| GregCr | 8:f956dee63a56 | 51 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) | 
| GregCr | 3:8b9e2a4df4b5 | 52 | DigitalOut led(LED2); | 
| GregCr | 3:8b9e2a4df4b5 | 53 | #else | 
| GregCr | 3:8b9e2a4df4b5 | 54 | DigitalOut led(LED1); | 
| GregCr | 3:8b9e2a4df4b5 | 55 | #endif | 
| GregCr | 3:8b9e2a4df4b5 | 56 | |
| GregCr | 0:1ed39951ab7b | 57 | /* | 
| GregCr | 0:1ed39951ab7b | 58 | * Global variables declarations | 
| GregCr | 0:1ed39951ab7b | 59 | */ | 
| Razorfoot | 17:4fcf3c76cab7 | 60 | /*typedef enum | 
| mluis | 10:7af820d1e1df | 61 | { | 
| mluis | 10:7af820d1e1df | 62 | LOWPOWER = 0, | 
| mluis | 10:7af820d1e1df | 63 | IDLE, | 
| mluis | 10:7af820d1e1df | 64 | |
| mluis | 10:7af820d1e1df | 65 | RX, | 
| mluis | 10:7af820d1e1df | 66 | RX_TIMEOUT, | 
| mluis | 10:7af820d1e1df | 67 | RX_ERROR, | 
| mluis | 10:7af820d1e1df | 68 | |
| mluis | 10:7af820d1e1df | 69 | TX, | 
| mluis | 10:7af820d1e1df | 70 | TX_TIMEOUT, | 
| mluis | 10:7af820d1e1df | 71 | |
| mluis | 10:7af820d1e1df | 72 | CAD, | 
| mluis | 10:7af820d1e1df | 73 | CAD_DONE | 
| Razorfoot | 17:4fcf3c76cab7 | 74 | }AppStates_t;*/ | 
| GregCr | 0:1ed39951ab7b | 75 | |
| Razorfoot | 17:4fcf3c76cab7 | 76 | //volatile AppStates_t State = LOWPOWER; //Not using anymore | 
| Razorfoot | 17:4fcf3c76cab7 | 77 | volatile enum RadioState State = LOWPOWER; | 
| mluis | 10:7af820d1e1df | 78 | |
| mluis | 10:7af820d1e1df | 79 | /*! | 
| mluis | 10:7af820d1e1df | 80 | * Radio events function pointer | 
| mluis | 10:7af820d1e1df | 81 | */ | 
| Razorfoot | 17:4fcf3c76cab7 | 82 | //static RadioEvents_t RadioEvents; //Not using anymore | 
| mluis | 10:7af820d1e1df | 83 | |
| mluis | 10:7af820d1e1df | 84 | /* | 
| mluis | 10:7af820d1e1df | 85 | * Global variables declarations | 
| mluis | 10:7af820d1e1df | 86 | */ | 
| Razorfoot | 17:4fcf3c76cab7 | 87 | SX1276inAir Radio(OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL); //No longer NULL | 
| Razorfoot | 17:4fcf3c76cab7 | 88 | //SX1276inAir Radio(NULL); | 
| GregCr | 0:1ed39951ab7b | 89 | |
| GregCr | 0:1ed39951ab7b | 90 | const uint8_t PingMsg[] = "PING"; | 
| GregCr | 0:1ed39951ab7b | 91 | const uint8_t PongMsg[] = "PONG"; | 
| GregCr | 0:1ed39951ab7b | 92 | |
| GregCr | 0:1ed39951ab7b | 93 | uint16_t BufferSize = BUFFER_SIZE; | 
| GregCr | 0:1ed39951ab7b | 94 | uint8_t Buffer[BUFFER_SIZE]; | 
| GregCr | 0:1ed39951ab7b | 95 | |
| GregCr | 5:f2431c4fe3bb | 96 | int16_t RssiValue = 0.0; | 
| GregCr | 5:f2431c4fe3bb | 97 | int8_t SnrValue = 0.0; | 
| GregCr | 0:1ed39951ab7b | 98 | |
| GregCr | 0:1ed39951ab7b | 99 | int main() | 
| GregCr | 0:1ed39951ab7b | 100 | { | 
| GregCr | 0:1ed39951ab7b | 101 | uint8_t i; | 
| GregCr | 0:1ed39951ab7b | 102 | bool isMaster = true; | 
| GregCr | 0:1ed39951ab7b | 103 | |
| rba90 | 15:916032fc76c0 | 104 | printf( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); | 
| mluis | 10:7af820d1e1df | 105 | |
| mluis | 10:7af820d1e1df | 106 | // Initialize Radio driver | 
| Razorfoot | 17:4fcf3c76cab7 | 107 | /*RadioEvents.TxDone = OnTxDone; | 
| mluis | 10:7af820d1e1df | 108 | RadioEvents.RxDone = OnRxDone; | 
| mluis | 10:7af820d1e1df | 109 | RadioEvents.RxError = OnRxError; | 
| mluis | 10:7af820d1e1df | 110 | RadioEvents.TxTimeout = OnTxTimeout; | 
| Razorfoot | 17:4fcf3c76cab7 | 111 | RadioEvents.RxTimeout = OnRxTimeout; //Moved to the class constructor | 
| Razorfoot | 17:4fcf3c76cab7 | 112 | Radio.Init( &RadioEvents );*/ | 
| GregCr | 0:1ed39951ab7b | 113 | |
| GregCr | 7:c1bbd6c56979 | 114 | // verify the connection with the board | 
| GregCr | 7:c1bbd6c56979 | 115 | while( Radio.Read( REG_VERSION ) == 0x00 ) | 
| GregCr | 2:59e108728d71 | 116 | { | 
| rba90 | 15:916032fc76c0 | 117 | printf( "Radio could not be detected!\n\r" ); | 
| GregCr | 7:c1bbd6c56979 | 118 | wait( 1 ); | 
| GregCr | 2:59e108728d71 | 119 | } | 
| rba90 | 15:916032fc76c0 | 120 | printf("RadioRegVersion: %d\r\n", Radio.Read( REG_VERSION )); | 
| GregCr | 14:c7251480feb9 | 121 | |
| GregCr | 0:1ed39951ab7b | 122 | Radio.SetChannel( RF_FREQUENCY ); | 
| GregCr | 0:1ed39951ab7b | 123 | |
| GregCr | 0:1ed39951ab7b | 124 | #if USE_MODEM_LORA == 1 | 
| GregCr | 7:c1bbd6c56979 | 125 | |
| GregCr | 0:1ed39951ab7b | 126 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, | 
| GregCr | 0:1ed39951ab7b | 127 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, | 
| GregCr | 0:1ed39951ab7b | 128 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, | 
| GregCr | 3:8b9e2a4df4b5 | 129 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, | 
| Razorfoot | 17:4fcf3c76cab7 | 130 | LORA_IQ_INVERSION_ON, 4000000 ); //changed from 2000000 to 4000000 | 
| GregCr | 0:1ed39951ab7b | 131 | |
| GregCr | 0:1ed39951ab7b | 132 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, | 
| GregCr | 0:1ed39951ab7b | 133 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, | 
| mluis | 9:e764990e45df | 134 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, | 
| GregCr | 3:8b9e2a4df4b5 | 135 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, | 
| GregCr | 3:8b9e2a4df4b5 | 136 | LORA_IQ_INVERSION_ON, true ); | 
| GregCr | 0:1ed39951ab7b | 137 | |
| GregCr | 0:1ed39951ab7b | 138 | #elif USE_MODEM_FSK == 1 | 
| GregCr | 0:1ed39951ab7b | 139 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, | 
| GregCr | 0:1ed39951ab7b | 140 | FSK_DATARATE, 0, | 
| GregCr | 0:1ed39951ab7b | 141 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, | 
| GregCr | 7:c1bbd6c56979 | 142 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); | 
| GregCr | 0:1ed39951ab7b | 143 | |
| GregCr | 0:1ed39951ab7b | 144 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, | 
| GregCr | 0:1ed39951ab7b | 145 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, | 
| mluis | 9:e764990e45df | 146 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, | 
| GregCr | 3:8b9e2a4df4b5 | 147 | 0, 0, false, true ); | 
| GregCr | 0:1ed39951ab7b | 148 | |
| GregCr | 0:1ed39951ab7b | 149 | #else | 
| GregCr | 0:1ed39951ab7b | 150 | |
| GregCr | 0:1ed39951ab7b | 151 | #error "Please define a modem in the compiler options." | 
| GregCr | 0:1ed39951ab7b | 152 | |
| GregCr | 0:1ed39951ab7b | 153 | #endif | 
| GregCr | 1:126d70d374f6 | 154 | |
| rba90 | 15:916032fc76c0 | 155 | printf( "Starting Ping-Pong loop\r\n" ); | 
| GregCr | 0:1ed39951ab7b | 156 | |
| GregCr | 3:8b9e2a4df4b5 | 157 | led = 0; | 
| GregCr | 3:8b9e2a4df4b5 | 158 | |
| GregCr | 0:1ed39951ab7b | 159 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 0:1ed39951ab7b | 160 | |
| GregCr | 0:1ed39951ab7b | 161 | while( 1 ) | 
| GregCr | 0:1ed39951ab7b | 162 | { | 
| GregCr | 0:1ed39951ab7b | 163 | switch( State ) | 
| GregCr | 0:1ed39951ab7b | 164 | { | 
| Razorfoot | 17:4fcf3c76cab7 | 165 | case RX_DONE: //No longer case RX | 
| GregCr | 0:1ed39951ab7b | 166 | if( isMaster == true ) | 
| GregCr | 0:1ed39951ab7b | 167 | { | 
| GregCr | 0:1ed39951ab7b | 168 | if( BufferSize > 0 ) | 
| GregCr | 0:1ed39951ab7b | 169 | { | 
| GregCr | 0:1ed39951ab7b | 170 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) | 
| GregCr | 0:1ed39951ab7b | 171 | { | 
| GregCr | 3:8b9e2a4df4b5 | 172 | led = !led; | 
| rba90 | 15:916032fc76c0 | 173 | printf( "...Pong\r\n" ); | 
| GregCr | 0:1ed39951ab7b | 174 | // Send the next PING frame | 
| GregCr | 7:c1bbd6c56979 | 175 | strcpy( ( char* )Buffer, ( char* )PingMsg ); | 
| GregCr | 0:1ed39951ab7b | 176 | // We fill the buffer with numbers for the payload | 
| GregCr | 0:1ed39951ab7b | 177 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 0:1ed39951ab7b | 178 | { | 
| GregCr | 0:1ed39951ab7b | 179 | Buffer[i] = i - 4; | 
| GregCr | 0:1ed39951ab7b | 180 | } | 
| GregCr | 0:1ed39951ab7b | 181 | wait_ms( 10 ); | 
| GregCr | 0:1ed39951ab7b | 182 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 183 | } | 
| GregCr | 2:59e108728d71 | 184 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) | 
| GregCr | 0:1ed39951ab7b | 185 | { // A master already exists then become a slave | 
| rba90 | 15:916032fc76c0 | 186 | printf( "...Ping\r\n" ); | 
| GregCr | 3:8b9e2a4df4b5 | 187 | led = !led; | 
| GregCr | 0:1ed39951ab7b | 188 | isMaster = false; | 
| GregCr | 7:c1bbd6c56979 | 189 | // Send the next PONG frame | 
| GregCr | 7:c1bbd6c56979 | 190 | strcpy( ( char* )Buffer, ( char* )PongMsg ); | 
| GregCr | 2:59e108728d71 | 191 | // We fill the buffer with numbers for the payload | 
| GregCr | 2:59e108728d71 | 192 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 2:59e108728d71 | 193 | { | 
| GregCr | 2:59e108728d71 | 194 | Buffer[i] = i - 4; | 
| GregCr | 2:59e108728d71 | 195 | } | 
| GregCr | 3:8b9e2a4df4b5 | 196 | wait_ms( 10 ); | 
| GregCr | 2:59e108728d71 | 197 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 2:59e108728d71 | 198 | } | 
| GregCr | 2:59e108728d71 | 199 | else // valid reception but neither a PING or a PONG message | 
| GregCr | 2:59e108728d71 | 200 | { // Set device as master ans start again | 
| GregCr | 2:59e108728d71 | 201 | isMaster = true; | 
| GregCr | 0:1ed39951ab7b | 202 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 2:59e108728d71 | 203 | } | 
| GregCr | 0:1ed39951ab7b | 204 | } | 
| GregCr | 0:1ed39951ab7b | 205 | } | 
| GregCr | 0:1ed39951ab7b | 206 | else | 
| GregCr | 0:1ed39951ab7b | 207 | { | 
| GregCr | 0:1ed39951ab7b | 208 | if( BufferSize > 0 ) | 
| GregCr | 0:1ed39951ab7b | 209 | { | 
| GregCr | 0:1ed39951ab7b | 210 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) | 
| GregCr | 0:1ed39951ab7b | 211 | { | 
| GregCr | 3:8b9e2a4df4b5 | 212 | led = !led; | 
| rba90 | 15:916032fc76c0 | 213 | printf( "...Ping\r\n" ); | 
| GregCr | 2:59e108728d71 | 214 | // Send the reply to the PING string | 
| GregCr | 7:c1bbd6c56979 | 215 | strcpy( ( char* )Buffer, ( char* )PongMsg ); | 
| GregCr | 0:1ed39951ab7b | 216 | // We fill the buffer with numbers for the payload | 
| GregCr | 0:1ed39951ab7b | 217 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 0:1ed39951ab7b | 218 | { | 
| GregCr | 0:1ed39951ab7b | 219 | Buffer[i] = i - 4; | 
| GregCr | 0:1ed39951ab7b | 220 | } | 
| GregCr | 0:1ed39951ab7b | 221 | wait_ms( 10 ); | 
| GregCr | 0:1ed39951ab7b | 222 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 223 | } | 
| GregCr | 2:59e108728d71 | 224 | else // valid reception but not a PING as expected | 
| GregCr | 2:59e108728d71 | 225 | { // Set device as master and start again | 
| GregCr | 2:59e108728d71 | 226 | isMaster = true; | 
| GregCr | 2:59e108728d71 | 227 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 2:59e108728d71 | 228 | } | 
| GregCr | 0:1ed39951ab7b | 229 | } | 
| GregCr | 0:1ed39951ab7b | 230 | } | 
| GregCr | 0:1ed39951ab7b | 231 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 232 | break; | 
| Razorfoot | 17:4fcf3c76cab7 | 233 | case TX_DONE: //No longer case TX | 
| GregCr | 3:8b9e2a4df4b5 | 234 | led = !led; | 
| GregCr | 2:59e108728d71 | 235 | if( isMaster == true ) | 
| GregCr | 0:1ed39951ab7b | 236 | { | 
| rba90 | 15:916032fc76c0 | 237 | printf( "Ping...\r\n" ); | 
| GregCr | 0:1ed39951ab7b | 238 | } | 
| GregCr | 0:1ed39951ab7b | 239 | else | 
| GregCr | 0:1ed39951ab7b | 240 | { | 
| rba90 | 15:916032fc76c0 | 241 | printf( "Pong...\r\n" ); | 
| GregCr | 0:1ed39951ab7b | 242 | } | 
| GregCr | 0:1ed39951ab7b | 243 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 0:1ed39951ab7b | 244 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 245 | break; | 
| GregCr | 0:1ed39951ab7b | 246 | case RX_TIMEOUT: | 
| GregCr | 0:1ed39951ab7b | 247 | if( isMaster == true ) | 
| GregCr | 0:1ed39951ab7b | 248 | { | 
| GregCr | 0:1ed39951ab7b | 249 | // Send the next PING frame | 
| GregCr | 7:c1bbd6c56979 | 250 | strcpy( ( char* )Buffer, ( char* )PingMsg ); | 
| GregCr | 0:1ed39951ab7b | 251 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 0:1ed39951ab7b | 252 | { | 
| GregCr | 0:1ed39951ab7b | 253 | Buffer[i] = i - 4; | 
| GregCr | 0:1ed39951ab7b | 254 | } | 
| GregCr | 0:1ed39951ab7b | 255 | wait_ms( 10 ); | 
| GregCr | 0:1ed39951ab7b | 256 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 257 | } | 
| GregCr | 0:1ed39951ab7b | 258 | else | 
| GregCr | 0:1ed39951ab7b | 259 | { | 
| GregCr | 0:1ed39951ab7b | 260 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 0:1ed39951ab7b | 261 | } | 
| GregCr | 0:1ed39951ab7b | 262 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 263 | break; | 
| GregCr | 0:1ed39951ab7b | 264 | case RX_ERROR: | 
| GregCr | 7:c1bbd6c56979 | 265 | // We have received a Packet with a CRC error, send reply as if packet was correct | 
| GregCr | 0:1ed39951ab7b | 266 | if( isMaster == true ) | 
| GregCr | 0:1ed39951ab7b | 267 | { | 
| GregCr | 0:1ed39951ab7b | 268 | // Send the next PING frame | 
| GregCr | 7:c1bbd6c56979 | 269 | strcpy( ( char* )Buffer, ( char* )PingMsg ); | 
| GregCr | 0:1ed39951ab7b | 270 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 0:1ed39951ab7b | 271 | { | 
| GregCr | 0:1ed39951ab7b | 272 | Buffer[i] = i - 4; | 
| GregCr | 0:1ed39951ab7b | 273 | } | 
| GregCr | 0:1ed39951ab7b | 274 | wait_ms( 10 ); | 
| GregCr | 0:1ed39951ab7b | 275 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 276 | } | 
| GregCr | 0:1ed39951ab7b | 277 | else | 
| GregCr | 0:1ed39951ab7b | 278 | { | 
| GregCr | 2:59e108728d71 | 279 | // Send the next PONG frame | 
| GregCr | 7:c1bbd6c56979 | 280 | strcpy( ( char* )Buffer, ( char* )PongMsg ); | 
| GregCr | 2:59e108728d71 | 281 | for( i = 4; i < BufferSize; i++ ) | 
| GregCr | 2:59e108728d71 | 282 | { | 
| GregCr | 2:59e108728d71 | 283 | Buffer[i] = i - 4; | 
| GregCr | 2:59e108728d71 | 284 | } | 
| GregCr | 2:59e108728d71 | 285 | wait_ms( 10 ); | 
| GregCr | 2:59e108728d71 | 286 | Radio.Send( Buffer, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 287 | } | 
| GregCr | 0:1ed39951ab7b | 288 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 289 | break; | 
| GregCr | 0:1ed39951ab7b | 290 | case TX_TIMEOUT: | 
| GregCr | 0:1ed39951ab7b | 291 | Radio.Rx( RX_TIMEOUT_VALUE ); | 
| GregCr | 0:1ed39951ab7b | 292 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 293 | break; | 
| GregCr | 0:1ed39951ab7b | 294 | case LOWPOWER: | 
| GregCr | 0:1ed39951ab7b | 295 | break; | 
| GregCr | 0:1ed39951ab7b | 296 | default: | 
| GregCr | 0:1ed39951ab7b | 297 | State = LOWPOWER; | 
| GregCr | 0:1ed39951ab7b | 298 | break; | 
| GregCr | 0:1ed39951ab7b | 299 | } | 
| GregCr | 0:1ed39951ab7b | 300 | } | 
| GregCr | 0:1ed39951ab7b | 301 | } | 
| GregCr | 0:1ed39951ab7b | 302 | |
| GregCr | 0:1ed39951ab7b | 303 | void OnTxDone( void ) | 
| GregCr | 0:1ed39951ab7b | 304 | { | 
| GregCr | 5:f2431c4fe3bb | 305 | Radio.Sleep( ); | 
| Razorfoot | 17:4fcf3c76cab7 | 306 | State = TX_DONE; //no longer TX | 
| rba90 | 15:916032fc76c0 | 307 | printf( "> OnTxDone\n\r" ); | 
| GregCr | 0:1ed39951ab7b | 308 | } | 
| GregCr | 0:1ed39951ab7b | 309 | |
| GregCr | 4:5ece30264cd9 | 310 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) | 
| GregCr | 0:1ed39951ab7b | 311 | { | 
| GregCr | 0:1ed39951ab7b | 312 | Radio.Sleep( ); | 
| GregCr | 0:1ed39951ab7b | 313 | BufferSize = size; | 
| GregCr | 0:1ed39951ab7b | 314 | memcpy( Buffer, payload, BufferSize ); | 
| GregCr | 0:1ed39951ab7b | 315 | RssiValue = rssi; | 
| GregCr | 0:1ed39951ab7b | 316 | SnrValue = snr; | 
| Razorfoot | 17:4fcf3c76cab7 | 317 | State = RX_DONE; //no longer RX | 
| rba90 | 15:916032fc76c0 | 318 | printf( "> OnRxDone\n\r" ); | 
| Razorfoot | 17:4fcf3c76cab7 | 319 | printf("RSSI: %d\r\nSNR: %d\r\n\r\n", RssiValue, SnrValue); | 
| GregCr | 0:1ed39951ab7b | 320 | } | 
| GregCr | 0:1ed39951ab7b | 321 | |
| GregCr | 0:1ed39951ab7b | 322 | void OnTxTimeout( void ) | 
| GregCr | 0:1ed39951ab7b | 323 | { | 
| GregCr | 0:1ed39951ab7b | 324 | Radio.Sleep( ); | 
| GregCr | 0:1ed39951ab7b | 325 | State = TX_TIMEOUT; | 
| rba90 | 15:916032fc76c0 | 326 | printf( "> OnTxTimeout\n\r" ); | 
| GregCr | 0:1ed39951ab7b | 327 | } | 
| GregCr | 0:1ed39951ab7b | 328 | |
| GregCr | 0:1ed39951ab7b | 329 | void OnRxTimeout( void ) | 
| GregCr | 0:1ed39951ab7b | 330 | { | 
| GregCr | 0:1ed39951ab7b | 331 | Radio.Sleep( ); | 
| GregCr | 1:126d70d374f6 | 332 | Buffer[ BufferSize ] = 0; | 
| GregCr | 0:1ed39951ab7b | 333 | State = RX_TIMEOUT; | 
| rba90 | 15:916032fc76c0 | 334 | printf( "> OnRxTimeout\n\r" ); | 
| GregCr | 0:1ed39951ab7b | 335 | } | 
| GregCr | 0:1ed39951ab7b | 336 | |
| GregCr | 0:1ed39951ab7b | 337 | void OnRxError( void ) | 
| GregCr | 0:1ed39951ab7b | 338 | { | 
| GregCr | 0:1ed39951ab7b | 339 | Radio.Sleep( ); | 
| GregCr | 0:1ed39951ab7b | 340 | State = RX_ERROR; | 
| rba90 | 15:916032fc76c0 | 341 | printf( "> OnRxError\n\r" ); | 
| GregCr | 0:1ed39951ab7b | 342 | } | 
| GregCr | 3:8b9e2a4df4b5 | 343 | 
