![](/media/cache/profiles/99E6D37B-BF62-4ED2-8CD4-003BF68D83CE.jpeg.50x50_q85.jpg)
123
Dependencies: mbed Grove_LCD_RGB_Backlight SX1272Lib
main.cpp@0:cc2b234b6255, 2022-05-11 (annotated)
- Committer:
- aizakharov
- Date:
- Wed May 11 14:56:17 2022 +0000
- Revision:
- 0:cc2b234b6255
- Child:
- 1:30ce00fbab55
gkj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aizakharov | 0:cc2b234b6255 | 1 | #include "mbed.h" |
aizakharov | 0:cc2b234b6255 | 2 | #include "main.h" |
aizakharov | 0:cc2b234b6255 | 3 | #include "sx1272-hal.h" |
aizakharov | 0:cc2b234b6255 | 4 | #include "debug.h" |
aizakharov | 0:cc2b234b6255 | 5 | |
aizakharov | 0:cc2b234b6255 | 6 | #include "Grove_LCD_RGB_Backlight.h" |
aizakharov | 0:cc2b234b6255 | 7 | |
aizakharov | 0:cc2b234b6255 | 8 | /* Set this flag to '1' to display debug messages on the console */ |
aizakharov | 0:cc2b234b6255 | 9 | #define DEBUG_MESSAGE 1 |
aizakharov | 0:cc2b234b6255 | 10 | |
aizakharov | 0:cc2b234b6255 | 11 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
aizakharov | 0:cc2b234b6255 | 12 | #define USE_MODEM_LORA 1 |
aizakharov | 0:cc2b234b6255 | 13 | #define USE_MODEM_FSK !USE_MODEM_LORA |
aizakharov | 0:cc2b234b6255 | 14 | |
aizakharov | 0:cc2b234b6255 | 15 | #define RF_FREQUENCY 868000000 // Hz |
aizakharov | 0:cc2b234b6255 | 16 | #define TX_OUTPUT_POWER 14 // 14 dBm |
aizakharov | 0:cc2b234b6255 | 17 | |
aizakharov | 0:cc2b234b6255 | 18 | #if USE_MODEM_LORA == 1 |
aizakharov | 0:cc2b234b6255 | 19 | |
aizakharov | 0:cc2b234b6255 | 20 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
aizakharov | 0:cc2b234b6255 | 21 | // 1: 250 kHz, |
aizakharov | 0:cc2b234b6255 | 22 | // 2: 500 kHz, |
aizakharov | 0:cc2b234b6255 | 23 | // 3: Reserved] |
aizakharov | 0:cc2b234b6255 | 24 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
aizakharov | 0:cc2b234b6255 | 25 | #define LORA_CODINGRATE 1 // [1: 4/5, |
aizakharov | 0:cc2b234b6255 | 26 | // 2: 4/6, |
aizakharov | 0:cc2b234b6255 | 27 | // 3: 4/7, |
aizakharov | 0:cc2b234b6255 | 28 | // 4: 4/8] |
aizakharov | 0:cc2b234b6255 | 29 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
aizakharov | 0:cc2b234b6255 | 30 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
aizakharov | 0:cc2b234b6255 | 31 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
aizakharov | 0:cc2b234b6255 | 32 | #define LORA_FHSS_ENABLED false |
aizakharov | 0:cc2b234b6255 | 33 | #define LORA_NB_SYMB_HOP 4 |
aizakharov | 0:cc2b234b6255 | 34 | #define LORA_IQ_INVERSION_ON false |
aizakharov | 0:cc2b234b6255 | 35 | #define LORA_CRC_ENABLED true |
aizakharov | 0:cc2b234b6255 | 36 | |
aizakharov | 0:cc2b234b6255 | 37 | #elif USE_MODEM_FSK == 1 |
aizakharov | 0:cc2b234b6255 | 38 | |
aizakharov | 0:cc2b234b6255 | 39 | #define FSK_FDEV 25000 // Hz |
aizakharov | 0:cc2b234b6255 | 40 | #define FSK_DATARATE 19200 // bps |
aizakharov | 0:cc2b234b6255 | 41 | #define FSK_BANDWIDTH 50000 // Hz |
aizakharov | 0:cc2b234b6255 | 42 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
aizakharov | 0:cc2b234b6255 | 43 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
aizakharov | 0:cc2b234b6255 | 44 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
aizakharov | 0:cc2b234b6255 | 45 | #define FSK_CRC_ENABLED true |
aizakharov | 0:cc2b234b6255 | 46 | |
aizakharov | 0:cc2b234b6255 | 47 | #else |
aizakharov | 0:cc2b234b6255 | 48 | #error "Please define a modem in the compiler options." |
aizakharov | 0:cc2b234b6255 | 49 | #endif |
aizakharov | 0:cc2b234b6255 | 50 | |
aizakharov | 0:cc2b234b6255 | 51 | #define RX_TIMEOUT_VALUE 35000 // in ms |
aizakharov | 0:cc2b234b6255 | 52 | #define BUFFER_SIZE 60 // Define the payload size here |
aizakharov | 0:cc2b234b6255 | 53 | |
aizakharov | 0:cc2b234b6255 | 54 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
aizakharov | 0:cc2b234b6255 | 55 | DigitalOut led( LED2 ); |
aizakharov | 0:cc2b234b6255 | 56 | #else |
aizakharov | 0:cc2b234b6255 | 57 | DigitalOut led( LED1 ); |
aizakharov | 0:cc2b234b6255 | 58 | #endif |
aizakharov | 0:cc2b234b6255 | 59 | |
aizakharov | 0:cc2b234b6255 | 60 | Serial pc(USBTX, USBRX); // tx, rx |
aizakharov | 0:cc2b234b6255 | 61 | AnalogIn voltage(PC_5); |
aizakharov | 0:cc2b234b6255 | 62 | AnalogIn current(A1); |
aizakharov | 0:cc2b234b6255 | 63 | Ticker measure; |
aizakharov | 0:cc2b234b6255 | 64 | Ticker lcd_print; |
aizakharov | 0:cc2b234b6255 | 65 | Ticker send; |
aizakharov | 0:cc2b234b6255 | 66 | Grove_LCD_RGB_Backlight lcd(I2C_SDA, I2C_SCL); |
aizakharov | 0:cc2b234b6255 | 67 | |
aizakharov | 0:cc2b234b6255 | 68 | //Grove_LCD_RGB_Backlight rgbLCD(I2C_SDA, I2C_SCL); |
aizakharov | 0:cc2b234b6255 | 69 | |
aizakharov | 0:cc2b234b6255 | 70 | /* |
aizakharov | 0:cc2b234b6255 | 71 | * Global variables declarations |
aizakharov | 0:cc2b234b6255 | 72 | */ |
aizakharov | 0:cc2b234b6255 | 73 | typedef enum |
aizakharov | 0:cc2b234b6255 | 74 | { |
aizakharov | 0:cc2b234b6255 | 75 | LOWPOWER = 0, |
aizakharov | 0:cc2b234b6255 | 76 | IDLE, |
aizakharov | 0:cc2b234b6255 | 77 | |
aizakharov | 0:cc2b234b6255 | 78 | RX, |
aizakharov | 0:cc2b234b6255 | 79 | RX_TIMEOUT, |
aizakharov | 0:cc2b234b6255 | 80 | RX_ERROR, |
aizakharov | 0:cc2b234b6255 | 81 | |
aizakharov | 0:cc2b234b6255 | 82 | TX, |
aizakharov | 0:cc2b234b6255 | 83 | TX_TIMEOUT, |
aizakharov | 0:cc2b234b6255 | 84 | |
aizakharov | 0:cc2b234b6255 | 85 | CAD, |
aizakharov | 0:cc2b234b6255 | 86 | CAD_DONE |
aizakharov | 0:cc2b234b6255 | 87 | }AppStates_t; |
aizakharov | 0:cc2b234b6255 | 88 | |
aizakharov | 0:cc2b234b6255 | 89 | volatile AppStates_t State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 90 | |
aizakharov | 0:cc2b234b6255 | 91 | /*! |
aizakharov | 0:cc2b234b6255 | 92 | * Radio events function pointer |
aizakharov | 0:cc2b234b6255 | 93 | */ |
aizakharov | 0:cc2b234b6255 | 94 | static RadioEvents_t RadioEvents; |
aizakharov | 0:cc2b234b6255 | 95 | |
aizakharov | 0:cc2b234b6255 | 96 | /* |
aizakharov | 0:cc2b234b6255 | 97 | * Global variables declarations |
aizakharov | 0:cc2b234b6255 | 98 | */ |
aizakharov | 0:cc2b234b6255 | 99 | SX1272MB2xAS Radio( NULL ); |
aizakharov | 0:cc2b234b6255 | 100 | |
aizakharov | 0:cc2b234b6255 | 101 | //const uint8_t Msg[]; |
aizakharov | 0:cc2b234b6255 | 102 | |
aizakharov | 0:cc2b234b6255 | 103 | uint16_t BufferSize = BUFFER_SIZE; |
aizakharov | 0:cc2b234b6255 | 104 | uint8_t Buffer[BUFFER_SIZE]; |
aizakharov | 0:cc2b234b6255 | 105 | |
aizakharov | 0:cc2b234b6255 | 106 | int16_t RssiValue = 0.0; |
aizakharov | 0:cc2b234b6255 | 107 | int8_t SnrValue = 0.0; |
aizakharov | 0:cc2b234b6255 | 108 | //float measured_sinus = 0.0; |
aizakharov | 0:cc2b234b6255 | 109 | int16_t j = 0; |
aizakharov | 0:cc2b234b6255 | 110 | float voltage_sinus[200], current_sinus[200]; |
aizakharov | 0:cc2b234b6255 | 111 | volatile float Vsum = 0,Vmax =0, Vmean = 0, Imax =0, Isum = 0, Imean = 0, P = 0, Ud = 0, U = 0, Id = 0, I = 0; |
aizakharov | 0:cc2b234b6255 | 112 | uint16_t strSize = 0; |
aizakharov | 0:cc2b234b6255 | 113 | char str[BUFFER_SIZE]; |
aizakharov | 0:cc2b234b6255 | 114 | char monitor[20]="0", monitor1[20]="0"; |
aizakharov | 0:cc2b234b6255 | 115 | |
aizakharov | 0:cc2b234b6255 | 116 | void meas (void) |
aizakharov | 0:cc2b234b6255 | 117 | { |
aizakharov | 0:cc2b234b6255 | 118 | |
aizakharov | 0:cc2b234b6255 | 119 | |
aizakharov | 0:cc2b234b6255 | 120 | |
aizakharov | 0:cc2b234b6255 | 121 | if (j>=200) |
aizakharov | 0:cc2b234b6255 | 122 | { |
aizakharov | 0:cc2b234b6255 | 123 | j = 0; |
aizakharov | 0:cc2b234b6255 | 124 | Vmean=Vsum/(200); |
aizakharov | 0:cc2b234b6255 | 125 | Imean = Isum/(200); |
aizakharov | 0:cc2b234b6255 | 126 | Vsum=0; |
aizakharov | 0:cc2b234b6255 | 127 | Isum=0; |
aizakharov | 0:cc2b234b6255 | 128 | Ud=(Vmax-Vmean)/1.41; |
aizakharov | 0:cc2b234b6255 | 129 | Id=(Imax-Imean)/1.41; |
aizakharov | 0:cc2b234b6255 | 130 | |
aizakharov | 0:cc2b234b6255 | 131 | Vsum = 0; Isum = 0; U = 0; I = 0; Vmax = 0; Imax = 0; |
aizakharov | 0:cc2b234b6255 | 132 | } |
aizakharov | 0:cc2b234b6255 | 133 | voltage_sinus[j] = voltage*2200; |
aizakharov | 0:cc2b234b6255 | 134 | current_sinus[j] = current*5; |
aizakharov | 0:cc2b234b6255 | 135 | if (voltage_sinus[j]>Vmax) |
aizakharov | 0:cc2b234b6255 | 136 | Vmax=voltage_sinus[j]; |
aizakharov | 0:cc2b234b6255 | 137 | if (current_sinus[j]>Imax) |
aizakharov | 0:cc2b234b6255 | 138 | Imax=current_sinus[j]; |
aizakharov | 0:cc2b234b6255 | 139 | P=P+(((voltage_sinus[j]-Vmean)*(current_sinus[j]-Imean))*(0.0002/3600000)); |
aizakharov | 0:cc2b234b6255 | 140 | |
aizakharov | 0:cc2b234b6255 | 141 | Vsum+=voltage_sinus[j]; |
aizakharov | 0:cc2b234b6255 | 142 | Isum+=current_sinus[j]; |
aizakharov | 0:cc2b234b6255 | 143 | |
aizakharov | 0:cc2b234b6255 | 144 | j++; |
aizakharov | 0:cc2b234b6255 | 145 | |
aizakharov | 0:cc2b234b6255 | 146 | } |
aizakharov | 0:cc2b234b6255 | 147 | |
aizakharov | 0:cc2b234b6255 | 148 | |
aizakharov | 0:cc2b234b6255 | 149 | |
aizakharov | 0:cc2b234b6255 | 150 | |
aizakharov | 0:cc2b234b6255 | 151 | |
aizakharov | 0:cc2b234b6255 | 152 | int main( void ) |
aizakharov | 0:cc2b234b6255 | 153 | { |
aizakharov | 0:cc2b234b6255 | 154 | uint16_t count = 0; |
aizakharov | 0:cc2b234b6255 | 155 | |
aizakharov | 0:cc2b234b6255 | 156 | |
aizakharov | 0:cc2b234b6255 | 157 | |
aizakharov | 0:cc2b234b6255 | 158 | |
aizakharov | 0:cc2b234b6255 | 159 | |
aizakharov | 0:cc2b234b6255 | 160 | //uint8_t i; |
aizakharov | 0:cc2b234b6255 | 161 | //bool isMaster = true; |
aizakharov | 0:cc2b234b6255 | 162 | |
aizakharov | 0:cc2b234b6255 | 163 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 164 | |
aizakharov | 0:cc2b234b6255 | 165 | // Initialize Radio driver |
aizakharov | 0:cc2b234b6255 | 166 | RadioEvents.TxDone = OnTxDone; |
aizakharov | 0:cc2b234b6255 | 167 | RadioEvents.RxDone = OnRxDone; |
aizakharov | 0:cc2b234b6255 | 168 | RadioEvents.RxError = OnRxError; |
aizakharov | 0:cc2b234b6255 | 169 | RadioEvents.TxTimeout = OnTxTimeout; |
aizakharov | 0:cc2b234b6255 | 170 | RadioEvents.RxTimeout = OnRxTimeout; |
aizakharov | 0:cc2b234b6255 | 171 | Radio.Init( &RadioEvents ); |
aizakharov | 0:cc2b234b6255 | 172 | |
aizakharov | 0:cc2b234b6255 | 173 | // verify the connection with the board |
aizakharov | 0:cc2b234b6255 | 174 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
aizakharov | 0:cc2b234b6255 | 175 | { |
aizakharov | 0:cc2b234b6255 | 176 | debug( "Radio could not be detected!\n\r", NULL ); |
aizakharov | 0:cc2b234b6255 | 177 | wait( 1 ); |
aizakharov | 0:cc2b234b6255 | 178 | } |
aizakharov | 0:cc2b234b6255 | 179 | |
aizakharov | 0:cc2b234b6255 | 180 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
aizakharov | 0:cc2b234b6255 | 181 | |
aizakharov | 0:cc2b234b6255 | 182 | Radio.SetChannel( RF_FREQUENCY ); |
aizakharov | 0:cc2b234b6255 | 183 | |
aizakharov | 0:cc2b234b6255 | 184 | #if USE_MODEM_LORA == 1 |
aizakharov | 0:cc2b234b6255 | 185 | |
aizakharov | 0:cc2b234b6255 | 186 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 187 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 188 | |
aizakharov | 0:cc2b234b6255 | 189 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
aizakharov | 0:cc2b234b6255 | 190 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
aizakharov | 0:cc2b234b6255 | 191 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
aizakharov | 0:cc2b234b6255 | 192 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
aizakharov | 0:cc2b234b6255 | 193 | LORA_IQ_INVERSION_ON, 2000 ); |
aizakharov | 0:cc2b234b6255 | 194 | |
aizakharov | 0:cc2b234b6255 | 195 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
aizakharov | 0:cc2b234b6255 | 196 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
aizakharov | 0:cc2b234b6255 | 197 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
aizakharov | 0:cc2b234b6255 | 198 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
aizakharov | 0:cc2b234b6255 | 199 | LORA_IQ_INVERSION_ON, true ); |
aizakharov | 0:cc2b234b6255 | 200 | |
aizakharov | 0:cc2b234b6255 | 201 | #elif USE_MODEM_FSK == 1 |
aizakharov | 0:cc2b234b6255 | 202 | |
aizakharov | 0:cc2b234b6255 | 203 | debug("\n\n\r > FSK Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 204 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
aizakharov | 0:cc2b234b6255 | 205 | FSK_DATARATE, 0, |
aizakharov | 0:cc2b234b6255 | 206 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
aizakharov | 0:cc2b234b6255 | 207 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
aizakharov | 0:cc2b234b6255 | 208 | |
aizakharov | 0:cc2b234b6255 | 209 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
aizakharov | 0:cc2b234b6255 | 210 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
aizakharov | 0:cc2b234b6255 | 211 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
aizakharov | 0:cc2b234b6255 | 212 | 0, 0, false, true ); |
aizakharov | 0:cc2b234b6255 | 213 | |
aizakharov | 0:cc2b234b6255 | 214 | #else |
aizakharov | 0:cc2b234b6255 | 215 | |
aizakharov | 0:cc2b234b6255 | 216 | #error "Please define a modem in the compiler options." |
aizakharov | 0:cc2b234b6255 | 217 | |
aizakharov | 0:cc2b234b6255 | 218 | #endif |
aizakharov | 0:cc2b234b6255 | 219 | |
aizakharov | 0:cc2b234b6255 | 220 | //debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
aizakharov | 0:cc2b234b6255 | 221 | |
aizakharov | 0:cc2b234b6255 | 222 | led = 0; |
aizakharov | 0:cc2b234b6255 | 223 | |
aizakharov | 0:cc2b234b6255 | 224 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 225 | |
aizakharov | 0:cc2b234b6255 | 226 | |
aizakharov | 0:cc2b234b6255 | 227 | //int n = 0; |
aizakharov | 0:cc2b234b6255 | 228 | |
aizakharov | 0:cc2b234b6255 | 229 | |
aizakharov | 0:cc2b234b6255 | 230 | |
aizakharov | 0:cc2b234b6255 | 231 | |
aizakharov | 0:cc2b234b6255 | 232 | measure.attach_us(&meas, 200); |
aizakharov | 0:cc2b234b6255 | 233 | |
aizakharov | 0:cc2b234b6255 | 234 | while( 1 ) |
aizakharov | 0:cc2b234b6255 | 235 | { |
aizakharov | 0:cc2b234b6255 | 236 | count++; |
aizakharov | 0:cc2b234b6255 | 237 | if(count>=30) |
aizakharov | 0:cc2b234b6255 | 238 | { |
aizakharov | 0:cc2b234b6255 | 239 | strSize = sprintf ( str, "U=%3.0fV, I=%3.2fA, W=%10.4fkWh", Ud, Id, P); |
aizakharov | 0:cc2b234b6255 | 240 | State = TX; |
aizakharov | 0:cc2b234b6255 | 241 | } |
aizakharov | 0:cc2b234b6255 | 242 | |
aizakharov | 0:cc2b234b6255 | 243 | |
aizakharov | 0:cc2b234b6255 | 244 | lcd.setRGB(0, 255, 0); |
aizakharov | 0:cc2b234b6255 | 245 | |
aizakharov | 0:cc2b234b6255 | 246 | |
aizakharov | 0:cc2b234b6255 | 247 | |
aizakharov | 0:cc2b234b6255 | 248 | sprintf(monitor, "U=%3.0fV, I=%3.2fA", Ud, Id); |
aizakharov | 0:cc2b234b6255 | 249 | sprintf(monitor1, "P=%10.4f kWh", P); |
aizakharov | 0:cc2b234b6255 | 250 | // Print a message to the LCD. |
aizakharov | 0:cc2b234b6255 | 251 | lcd.locate(0, 0); |
aizakharov | 0:cc2b234b6255 | 252 | lcd.print(monitor); |
aizakharov | 0:cc2b234b6255 | 253 | lcd.locate(0, 1); |
aizakharov | 0:cc2b234b6255 | 254 | lcd.print(monitor1); |
aizakharov | 0:cc2b234b6255 | 255 | |
aizakharov | 0:cc2b234b6255 | 256 | |
aizakharov | 0:cc2b234b6255 | 257 | switch( State ) |
aizakharov | 0:cc2b234b6255 | 258 | { |
aizakharov | 0:cc2b234b6255 | 259 | case RX: |
aizakharov | 0:cc2b234b6255 | 260 | /*if( isMaster == true ) |
aizakharov | 0:cc2b234b6255 | 261 | { |
aizakharov | 0:cc2b234b6255 | 262 | if( BufferSize > 0 ) |
aizakharov | 0:cc2b234b6255 | 263 | { |
aizakharov | 0:cc2b234b6255 | 264 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) |
aizakharov | 0:cc2b234b6255 | 265 | { |
aizakharov | 0:cc2b234b6255 | 266 | led = !led; |
aizakharov | 0:cc2b234b6255 | 267 | debug( "...Pong\r\n" ); |
aizakharov | 0:cc2b234b6255 | 268 | // Send the next PING frame |
aizakharov | 0:cc2b234b6255 | 269 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
aizakharov | 0:cc2b234b6255 | 270 | // We fill the buffer with numbers for the payload |
aizakharov | 0:cc2b234b6255 | 271 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 272 | { |
aizakharov | 0:cc2b234b6255 | 273 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 274 | } |
aizakharov | 0:cc2b234b6255 | 275 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 276 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 277 | } |
aizakharov | 0:cc2b234b6255 | 278 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
aizakharov | 0:cc2b234b6255 | 279 | { // A master already exists then become a slave |
aizakharov | 0:cc2b234b6255 | 280 | debug( "...Ping\r\n" ); |
aizakharov | 0:cc2b234b6255 | 281 | led = !led; |
aizakharov | 0:cc2b234b6255 | 282 | isMaster = false; |
aizakharov | 0:cc2b234b6255 | 283 | // Send the next PONG frame |
aizakharov | 0:cc2b234b6255 | 284 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
aizakharov | 0:cc2b234b6255 | 285 | // We fill the buffer with numbers for the payload |
aizakharov | 0:cc2b234b6255 | 286 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 287 | { |
aizakharov | 0:cc2b234b6255 | 288 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 289 | } |
aizakharov | 0:cc2b234b6255 | 290 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 291 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 292 | } |
aizakharov | 0:cc2b234b6255 | 293 | else // valid reception but neither a PING or a PONG message |
aizakharov | 0:cc2b234b6255 | 294 | { // Set device as master ans start again |
aizakharov | 0:cc2b234b6255 | 295 | isMaster = true; |
aizakharov | 0:cc2b234b6255 | 296 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 297 | } |
aizakharov | 0:cc2b234b6255 | 298 | } |
aizakharov | 0:cc2b234b6255 | 299 | } |
aizakharov | 0:cc2b234b6255 | 300 | else |
aizakharov | 0:cc2b234b6255 | 301 | { |
aizakharov | 0:cc2b234b6255 | 302 | if( BufferSize > 0 ) |
aizakharov | 0:cc2b234b6255 | 303 | { |
aizakharov | 0:cc2b234b6255 | 304 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
aizakharov | 0:cc2b234b6255 | 305 | { |
aizakharov | 0:cc2b234b6255 | 306 | led = !led; |
aizakharov | 0:cc2b234b6255 | 307 | debug( "...Ping\r\n" ); |
aizakharov | 0:cc2b234b6255 | 308 | // Send the reply to the PING string |
aizakharov | 0:cc2b234b6255 | 309 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
aizakharov | 0:cc2b234b6255 | 310 | // We fill the buffer with numbers for the payload |
aizakharov | 0:cc2b234b6255 | 311 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 312 | { |
aizakharov | 0:cc2b234b6255 | 313 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 314 | } |
aizakharov | 0:cc2b234b6255 | 315 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 316 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 317 | } |
aizakharov | 0:cc2b234b6255 | 318 | else // valid reception but not a PING as expected |
aizakharov | 0:cc2b234b6255 | 319 | { // Set device as master and start again |
aizakharov | 0:cc2b234b6255 | 320 | isMaster = true; |
aizakharov | 0:cc2b234b6255 | 321 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 322 | } |
aizakharov | 0:cc2b234b6255 | 323 | } |
aizakharov | 0:cc2b234b6255 | 324 | }*/ |
aizakharov | 0:cc2b234b6255 | 325 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 326 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 327 | break; |
aizakharov | 0:cc2b234b6255 | 328 | case TX: |
aizakharov | 0:cc2b234b6255 | 329 | |
aizakharov | 0:cc2b234b6255 | 330 | strcpy(( char* )Buffer, ( char* ) str); |
aizakharov | 0:cc2b234b6255 | 331 | |
aizakharov | 0:cc2b234b6255 | 332 | /*led = !led; |
aizakharov | 0:cc2b234b6255 | 333 | if( isMaster == true ) |
aizakharov | 0:cc2b234b6255 | 334 | { |
aizakharov | 0:cc2b234b6255 | 335 | debug( "Ping...\r\n" ); |
aizakharov | 0:cc2b234b6255 | 336 | } |
aizakharov | 0:cc2b234b6255 | 337 | else |
aizakharov | 0:cc2b234b6255 | 338 | { |
aizakharov | 0:cc2b234b6255 | 339 | debug( "Pong...\r\n" ); |
aizakharov | 0:cc2b234b6255 | 340 | }*/ |
aizakharov | 0:cc2b234b6255 | 341 | /*for( i = strSize; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 342 | { |
aizakharov | 0:cc2b234b6255 | 343 | Buffer[i] = i - strSize; |
aizakharov | 0:cc2b234b6255 | 344 | }*/ |
aizakharov | 0:cc2b234b6255 | 345 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 346 | Radio.Send( Buffer, BUFFER_SIZE ); |
aizakharov | 0:cc2b234b6255 | 347 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 348 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 349 | break; |
aizakharov | 0:cc2b234b6255 | 350 | case RX_TIMEOUT: |
aizakharov | 0:cc2b234b6255 | 351 | /*if( isMaster == true ) |
aizakharov | 0:cc2b234b6255 | 352 | { |
aizakharov | 0:cc2b234b6255 | 353 | // Send the next PING frame |
aizakharov | 0:cc2b234b6255 | 354 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
aizakharov | 0:cc2b234b6255 | 355 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 356 | { |
aizakharov | 0:cc2b234b6255 | 357 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 358 | } |
aizakharov | 0:cc2b234b6255 | 359 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 360 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 361 | } |
aizakharov | 0:cc2b234b6255 | 362 | else |
aizakharov | 0:cc2b234b6255 | 363 | { |
aizakharov | 0:cc2b234b6255 | 364 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 365 | }*/ |
aizakharov | 0:cc2b234b6255 | 366 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 367 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 368 | break; |
aizakharov | 0:cc2b234b6255 | 369 | case RX_ERROR: |
aizakharov | 0:cc2b234b6255 | 370 | // We have received a Packet with a CRC error, send reply as if packet was correct |
aizakharov | 0:cc2b234b6255 | 371 | /*if( isMaster == true ) |
aizakharov | 0:cc2b234b6255 | 372 | { |
aizakharov | 0:cc2b234b6255 | 373 | // Send the next PING frame |
aizakharov | 0:cc2b234b6255 | 374 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
aizakharov | 0:cc2b234b6255 | 375 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 376 | { |
aizakharov | 0:cc2b234b6255 | 377 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 378 | } |
aizakharov | 0:cc2b234b6255 | 379 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 380 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 381 | } |
aizakharov | 0:cc2b234b6255 | 382 | else |
aizakharov | 0:cc2b234b6255 | 383 | { |
aizakharov | 0:cc2b234b6255 | 384 | // Send the next PONG frame |
aizakharov | 0:cc2b234b6255 | 385 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
aizakharov | 0:cc2b234b6255 | 386 | for( i = 4; i < BufferSize; i++ ) |
aizakharov | 0:cc2b234b6255 | 387 | { |
aizakharov | 0:cc2b234b6255 | 388 | Buffer[i] = i - 4; |
aizakharov | 0:cc2b234b6255 | 389 | } |
aizakharov | 0:cc2b234b6255 | 390 | wait_ms( 10 ); |
aizakharov | 0:cc2b234b6255 | 391 | Radio.Send( Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 392 | }*/ |
aizakharov | 0:cc2b234b6255 | 393 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 394 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 395 | break; |
aizakharov | 0:cc2b234b6255 | 396 | case TX_TIMEOUT: |
aizakharov | 0:cc2b234b6255 | 397 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 398 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 399 | break; |
aizakharov | 0:cc2b234b6255 | 400 | case LOWPOWER: |
aizakharov | 0:cc2b234b6255 | 401 | break; |
aizakharov | 0:cc2b234b6255 | 402 | default: |
aizakharov | 0:cc2b234b6255 | 403 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 404 | break; |
aizakharov | 0:cc2b234b6255 | 405 | } |
aizakharov | 0:cc2b234b6255 | 406 | wait(1); |
aizakharov | 0:cc2b234b6255 | 407 | } |
aizakharov | 0:cc2b234b6255 | 408 | } |
aizakharov | 0:cc2b234b6255 | 409 | |
aizakharov | 0:cc2b234b6255 | 410 | void OnTxDone( void ) |
aizakharov | 0:cc2b234b6255 | 411 | { |
aizakharov | 0:cc2b234b6255 | 412 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 413 | State = TX; |
aizakharov | 0:cc2b234b6255 | 414 | debug_if( DEBUG_MESSAGE, "\r\n> OnTxDone\n\r" ); |
aizakharov | 0:cc2b234b6255 | 415 | } |
aizakharov | 0:cc2b234b6255 | 416 | |
aizakharov | 0:cc2b234b6255 | 417 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
aizakharov | 0:cc2b234b6255 | 418 | { |
aizakharov | 0:cc2b234b6255 | 419 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 420 | BufferSize = size; |
aizakharov | 0:cc2b234b6255 | 421 | memcpy( Buffer, payload, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 422 | RssiValue = rssi; |
aizakharov | 0:cc2b234b6255 | 423 | SnrValue = snr; |
aizakharov | 0:cc2b234b6255 | 424 | |
aizakharov | 0:cc2b234b6255 | 425 | State = RX; |
aizakharov | 0:cc2b234b6255 | 426 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxDone\n\r" ); |
aizakharov | 0:cc2b234b6255 | 427 | } |
aizakharov | 0:cc2b234b6255 | 428 | |
aizakharov | 0:cc2b234b6255 | 429 | void OnTxTimeout( void ) |
aizakharov | 0:cc2b234b6255 | 430 | { |
aizakharov | 0:cc2b234b6255 | 431 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 432 | State = TX_TIMEOUT; |
aizakharov | 0:cc2b234b6255 | 433 | debug_if( DEBUG_MESSAGE, "\r\n> OnTxTimeout\n\r" ); |
aizakharov | 0:cc2b234b6255 | 434 | } |
aizakharov | 0:cc2b234b6255 | 435 | |
aizakharov | 0:cc2b234b6255 | 436 | void OnRxTimeout( void ) |
aizakharov | 0:cc2b234b6255 | 437 | { |
aizakharov | 0:cc2b234b6255 | 438 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 439 | Buffer[BufferSize] = 0; |
aizakharov | 0:cc2b234b6255 | 440 | State = RX_TIMEOUT; |
aizakharov | 0:cc2b234b6255 | 441 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxTimeout\n\r" ); |
aizakharov | 0:cc2b234b6255 | 442 | } |
aizakharov | 0:cc2b234b6255 | 443 | |
aizakharov | 0:cc2b234b6255 | 444 | void OnRxError( void ) |
aizakharov | 0:cc2b234b6255 | 445 | { |
aizakharov | 0:cc2b234b6255 | 446 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 447 | State = RX_ERROR; |
aizakharov | 0:cc2b234b6255 | 448 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxError\n\r" ); |
aizakharov | 0:cc2b234b6255 | 449 | } |