![](/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@24:e8d03912f303, 2018-07-18 (annotated)
- Committer:
- dev_alexander
- Date:
- Wed Jul 18 21:25:17 2018 +0000
- Revision:
- 24:e8d03912f303
- Parent:
- 23:f74a50977593
- Child:
- 25:1a031add188a
Modified how buffers are handled in various functions in GenericPingPong.cpp to accommodate for how the buffer sizes for TX and RX are different depending on slave or master status. Still need to finish global_buffers.cpp.
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 | */ |
Helmut64 | 0:c43b6919ae15 | 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 */ |
Helmut64 | 0:c43b6919ae15 | 18 | #define DEBUG_MESSAGE 1 |
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 | |
Helmut64 | 0:c43b6919ae15 | 55 | #define RX_TIMEOUT_VALUE 3500 // in ms |
Helmut64 | 0:c43b6919ae15 | 56 | |
dev_alexander | 24:e8d03912f303 | 57 | //#define BUFFER_SIZE 64 // Define the payload size here |
Helmut64 | 0:c43b6919ae15 | 58 | |
Helmut64 | 0:c43b6919ae15 | 59 | /* |
Helmut64 | 0:c43b6919ae15 | 60 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 61 | */ |
Helmut64 | 0:c43b6919ae15 | 62 | typedef enum |
Helmut64 | 0:c43b6919ae15 | 63 | { |
Helmut64 | 0:c43b6919ae15 | 64 | LOWPOWER = 0, |
Helmut64 | 0:c43b6919ae15 | 65 | IDLE, |
Helmut64 | 0:c43b6919ae15 | 66 | |
Helmut64 | 0:c43b6919ae15 | 67 | RX, |
Helmut64 | 0:c43b6919ae15 | 68 | RX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 69 | RX_ERROR, |
Helmut64 | 0:c43b6919ae15 | 70 | |
Helmut64 | 0:c43b6919ae15 | 71 | TX, |
Helmut64 | 0:c43b6919ae15 | 72 | TX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 73 | |
Helmut64 | 0:c43b6919ae15 | 74 | CAD, |
Helmut64 | 0:c43b6919ae15 | 75 | CAD_DONE |
Helmut64 | 0:c43b6919ae15 | 76 | } AppStates_t; |
Helmut64 | 0:c43b6919ae15 | 77 | |
Helmut64 | 0:c43b6919ae15 | 78 | volatile AppStates_t State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 79 | |
Helmut64 | 0:c43b6919ae15 | 80 | /*! |
Helmut64 | 0:c43b6919ae15 | 81 | * Radio events function pointer |
Helmut64 | 0:c43b6919ae15 | 82 | */ |
Helmut64 | 0:c43b6919ae15 | 83 | static RadioEvents_t RadioEvents; |
Helmut64 | 0:c43b6919ae15 | 84 | |
Helmut64 | 0:c43b6919ae15 | 85 | /* |
Helmut64 | 0:c43b6919ae15 | 86 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 87 | */ |
Helmut64 | 0:c43b6919ae15 | 88 | SX1276Generic *Radio; |
Helmut64 | 0:c43b6919ae15 | 89 | |
Helmut64 | 0:c43b6919ae15 | 90 | |
Helmut64 | 0:c43b6919ae15 | 91 | const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; |
Helmut64 | 0:c43b6919ae15 | 92 | const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; |
Helmut64 | 0:c43b6919ae15 | 93 | |
dev_alexander | 24:e8d03912f303 | 94 | /* determine the appropriate buffer sizes */ |
dev_alexander | 24:e8d03912f303 | 95 | #if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway |
dev_alexander | 24:e8d03912f303 | 96 | uint16_t BufferSizeTx = PAYLOAD_BUFFER_SIZE_MASTER_TO_SLAVE; |
dev_alexander | 24:e8d03912f303 | 97 | uint16_t BufferSizeRx = PAYLOAD_BUFFER_SIZE_SLAVE_TO_MASTER; |
dev_alexander | 24:e8d03912f303 | 98 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot |
dev_alexander | 24:e8d03912f303 | 99 | uint16_t BufferSizeTx = PAYLOAD_BUFFER_SIZE_SLAVE_TO_MASTER; |
dev_alexander | 24:e8d03912f303 | 100 | uint16_t BufferSizeRx = PAYLOAD_BUFFER_SIZE_MASTER_TO_SLAVE; |
dev_alexander | 24:e8d03912f303 | 101 | #endif |
dev_alexander | 24:e8d03912f303 | 102 | |
dev_alexander | 24:e8d03912f303 | 103 | uint8_t *BufferTx; |
dev_alexander | 24:e8d03912f303 | 104 | uint8_t *BufferRx; |
Helmut64 | 0:c43b6919ae15 | 105 | |
Helmut64 | 0:c43b6919ae15 | 106 | DigitalOut *led3; |
Helmut64 | 0:c43b6919ae15 | 107 | |
Helmut64 | 0:c43b6919ae15 | 108 | |
dev_alexander | 22:abca9d17d13d | 109 | int SX1276PingPongSetup(void) |
Helmut64 | 0:c43b6919ae15 | 110 | { |
dev_alexander | 19:9f035b9e65ec | 111 | dprintf("TEST" ); |
Helmut64 | 0:c43b6919ae15 | 112 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
Helmut64 | 0:c43b6919ae15 | 113 | DigitalOut *led = new DigitalOut(LED2); |
Helmut64 | 13:5a32a1922fbc | 114 | #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
Helmut64 | 0:c43b6919ae15 | 115 | DigitalOut *led = new DigitalOut(LED4); // RX red |
Helmut64 | 0:c43b6919ae15 | 116 | led3 = new DigitalOut(LED3); // TX blue |
Helmut64 | 0:c43b6919ae15 | 117 | #else |
Helmut64 | 0:c43b6919ae15 | 118 | DigitalOut *led = new DigitalOut(LED1); |
Helmut64 | 0:c43b6919ae15 | 119 | led3 = led; |
Helmut64 | 0:c43b6919ae15 | 120 | #endif |
Helmut64 | 0:c43b6919ae15 | 121 | |
dev_alexander | 24:e8d03912f303 | 122 | BufferTx = new uint8_t[BufferSizeTx]; |
dev_alexander | 24:e8d03912f303 | 123 | BufferRx = new uint8_t[BufferSizeRx]; |
Helmut64 | 0:c43b6919ae15 | 124 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 125 | |
Helmut64 | 0:c43b6919ae15 | 126 | #ifdef B_L072Z_LRWAN1_LORA |
Helmut64 | 0:c43b6919ae15 | 127 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
Helmut64 | 0:c43b6919ae15 | 128 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 129 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
Helmut64 | 0:c43b6919ae15 | 130 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
Helmut64 | 0:c43b6919ae15 | 131 | #else // RFM95 |
Helmut64 | 0:c43b6919ae15 | 132 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
Helmut64 | 0:c43b6919ae15 | 133 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 134 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
Helmut64 | 0:c43b6919ae15 | 135 | |
Helmut64 | 0:c43b6919ae15 | 136 | #endif |
Helmut64 | 0:c43b6919ae15 | 137 | |
Helmut64 | 0:c43b6919ae15 | 138 | dprintf("SX1276 Ping Pong Demo Application" ); |
Helmut64 | 0:c43b6919ae15 | 139 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 140 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 141 | #if USE_MODEM_LORA == 1 |
Helmut64 | 8:3b0d7b4ff28f | 142 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 143 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 144 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 145 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 146 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 147 | #endif |
Helmut64 | 0:c43b6919ae15 | 148 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 149 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 150 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 151 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 152 | RadioEvents.TxTimeout = OnTxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 153 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 154 | if (Radio->Init( &RadioEvents ) == false) { |
Helmut64 | 6:1b598b0e52e4 | 155 | while(1) { |
Helmut64 | 6:1b598b0e52e4 | 156 | dprintf("Radio could not be detected!"); |
Helmut64 | 6:1b598b0e52e4 | 157 | wait( 1 ); |
Helmut64 | 6:1b598b0e52e4 | 158 | } |
Helmut64 | 0:c43b6919ae15 | 159 | } |
Helmut64 | 6:1b598b0e52e4 | 160 | |
Helmut64 | 0:c43b6919ae15 | 161 | |
Helmut64 | 0:c43b6919ae15 | 162 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 163 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 164 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 165 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 166 | break; |
Helmut64 | 0:c43b6919ae15 | 167 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 168 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 169 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 170 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 171 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 172 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 173 | break; |
Helmut64 | 0:c43b6919ae15 | 174 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 175 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 176 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 177 | break; |
Helmut64 | 0:c43b6919ae15 | 178 | default: |
Helmut64 | 0:c43b6919ae15 | 179 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 180 | } |
Helmut64 | 0:c43b6919ae15 | 181 | |
Helmut64 | 0:c43b6919ae15 | 182 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 183 | |
Helmut64 | 0:c43b6919ae15 | 184 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 185 | |
Helmut64 | 0:c43b6919ae15 | 186 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 187 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 188 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 189 | dprintf(" > LORA Mode <"); |
Helmut64 | 0:c43b6919ae15 | 190 | |
Helmut64 | 0:c43b6919ae15 | 191 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
Helmut64 | 0:c43b6919ae15 | 192 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
Helmut64 | 0:c43b6919ae15 | 193 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 194 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 195 | LORA_IQ_INVERSION_ON, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 196 | |
Helmut64 | 0:c43b6919ae15 | 197 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Helmut64 | 0:c43b6919ae15 | 198 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 199 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Helmut64 | 0:c43b6919ae15 | 200 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 201 | LORA_IQ_INVERSION_ON, true ); |
Helmut64 | 0:c43b6919ae15 | 202 | |
Helmut64 | 0:c43b6919ae15 | 203 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 204 | |
Helmut64 | 0:c43b6919ae15 | 205 | dprintf(" > FSK Mode <"); |
Helmut64 | 0:c43b6919ae15 | 206 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
Helmut64 | 0:c43b6919ae15 | 207 | FSK_DATARATE, 0, |
Helmut64 | 0:c43b6919ae15 | 208 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 209 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 210 | |
Helmut64 | 0:c43b6919ae15 | 211 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
Helmut64 | 0:c43b6919ae15 | 212 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 213 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
Helmut64 | 0:c43b6919ae15 | 214 | 0, 0, false, true ); |
Helmut64 | 0:c43b6919ae15 | 215 | |
Helmut64 | 0:c43b6919ae15 | 216 | #else |
Helmut64 | 0:c43b6919ae15 | 217 | |
Helmut64 | 0:c43b6919ae15 | 218 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 219 | |
Helmut64 | 0:c43b6919ae15 | 220 | #endif |
Helmut64 | 0:c43b6919ae15 | 221 | |
Helmut64 | 0:c43b6919ae15 | 222 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 223 | dprintf("Starting Ping-Pong loop"); |
Helmut64 | 0:c43b6919ae15 | 224 | |
Helmut64 | 0:c43b6919ae15 | 225 | |
Helmut64 | 0:c43b6919ae15 | 226 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 227 | } |
dev_alexander | 22:abca9d17d13d | 228 | |
dev_alexander | 22:abca9d17d13d | 229 | int SX1276PingPong(void) |
dev_alexander | 22:abca9d17d13d | 230 | { |
dev_alexander | 22:abca9d17d13d | 231 | uint8_t i; |
dev_alexander | 22:abca9d17d13d | 232 | #if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway |
dev_alexander | 22:abca9d17d13d | 233 | bool isMaster = true; |
dev_alexander | 23:f74a50977593 | 234 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot |
dev_alexander | 22:abca9d17d13d | 235 | bool isMaster = false; |
Helmut64 | 0:c43b6919ae15 | 236 | #endif |
dev_alexander | 22:abca9d17d13d | 237 | |
dev_alexander | 22:abca9d17d13d | 238 | switch( State ) |
dev_alexander | 22:abca9d17d13d | 239 | { |
dev_alexander | 22:abca9d17d13d | 240 | case RX: |
dev_alexander | 22:abca9d17d13d | 241 | // *led3 = 0; |
dev_alexander | 22:abca9d17d13d | 242 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 243 | { |
dev_alexander | 24:e8d03912f303 | 244 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 245 | { |
dev_alexander | 24:e8d03912f303 | 246 | if( memcmp(BufferRx, PongMsg, sizeof(PongMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 247 | { |
dev_alexander | 22:abca9d17d13d | 248 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 249 | dprintf( "...Pong" ); |
dev_alexander | 22:abca9d17d13d | 250 | // Send the next PING frame |
dev_alexander | 24:e8d03912f303 | 251 | memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 22:abca9d17d13d | 252 | // We fill the buffer with numbers for the payload |
dev_alexander | 24:e8d03912f303 | 253 | for( i = sizeof(PingMsg); i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 254 | { |
dev_alexander | 24:e8d03912f303 | 255 | BufferTx[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 256 | } |
dev_alexander | 22:abca9d17d13d | 257 | wait_ms( 10 ); |
dev_alexander | 24:e8d03912f303 | 258 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 259 | } |
dev_alexander | 22:abca9d17d13d | 260 | /* else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
dev_alexander | 22:abca9d17d13d | 261 | { // A master already exists then become a slave |
dev_alexander | 22:abca9d17d13d | 262 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 263 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 264 | isMaster = false; |
dev_alexander | 22:abca9d17d13d | 265 | // Send the next PONG frame |
dev_alexander | 22:abca9d17d13d | 266 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
dev_alexander | 22:abca9d17d13d | 267 | // We fill the buffer with numbers for the payload |
dev_alexander | 22:abca9d17d13d | 268 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
dev_alexander | 22:abca9d17d13d | 269 | { |
dev_alexander | 22:abca9d17d13d | 270 | Buffer[i] = i - sizeof(PongMsg); |
Helmut64 | 0:c43b6919ae15 | 271 | } |
dev_alexander | 22:abca9d17d13d | 272 | wait_ms( 10 ); |
dev_alexander | 22:abca9d17d13d | 273 | Radio->Send( Buffer, BufferSize ); |
dev_alexander | 22:abca9d17d13d | 274 | } |
dev_alexander | 22:abca9d17d13d | 275 | */ |
dev_alexander | 22:abca9d17d13d | 276 | else // valid reception but neither a PING or a PONG message |
dev_alexander | 22:abca9d17d13d | 277 | { // Set device as master ans start again |
dev_alexander | 24:e8d03912f303 | 278 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 279 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 280 | } |
Helmut64 | 0:c43b6919ae15 | 281 | } |
dev_alexander | 22:abca9d17d13d | 282 | } |
dev_alexander | 22:abca9d17d13d | 283 | else |
dev_alexander | 22:abca9d17d13d | 284 | { |
dev_alexander | 24:e8d03912f303 | 285 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 286 | { |
dev_alexander | 24:e8d03912f303 | 287 | if( memcmp(BufferRx, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 288 | { |
dev_alexander | 22:abca9d17d13d | 289 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 290 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 291 | // Send the reply to the PING string |
dev_alexander | 24:e8d03912f303 | 292 | memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 22:abca9d17d13d | 293 | // We fill the buffer with numbers for the payload |
dev_alexander | 24:e8d03912f303 | 294 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 295 | { |
dev_alexander | 24:e8d03912f303 | 296 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 297 | } |
dev_alexander | 22:abca9d17d13d | 298 | wait_ms( 10 ); |
dev_alexander | 24:e8d03912f303 | 299 | Radio->Send( BufferTx, BufferSizeTx ); |
Helmut64 | 0:c43b6919ae15 | 300 | } |
dev_alexander | 22:abca9d17d13d | 301 | else // valid reception but not a PING as expected |
dev_alexander | 22:abca9d17d13d | 302 | { // Set device as master and start again |
dev_alexander | 22:abca9d17d13d | 303 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 304 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 305 | } |
Helmut64 | 0:c43b6919ae15 | 306 | } |
dev_alexander | 22:abca9d17d13d | 307 | } |
dev_alexander | 22:abca9d17d13d | 308 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 309 | break; |
dev_alexander | 22:abca9d17d13d | 310 | case TX: |
dev_alexander | 22:abca9d17d13d | 311 | // *led3 = 1; |
dev_alexander | 22:abca9d17d13d | 312 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 313 | { |
dev_alexander | 22:abca9d17d13d | 314 | dprintf("Ping..." ); |
dev_alexander | 22:abca9d17d13d | 315 | } |
dev_alexander | 22:abca9d17d13d | 316 | else |
dev_alexander | 22:abca9d17d13d | 317 | { |
dev_alexander | 22:abca9d17d13d | 318 | dprintf("Pong..." ); |
dev_alexander | 22:abca9d17d13d | 319 | } |
dev_alexander | 22:abca9d17d13d | 320 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 321 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 322 | break; |
dev_alexander | 22:abca9d17d13d | 323 | case RX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 324 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 325 | { |
dev_alexander | 22:abca9d17d13d | 326 | // Send the next PING frame |
dev_alexander | 24:e8d03912f303 | 327 | memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 24:e8d03912f303 | 328 | for( i = sizeof(PingMsg); i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 329 | { |
dev_alexander | 24:e8d03912f303 | 330 | BufferTx[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 331 | } |
dev_alexander | 22:abca9d17d13d | 332 | wait_ms( 10 ); |
dev_alexander | 24:e8d03912f303 | 333 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 334 | } |
dev_alexander | 22:abca9d17d13d | 335 | else |
dev_alexander | 22:abca9d17d13d | 336 | { |
dev_alexander | 22:abca9d17d13d | 337 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 338 | } |
dev_alexander | 22:abca9d17d13d | 339 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 340 | break; |
dev_alexander | 22:abca9d17d13d | 341 | case RX_ERROR: |
dev_alexander | 22:abca9d17d13d | 342 | // We have received a Packet with a CRC error, send reply as if packet was correct |
dev_alexander | 22:abca9d17d13d | 343 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 344 | { |
dev_alexander | 22:abca9d17d13d | 345 | // Send the next PING frame |
dev_alexander | 24:e8d03912f303 | 346 | memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 24:e8d03912f303 | 347 | for( i = 4; i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 348 | { |
dev_alexander | 24:e8d03912f303 | 349 | BufferTx[i] = i - 4; |
Helmut64 | 0:c43b6919ae15 | 350 | } |
dev_alexander | 22:abca9d17d13d | 351 | wait_ms( 10 ); |
dev_alexander | 24:e8d03912f303 | 352 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 353 | } |
dev_alexander | 22:abca9d17d13d | 354 | else |
dev_alexander | 22:abca9d17d13d | 355 | { |
dev_alexander | 22:abca9d17d13d | 356 | // Send the next PONG frame |
dev_alexander | 24:e8d03912f303 | 357 | memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 24:e8d03912f303 | 358 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 359 | { |
dev_alexander | 24:e8d03912f303 | 360 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 361 | } |
dev_alexander | 22:abca9d17d13d | 362 | wait_ms( 10 ); |
dev_alexander | 24:e8d03912f303 | 363 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 364 | } |
dev_alexander | 22:abca9d17d13d | 365 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 366 | break; |
dev_alexander | 22:abca9d17d13d | 367 | case TX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 368 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 369 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 370 | break; |
dev_alexander | 22:abca9d17d13d | 371 | case LOWPOWER: |
dev_alexander | 22:abca9d17d13d | 372 | sleep(); |
dev_alexander | 22:abca9d17d13d | 373 | break; |
dev_alexander | 22:abca9d17d13d | 374 | default: |
dev_alexander | 22:abca9d17d13d | 375 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 376 | break; |
dev_alexander | 22:abca9d17d13d | 377 | } |
Helmut64 | 0:c43b6919ae15 | 378 | } |
Helmut64 | 0:c43b6919ae15 | 379 | |
Helmut64 | 11:d3a591c20cd7 | 380 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 381 | { |
Helmut64 | 0:c43b6919ae15 | 382 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 383 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 384 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 385 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 386 | } |
Helmut64 | 0:c43b6919ae15 | 387 | |
Helmut64 | 11:d3a591c20cd7 | 388 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 389 | { |
Helmut64 | 0:c43b6919ae15 | 390 | Radio->Sleep( ); |
dev_alexander | 24:e8d03912f303 | 391 | BufferSizeRx = size; |
dev_alexander | 24:e8d03912f303 | 392 | memcpy( BufferRx, payload, BufferSizeRx ); |
Helmut64 | 0:c43b6919ae15 | 393 | State = RX; |
Helmut64 | 0:c43b6919ae15 | 394 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 395 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
Helmut64 | 0:c43b6919ae15 | 396 | dump("Data:", payload, size); |
Helmut64 | 0:c43b6919ae15 | 397 | } |
Helmut64 | 0:c43b6919ae15 | 398 | |
Helmut64 | 11:d3a591c20cd7 | 399 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 400 | { |
dev_alexander | 22:abca9d17d13d | 401 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 402 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 403 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 404 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 405 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 406 | } |
Helmut64 | 0:c43b6919ae15 | 407 | |
Helmut64 | 11:d3a591c20cd7 | 408 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 409 | { |
dev_alexander | 22:abca9d17d13d | 410 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 411 | Radio->Sleep( ); |
dev_alexander | 24:e8d03912f303 | 412 | BufferRx[BufferSizeRx-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 413 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 414 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 415 | dprintf("> OnRxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 416 | } |
Helmut64 | 0:c43b6919ae15 | 417 | |
Helmut64 | 11:d3a591c20cd7 | 418 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 419 | { |
Helmut64 | 0:c43b6919ae15 | 420 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 421 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 422 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 423 | dprintf("> OnRxError"); |
Helmut64 | 0:c43b6919ae15 | 424 | } |
Helmut64 | 0:c43b6919ae15 | 425 | |
Helmut64 | 0:c43b6919ae15 | 426 | #endif |