![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
hg
Dependencies: LoRaTransmitter mbed BME280 SX1272Lib
main.cpp@1:30ce00fbab55, 2022-05-13 (annotated)
- Committer:
- aizakharov
- Date:
- Fri May 13 15:45:57 2022 +0000
- Revision:
- 1:30ce00fbab55
- Parent:
- 0:cc2b234b6255
- Child:
- 2:2bcb1840d680
- Child:
- 3:4e51739d3a97
dfgdfg
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 send; |
aizakharov | 0:cc2b234b6255 | 65 | Grove_LCD_RGB_Backlight lcd(I2C_SDA, I2C_SCL); |
aizakharov | 0:cc2b234b6255 | 66 | |
aizakharov | 0:cc2b234b6255 | 67 | //Grove_LCD_RGB_Backlight rgbLCD(I2C_SDA, I2C_SCL); |
aizakharov | 0:cc2b234b6255 | 68 | |
aizakharov | 0:cc2b234b6255 | 69 | /* |
aizakharov | 0:cc2b234b6255 | 70 | * Global variables declarations |
aizakharov | 0:cc2b234b6255 | 71 | */ |
aizakharov | 0:cc2b234b6255 | 72 | typedef enum |
aizakharov | 0:cc2b234b6255 | 73 | { |
aizakharov | 0:cc2b234b6255 | 74 | LOWPOWER = 0, |
aizakharov | 0:cc2b234b6255 | 75 | IDLE, |
aizakharov | 0:cc2b234b6255 | 76 | |
aizakharov | 0:cc2b234b6255 | 77 | RX, |
aizakharov | 0:cc2b234b6255 | 78 | RX_TIMEOUT, |
aizakharov | 0:cc2b234b6255 | 79 | RX_ERROR, |
aizakharov | 0:cc2b234b6255 | 80 | |
aizakharov | 0:cc2b234b6255 | 81 | TX, |
aizakharov | 0:cc2b234b6255 | 82 | TX_TIMEOUT, |
aizakharov | 0:cc2b234b6255 | 83 | |
aizakharov | 0:cc2b234b6255 | 84 | CAD, |
aizakharov | 0:cc2b234b6255 | 85 | CAD_DONE |
aizakharov | 0:cc2b234b6255 | 86 | }AppStates_t; |
aizakharov | 0:cc2b234b6255 | 87 | |
aizakharov | 0:cc2b234b6255 | 88 | volatile AppStates_t State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 89 | |
aizakharov | 0:cc2b234b6255 | 90 | /*! |
aizakharov | 0:cc2b234b6255 | 91 | * Radio events function pointer |
aizakharov | 0:cc2b234b6255 | 92 | */ |
aizakharov | 0:cc2b234b6255 | 93 | static RadioEvents_t RadioEvents; |
aizakharov | 0:cc2b234b6255 | 94 | |
aizakharov | 0:cc2b234b6255 | 95 | /* |
aizakharov | 0:cc2b234b6255 | 96 | * Global variables declarations |
aizakharov | 0:cc2b234b6255 | 97 | */ |
aizakharov | 0:cc2b234b6255 | 98 | SX1272MB2xAS Radio( NULL ); |
aizakharov | 0:cc2b234b6255 | 99 | |
aizakharov | 0:cc2b234b6255 | 100 | //const uint8_t Msg[]; |
aizakharov | 0:cc2b234b6255 | 101 | |
aizakharov | 0:cc2b234b6255 | 102 | uint16_t BufferSize = BUFFER_SIZE; |
aizakharov | 0:cc2b234b6255 | 103 | uint8_t Buffer[BUFFER_SIZE]; |
aizakharov | 0:cc2b234b6255 | 104 | |
aizakharov | 0:cc2b234b6255 | 105 | int16_t RssiValue = 0.0; |
aizakharov | 0:cc2b234b6255 | 106 | int8_t SnrValue = 0.0; |
aizakharov | 0:cc2b234b6255 | 107 | //float measured_sinus = 0.0; |
aizakharov | 0:cc2b234b6255 | 108 | int16_t j = 0; |
aizakharov | 1:30ce00fbab55 | 109 | float voltage_sinus, current_sinus; |
aizakharov | 1:30ce00fbab55 | 110 | volatile float Vsum = 0,Vmax =0, Vmean = 0, Imax =0, Isum = 0, Imean = 0, Ud = 0, Id = 0, cosi = 0; |
aizakharov | 1:30ce00fbab55 | 111 | volatile double P = 0, P2 = 0, S2 = 0, P1=0, S1=0; |
aizakharov | 1:30ce00fbab55 | 112 | |
aizakharov | 0:cc2b234b6255 | 113 | uint16_t strSize = 0; |
aizakharov | 0:cc2b234b6255 | 114 | char str[BUFFER_SIZE]; |
aizakharov | 1:30ce00fbab55 | 115 | char monitor[16]="0", monitor1[16]="0"; |
aizakharov | 1:30ce00fbab55 | 116 | |
aizakharov | 1:30ce00fbab55 | 117 | |
aizakharov | 0:cc2b234b6255 | 118 | |
aizakharov | 0:cc2b234b6255 | 119 | void meas (void) |
aizakharov | 0:cc2b234b6255 | 120 | { |
aizakharov | 0:cc2b234b6255 | 121 | |
aizakharov | 1:30ce00fbab55 | 122 | j++; |
aizakharov | 1:30ce00fbab55 | 123 | voltage_sinus = voltage*2200; |
aizakharov | 1:30ce00fbab55 | 124 | current_sinus = current*5; |
aizakharov | 1:30ce00fbab55 | 125 | Vsum+=voltage_sinus; |
aizakharov | 1:30ce00fbab55 | 126 | Isum+=current_sinus; |
aizakharov | 1:30ce00fbab55 | 127 | if (voltage_sinus>Vmax) |
aizakharov | 1:30ce00fbab55 | 128 | Vmax=voltage_sinus; |
aizakharov | 1:30ce00fbab55 | 129 | if (current_sinus>Imax) |
aizakharov | 1:30ce00fbab55 | 130 | Imax=current_sinus; |
aizakharov | 1:30ce00fbab55 | 131 | |
aizakharov | 1:30ce00fbab55 | 132 | P=P+(((voltage_sinus-Vmean)*(current_sinus-Imean))*(0.0002/3600000)); |
aizakharov | 1:30ce00fbab55 | 133 | P2=P2+((voltage_sinus-Vmean)*(current_sinus-Imean)); |
aizakharov | 1:30ce00fbab55 | 134 | if((((voltage_sinus-Vmean)*(current_sinus-Imean))*(0.0002/3600000))>0) |
aizakharov | 1:30ce00fbab55 | 135 | S2=S2+((voltage_sinus-Vmean)*(current_sinus-Imean)); |
aizakharov | 1:30ce00fbab55 | 136 | |
aizakharov | 1:30ce00fbab55 | 137 | |
aizakharov | 1:30ce00fbab55 | 138 | |
aizakharov | 1:30ce00fbab55 | 139 | if (j>=300) |
aizakharov | 0:cc2b234b6255 | 140 | { |
aizakharov | 0:cc2b234b6255 | 141 | j = 0; |
aizakharov | 1:30ce00fbab55 | 142 | Vmean=Vsum/(300); |
aizakharov | 1:30ce00fbab55 | 143 | Imean = Isum/(300); |
aizakharov | 1:30ce00fbab55 | 144 | P1=P2/300; S1=S2/300; |
aizakharov | 0:cc2b234b6255 | 145 | Ud=(Vmax-Vmean)/1.41; |
aizakharov | 0:cc2b234b6255 | 146 | Id=(Imax-Imean)/1.41; |
aizakharov | 1:30ce00fbab55 | 147 | Vmax = 0; Imax = 0; |
aizakharov | 1:30ce00fbab55 | 148 | |
aizakharov | 1:30ce00fbab55 | 149 | cosi = P2/S2; |
aizakharov | 1:30ce00fbab55 | 150 | P2=0; S2=0; |
aizakharov | 0:cc2b234b6255 | 151 | } |
aizakharov | 1:30ce00fbab55 | 152 | } |
aizakharov | 1:30ce00fbab55 | 153 | |
aizakharov | 1:30ce00fbab55 | 154 | void transmit (void) |
aizakharov | 1:30ce00fbab55 | 155 | { |
aizakharov | 1:30ce00fbab55 | 156 | strSize = sprintf ( str, "W=%10.4fkWh, cos fi=%5.4f, P1=%5.4f, S1=%5.4f", P, cosi, P2, S2); |
aizakharov | 1:30ce00fbab55 | 157 | State = TX; |
aizakharov | 1:30ce00fbab55 | 158 | |
aizakharov | 0:cc2b234b6255 | 159 | } |
aizakharov | 0:cc2b234b6255 | 160 | |
aizakharov | 0:cc2b234b6255 | 161 | |
aizakharov | 0:cc2b234b6255 | 162 | |
aizakharov | 0:cc2b234b6255 | 163 | |
aizakharov | 0:cc2b234b6255 | 164 | |
aizakharov | 0:cc2b234b6255 | 165 | int main( void ) |
aizakharov | 0:cc2b234b6255 | 166 | { |
aizakharov | 1:30ce00fbab55 | 167 | |
aizakharov | 0:cc2b234b6255 | 168 | |
aizakharov | 0:cc2b234b6255 | 169 | |
aizakharov | 0:cc2b234b6255 | 170 | |
aizakharov | 0:cc2b234b6255 | 171 | |
aizakharov | 0:cc2b234b6255 | 172 | |
aizakharov | 0:cc2b234b6255 | 173 | //uint8_t i; |
aizakharov | 0:cc2b234b6255 | 174 | //bool isMaster = true; |
aizakharov | 0:cc2b234b6255 | 175 | |
aizakharov | 0:cc2b234b6255 | 176 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 177 | |
aizakharov | 0:cc2b234b6255 | 178 | // Initialize Radio driver |
aizakharov | 0:cc2b234b6255 | 179 | RadioEvents.TxDone = OnTxDone; |
aizakharov | 0:cc2b234b6255 | 180 | RadioEvents.RxDone = OnRxDone; |
aizakharov | 0:cc2b234b6255 | 181 | RadioEvents.RxError = OnRxError; |
aizakharov | 0:cc2b234b6255 | 182 | RadioEvents.TxTimeout = OnTxTimeout; |
aizakharov | 0:cc2b234b6255 | 183 | RadioEvents.RxTimeout = OnRxTimeout; |
aizakharov | 0:cc2b234b6255 | 184 | Radio.Init( &RadioEvents ); |
aizakharov | 0:cc2b234b6255 | 185 | |
aizakharov | 0:cc2b234b6255 | 186 | // verify the connection with the board |
aizakharov | 0:cc2b234b6255 | 187 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
aizakharov | 0:cc2b234b6255 | 188 | { |
aizakharov | 0:cc2b234b6255 | 189 | debug( "Radio could not be detected!\n\r", NULL ); |
aizakharov | 0:cc2b234b6255 | 190 | wait( 1 ); |
aizakharov | 0:cc2b234b6255 | 191 | } |
aizakharov | 0:cc2b234b6255 | 192 | |
aizakharov | 0:cc2b234b6255 | 193 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
aizakharov | 0:cc2b234b6255 | 194 | |
aizakharov | 0:cc2b234b6255 | 195 | Radio.SetChannel( RF_FREQUENCY ); |
aizakharov | 0:cc2b234b6255 | 196 | |
aizakharov | 0:cc2b234b6255 | 197 | #if USE_MODEM_LORA == 1 |
aizakharov | 0:cc2b234b6255 | 198 | |
aizakharov | 0:cc2b234b6255 | 199 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 200 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 201 | |
aizakharov | 0:cc2b234b6255 | 202 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
aizakharov | 0:cc2b234b6255 | 203 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
aizakharov | 0:cc2b234b6255 | 204 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
aizakharov | 0:cc2b234b6255 | 205 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
aizakharov | 0:cc2b234b6255 | 206 | LORA_IQ_INVERSION_ON, 2000 ); |
aizakharov | 0:cc2b234b6255 | 207 | |
aizakharov | 0:cc2b234b6255 | 208 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
aizakharov | 0:cc2b234b6255 | 209 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
aizakharov | 0:cc2b234b6255 | 210 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
aizakharov | 0:cc2b234b6255 | 211 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
aizakharov | 0:cc2b234b6255 | 212 | LORA_IQ_INVERSION_ON, true ); |
aizakharov | 0:cc2b234b6255 | 213 | |
aizakharov | 0:cc2b234b6255 | 214 | #elif USE_MODEM_FSK == 1 |
aizakharov | 0:cc2b234b6255 | 215 | |
aizakharov | 0:cc2b234b6255 | 216 | debug("\n\n\r > FSK Mode < \n\n\r" ); |
aizakharov | 0:cc2b234b6255 | 217 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
aizakharov | 0:cc2b234b6255 | 218 | FSK_DATARATE, 0, |
aizakharov | 0:cc2b234b6255 | 219 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
aizakharov | 0:cc2b234b6255 | 220 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
aizakharov | 0:cc2b234b6255 | 221 | |
aizakharov | 0:cc2b234b6255 | 222 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
aizakharov | 0:cc2b234b6255 | 223 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
aizakharov | 0:cc2b234b6255 | 224 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
aizakharov | 0:cc2b234b6255 | 225 | 0, 0, false, true ); |
aizakharov | 0:cc2b234b6255 | 226 | |
aizakharov | 0:cc2b234b6255 | 227 | #else |
aizakharov | 0:cc2b234b6255 | 228 | |
aizakharov | 0:cc2b234b6255 | 229 | #error "Please define a modem in the compiler options." |
aizakharov | 0:cc2b234b6255 | 230 | |
aizakharov | 0:cc2b234b6255 | 231 | #endif |
aizakharov | 0:cc2b234b6255 | 232 | |
aizakharov | 0:cc2b234b6255 | 233 | //debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
aizakharov | 0:cc2b234b6255 | 234 | |
aizakharov | 0:cc2b234b6255 | 235 | led = 0; |
aizakharov | 0:cc2b234b6255 | 236 | |
aizakharov | 0:cc2b234b6255 | 237 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 238 | |
aizakharov | 0:cc2b234b6255 | 239 | |
aizakharov | 0:cc2b234b6255 | 240 | //int n = 0; |
aizakharov | 0:cc2b234b6255 | 241 | |
aizakharov | 0:cc2b234b6255 | 242 | |
aizakharov | 0:cc2b234b6255 | 243 | |
aizakharov | 0:cc2b234b6255 | 244 | |
aizakharov | 0:cc2b234b6255 | 245 | measure.attach_us(&meas, 200); |
aizakharov | 1:30ce00fbab55 | 246 | send.attach(&transmit, 30); |
aizakharov | 0:cc2b234b6255 | 247 | |
aizakharov | 0:cc2b234b6255 | 248 | while( 1 ) |
aizakharov | 0:cc2b234b6255 | 249 | { |
aizakharov | 1:30ce00fbab55 | 250 | |
aizakharov | 0:cc2b234b6255 | 251 | |
aizakharov | 0:cc2b234b6255 | 252 | |
aizakharov | 0:cc2b234b6255 | 253 | lcd.setRGB(0, 255, 0); |
aizakharov | 0:cc2b234b6255 | 254 | |
aizakharov | 0:cc2b234b6255 | 255 | |
aizakharov | 0:cc2b234b6255 | 256 | |
aizakharov | 0:cc2b234b6255 | 257 | sprintf(monitor, "U=%3.0fV, I=%3.2fA", Ud, Id); |
aizakharov | 1:30ce00fbab55 | 258 | sprintf(monitor1, "P=%8.2f kWh", P); |
aizakharov | 0:cc2b234b6255 | 259 | // Print a message to the LCD. |
aizakharov | 0:cc2b234b6255 | 260 | lcd.locate(0, 0); |
aizakharov | 0:cc2b234b6255 | 261 | lcd.print(monitor); |
aizakharov | 0:cc2b234b6255 | 262 | lcd.locate(0, 1); |
aizakharov | 0:cc2b234b6255 | 263 | lcd.print(monitor1); |
aizakharov | 0:cc2b234b6255 | 264 | |
aizakharov | 1:30ce00fbab55 | 265 | switch( State ) |
aizakharov | 0:cc2b234b6255 | 266 | { |
aizakharov | 0:cc2b234b6255 | 267 | case RX: |
aizakharov | 1:30ce00fbab55 | 268 | |
aizakharov | 0:cc2b234b6255 | 269 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 270 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 271 | break; |
aizakharov | 0:cc2b234b6255 | 272 | case TX: |
aizakharov | 0:cc2b234b6255 | 273 | |
aizakharov | 0:cc2b234b6255 | 274 | strcpy(( char* )Buffer, ( char* ) str); |
aizakharov | 1:30ce00fbab55 | 275 | Radio.Send((uint8_t *) Buffer, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 276 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 277 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 278 | break; |
aizakharov | 0:cc2b234b6255 | 279 | case RX_TIMEOUT: |
aizakharov | 0:cc2b234b6255 | 280 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 281 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 282 | break; |
aizakharov | 0:cc2b234b6255 | 283 | case RX_ERROR: |
aizakharov | 0:cc2b234b6255 | 284 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 285 | break; |
aizakharov | 0:cc2b234b6255 | 286 | case TX_TIMEOUT: |
aizakharov | 1:30ce00fbab55 | 287 | Radio.Rx( RX_TIMEOUT_VALUE ); |
aizakharov | 0:cc2b234b6255 | 288 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 289 | break; |
aizakharov | 0:cc2b234b6255 | 290 | case LOWPOWER: |
aizakharov | 0:cc2b234b6255 | 291 | break; |
aizakharov | 0:cc2b234b6255 | 292 | default: |
aizakharov | 0:cc2b234b6255 | 293 | State = LOWPOWER; |
aizakharov | 0:cc2b234b6255 | 294 | break; |
aizakharov | 0:cc2b234b6255 | 295 | } |
aizakharov | 1:30ce00fbab55 | 296 | |
aizakharov | 1:30ce00fbab55 | 297 | wait(5); |
aizakharov | 0:cc2b234b6255 | 298 | } |
aizakharov | 0:cc2b234b6255 | 299 | } |
aizakharov | 0:cc2b234b6255 | 300 | |
aizakharov | 0:cc2b234b6255 | 301 | void OnTxDone( void ) |
aizakharov | 0:cc2b234b6255 | 302 | { |
aizakharov | 0:cc2b234b6255 | 303 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 304 | State = TX; |
aizakharov | 0:cc2b234b6255 | 305 | debug_if( DEBUG_MESSAGE, "\r\n> OnTxDone\n\r" ); |
aizakharov | 0:cc2b234b6255 | 306 | } |
aizakharov | 0:cc2b234b6255 | 307 | |
aizakharov | 0:cc2b234b6255 | 308 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
aizakharov | 0:cc2b234b6255 | 309 | { |
aizakharov | 0:cc2b234b6255 | 310 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 311 | BufferSize = size; |
aizakharov | 0:cc2b234b6255 | 312 | memcpy( Buffer, payload, BufferSize ); |
aizakharov | 0:cc2b234b6255 | 313 | RssiValue = rssi; |
aizakharov | 0:cc2b234b6255 | 314 | SnrValue = snr; |
aizakharov | 0:cc2b234b6255 | 315 | |
aizakharov | 0:cc2b234b6255 | 316 | State = RX; |
aizakharov | 0:cc2b234b6255 | 317 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxDone\n\r" ); |
aizakharov | 0:cc2b234b6255 | 318 | } |
aizakharov | 0:cc2b234b6255 | 319 | |
aizakharov | 0:cc2b234b6255 | 320 | void OnTxTimeout( void ) |
aizakharov | 0:cc2b234b6255 | 321 | { |
aizakharov | 0:cc2b234b6255 | 322 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 323 | State = TX_TIMEOUT; |
aizakharov | 0:cc2b234b6255 | 324 | debug_if( DEBUG_MESSAGE, "\r\n> OnTxTimeout\n\r" ); |
aizakharov | 0:cc2b234b6255 | 325 | } |
aizakharov | 0:cc2b234b6255 | 326 | |
aizakharov | 0:cc2b234b6255 | 327 | void OnRxTimeout( void ) |
aizakharov | 0:cc2b234b6255 | 328 | { |
aizakharov | 0:cc2b234b6255 | 329 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 330 | Buffer[BufferSize] = 0; |
aizakharov | 0:cc2b234b6255 | 331 | State = RX_TIMEOUT; |
aizakharov | 0:cc2b234b6255 | 332 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxTimeout\n\r" ); |
aizakharov | 0:cc2b234b6255 | 333 | } |
aizakharov | 0:cc2b234b6255 | 334 | |
aizakharov | 0:cc2b234b6255 | 335 | void OnRxError( void ) |
aizakharov | 0:cc2b234b6255 | 336 | { |
aizakharov | 0:cc2b234b6255 | 337 | Radio.Sleep( ); |
aizakharov | 0:cc2b234b6255 | 338 | State = RX_ERROR; |
aizakharov | 0:cc2b234b6255 | 339 | debug_if( DEBUG_MESSAGE, "\r\n> OnRxError\n\r" ); |
aizakharov | 0:cc2b234b6255 | 340 | } |