![](/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
SX1276GenericPingPong/GenericPingPong.cpp@35:bae9b236070b, 2018-07-31 (annotated)
- Committer:
- dev_alexander
- Date:
- Tue Jul 31 21:16:10 2018 +0000
- Revision:
- 35:bae9b236070b
- Parent:
- 32:b108ed6096b0
- Child:
- 36:09e8b51fc91d
This got the theoretical transmition rate up to 160 received LoRa transmitions per minute, however there is still some issues.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:c43b6919ae15 | 1 | /* |
Helmut64 | 0:c43b6919ae15 | 2 | * This file contains a copy of the master content sx1276PingPong |
Helmut64 | 0:c43b6919ae15 | 3 | * with adaption for the SX1276Generic environment |
Helmut64 | 0:c43b6919ae15 | 4 | * (c) 2017 Helmut Tschemernjak |
Helmut64 | 0:c43b6919ae15 | 5 | * 30826 Garbsen (Hannover) Germany |
Helmut64 | 0:c43b6919ae15 | 6 | */ |
dev_alexander | 32:b108ed6096b0 | 7 | |
Helmut64 | 0:c43b6919ae15 | 8 | #include "mbed.h" |
Helmut64 | 0:c43b6919ae15 | 9 | #include "PinMap.h" |
Helmut64 | 0:c43b6919ae15 | 10 | #include "GenericPingPong.h" |
Helmut64 | 0:c43b6919ae15 | 11 | #include "sx1276-mbed-hal.h" |
Helmut64 | 0:c43b6919ae15 | 12 | #include "main.h" |
dev_alexander | 24:e8d03912f303 | 13 | #include "global_buffers.h" |
Helmut64 | 0:c43b6919ae15 | 14 | |
Helmut64 | 0:c43b6919ae15 | 15 | #ifdef FEATURE_LORA |
Helmut64 | 0:c43b6919ae15 | 16 | |
Helmut64 | 0:c43b6919ae15 | 17 | /* Set this flag to '1' to display debug messages on the console */ |
dev_alexander | 32:b108ed6096b0 | 18 | #define DEBUG_MESSAGE 0 |
Helmut64 | 0:c43b6919ae15 | 19 | |
Helmut64 | 0:c43b6919ae15 | 20 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
Helmut64 | 0:c43b6919ae15 | 21 | #define USE_MODEM_LORA 1 |
Helmut64 | 0:c43b6919ae15 | 22 | #define USE_MODEM_FSK !USE_MODEM_LORA |
dev_alexander | 19:9f035b9e65ec | 23 | #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz |
Helmut64 | 0:c43b6919ae15 | 24 | #define TX_OUTPUT_POWER 14 // 14 dBm |
Helmut64 | 0:c43b6919ae15 | 25 | |
Helmut64 | 0:c43b6919ae15 | 26 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 27 | |
Helmut64 | 7:6a8a82bfb0c6 | 28 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
Helmut64 | 0:c43b6919ae15 | 29 | #define LORA_SPREADING_FACTOR LORA_SF7 |
Helmut64 | 0:c43b6919ae15 | 30 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
Helmut64 | 0:c43b6919ae15 | 31 | |
Helmut64 | 0:c43b6919ae15 | 32 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 33 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
Helmut64 | 0:c43b6919ae15 | 34 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
Helmut64 | 0:c43b6919ae15 | 35 | #define LORA_FHSS_ENABLED false |
Helmut64 | 0:c43b6919ae15 | 36 | #define LORA_NB_SYMB_HOP 4 |
Helmut64 | 0:c43b6919ae15 | 37 | #define LORA_IQ_INVERSION_ON false |
Helmut64 | 0:c43b6919ae15 | 38 | #define LORA_CRC_ENABLED true |
Helmut64 | 0:c43b6919ae15 | 39 | |
Helmut64 | 0:c43b6919ae15 | 40 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 41 | |
Helmut64 | 0:c43b6919ae15 | 42 | #define FSK_FDEV 25000 // Hz |
Helmut64 | 0:c43b6919ae15 | 43 | #define FSK_DATARATE 19200 // bps |
Helmut64 | 0:c43b6919ae15 | 44 | #define FSK_BANDWIDTH 50000 // Hz |
Helmut64 | 0:c43b6919ae15 | 45 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
Helmut64 | 0:c43b6919ae15 | 46 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 47 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
Helmut64 | 0:c43b6919ae15 | 48 | #define FSK_CRC_ENABLED true |
Helmut64 | 0:c43b6919ae15 | 49 | |
Helmut64 | 0:c43b6919ae15 | 50 | #else |
Helmut64 | 0:c43b6919ae15 | 51 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 52 | #endif |
Helmut64 | 0:c43b6919ae15 | 53 | |
Helmut64 | 0:c43b6919ae15 | 54 | |
dev_alexander | 35:bae9b236070b | 55 | //#define RX_TIMEOUT_VALUE 3500 // in ms |
dev_alexander | 35:bae9b236070b | 56 | #define RX_TIMEOUT_VALUE 370 // in ms |
Helmut64 | 0:c43b6919ae15 | 57 | |
dev_alexander | 24:e8d03912f303 | 58 | //#define BUFFER_SIZE 64 // Define the payload size here |
Helmut64 | 0:c43b6919ae15 | 59 | |
Helmut64 | 0:c43b6919ae15 | 60 | /* |
Helmut64 | 0:c43b6919ae15 | 61 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 62 | */ |
Helmut64 | 0:c43b6919ae15 | 63 | typedef enum |
Helmut64 | 0:c43b6919ae15 | 64 | { |
Helmut64 | 0:c43b6919ae15 | 65 | LOWPOWER = 0, |
Helmut64 | 0:c43b6919ae15 | 66 | IDLE, |
Helmut64 | 0:c43b6919ae15 | 67 | |
Helmut64 | 0:c43b6919ae15 | 68 | RX, |
Helmut64 | 0:c43b6919ae15 | 69 | RX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 70 | RX_ERROR, |
Helmut64 | 0:c43b6919ae15 | 71 | |
Helmut64 | 0:c43b6919ae15 | 72 | TX, |
Helmut64 | 0:c43b6919ae15 | 73 | TX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 74 | |
Helmut64 | 0:c43b6919ae15 | 75 | CAD, |
Helmut64 | 0:c43b6919ae15 | 76 | CAD_DONE |
Helmut64 | 0:c43b6919ae15 | 77 | } AppStates_t; |
Helmut64 | 0:c43b6919ae15 | 78 | |
Helmut64 | 0:c43b6919ae15 | 79 | volatile AppStates_t State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 80 | |
Helmut64 | 0:c43b6919ae15 | 81 | /*! |
Helmut64 | 0:c43b6919ae15 | 82 | * Radio events function pointer |
Helmut64 | 0:c43b6919ae15 | 83 | */ |
Helmut64 | 0:c43b6919ae15 | 84 | static RadioEvents_t RadioEvents; |
Helmut64 | 0:c43b6919ae15 | 85 | |
Helmut64 | 0:c43b6919ae15 | 86 | /* |
Helmut64 | 0:c43b6919ae15 | 87 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 88 | */ |
Helmut64 | 0:c43b6919ae15 | 89 | SX1276Generic *Radio; |
Helmut64 | 0:c43b6919ae15 | 90 | |
dev_alexander | 27:6b549f838f0a | 91 | |
dev_alexander | 28:0ed92c590607 | 92 | // Aliases for the buffers that are made in the main() |
dev_alexander | 28:0ed92c590607 | 93 | uint8_t *BufferTx; |
dev_alexander | 28:0ed92c590607 | 94 | uint8_t *BufferRx; |
Helmut64 | 0:c43b6919ae15 | 95 | |
Helmut64 | 0:c43b6919ae15 | 96 | DigitalOut *led3; |
Helmut64 | 0:c43b6919ae15 | 97 | |
Helmut64 | 0:c43b6919ae15 | 98 | |
dev_alexander | 28:0ed92c590607 | 99 | int SX1276PingPongSetup(uint8_t *BufferTxFromMain, uint8_t *BufferRxFromMain) |
Helmut64 | 0:c43b6919ae15 | 100 | { |
dev_alexander | 19:9f035b9e65ec | 101 | dprintf("TEST" ); |
Helmut64 | 0:c43b6919ae15 | 102 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
Helmut64 | 0:c43b6919ae15 | 103 | DigitalOut *led = new DigitalOut(LED2); |
Helmut64 | 13:5a32a1922fbc | 104 | #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
Helmut64 | 0:c43b6919ae15 | 105 | DigitalOut *led = new DigitalOut(LED4); // RX red |
Helmut64 | 0:c43b6919ae15 | 106 | led3 = new DigitalOut(LED3); // TX blue |
Helmut64 | 0:c43b6919ae15 | 107 | #else |
Helmut64 | 0:c43b6919ae15 | 108 | DigitalOut *led = new DigitalOut(LED1); |
Helmut64 | 0:c43b6919ae15 | 109 | led3 = led; |
Helmut64 | 0:c43b6919ae15 | 110 | #endif |
Helmut64 | 0:c43b6919ae15 | 111 | |
dev_alexander | 28:0ed92c590607 | 112 | BufferTx = BufferTxFromMain; |
dev_alexander | 28:0ed92c590607 | 113 | BufferRx = BufferRxFromMain; |
Helmut64 | 0:c43b6919ae15 | 114 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 115 | |
Helmut64 | 0:c43b6919ae15 | 116 | #ifdef B_L072Z_LRWAN1_LORA |
Helmut64 | 0:c43b6919ae15 | 117 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
Helmut64 | 0:c43b6919ae15 | 118 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 119 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
Helmut64 | 0:c43b6919ae15 | 120 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
Helmut64 | 0:c43b6919ae15 | 121 | #else // RFM95 |
Helmut64 | 0:c43b6919ae15 | 122 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
Helmut64 | 0:c43b6919ae15 | 123 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 124 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
Helmut64 | 0:c43b6919ae15 | 125 | |
Helmut64 | 0:c43b6919ae15 | 126 | #endif |
Helmut64 | 0:c43b6919ae15 | 127 | |
Helmut64 | 0:c43b6919ae15 | 128 | dprintf("SX1276 Ping Pong Demo Application" ); |
Helmut64 | 0:c43b6919ae15 | 129 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 130 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 131 | #if USE_MODEM_LORA == 1 |
Helmut64 | 8:3b0d7b4ff28f | 132 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 133 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 134 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 135 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 136 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 137 | #endif |
Helmut64 | 0:c43b6919ae15 | 138 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 139 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 140 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 141 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 142 | RadioEvents.TxTimeout = OnTxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 143 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 144 | if (Radio->Init( &RadioEvents ) == false) { |
Helmut64 | 6:1b598b0e52e4 | 145 | while(1) { |
Helmut64 | 6:1b598b0e52e4 | 146 | dprintf("Radio could not be detected!"); |
Helmut64 | 6:1b598b0e52e4 | 147 | wait( 1 ); |
Helmut64 | 6:1b598b0e52e4 | 148 | } |
Helmut64 | 0:c43b6919ae15 | 149 | } |
Helmut64 | 6:1b598b0e52e4 | 150 | |
Helmut64 | 0:c43b6919ae15 | 151 | |
Helmut64 | 0:c43b6919ae15 | 152 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 153 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 154 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 155 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 156 | break; |
Helmut64 | 0:c43b6919ae15 | 157 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 158 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 159 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 160 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 161 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 162 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 163 | break; |
Helmut64 | 0:c43b6919ae15 | 164 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 165 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 166 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 167 | break; |
Helmut64 | 0:c43b6919ae15 | 168 | default: |
Helmut64 | 0:c43b6919ae15 | 169 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 170 | } |
Helmut64 | 0:c43b6919ae15 | 171 | |
Helmut64 | 0:c43b6919ae15 | 172 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 173 | |
Helmut64 | 0:c43b6919ae15 | 174 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 175 | |
Helmut64 | 0:c43b6919ae15 | 176 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 177 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 178 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 179 | dprintf(" > LORA Mode <"); |
dev_alexander | 25:1a031add188a | 180 | |
dev_alexander | 25:1a031add188a | 181 | |
dev_alexander | 25:1a031add188a | 182 | // Without this line, the default max payload length is only 64 bytes |
dev_alexander | 25:1a031add188a | 183 | Radio->SetMaxPayloadLength(MODEM_LORA, 255); |
Helmut64 | 0:c43b6919ae15 | 184 | |
Helmut64 | 0:c43b6919ae15 | 185 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
Helmut64 | 0:c43b6919ae15 | 186 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
Helmut64 | 0:c43b6919ae15 | 187 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 188 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 189 | LORA_IQ_INVERSION_ON, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 190 | |
Helmut64 | 0:c43b6919ae15 | 191 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Helmut64 | 0:c43b6919ae15 | 192 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 193 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Helmut64 | 0:c43b6919ae15 | 194 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 195 | LORA_IQ_INVERSION_ON, true ); |
Helmut64 | 0:c43b6919ae15 | 196 | |
Helmut64 | 0:c43b6919ae15 | 197 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 198 | |
Helmut64 | 0:c43b6919ae15 | 199 | dprintf(" > FSK Mode <"); |
Helmut64 | 0:c43b6919ae15 | 200 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
Helmut64 | 0:c43b6919ae15 | 201 | FSK_DATARATE, 0, |
Helmut64 | 0:c43b6919ae15 | 202 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 203 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 204 | |
Helmut64 | 0:c43b6919ae15 | 205 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
Helmut64 | 0:c43b6919ae15 | 206 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 207 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
Helmut64 | 0:c43b6919ae15 | 208 | 0, 0, false, true ); |
Helmut64 | 0:c43b6919ae15 | 209 | |
Helmut64 | 0:c43b6919ae15 | 210 | #else |
Helmut64 | 0:c43b6919ae15 | 211 | |
Helmut64 | 0:c43b6919ae15 | 212 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 213 | |
Helmut64 | 0:c43b6919ae15 | 214 | #endif |
Helmut64 | 0:c43b6919ae15 | 215 | |
Helmut64 | 0:c43b6919ae15 | 216 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 217 | dprintf("Starting Ping-Pong loop"); |
Helmut64 | 0:c43b6919ae15 | 218 | |
Helmut64 | 0:c43b6919ae15 | 219 | |
Helmut64 | 0:c43b6919ae15 | 220 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 221 | } |
dev_alexander | 22:abca9d17d13d | 222 | |
dev_alexander | 25:1a031add188a | 223 | /**************************************************************************************************************************************** |
dev_alexander | 25:1a031add188a | 224 | * |
dev_alexander | 25:1a031add188a | 225 | ****************************************************************************************************************************************/ |
dev_alexander | 22:abca9d17d13d | 226 | int SX1276PingPong(void) |
dev_alexander | 22:abca9d17d13d | 227 | { |
dev_alexander | 22:abca9d17d13d | 228 | #if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway |
dev_alexander | 22:abca9d17d13d | 229 | bool isMaster = true; |
dev_alexander | 23:f74a50977593 | 230 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot |
dev_alexander | 22:abca9d17d13d | 231 | bool isMaster = false; |
Helmut64 | 0:c43b6919ae15 | 232 | #endif |
dev_alexander | 25:1a031add188a | 233 | |
dev_alexander | 22:abca9d17d13d | 234 | switch( State ) |
dev_alexander | 22:abca9d17d13d | 235 | { |
dev_alexander | 22:abca9d17d13d | 236 | case RX: |
dev_alexander | 28:0ed92c590607 | 237 | // *led3 = 0; |
dev_alexander | 22:abca9d17d13d | 238 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 239 | { |
dev_alexander | 24:e8d03912f303 | 240 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 241 | { |
dev_alexander | 32:b108ed6096b0 | 242 | /*This checks if the ID of the received transaction is matching that of |
dev_alexander | 32:b108ed6096b0 | 243 | * the slave device's ID. This is Defined in the GenericPingPong.h |
dev_alexander | 32:b108ed6096b0 | 244 | */ |
dev_alexander | 28:0ed92c590607 | 245 | if( memcmp(&BufferRx[rx_idx_signature], PongMsg, sizeof(PongMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 246 | { |
dev_alexander | 22:abca9d17d13d | 247 | // *led = !*led; |
dev_alexander | 32:b108ed6096b0 | 248 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 249 | dprintf( "...Pong" ); |
dev_alexander | 22:abca9d17d13d | 250 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 251 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 252 | } |
dev_alexander | 22:abca9d17d13d | 253 | else // valid reception but neither a PING or a PONG message |
dev_alexander | 22:abca9d17d13d | 254 | { // Set device as master ans start again |
dev_alexander | 24:e8d03912f303 | 255 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 256 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 257 | } |
Helmut64 | 0:c43b6919ae15 | 258 | } |
dev_alexander | 22:abca9d17d13d | 259 | } |
dev_alexander | 22:abca9d17d13d | 260 | else |
dev_alexander | 22:abca9d17d13d | 261 | { |
dev_alexander | 24:e8d03912f303 | 262 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 263 | { |
dev_alexander | 28:0ed92c590607 | 264 | if( memcmp(BufferRx, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 265 | { |
dev_alexander | 22:abca9d17d13d | 266 | // *led = !*led; |
dev_alexander | 32:b108ed6096b0 | 267 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 268 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 269 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 270 | Radio->Send( BufferTx, BufferSizeTx ); |
Helmut64 | 0:c43b6919ae15 | 271 | } |
dev_alexander | 22:abca9d17d13d | 272 | else // valid reception but not a PING as expected |
dev_alexander | 22:abca9d17d13d | 273 | { // Set device as master and start again |
dev_alexander | 22:abca9d17d13d | 274 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 275 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 276 | } |
Helmut64 | 0:c43b6919ae15 | 277 | } |
dev_alexander | 22:abca9d17d13d | 278 | } |
dev_alexander | 22:abca9d17d13d | 279 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 280 | break; |
dev_alexander | 22:abca9d17d13d | 281 | case TX: |
dev_alexander | 28:0ed92c590607 | 282 | // *led3 = 1; |
dev_alexander | 22:abca9d17d13d | 283 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 284 | { |
dev_alexander | 32:b108ed6096b0 | 285 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 286 | dprintf("Ping..." ); |
dev_alexander | 22:abca9d17d13d | 287 | } |
dev_alexander | 22:abca9d17d13d | 288 | else |
dev_alexander | 22:abca9d17d13d | 289 | { |
dev_alexander | 32:b108ed6096b0 | 290 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 291 | dprintf("Pong..." ); |
dev_alexander | 22:abca9d17d13d | 292 | } |
dev_alexander | 22:abca9d17d13d | 293 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 294 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 295 | break; |
dev_alexander | 22:abca9d17d13d | 296 | case RX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 297 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 298 | { |
dev_alexander | 32:b108ed6096b0 | 299 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 300 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 301 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 302 | } |
dev_alexander | 22:abca9d17d13d | 303 | else |
dev_alexander | 22:abca9d17d13d | 304 | { |
dev_alexander | 22:abca9d17d13d | 305 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 306 | } |
dev_alexander | 22:abca9d17d13d | 307 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 308 | break; |
dev_alexander | 22:abca9d17d13d | 309 | case RX_ERROR: |
dev_alexander | 22:abca9d17d13d | 310 | // We have received a Packet with a CRC error, send reply as if packet was correct |
dev_alexander | 22:abca9d17d13d | 311 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 312 | { |
dev_alexander | 22:abca9d17d13d | 313 | // Send the next PING frame |
dev_alexander | 32:b108ed6096b0 | 314 | // memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 25:1a031add188a | 315 | /* |
dev_alexander | 24:e8d03912f303 | 316 | for( i = 4; i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 317 | { |
dev_alexander | 24:e8d03912f303 | 318 | BufferTx[i] = i - 4; |
Helmut64 | 0:c43b6919ae15 | 319 | } |
dev_alexander | 25:1a031add188a | 320 | */ |
dev_alexander | 28:0ed92c590607 | 321 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 322 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 323 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 324 | } |
dev_alexander | 22:abca9d17d13d | 325 | else |
dev_alexander | 22:abca9d17d13d | 326 | { |
dev_alexander | 22:abca9d17d13d | 327 | // Send the next PONG frame |
dev_alexander | 25:1a031add188a | 328 | // memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 25:1a031add188a | 329 | /* |
dev_alexander | 24:e8d03912f303 | 330 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 331 | { |
dev_alexander | 24:e8d03912f303 | 332 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 333 | } |
dev_alexander | 25:1a031add188a | 334 | */ |
dev_alexander | 28:0ed92c590607 | 335 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 336 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 337 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 338 | } |
dev_alexander | 22:abca9d17d13d | 339 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 340 | break; |
dev_alexander | 22:abca9d17d13d | 341 | case TX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 342 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 343 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 344 | break; |
dev_alexander | 22:abca9d17d13d | 345 | case LOWPOWER: |
dev_alexander | 22:abca9d17d13d | 346 | sleep(); |
dev_alexander | 22:abca9d17d13d | 347 | break; |
dev_alexander | 22:abca9d17d13d | 348 | default: |
dev_alexander | 22:abca9d17d13d | 349 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 350 | break; |
dev_alexander | 22:abca9d17d13d | 351 | } |
Helmut64 | 0:c43b6919ae15 | 352 | } |
Helmut64 | 0:c43b6919ae15 | 353 | |
Helmut64 | 11:d3a591c20cd7 | 354 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 355 | { |
Helmut64 | 0:c43b6919ae15 | 356 | Radio->Sleep( ); |
dev_alexander | 25:1a031add188a | 357 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 358 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 359 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 360 | } |
Helmut64 | 0:c43b6919ae15 | 361 | |
Helmut64 | 11:d3a591c20cd7 | 362 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 363 | { |
Helmut64 | 0:c43b6919ae15 | 364 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 365 | |
dev_alexander | 28:0ed92c590607 | 366 | if(BufferSizeRx != size) |
dev_alexander | 28:0ed92c590607 | 367 | memcpy( BufferRx, payload, size ); |
dev_alexander | 28:0ed92c590607 | 368 | else |
dev_alexander | 28:0ed92c590607 | 369 | memcpy( BufferRx, payload, BufferSizeRx ); |
Helmut64 | 0:c43b6919ae15 | 370 | State = RX; |
dev_alexander | 25:1a031add188a | 371 | |
dev_alexander | 25:1a031add188a | 372 | // Call function that deconstructs payload |
dev_alexander | 28:0ed92c590607 | 373 | // fillGlobalBufsWithPayload(payload); |
dev_alexander | 25:1a031add188a | 374 | |
dev_alexander | 32:b108ed6096b0 | 375 | if (DEBUG_MESSAGE) { |
Helmut64 | 0:c43b6919ae15 | 376 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
dev_alexander | 32:b108ed6096b0 | 377 | dump("Data:", payload, size); |
dev_alexander | 32:b108ed6096b0 | 378 | } |
Helmut64 | 0:c43b6919ae15 | 379 | } |
Helmut64 | 0:c43b6919ae15 | 380 | |
Helmut64 | 11:d3a591c20cd7 | 381 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 382 | { |
dev_alexander | 28:0ed92c590607 | 383 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 384 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 385 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 386 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 387 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 388 | } |
Helmut64 | 0:c43b6919ae15 | 389 | |
Helmut64 | 11:d3a591c20cd7 | 390 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 391 | { |
dev_alexander | 28:0ed92c590607 | 392 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 393 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 394 | BufferRx[BufferSizeRx-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 395 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 396 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 397 | dprintf("> OnRxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 398 | } |
Helmut64 | 0:c43b6919ae15 | 399 | |
Helmut64 | 11:d3a591c20cd7 | 400 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 401 | { |
Helmut64 | 0:c43b6919ae15 | 402 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 403 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 404 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 405 | dprintf("> OnRxError"); |
Helmut64 | 0:c43b6919ae15 | 406 | } |
Helmut64 | 0:c43b6919ae15 | 407 | |
Helmut64 | 0:c43b6919ae15 | 408 | #endif |