-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrak11300_gesture_control_rx.ino
105 lines (88 loc) · 2.9 KB
/
rak11300_gesture_control_rx.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <Arduino.h>
#include "LoRaWan-Arduino.h" // Include LoRa library
#include <SPI.h>
#include <stdio.h>
//function declaration
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
void OnRxTimeout(void);
void OnRxError(void);
//define LoRa parameters
#define RF_FREQUENCY 868300000 // Hz
#define TX_OUTPUT_POWER 22 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 3000
static RadioEvents_t RadioEvents;
static uint8_t RcvBuffer[64];
void setup()
{
//initialize serial
Serial.begin(115200);
while (!Serial) {
delay(100);
}
//initialize LoRa chip.
lora_rak11300_init();
Serial.println("=====================================");
Serial.println("Gesture Control Rx");
Serial.println("=====================================");
//initialize the Radio callbacks
RadioEvents.TxDone = NULL;
RadioEvents.RxDone = OnRxDone;
RadioEvents.TxTimeout = NULL;
RadioEvents.RxTimeout = OnRxTimeout;
RadioEvents.RxError = OnRxError;
RadioEvents.CadDone = NULL;
//initialize the radio
Radio.Init(&RadioEvents);
//set radio channel
Radio.SetChannel(RF_FREQUENCY);
//set radio rx configuration
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
//start LoRa
Serial.println("Starting Radio.Rx");
Radio.Rx(RX_TIMEOUT_VALUE);
}
void loop()
{
// Empty loop - all work is handled in the RX callback
}
/**@brief Function to be executed on Radio Rx Done event
*/
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
/*Serial.println("OnRxDone");*/
// ensure received size does not exceed buffer size
if (size < sizeof(RcvBuffer)) {
memcpy(RcvBuffer, payload, size);
RcvBuffer[size] = '\0'; //null-terminate the buffer for string operations
//print the received payload
/*Serial.print("Received payload: ");*/
Serial.println((char *)RcvBuffer); //cast buffer to char* to print as string
} else {
Serial.println("Received data size exceeds buffer size");
}
//restart listening for new messages
Radio.Rx(RX_TIMEOUT_VALUE);
}
/**@brief Function to be executed on Radio Rx Timeout event
*/
void OnRxTimeout(void)
{
Serial.println("OnRxTimeout");
Radio.Rx(RX_TIMEOUT_VALUE);
}
/**@brief Function to be executed on Radio Rx Error event
*/
void OnRxError(void)
{
Serial.println("OnRxError");
Radio.Rx(RX_TIMEOUT_VALUE);
}