Added RFM69 helper

LGE tool: new frames
Text entry string length bugfix
This commit is contained in:
furrtek 2019-05-05 00:43:36 +01:00
parent dd35bda197
commit b1e72c788b
18 changed files with 417 additions and 130 deletions

View file

@ -20,12 +20,15 @@
* Boston, MA 02110-1301, USA.
*/
// The UI for this app is in French because it concerns leisure centers
// only established in France. "LGE" stands for a trademark I'd rather
// not spell out completely here.
#include "lge_app.hpp"
#include "baseband_api.hpp"
#include "portapack_persistent_memory.hpp"
#include "ui_textentry.hpp"
#include "crc.hpp"
#include "string_format.hpp"
#include <cstring>
@ -36,7 +39,7 @@ using namespace portapack;
namespace ui {
void LGEView::focus() {
tx_view.focus();
options_trame.focus();
}
LGEView::~LGEView() {
@ -44,64 +47,145 @@ LGEView::~LGEView() {
baseband::shutdown();
}
void LGEView::generate_frame() {
CRC<16> crc { 0x1021, 0x90BE };
std::vector<uint8_t> frame { };
uint8_t payload[9] = { 0x06, 0x0D, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00 };
uint8_t out = 0;
void LGEView::generate_lge_frame(const uint8_t command, const uint16_t address_a, const uint16_t address_b, std::vector<uint8_t>& data) {
std::array<uint8_t, 5> header = {
command,
(uint8_t)(address_a & 255),
(uint8_t)(address_a >> 8),
(uint8_t)(address_b & 255),
(uint8_t)(address_b >> 8),
};
data.insert(data.begin(), header.begin(), header.end());
frame_size = rfm69.gen_frame(data);
for (auto b : data)
console.write(to_string_hex(b, 2) + " ");
}
void LGEView::generate_frame_pseudo() {
// 0040.48s:
// 30 02 1A 00 19 00 FF 00 02 19 42 52 45 42 49 53 20 00 00 00 00 00 00 00 00 00
// 04 01 B0 04 7F 1F 11 33 40 1F 22 01 07 00 00 01 07 00 00 63 05 00 00 99 A2
std::vector<uint8_t> data { };
std::array<uint8_t, 3> data_header = { 0xFF, 0x00, 0x02 };
std::array<uint8_t, 22> data_footer = {
0x01, 0xB0, 0x04, 0x7F,
0x1F, 0x11, 0x33, 0x40,
0x1F, 0x22, 0x01, 0x07,
0x00, 0x00, 0x01, 0x07,
0x00, 0x00, 0x63, 0x05,
0x00, 0x00
};
uint32_t c;
payload[6] = field_zone.value(); // Zone
//data_header[2] = field_salle.value(); // ?
//data_footer[0] = field_salle.value(); // ?
// Preamble
// Really is 0xAA but the RFM69 skips the very last bit (bug ?)
// so the whole preamble should be shifted right 1 bit to simulate that
for (c = 0; c < 5; c++)
frame.push_back(0x55);
data.insert(data.begin(), data_header.begin(), data_header.end());
frame.push_back(0x2D); // Sync word
frame.push_back(0xD4);
data.push_back(field_joueur.value());
crc.process_bytes(payload, 9 - 2);
payload[7] = crc.checksum() >> 8;
payload[8] = crc.checksum() & 0xFF;
// Manchester-encode payload
for (c = 0; c < 9; c++) {
uint8_t byte = payload[c];
for (uint32_t b = 0; b < 8; b++) {
out <<= 2;
if (byte & 0x80)
out |= 0b10;
else
out |= 0b01;
if ((b & 3) == 3)
frame.push_back(out);
byte <<= 1;
}
c = 0;
for (auto &ch : pseudo) {
data.push_back(ch);
c++;
}
// Space at the end, is this required ?
data.push_back(0x20);
// Pad with zeroes
while (++c < 16)
data.push_back(0x00);
frame_size = frame.size();
data.push_back(field_equipe.value());
/*std::string debug_str { "" };
data.insert(data.end(), data_footer.begin(), data_footer.end());
for (c = 0; c < 10; c++)
debug_str += (to_string_hex(frame[c], 2) + " ");
console.write("\n\x1B\x0ESet pseudo:\x1B\x10");
text_messagea.set(debug_str);
generate_lge_frame(0x02, 0x001A, field_joueur.value(), data);
}
debug_str = "";
for (c = 15; c < frame_size; c++)
debug_str += (to_string_hex(frame[c], 2) + " ");
text_messageb.set(debug_str);*/
void LGEView::generate_frame_equipe() {
// 0041.83s:
// 3D 03 FF FF FF FF 02 03 01 52 4F 55 47 45 00 00 00 00 00 00 00 00 00 00 00 00
// 02 56 45 52 54 45 00 00 00 00 00 00 00 00 00 00 00 01 03 42 4C 45 55 45 00 00
// 00 00 00 00 00 00 00 00 00 02 43 29
// Copy for baseband
memcpy(shared_memory.bb_data.data, frame.data(), frame_size);
std::vector<uint8_t> data { };
std::array<uint8_t, 2> data_header = { 0x02, 0x01 };
uint32_t c;
data.insert(data.begin(), data_header.begin(), data_header.end());
data.push_back(field_equipe.value());
c = 0;
for (auto &ch : pseudo) {
data.push_back(ch);
c++;
}
// Pad with zeroes
while (c++ < 16)
data.push_back(0x00);
data.push_back(field_equipe.value() - 1); // Color ?
console.write("\n\x1B\x0ASet equipe:\x1B\x10");
generate_lge_frame(0x03, data);
}
void LGEView::generate_frame_broadcast_pseudo() {
// 0043.86s:
// 3D 04 FF FF FF FF 02 03 19 42 52 45 42 49 53 20 00 00 00 00 00 00 00 00 00 04
// 07 50 4F 4E 45 59 20 00 00 00 00 00 00 00 00 00 00 05 1B 41 42 42 59 20 00 00
// 00 00 00 00 00 00 00 00 00 04 0A 02
std::vector<uint8_t> data { };
std::array<uint8_t, 2> data_header = { 0x02, 0x01 };
uint32_t c;
data.insert(data.begin(), data_header.begin(), data_header.end());
data.push_back(field_joueur.value());
c = 0;
for (auto &ch : pseudo) {
data.push_back(ch);
c++;
}
// Space at the end, is this required ?
data.push_back(0x20);
// Pad with zeroes
while (++c < 16)
data.push_back(0x00);
data.push_back(field_equipe.value());
console.write("\n\x1B\x09" "Broadcast pseudo:\x1B\x10");
generate_lge_frame(0x04, data);
}
void LGEView::generate_frame_start() {
// 0166.13s:
// 0A 05 FF FF FF FF 02 EC FF FF FF A3 35
std::vector<uint8_t> data { 0x02, 0xEC, 0xFF, 0xFF, 0xFF };
//data[0] = field_salle.value(); // ?
console.write("\n\x1B\x0DStart:\x1B\x10");
generate_lge_frame(0x05, data);
}
void LGEView::generate_frame_gameover() {
std::vector<uint8_t> data { field_salle.value() };
console.write("\n\x1B\x0CGameover:\x1B\x10");
generate_lge_frame(0x0D, data);
}
void LGEView::start_tx() {
@ -115,7 +199,7 @@ void LGEView::start_tx() {
transmitter_model.set_baseband_bandwidth(1750000);
transmitter_model.enable();
chThdSleep(100);
chThdSleep(100);
baseband::set_fsk_data(frame_size * 8, 2280000 / 9600, 4000, 256);
}
@ -157,20 +241,29 @@ LGEView::LGEView(NavigationView& nav) {
add_children({
&labels,
&field_zone,
&options_trame,
&field_salle,
&button_texte,
&field_equipe,
&field_joueur,
&checkbox_channels,
&text_messagea,
&text_messageb,
&console,
&tx_view
});
field_zone.set_value(1);
field_salle.set_value(1);
field_equipe.set_value(1);
field_joueur.set_value(1);
checkbox_channels.set_value(true);
generate_frame();
field_zone.on_change = [this](int32_t) {
generate_frame();
button_texte.on_select = [this, &nav](Button&) {
text_prompt(
nav,
pseudo,
15,
[this](std::string& buffer) {
button_texte.set_text(buffer);
});
};
tx_view.on_edit_frequency = [this, &nav]() {
@ -182,7 +275,18 @@ LGEView::LGEView(NavigationView& nav) {
tx_view.on_start = [this]() {
if (tx_mode == IDLE) {
generate_frame();
auto i = options_trame.selected_index_value();
if (i == 0)
generate_frame_pseudo();
else if (i == 1)
generate_frame_equipe();
else if (i == 2)
generate_frame_broadcast_pseudo();
else if (i == 3)
generate_frame_start();
else if (i == 4)
generate_frame_gameover();
repeats = 0;
channel_index = 0;
tx_mode = checkbox_channels.value() ? ALL : SINGLE;