-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLoRa.cpp
110 lines (89 loc) · 2.8 KB
/
MyLoRa.cpp
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
106
107
108
109
110
#include "MyLoRa.h"
MyLoRa::MyLoRa (int lora_band, int lora_sck_pin, int lora_miso_pin, int lora_mosi_pin, int lora_ss_pin, int lora_rst_pin, int lora_di00_pin) {
SPI.begin (lora_sck_pin, lora_miso_pin, lora_mosi_pin, lora_ss_pin); // Initiating SPI communication
LoRa.setPins (lora_ss_pin, lora_rst_pin, lora_di00_pin); // Setting LoRa pins
if (!LoRa.begin(lora_band)) { // Initiating LoRa radio
Serial.println("Error when trying to initialize LoRa.");
while (1);
}
// Setting our own defaults if we want something different from the LoRa.h library's defaults.
//setSF (11);
//setBW (62.5E3);
//setSW (0x44);
LoRa.receive(); // Enables receiving packets
}
//##########################################################################################
// Set functions
void MyLoRa::setSF (int sf) {
if (sf >= 6 && sf <= 12) {
LoRa.setSpreadingFactor (sf);
_setting_spreading_factor = sf;
}
}
void MyLoRa::setBW (int bw) {
if (bw == 7.8E3 || bw == 10.4E3 || bw == 15.6E3 || bw == 20.8E3 || bw == 31.25E3 || bw == 41.7E3 || bw == 62.5E3 || bw == 125E3 || bw == 250E3 || bw == 500E3) {
LoRa.setSignalBandwidth(bw);
_setting_bandwidth = bw;
}
}
void MyLoRa::setCRC (bool crc) {
if (crc) {
LoRa.enableCrc();
} else {
LoRa.disableCrc();
}
_setting_crc = crc;
}
void MyLoRa::setSW (byte sw) {
LoRa.setSyncWord (sw);
_setting_syncword = sw;
}
void MyLoRa::setTXPOWER (int txpwr) {
if (txpwr >= 2 && txpwr <= 20) {
LoRa.setTxPower (txpwr);
_setting_tx_power = txpwr;
}
}
//##########################################################################################
// Get functions
String MyLoRa::getLastMessage () {
return _lastMessage;
}
int MyLoRa::getSF () {
return _setting_spreading_factor;
}
int MyLoRa::getBW () {
return _setting_bandwidth;
}
bool MyLoRa::getCRC () {
return _setting_crc;
}
byte MyLoRa::getSW () {
return _setting_syncword;
}
int MyLoRa::getTXPOWER () {
return _setting_tx_power;
}
//##########################################################################################
// Send / Receive functions
bool MyLoRa::checkReceive () {
int packetSize = LoRa.parsePacket(); // Attempts to read packet
if (packetSize > 1) {
_lastMessageSize = packetSize;
String received = "";
while (LoRa.available()) {
received += (char) LoRa.read(); // Store the packet's data in a string
}
_lastMessage = received;
_lastMessageRSSI = LoRa.packetRssi();
_lastMessageSNR = LoRa.packetSnr();
return true;
} else {
return false;
}
}
void MyLoRa::sendMsg (String msg) {
LoRa.beginPacket();
LoRa.print(msg);
LoRa.endPacket();
}