-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLora_tx.cpp
219 lines (172 loc) · 6.67 KB
/
Lora_tx.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
This file is part of SX128x Linux driver.
Copyright (C) 2020 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "SX128x_Linux.hpp"
#include "pins.hpp"
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <cstdint>
#include <fstream>
#include <iterator>
#include <cstddef>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
constexpr size_t PACKET_SIZE = 253;
constexpr size_t HEADER_LEN = 0; //currently not using the header, might become useful in the future
using Packet = std::vector<uint8_t>;
class PacketizedFile {
public:
std::string filepath;
size_t filesize;
std::ifstream file;
size_t numpackets;
size_t currentpacket_id;
PacketizedFile(std::string fpath) {
filepath = fpath;
file = std::ifstream(filepath, std::ios::binary);
file.seekg(0, std::ios::end);
filesize = file.tellg();
file.seekg(0, std::ios::beg);
currentpacket_id = 0;
numpackets = ceil(static_cast<float>(filesize) / PACKET_SIZE);
}
Packet calculate_header(const Packet& packet, uint16_t id = 0) {
//Currently an empty function, in case we might need to add a header in the future.
Packet header;
return header;
}
Packet getNextPacket() {
Packet packet(PACKET_SIZE, 0); // Initialize with PACKET_SIZE zeros
file.read(reinterpret_cast<char*>(packet.data()), PACKET_SIZE);
size_t N = (currentpacket_id != numpackets-1 ? PACKET_SIZE : filesize % PACKET_SIZE);
packet.resize(N);
Packet finalpacket = calculate_header(packet, currentpacket_id);
finalpacket.insert(finalpacket.end(), packet.begin(), packet.end());
return finalpacket;
}
class Iterator {
public:
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = Packet;
using pointer = Packet*;
using reference = Packet&;
Iterator(PacketizedFile* pt, bool beginning = false) {
p = pt;
ptr = ¤tpacket;
if (beginning) { //If we're initialing the begin() iterator, we also generate the first Packet
operator++();
}
}
reference operator*() const { return *ptr; }
pointer operator->() { return ptr; }
Iterator& operator++() {
currentpacket = p->getNextPacket();
p->currentpacket_id++;
return *this;
}
bool operator!= (const Iterator& b) {
if (p->currentpacket_id > p->numpackets) {
printf("File Closed");
p->file.close();}
return (p->currentpacket_id <= p->numpackets);
}
private:
pointer ptr;
PacketizedFile* p;
Packet currentpacket;
};
Iterator begin() {return Iterator(this, true);}
Iterator end() {return Iterator(this);}
};
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: Lora_tx <freq in MHz> <file to send>\nTCXO_EN Included");
return 1;
}
// Pins assignment
SX128x_Linux Radio("/dev/spidev0.0", 0, Hunter_pins);
// Assume we're running on a high-end Raspberry Pi,
// so we set the SPI clock speed to the maximum value supported by the chip
Radio.SetSpiSpeed(8000000);
Radio.Init();
puts("Init done");
Radio.SetStandby(SX128x::STDBY_XOSC);
puts("SetStandby done");
Radio.SetRegulatorMode(static_cast<SX128x::RadioRegulatorModes_t>(0));
puts("SetRegulatorMode done");
Radio.SetLNAGainSetting(SX128x::LNA_HIGH_SENSITIVITY_MODE);
puts("SetLNAGainSetting done");
Radio.SetTxParams(0, SX128x::RADIO_RAMP_20_US);
puts("SetTxParams done");
Radio.SetBufferBaseAddresses(0x00, 0x00);
puts("SetBufferBaseAddresses done");
SX128x::ModulationParams_t ModulationParams;
SX128x::PacketParams_t PacketParams;
// Modulation Parameters
ModulationParams.PacketType = SX128x::PACKET_TYPE_LORA;
ModulationParams.Params.LoRa.CodingRate = SX128x::LORA_CR_4_8;
ModulationParams.Params.LoRa.Bandwidth = SX128x::LORA_BW_1600;
ModulationParams.Params.LoRa.SpreadingFactor = SX128x::LORA_SF7;
PacketParams.PacketType = SX128x::PACKET_TYPE_LORA;
// Packet Parameters
auto &l = PacketParams.Params.LoRa;
l.PayloadLength = PACKET_SIZE;
l.HeaderType = SX128x::LORA_PACKET_FIXED_LENGTH;
l.PreambleLength = 12;
l.Crc = SX128x::LORA_CRC_ON;
l.InvertIQ = SX128x::LORA_IQ_NORMAL;
Radio.SetPacketType(SX128x::PACKET_TYPE_LORA);
puts("SetPacketType done");
Radio.SetModulationParams(ModulationParams);
puts("SetModulationParams done");
Radio.SetPacketParams(PacketParams);
puts("SetPacketParams done");
auto freq = strtol(argv[1], nullptr, 10);
Radio.SetRfFrequency(freq * 1000000UL);
puts("SetRfFrequency done");
std::cout << "Firmware version: " << Radio.GetFirmwareVersion() << "\n";
// TX done interrupt handler
Radio.callbacks.txDone = []{
//puts("Done!");
};
auto IrqMask = SX128x::IRQ_TX_DONE | SX128x::IRQ_RX_TX_TIMEOUT;
Radio.SetDioIrqParams(IrqMask, IrqMask, SX128x::IRQ_RADIO_NONE, SX128x::IRQ_RADIO_NONE);
puts("SetDioIrqParams done");
Radio.StartIrqHandler();
puts("StartIrqHandler done");
auto pkt_ToA = Radio.GetTimeOnAir();
// Create packetized file
PacketizedFile packetizedFile(argv[2]);
//Sending beginning packet with filesize
printf("Sending %d packets (%d bytes)...\n", packetizedFile.numpackets, packetizedFile.filesize);
std::string numBytes = std::to_string(packetizedFile.filesize) + "\0";
Radio.SendPayload((uint8_t*)numBytes.data(), numBytes.size(), {SX128x::RADIO_TICK_SIZE_1000_US, 1000});
usleep((pkt_ToA + 20) * 1'000);
usleep(1'000);
for (Packet& packet : packetizedFile) {
Radio.SendPayload(packet.data(), packet.size(), {SX128x::RADIO_TICK_SIZE_1000_US, 1000});
printf("Sent packet %d with size %d\n", packetizedFile.currentpacket_id-1, packet.size());
usleep((pkt_ToA+20)*1000);
}
printf("Done!, Now exiting...\n");
Radio.StopIrqHandler();
return EXIT_SUCCESS;
}