Compare commits

...

45 Commits

Author SHA1 Message Date
Netro b7b4a10485 Refined Tx Random Data (Still WIP) (#1616)
* managing initial cursor
* work on adding marked cursors
2023-11-30 21:57:22 +01:00
Totoo cca0e18f5a Weather improvements (#1615)
* Added Acurite986 protocol
* Added signal age
* Added myself to about screen
2023-11-30 12:36:59 +01:00
Netro 8846926b68 BLE TX: use text viewer to inject rand data WIP (#1613) 2023-11-29 18:54:17 +01:00
Mark Thompson 7ceefb1b81 Weather app tweaks (#1610)
* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload
2023-11-29 10:37:01 -06:00
Erwin Ried 861f684d00 Discord icon/invite more clear on top (#1612) 2023-11-29 13:22:51 +01:00
Erwin Ried 17b146b960 Update README.md (#1611) 2023-11-29 11:23:27 +01:00
Totoo 02251eeeb4 Added weather station app with 18 protocol parsers (#1607)
* Added weather station app with 18 protocol parsers
* Fix button and formatting
* Set BW to 1.75m, changed to us in dsp part
2023-11-28 21:11:30 +01:00
Netro c486572d3d BLE TX; Added feature to randomize found substring in data (#1609) 2023-11-28 21:11:07 +01:00
Netro a6ed6e3099 BLE RX: Making auto channel timer independent of packet events. (#1608) 2023-11-28 18:18:15 +01:00
Netro 175e5e2e8c BLE: Minor improvements to fix occasional not start on tx app first time. (#1605) 2023-11-27 21:59:29 +01:00
Mark Thompson 7bc27038be Save Morse app settings (#1602) 2023-11-26 12:17:23 +01:00
Brumi-2021 609bf3219f solve Morse_TX_App latch up in r9 (#1601) 2023-11-25 01:01:52 +01:00
Mark Thompson bdbd616a84 added a frequency field to the ERT app 2023-11-24 09:25:50 +01:00
Mark Thompson 1cbe5cb293 Save TPMS units in App Settings and tune frequency (#1598) 2023-11-21 18:44:10 +01:00
gullradriel df6003c08f fix button label (#1597)
Co-authored-by: GullCode <gullradriel@hotmail.com>
2023-11-20 12:09:47 -05:00
Netro 50fcbf0f5a BLE: added deleting and renaming temp file (#1596) 2023-11-20 08:37:56 +01:00
Netro 5a1676a0b9 BLE List improvements (#1595)
* WIP on add to save list

* Added rewriting existing lines and appending new to same file.

* format

* not looking on new file.

* Added temp list due to some issue of recent being updated during save

I was receiving a system halt and this seems to fix it. Needs more testing though.

* clang format

* Stored entries backward. Fix.

* Update file working, need to replace temp file though.
2023-11-19 20:53:53 -05:00
gullradriel 309f2fbd2c support for a sdcard speed option (#1594)
* support for a sdcard speed option

* trying to link sdio_cclk_set from file

* changed io to IO in checkbox text

* changed order so high speed option is read after pmem is restored from sd

* test button

* took out unneeded comment

* force behavior of test button

---------

Co-authored-by: GullCode <gullradriel@hotmail.com>
2023-11-19 08:08:29 -06:00
gullradriel 650aacfaa7 BLE RX: added clear list button (#1593)
* added clear list button

* Bigger buttons and text

* Change case on labels

---------

Co-authored-by: GullCode <gullradriel@hotmail.com>
2023-11-18 16:32:52 -05:00
Mark Thompson 8655027a5b Fixed SPI Read timing issue affecting MAX283x chips (#1590)
* Slowed SPI timing to MAX283x by 20%

* Fix temperature scaling
2023-11-18 09:50:57 -06:00
Mark Thompson de937f02d4 Added Memory Dump app for Debug (#1588)
* Add files via upload

* Add files via upload

* Added single word read/write support

* Add files via upload
2023-11-18 07:16:56 -06:00
Mark Thompson 2c0446e83d Improved Focus Manager navigation (#1589)
* Focus manager navigation fix

* Add files via upload
2023-11-18 06:58:38 -06:00
Netro 9e6699d8cc Fixed name. (#1586) 2023-11-17 13:28:09 -05:00
Mark Thompson ef92c5bc0a Enhanced Debug -> Peripherals app to allow direct register modification (#1584)
* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Clang

* Clang

* Tweak position of field on screen

* Reverted text on Write button per reviewer feedback
2023-11-17 12:10:11 -06:00
Netro 10f6600f52 No need limit to 50 since we moved to min 16ms send times. (#1583) 2023-11-16 21:28:22 +01:00
Netro aeb607e73b BLE Rx Save List / Save packet from BLE Rx (#1582)
* Added Tx Save from Rx Details View
* Added saving of current list
2023-11-16 20:00:52 +01:00
Netro 8479d2edf0 BLE Comm WIP (#1578)
* Initial BLE Comm commit.
* SCAN_RSP MAC was reversed.
* Added Auto Channel Hop.
* Improvements to Tx to better handle timers.
* Auto channel and more work on timers.
* more advertisement numbers.
2023-11-16 14:46:45 +01:00
Bernd Herzog d2e9a8dc06 Hackrf mode when no pp attached (#1580)
* added portapack presence detection

* adjusted timeout

* fixed ak4951 initialisation
2023-11-14 12:22:26 +01:00
Netro 4571cb0b61 BLE Rx Refactoring / Cleanup (#1577) , also added showing MAC for Scan_Rsp Adv_Direct 2023-11-12 19:10:53 +01:00
gullradriel 351f7b13b3 BTLE RX: fix for name disappearing (#1575)
* Fix for name disappearing

* cleanings

---------

Co-authored-by: GullCode <gullradriel@hotmail.com>
2023-11-12 09:59:36 -05:00
Netro 5953cb57b0 Rx parsing fix (#1573)
* restored mistakenly quoted code
* removed unneeded line.
* added parsing for other PDU Types and added PDU Type label.
* added missing SCAN_IND
2023-11-12 08:54:04 +01:00
Brumi-2021 b60b873428 Changing BLTE FIR filter to improve RX sensitivity (#1574) 2023-11-11 20:43:10 -05:00
Netro a3249cae26 BLE Rx/Tx App Cleanup (#1570)
* Showing highest dB first
* Including namestring in search
* Using replace seems to work better.
* bletx cleanup
* removing pop
* pop is needed on this view.
* cleanup switching
* reduce to 1 entry list
* Setting to use the name of BLE with a toggle checkbox.
* Removed &nav reference.
* Removing const
* Fixed issue with memory being run out when switching from a previous app
* Fixed not setting updated progressbar for each new packet.
2023-11-11 20:46:51 +01:00
Kyle Reed f7f784c0f4 Cleanup dead code in the ADSB RX proc (#1572)
* Cleanup dead code in the ADSB RX proc

* Fix comments

* Formatting
2023-11-10 11:38:06 -08:00
Kyle Reed f4f538f69b ADSB RX App cleanup (#1569)
* WIP refactoring/gardening

* WIP cleanup, adding status dots

* Rename ageStep

* WIP Cleanup

* Wrapping up ADSB refactor/cleanup.

* Don't initialize strings to "     "

* Better map refresh

* Fix colorization of recent entries

* Fit and finish
2023-11-10 08:22:29 -08:00
Netro bbd1a5a2ef Changing filter on ble rx. Seems to work better? (#1568) 2023-11-08 19:22:57 +01:00
Nemanja Nedeljković bc71238046 Patch build for gcc 13.2.0 (#1567) 2023-11-08 19:22:26 +01:00
Netro a9df9dde69 BLE Rx parsing improvements (#1566)
* Running simple state machine to better catch in between buffers. 
* Fixed buffer size 2048 was too large for a decimated before only containing 512 (2048/4 Decimation) Bytes.
* attempt at trying to minimize uncleared str buffers
2023-11-07 18:10:18 +01:00
Mark Thompson 7a87e3f3af Added File Manager button to show hidden files (#1564)
* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload
2023-11-06 16:33:19 -06:00
Mark Thompson 68f960e4e7 Temperature scaling changes for HackRF r9 (MAX2839) (#1561)
* Add files via upload

* Add files via upload

* Add files via upload
2023-11-06 13:56:09 -06:00
gullradriel 6784fe72dd fixed display when delete/remove entry (#1563) 2023-11-06 20:38:37 +01:00
Netro 312df0fcec Save tx file (#1560)
* Added saving current packets to file
2023-11-06 19:09:26 +01:00
Erwin Ried 2913a41aec Update README.md (#1559) 2023-11-06 11:16:13 +01:00
Netro adabbfbef1 Send on select (#1557)
* fixed bug not fully displaying rx packet
* added sending selected packet
* setting packet count to 100 as 1 is too aggressive for speed > 2
* setting limit to 50 as 50 does not bog down UI at speed 5.
* fix tx channel getting out of sync with freq
2023-11-06 11:16:01 +01:00
gullradriel 2b7962fa7c Level RSSIGraph tuning (#1556)
* Added whole graph RSSI min/avg/max/delta to RSSIGraph
* fix for min=0 values when modulation is changed
* literal number as a #define flag
2023-11-06 11:12:36 +01:00
102 changed files with 7634 additions and 1883 deletions
+8 -4
View File
@@ -1,6 +1,6 @@
# PortaPack Mayhem
[![Build Status](https://travis-ci.com/eried/portapack-mayhem.svg?branch=master)](https://travis-ci.com/eried/portapack-mayhem) [![Nightly Release](https://github.com/eried/portapack-mayhem/actions/workflows/create_nightly_release.yml/badge.svg?branch=next)](https://github.com/eried/portapack-mayhem/actions/workflows/create_nightly_release.yml) [![CodeScene Code Health](https://codescene.io/projects/8381/status-badges/code-health)](https://codescene.io/projects/8381) [![GitHub All Releases](https://img.shields.io/github/downloads/eried/portapack-mayhem/total)](https://github.com/eried/portapack-mayhem/releases) [![GitHub Releases](https://img.shields.io/github/downloads/eried/portapack-mayhem/latest/total)](https://github.com/eried/portapack-mayhem/releases/latest) [![Docker Hub Pulls](https://img.shields.io/docker/pulls/eried/portapack.svg)](https://hub.docker.com/r/eried/portapack) [![Discord Chat](https://img.shields.io/discord/719669764804444213.svg)](https://discord.gg/tuwVMv3) [![Check bounties!](https://img.shields.io/bountysource/team/portapack-mayhem/activity?color=%2333ccff&label=bountysource%20%28USD%29&style=plastic)](https://www.bountysource.com/teams/portapack-mayhem/issues)
[![Build Status](https://travis-ci.com/eried/portapack-mayhem.svg?branch=master)](https://travis-ci.com/eried/portapack-mayhem) [![Nightly Release](https://github.com/eried/portapack-mayhem/actions/workflows/create_nightly_release.yml/badge.svg?branch=next)](https://github.com/eried/portapack-mayhem/actions/workflows/create_nightly_release.yml) [![CodeScene Code Health](https://codescene.io/projects/8381/status-badges/code-health)](https://codescene.io/projects/8381) [![GitHub All Releases](https://img.shields.io/github/downloads/eried/portapack-mayhem/total)](https://github.com/eried/portapack-mayhem/releases) [![GitHub Releases](https://img.shields.io/github/downloads/eried/portapack-mayhem/latest/total)](https://github.com/eried/portapack-mayhem/releases/latest) [![Docker Hub Pulls](https://img.shields.io/docker/pulls/eried/portapack.svg)](https://hub.docker.com/r/eried/portapack) [![Discord Chat](https://dcbadge.vercel.app/api/server/tuwVMv3?style=flat)](https://discord.gg/tuwVMv3)
This is a fork of the [Havoc](https://github.com/furrtek/portapack-havoc/) firmware, which itself was a fork of the [PortaPack](https://github.com/sharebrained/portapack-hackrf) firmware, an add-on for the [HackRF](http://greatscottgadgets.com/hackrf/). A fork is a derivate, in this case one that has extra features and fixes when compared to the older versions.
@@ -20,7 +20,9 @@ This repository expands upon the previous work by many people and aims to consta
## What to buy?
:heavy_check_mark: A recommended one is this [PortaPack H2](https://s.click.aliexpress.com/e/_DmU7GQX) pack, that includes everything you need. Sadly, the people making the H2 never made the updated schematics available (against the terms of the license).
:heavy_check_mark: A recommended one is this [PortaPack H2](https://s.click.aliexpress.com/e/_DmU7GQX), that includes everything you need with the plastic case "inspired" on [this](https://github.com/eried/portapack-mayhem/wiki/H2-Enclosure).
:heavy_check_mark: Our friends at OpenSourceSDRLab give away five units every three months in our discord (check the badge on top) of their [PortaPack H2](https://www.aliexpress.com/item/4000247041639.html?gatewayAdapt=4itemAdapt), you can support them too by ordering.
:heavy_check_mark: Another popular option is the clone of the [PortaPack H1](https://s.click.aliexpress.com/e/_Dkbqs2X).
@@ -48,9 +50,11 @@ To support the people behind the hardware, please buy a genuine [HackRF](https:/
## What if I really want something specific?
If what you need can be relevant in general, you can [request a feature](https://github.com/eried/portapack-mayhem/issues/new?labels=enhancement&template=feature_request.md).
You can create a bounty and invite people to your own bounty. This will incentivize coders to work on a new feature, solving a bug or even writting documentation. Start a bounty by [creating](https://github.com/eried/portapack-mayhem/issues/new/choose) or [choosing](https://github.com/eried/portapack-mayhem/issues/) an existing issue. Then, go to [Bountysource](https://www.bountysource.com/) and post a bounty using the link to that specific [issue](https://www.bountysource.com/teams/portapack-mayhem/issues).
<del>You can create a bounty and invite people to your own bounty. This will incentivize coders to work on a new feature, solving a bug or even writting documentation. Start a bounty by [creating](https://github.com/eried/portapack-mayhem/issues/new/choose) or [choosing](https://github.com/eried/portapack-mayhem/issues/) an existing issue. Then, go to [Bountysource](https://www.bountysource.com/) and post a bounty using the link to that specific [issue](https://www.bountysource.com/teams/portapack-mayhem/issues).</del>
Promote your bounty over our Discord by clicking the chat badge on [top](#portapack-mayhem).
<del>Promote your bounty over our Discord by clicking the chat badge on [top](#portapack-mayhem).</del>
Bountysource has not been reliable lately, so until this changes, please **DO NOT** post a bounty there. Go to our Discord by clicking the chat badge on [top](#portapack-mayhem) and discuss there.
## What if I need help?
First, check the [documentation](https://github.com/eried/portapack-mayhem/wiki). If you find a bug or you think the problem is related to the current repository, please open an [issue](https://github.com/eried/portapack-mayhem/issues/new/choose).
+2
View File
@@ -244,6 +244,7 @@ set(CPPSRC
apps/ais_app.cpp
apps/analog_audio_app.cpp
apps/analog_tv_app.cpp
apps/ble_comm_app.cpp
apps/ble_rx_app.cpp
apps/ble_tx_app.cpp
apps/capture_app.cpp
@@ -306,6 +307,7 @@ set(CPPSRC
apps/ui_touch_calibration.cpp
apps/ui_touchtunes.cpp
apps/ui_view_wav.cpp
apps/ui_weatherstation.cpp
apps/ui_whipcalc.cpp
protocols/aprs.cpp
protocols/ax25.cpp
+331
View File
@@ -0,0 +1,331 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* Copyright (C) 2023 TJ Baginski
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ble_comm_app.hpp"
#include "ui_modemsetup.hpp"
#include "modems.hpp"
#include "audio.hpp"
#include "rtc_time.hpp"
#include "baseband_api.hpp"
#include "string_format.hpp"
#include "portapack_persistent_memory.hpp"
#include "ui_text.hpp"
using namespace portapack;
using namespace modems;
void BLECommLogger::log_raw_data(const std::string& data) {
log_file.write_entry(data);
}
namespace ui {
static std::uint64_t get_freq_by_channel_number(uint8_t channel_number) {
uint64_t freq_hz;
switch (channel_number) {
case 37:
freq_hz = 2'402'000'000ull;
break;
case 38:
freq_hz = 2'426'000'000ull;
break;
case 39:
freq_hz = 2'480'000'000ull;
break;
case 0 ... 10:
freq_hz = 2'404'000'000ull + channel_number * 2'000'000ull;
break;
case 11 ... 36:
freq_hz = 2'428'000'000ull + (channel_number - 11) * 2'000'000ull;
break;
default:
freq_hz = UINT64_MAX;
}
return freq_hz;
}
void BLECommView::focus() {
options_channel.focus();
}
BLECommView::BLECommView(NavigationView& nav)
: nav_{nav} {
add_children({&rssi,
&channel,
&field_rf_amp,
&field_lna,
&field_vga,
&options_channel,
&field_frequency,
&label_send_adv,
&button_send_adv,
&check_log,
&label_packets_sent,
&text_packets_sent,
&console});
field_frequency.set_step(0);
button_send_adv.on_select = [this](ImageButton&) {
this->toggle();
};
check_log.set_value(logging);
check_log.on_select = [this](Checkbox&, bool v) {
str_log = "";
logging = v;
if (logger && logging)
logger->append(LOG_ROOT_DIR "/BLELOG_" + to_string_timestamp(rtc_time::now()) + ".TXT");
};
options_channel.on_change = [this](size_t, int32_t i) {
// If we selected Auto don't do anything and Auto will handle changing.
if (i == 40) {
auto_channel = true;
return;
} else {
auto_channel = false;
}
field_frequency.set_value(get_freq_by_channel_number(i));
channel_number_rx = i;
channel_number_tx = i;
};
options_channel.set_selected_index(3, true);
logger = std::make_unique<BLECommLogger>();
// Generate new random Mac Address upon each new startup.
generateRandomMacAddress(randomMac);
// Setup Initial Advertise Packet.
advertisePacket = build_adv_packet();
}
void BLECommView::set_parent_rect(const Rect new_parent_rect) {
View::set_parent_rect(new_parent_rect);
const Rect content_rect{0, header_height, new_parent_rect.width(), new_parent_rect.height() - header_height};
console.set_parent_rect(content_rect);
}
BLECommView::~BLECommView() {
receiver_model.disable();
transmitter_model.disable();
baseband::shutdown();
}
bool BLECommView::in_tx_mode() const {
return (bool)is_running_tx;
}
void BLECommView::toggle() {
if (in_tx_mode()) {
sendAdvertisement(false);
} else {
sendAdvertisement(true);
}
}
BLETxPacket BLECommView::build_adv_packet() {
BLETxPacket bleTxPacket;
memset(&bleTxPacket, 0, sizeof(BLETxPacket));
std::string dataString = "11094861636b524620506f7274617061636b";
strncpy(bleTxPacket.macAddress, randomMac, 12);
strncpy(bleTxPacket.advertisementData, dataString.c_str(), dataString.length());
// Duty cycle of 40% per 100ms advertisment periods.
strncpy(bleTxPacket.packetCount, "80", 3);
bleTxPacket.packet_count = 80;
bleTxPacket.packetType = PKT_TYPE_DISCOVERY;
return bleTxPacket;
}
void BLECommView::sendAdvertisement(bool enable) {
if (enable) {
startTx(advertisePacket);
is_adv = true;
} else {
is_adv = false;
stopTx();
}
}
void BLECommView::startTx(BLETxPacket packetToSend) {
int randomChannel = channel_number_tx;
if (auto_channel) {
int min = 37;
int max = 39;
randomChannel = min + std::rand() % (max - min + 1);
field_frequency.set_value(get_freq_by_channel_number(randomChannel));
}
if (!in_tx_mode()) {
switch_rx_tx(false);
currentPacket = packetToSend;
packet_counter = currentPacket.packet_count;
button_send_adv.set_bitmap(&bitmap_stop);
baseband::set_btletx(randomChannel, currentPacket.macAddress, currentPacket.advertisementData, currentPacket.packetType);
transmitter_model.set_tx_gain(47);
transmitter_model.enable();
is_running_tx = true;
} else {
baseband::set_btletx(randomChannel, currentPacket.macAddress, currentPacket.advertisementData, currentPacket.packetType);
}
if ((packet_counter % 10) == 0) {
text_packets_sent.set(to_string_dec_uint(packet_counter));
}
is_sending = true;
packet_counter--;
}
void BLECommView::stopTx() {
button_send_adv.set_bitmap(&bitmap_play);
text_packets_sent.set(to_string_dec_uint(packet_counter));
switch_rx_tx(true);
baseband::set_btlerx(channel_number_rx);
receiver_model.enable();
is_running_tx = false;
}
void BLECommView::switch_rx_tx(bool inRxMode) {
if (inRxMode) {
// Start Rx
transmitter_model.disable();
baseband::shutdown();
baseband::run_image(portapack::spi_flash::image_tag_btle_rx);
} else {
// Start Tx
receiver_model.disable();
baseband::shutdown();
baseband::run_image(portapack::spi_flash::image_tag_btle_tx);
}
}
void BLECommView::on_data(BlePacketData* packet) {
parse_received_packet(packet, (ADV_PDU_TYPE)packet->type);
}
// called each 1/60th of second, so 6 = 100ms
void BLECommView::on_timer() {
// Send advertise burst only once every 100ms
if (++timer_counter == timer_period) {
timer_counter = 0;
if (!is_adv) {
sendAdvertisement(true);
}
}
}
void BLECommView::on_tx_progress(const bool done) {
if (done) {
if (in_tx_mode()) {
is_sending = false;
if (packet_counter == 0) {
if (is_adv) {
sendAdvertisement(false);
} else {
stopTx();
}
} else {
startTx(currentPacket);
}
}
}
}
void BLECommView::parse_received_packet(const BlePacketData* packet, ADV_PDU_TYPE pdu_type) {
std::string data_string;
int i;
for (i = 0; i < packet->dataLen; i++) {
data_string += to_string_hex(packet->data[i], 2);
}
receivedPacket.dbValue = packet->max_dB;
receivedPacket.timestamp = to_string_timestamp(rtc_time::now());
receivedPacket.dataString = data_string;
receivedPacket.packetData.type = packet->type;
receivedPacket.packetData.size = packet->size;
receivedPacket.packetData.dataLen = packet->dataLen;
receivedPacket.packetData.macAddress[0] = packet->macAddress[0];
receivedPacket.packetData.macAddress[1] = packet->macAddress[1];
receivedPacket.packetData.macAddress[2] = packet->macAddress[2];
receivedPacket.packetData.macAddress[3] = packet->macAddress[3];
receivedPacket.packetData.macAddress[4] = packet->macAddress[4];
receivedPacket.packetData.macAddress[5] = packet->macAddress[5];
receivedPacket.numHits++;
for (int i = 0; i < packet->dataLen; i++) {
receivedPacket.packetData.data[i] = packet->data[i];
}
if (pdu_type == SCAN_REQ || pdu_type == CONNECT_REQ) {
ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)packet->data;
std::reverse(directed_mac_data->A1, directed_mac_data->A1 + 6);
console.clear(true);
std::string str_console = "";
std::string pduTypeStr = "";
if (pdu_type == SCAN_REQ) {
pduTypeStr += "SCAN_REQ";
} else if (pdu_type == CONNECT_REQ) {
pduTypeStr += "CONNECT_REQ";
}
str_console += "PACKET TYPE:" + pduTypeStr + "\n";
str_console += "MY MAC:" + to_string_formatted_mac_address(randomMac) + "\n";
str_console += "SCAN MAC:" + to_string_mac_address(directed_mac_data->A1, 6, false) + "\n";
console.write(str_console);
}
}
} /* namespace ui */
+203
View File
@@ -0,0 +1,203 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* Copyright (C) 2023 TJ Baginski
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __BLE_COMM_APP_H__
#define __BLE_COMM_APP_H__
#include "ble_rx_app.hpp"
#include "ble_tx_app.hpp"
#include "ui.hpp"
#include "ui_navigation.hpp"
#include "ui_receiver.hpp"
#include "ui_transmitter.hpp"
#include "ui_freq_field.hpp"
#include "ui_record_view.hpp"
#include "app_settings.hpp"
#include "radio_state.hpp"
#include "log_file.hpp"
#include "utility.hpp"
#include "recent_entries.hpp"
class BLECommLogger {
public:
Optional<File::Error> append(const std::string& filename) {
return log_file.append(filename);
}
void log_raw_data(const std::string& data);
private:
LogFile log_file{};
};
namespace ui {
class BLECommView : public View {
public:
BLECommView(NavigationView& nav);
~BLECommView();
void set_parent_rect(const Rect new_parent_rect) override;
void paint(Painter&) override{};
void focus() override;
std::string title() const override { return "BLE Comm"; };
private:
BLETxPacket build_adv_packet();
void switch_rx_tx(bool inRxMode);
void startTx(BLETxPacket packetToSend);
void stopTx();
void toggle();
bool in_tx_mode() const;
void on_timer();
void on_data(BlePacketData* packetData);
void on_tx_progress(const bool done);
void parse_received_packet(const BlePacketData* packet, ADV_PDU_TYPE pdu_type);
void sendAdvertisement(bool enable);
NavigationView& nav_;
RxRadioState radio_state_rx_{
2'402'000'000 /* frequency */,
4'000'000 /* bandwidth */,
4'000'000 /* sampling rate */,
ReceiverModel::Mode::WidebandFMAudio};
TxRadioState radio_state_tx_{
2'402'000'000 /* frequency */,
4'000'000 /* bandwidth */,
4'000'000 /* sampling rate */
};
app_settings::SettingsManager settings_{
"BLE Comm Tx", app_settings::Mode::RX_TX};
uint8_t console_color{0};
uint32_t prev_value{0};
uint8_t channel_number_tx = 37;
uint8_t channel_number_rx = 37;
bool auto_channel = false;
char randomMac[13] = "010203040506";
bool is_running_tx = false;
bool is_sending = false;
bool is_adv = false;
int16_t timer_period{6}; // Delay each packet by 16ms.
int16_t timer_counter = 0;
int16_t timer_rx_counter = 0;
int16_t timer_rx_period{12}; // Poll Rx for at least 200ms. (TBD)
uint32_t packet_counter{0};
BLETxPacket advertisePacket{};
BLETxPacket currentPacket{};
BleRecentEntry receivedPacket{};
static constexpr auto header_height = 5 * 16;
OptionsField options_channel{
{0 * 8, 0 * 8},
5,
{{"Ch.37 ", 37},
{"Ch.38", 38},
{"Ch.39", 39},
{"Auto", 40}}};
RxFrequencyField field_frequency{
{6 * 8, 0 * 16},
nav_};
RFAmpField field_rf_amp{
{16 * 8, 0 * 16}};
LNAGainField field_lna{
{18 * 8, 0 * 16}};
VGAGainField field_vga{
{21 * 8, 0 * 16}};
RSSI rssi{
{24 * 8, 0, 6 * 8, 4}};
Channel channel{
{24 * 8, 5, 6 * 8, 4}};
Labels label_send_adv{
{{0 * 8, 2 * 8}, "Send Advertisement:", Color::light_grey()}};
ImageButton button_send_adv{
{21 * 8, 1 * 16, 10 * 8, 2 * 16},
&bitmap_play,
Color::green(),
Color::black()};
Checkbox check_log{
{24 * 8, 2 * 8},
3,
"Log",
true};
Labels label_packets_sent{
{{0 * 8, 4 * 8}, "Packets Left:", Color::light_grey()}};
Text text_packets_sent{
{13 * 8, 2 * 16, 12 * 8, 16},
"-"};
Console console{
{0, 4 * 16, 240, 240}};
std::string str_log{""};
bool logging{false};
std::unique_ptr<BLECommLogger> logger{};
MessageHandlerRegistration message_handler_packet{
Message::ID::BlePacket,
[this](Message* const p) {
const auto message = static_cast<const BLEPacketMessage*>(p);
this->on_data(message->packet);
}};
MessageHandlerRegistration message_handler_tx_progress{
Message::ID::TXProgress,
[this](const Message* const p) {
const auto message = *reinterpret_cast<const TXProgressMessage*>(p);
this->on_tx_progress(message.done);
}};
MessageHandlerRegistration message_handler_frame_sync{
Message::ID::DisplayFrameSync,
[this](const Message* const) {
this->on_timer();
}};
};
} /* namespace ui */
#endif /*__UI_AFSK_RX_H__*/
+493 -203
View File
@@ -22,7 +22,6 @@
*/
#include "ble_rx_app.hpp"
#include "ble_tx_app.hpp"
#include "ui_modemsetup.hpp"
@@ -57,7 +56,68 @@ uint64_t copy_mac_address_to_uint64(const uint8_t* macAddress) {
return result;
}
void reverse_byte_array(uint8_t* arr, int length) {
int start = 0;
int end = length - 1;
while (start < end) {
// Swap elements at start and end
uint8_t temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move the indices towards the center
start++;
end--;
}
}
namespace ui {
std::string pdu_type_to_string(ADV_PDU_TYPE type) {
std::string pduTypeStr = "";
switch (type) {
case ADV_IND:
pduTypeStr += "ADV_IND";
break;
case ADV_DIRECT_IND:
pduTypeStr += "ADV_DIRECT_IND";
break;
case ADV_NONCONN_IND:
pduTypeStr += "ADV_NONCONN_IND";
break;
case SCAN_REQ:
pduTypeStr += "SCAN_REQ";
break;
case SCAN_RSP:
pduTypeStr += "SCAN_RSP";
break;
case CONNECT_REQ:
pduTypeStr += "CONNECT_REQ";
break;
case ADV_SCAN_IND:
pduTypeStr += "ADV_SCAN_IND";
break;
case RESERVED0:
case RESERVED1:
case RESERVED2:
case RESERVED3:
case RESERVED4:
case RESERVED5:
case RESERVED6:
case RESERVED7:
case RESERVED8:
pduTypeStr += "RESERVED";
break;
default:
pduTypeStr += "UNKNOWN";
break;
}
return pduTypeStr;
}
template <>
void RecentEntriesTable<BleRecentEntries>::draw(
const Entry& entry,
@@ -65,8 +125,9 @@ void RecentEntriesTable<BleRecentEntries>::draw(
Painter& painter,
const Style& style) {
std::string line{};
line.reserve(30);
if (!entry.nameString.empty()) {
if (!entry.nameString.empty() && entry.include_name) {
line = entry.nameString;
if (line.length() < 17) {
@@ -75,7 +136,7 @@ void RecentEntriesTable<BleRecentEntries>::draw(
line = truncate(line, 17);
}
} else {
line = to_string_mac_address(entry.packetData.macAddress, 6);
line = to_string_mac_address(entry.packetData.macAddress, 6, false);
}
// Pushing single digit values down right justified.
@@ -100,15 +161,69 @@ BleRecentEntryDetailView::BleRecentEntryDetailView(NavigationView& nav, const Bl
: nav_{nav},
entry_{entry} {
add_children({&button_done,
&button_send,
&button_save,
&label_mac_address,
&text_mac_address,
&label_pdu_type,
&text_pdu_type,
&labels});
text_mac_address.set(to_string_mac_address(entry.packetData.macAddress, 6));
text_mac_address.set(to_string_mac_address(entry.packetData.macAddress, 6, false));
text_pdu_type.set(pdu_type_to_string(entry.pduType));
button_done.on_select = [this](const ui::Button&) {
nav_.pop();
button_done.on_select = [&nav](const ui::Button&) {
nav.pop();
};
button_send.on_select = [this, &nav](const ui::Button&) {
auto packetToSend = build_packet();
nav.set_on_pop([packetToSend, &nav]() {
nav.replace<BLETxView>(packetToSend);
});
nav.pop();
};
button_save.on_select = [this, &nav](const ui::Button&) {
auto packetToSave = build_packet();
packetFileBuffer = "";
text_prompt(
nav,
packetFileBuffer,
64,
[this, packetToSave](std::string& buffer) {
on_save_file(buffer, packetToSave);
});
};
}
void BleRecentEntryDetailView::on_save_file(const std::string value, BLETxPacket packetToSave) {
std::filesystem::path packet_save_path{u"BLERX/Packets/????.TXT"};
ensure_directory(packet_save_path);
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;
saveFile(new_path, packetToSave);
}
bool BleRecentEntryDetailView::saveFile(const std::filesystem::path& path, BLETxPacket packetToSave) {
File f;
auto error = f.create(path);
if (error)
return false;
std::string macAddressStr = packetToSave.macAddress;
std::string advertisementDataStr = packetToSave.advertisementData;
std::string packetCountStr = packetToSave.packetCount;
std::string packetString = macAddressStr + ' ' + advertisementDataStr + ' ' + packetCountStr;
f.write(packetString.c_str(), packetString.length());
return true;
}
void BleRecentEntryDetailView::update_data() {
@@ -138,7 +253,7 @@ void BleRecentEntryDetailView::paint(Painter& painter) {
const auto s = style();
const auto rect = screen_rect();
auto field_rect = Rect{rect.left(), rect.top() + 48, rect.width(), 16};
auto field_rect = Rect{rect.left(), rect.top() + 64, rect.width(), 16};
uint8_t type[total_data_lines];
uint8_t length[total_data_lines];
@@ -150,41 +265,92 @@ void BleRecentEntryDetailView::paint(Painter& painter) {
int j = 0;
int k = 0;
for (currentByte = 0; (currentByte < entry_.packetData.dataLen) && (currentPacket < total_data_lines);) {
length[currentPacket] = entry_.packetData.data[currentByte++];
type[currentPacket] = entry_.packetData.data[currentByte++];
switch (entry_.pduType) {
case ADV_IND:
case ADV_NONCONN_IND:
case SCAN_RSP:
case ADV_SCAN_IND: {
ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)entry_.packetData.data;
// Subtract 1 because type is part of the length.
for (i = 0; i < length[currentPacket] - 1; i++) {
data[currentPacket][i] = entry_.packetData.data[currentByte++];
}
for (currentByte = 0; (currentByte < entry_.packetData.dataLen) && (currentPacket < total_data_lines);) {
length[currentPacket] = advertiseData->Data[currentByte++];
type[currentPacket] = advertiseData->Data[currentByte++];
currentPacket++;
}
// Subtract 1 because type is part of the length.
for (i = 0; i < length[currentPacket] - 1; i++) {
data[currentPacket][i] = advertiseData->Data[currentByte++];
}
for (i = 0; i < currentPacket; i++) {
uint8_t number_data_lines = ceil((float)(length[i] - 1) / 10.0);
uint8_t current_line = 0;
std::array<std::string, total_data_lines> data_strings{};
for (j = 0; (j < (number_data_lines * 10)) && (j < length[i] - 1); j++) {
if ((j / 10) != current_line) {
current_line++;
currentPacket++;
}
data_strings[current_line] += to_string_hex(data[i][j], 2);
}
for (i = 0; i < currentPacket; i++) {
uint8_t number_data_lines = ceil((float)(length[i] - 1) / 10.0);
uint8_t current_line = 0;
std::array<std::string, total_data_lines> data_strings{};
// Read the type back to the total length.
field_rect = draw_field(painter, field_rect, s, to_string_hex(length[i]), to_string_hex(type[i]) + pad_string_with_spaces(3) + data_strings[0]);
for (j = 0; (j < (number_data_lines * 10)) && (j < length[i] - 1); j++) {
if ((j / 10) != current_line) {
current_line++;
}
if (number_data_lines > 1) {
for (k = 1; k < number_data_lines; k++) {
if (data_strings[k].empty()) {
field_rect = draw_field(painter, field_rect, s, "", pad_string_with_spaces(5) + data_strings[k]);
data_strings[current_line] += to_string_hex(data[i][j], 2);
}
// Read the type back to the total length.
field_rect = draw_field(painter, field_rect, s, to_string_hex(length[i]), to_string_hex(type[i]) + pad_string_with_spaces(3) + data_strings[0]);
if (number_data_lines > 1) {
for (k = 1; k < number_data_lines; k++) {
if (!data_strings[k].empty()) {
field_rect = draw_field(painter, field_rect, s, "", pad_string_with_spaces(5) + data_strings[k]);
}
}
}
}
}
} break;
case ADV_DIRECT_IND:
case SCAN_REQ: {
ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)entry_.packetData.data;
uint8_t type = 0xFF;
field_rect = draw_field(painter, field_rect, s, to_string_hex(entry_.packetData.dataLen), to_string_hex(type) + pad_string_with_spaces(3) + to_string_mac_address(directed_mac_data->A1, 6, false));
} break;
case CONNECT_REQ:
default: {
uint8_t type = 0xFF;
// TODO: Display Connect Request Information. For right now just printing full hex data.
// This struct will eventually be used to break apart containing data of Connect Request.
// ADV_PDU_PAYLOAD_TYPE_5 * connect_req = (ADV_PDU_PAYLOAD_TYPE_5 *)entry_.packetData.data;
for (currentByte = 0; (currentByte < entry_.packetData.dataLen); currentByte++) {
data[0][currentByte] = entry_.packetData.data[currentByte];
}
uint8_t number_data_lines = ceil((float)entry_.packetData.dataLen / 10.0);
uint8_t current_line = 0;
std::array<std::string, total_data_lines> data_strings{};
for (j = 0; (j < (number_data_lines * 10)) && (j < entry_.packetData.dataLen); j++) {
if ((j / 10) != current_line) {
current_line++;
}
data_strings[current_line] += to_string_hex(data[0][j], 2);
}
field_rect = draw_field(painter, field_rect, s, to_string_hex(entry_.packetData.dataLen), to_string_hex(type) + pad_string_with_spaces(3) + data_strings[0]);
if (number_data_lines > 1) {
for (k = 1; k < number_data_lines; k++) {
if (!data_strings[k].empty()) {
field_rect = draw_field(painter, field_rect, s, "", pad_string_with_spaces(5) + data_strings[k]);
}
}
}
} break;
}
}
@@ -193,6 +359,20 @@ void BleRecentEntryDetailView::set_entry(const BleRecentEntry& entry) {
set_dirty();
}
BLETxPacket BleRecentEntryDetailView::build_packet() {
BLETxPacket bleTxPacket;
memset(&bleTxPacket, 0, sizeof(BLETxPacket));
std::string macAddressStr = to_string_mac_address(entry_.packetData.macAddress, 6, true);
strncpy(bleTxPacket.macAddress, macAddressStr.c_str(), 12);
strncpy(bleTxPacket.advertisementData, entry_.dataString.c_str(), entry_.packetData.dataLen * 2);
strncpy(bleTxPacket.packetCount, "50", 3);
bleTxPacket.packet_count = 50;
return bleTxPacket;
}
static std::uint64_t get_freq_by_channel_number(uint8_t channel_number) {
uint64_t freq_hz;
@@ -235,38 +415,46 @@ BLERxView::BLERxView(NavigationView& nav)
&options_channel,
&field_frequency,
&check_log,
&check_name,
&label_sort,
&options_sort,
&button_filter,
&button_save_list,
&button_clear_list,
&button_switch,
&recent_entries_view,
&recent_entries_filter_view,
&recent_entry_detail_view});
recent_entry_detail_view.hidden(true);
recent_entries_filter_view.hidden(true);
&recent_entries_view});
recent_entries_view.on_select = [this](const BleRecentEntry& entry) {
nav_.push<BleRecentEntryDetailView>(entry);
};
recent_entries_filter_view.on_select = [this](const BleRecentEntry& entry) {
nav_.push<BleRecentEntryDetailView>(entry);
};
button_filter.on_select = [this, &nav](Button&) {
button_filter.on_select = [this](Button&) {
text_prompt(
nav,
nav_,
filterBuffer,
64,
[this](std::string& buffer) {
on_switch_table(buffer);
on_filter_change(buffer);
});
};
button_switch.on_select = [this, &nav](Button&) {
nav_.set_on_pop([this]() { nav_.push<BLETxView>(); });
nav_.pop();
button_save_list.on_select = [this, &nav](const ui::Button&) {
listFileBuffer = "";
text_prompt(
nav,
listFileBuffer,
64,
[this](std::string& buffer) {
on_save_file(buffer);
});
};
button_clear_list.on_select = [this](Button&) {
recent.clear();
};
button_switch.on_select = [&nav](Button&) {
nav.replace<BLETxView>();
};
field_frequency.set_step(0);
@@ -281,51 +469,30 @@ BLERxView::BLERxView(NavigationView& nav)
logger->append(LOG_ROOT_DIR "/BLELOG_" + to_string_timestamp(rtc_time::now()) + ".TXT");
};
check_name.set_value(true);
check_name.on_select = [this](Checkbox&, bool v) {
setAllMembersToValue(recent, &BleRecentEntry::include_name, v);
recent_entries_view.set_dirty();
};
options_channel.on_change = [this](size_t, int32_t i) {
// If we selected Auto don't do anything and Auto will handle changing.
if (i == 40) {
auto_channel = true;
return;
} else {
auto_channel = false;
}
field_frequency.set_value(get_freq_by_channel_number(i));
channel_number = i;
baseband::set_btlerx(channel_number);
};
options_sort.on_change = [this](size_t, int32_t i) {
switch (i) {
case 0:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
break;
case 1:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
break;
case 2:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.dbValue; }, true);
break;
case 3:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
break;
case 4:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
break;
default:
break;
}
recent_entries_view.set_dirty();
recent_entries_filter_view.set_dirty();
options_sort.on_change = [this](size_t, int32_t index) {
handle_entries_sort(index);
};
options_channel.set_selected_index(0, true);
@@ -339,6 +506,156 @@ BLERxView::BLERxView(NavigationView& nav)
receiver_model.enable();
}
std::string BLERxView::build_line_str(BleRecentEntry entry) {
std::string macAddressStr = to_string_mac_address(entry.packetData.macAddress, 6, false) + ",";
std::string timestameStr = entry.timestamp + ",";
std::string nameStr = entry.nameString + ",";
std::string pduStr = pdu_type_to_string(entry.pduType) + ",";
std::string dataStr = "0x" + entry.dataString + ",";
std::string hitsStr = to_string_dec_int(entry.numHits) + ",";
std::string dbStr = to_string_dec_int(entry.dbValue) + ",";
std::string channelStr = to_string_dec_int(entry.channelNumber) + ",";
std::string lineStr = timestameStr + macAddressStr + nameStr + pduStr + dataStr + hitsStr + dbStr + channelStr;
lineStr += pad_string_with_spaces(maxLineLength - lineStr.length());
return lineStr;
}
void BLERxView::on_save_file(const std::string value) {
std::filesystem::path packet_save_path{u"BLERX/Lists/????.csv"};
ensure_directory(packet_save_path);
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;
saveFile(new_path);
}
bool BLERxView::saveFile(const std::filesystem::path& path) {
// Check to see if file was previously saved.
bool file_existed = file_exists(path);
// Attempt to open, if it can't be opened. Create new.
auto src = std::make_unique<File>();
auto error = src->open(path, false, true);
if (error) {
return false;
}
for (const auto& entry : recent) {
tempList.emplace_back(entry);
}
if (!file_existed) {
src->write_line(headerStr.c_str());
auto it = tempList.begin();
while (it != tempList.end()) {
BleRecentEntry entry = (BleRecentEntry)*it;
src->write_line(build_line_str(entry).c_str());
it++;
}
} else {
// Check file for macAddressStr before adding.
char currentLine[maxLineLength];
uint64_t startPos = headerStr.length();
uint64_t bytesRead = 0;
uint64_t bytePos = 0;
File::Size currentSize = src->size();
auto dst = std::make_unique<File>();
const std::filesystem::path tempFilePath = path + "~";
auto error = dst->open(tempFilePath, false, true);
if (error) {
return false;
}
dst->write_line(headerStr.c_str());
src->seek(startPos);
// Look for ones found and rewrite.
do {
memset(currentLine, 0, maxLineLength);
bytesRead = readUntil(*src, currentLine, currentSize, '\n');
if (!bytesRead) {
break;
}
bytePos += bytesRead;
std::string lineStr = "";
std::string macAddressStr = "";
BleRecentEntry foundEntry;
char* token;
token = strtok(currentLine, ",");
while (token != NULL) {
auto it = tempList.begin();
while (it != tempList.end()) {
BleRecentEntry& entry = reinterpret_cast<BleRecentEntry&>(*it);
macAddressStr = to_string_mac_address(entry.packetData.macAddress, 6, false);
if (strstr(token, macAddressStr.c_str()) != NULL) {
entry.entryFound = true;
foundEntry = entry;
break;
}
it++;
}
if (foundEntry.entryFound) {
break;
}
token = strtok(NULL, ",");
}
if (foundEntry.entryFound) {
dst->write_line(build_line_str(foundEntry).c_str());
}
} while (bytePos <= currentSize);
// Write the ones not found.
auto it = tempList.begin();
while (it != tempList.end()) {
BleRecentEntry entry = (BleRecentEntry)*it;
if (!entry.entryFound) {
dst->write_line(build_line_str(entry).c_str());
}
it++;
}
// Close files before renaming/deleting.
src.reset();
dst.reset();
// Delete original and overwrite with temp file.
delete_file(path);
rename_file(tempFilePath, path);
}
tempList.clear();
return true;
}
void BLERxView::on_data(BlePacketData* packet) {
std::string str_console = "";
@@ -346,43 +663,7 @@ void BLERxView::on_data(BlePacketData* packet) {
str_log = "";
}
switch ((ADV_PDU_TYPE)packet->type) {
case ADV_IND:
str_console += "ADV_IND";
break;
case ADV_DIRECT_IND:
str_console += "ADV_DIRECT_IND";
break;
case ADV_NONCONN_IND:
str_console += "ADV_NONCONN_IND";
break;
case SCAN_REQ:
str_console += "SCAN_REQ";
break;
case SCAN_RSP:
str_console += "SCAN_RSP";
break;
case CONNECT_REQ:
str_console += "CONNECT_REQ";
break;
case ADV_SCAN_IND:
str_console += "ADV_SCAN_IND";
break;
case RESERVED0:
case RESERVED1:
case RESERVED2:
case RESERVED3:
case RESERVED4:
case RESERVED5:
case RESERVED6:
case RESERVED7:
case RESERVED8:
str_console += "RESERVED";
break;
default:
str_console += "UNKNOWN";
break;
}
str_console += pdu_type_to_string((ADV_PDU_TYPE)packet->type);
str_console += " Len:";
str_console += to_string_dec_uint(packet->size);
@@ -390,7 +671,7 @@ void BLERxView::on_data(BlePacketData* packet) {
str_console += "\n";
str_console += "Mac:";
str_console += to_string_mac_address(packet->macAddress, 6);
str_console += to_string_mac_address(packet->macAddress, 6, false);
str_console += "\n";
str_console += "Data:";
@@ -408,8 +689,15 @@ void BLERxView::on_data(BlePacketData* packet) {
// Start of Packet stuffing.
// Masking off the top 2 bytes to avoid invalid keys.
auto& entry = ::on_packet(recent, macAddressEncoded & 0xFFFFFFFFFFFF);
truncate_entries(recent, 32);
updateEntry(packet, entry);
updateEntry(packet, entry, (ADV_PDU_TYPE)packet->type);
// Add entries if they meet the criteria.
auto value = filter;
resetFilteredEntries(recent, [&value](const BleRecentEntry& entry) {
return (entry.dataString.find(value) == std::string::npos) && (entry.nameString.find(value) == std::string::npos);
});
handle_entries_sort(options_sort.selected_index());
// Log at End of Packet.
if (logger && logging) {
@@ -417,33 +705,67 @@ void BLERxView::on_data(BlePacketData* packet) {
}
}
void BLERxView::on_switch_table(const std::string value) {
filter = value;
if (!value.empty()) {
removeEntriesWithoutKey(recent, filterEntries, [&value](const BleRecentEntry& entry) {
return entry.dataString.find(value) == std::string::npos;
void BLERxView::on_filter_change(std::string value) {
// New filter? Reset list from recent entries.
if (filter != value) {
resetFilteredEntries(recent, [&value](const BleRecentEntry& entry) {
return (entry.dataString.find(value) == std::string::npos) && (entry.nameString.find(value) == std::string::npos);
});
}
recent_entries_view.set_dirty();
filter = value;
}
recent_entries_filter_view.hidden(false);
recent_entries_view.hidden(true);
} else {
recent_entries_view.hidden(false);
recent_entries_filter_view.hidden(true);
// called each 1/60th of second, so 6 = 100ms
void BLERxView::on_timer() {
if (++timer_count == timer_period) {
timer_count = 0;
if (auto_channel) {
int min = 37;
int max = 39;
int randomChannel = min + std::rand() % (max - min + 1);
field_frequency.set_value(get_freq_by_channel_number(randomChannel));
baseband::set_btlerx(randomChannel);
}
}
}
void BLERxView::handle_entries_sort(uint8_t index) {
switch (index) {
case 0:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
break;
case 1:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
break;
case 2:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, false);
break;
case 3:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
break;
case 4:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
break;
default:
break;
}
recent_entries_view.set_dirty();
recent_entries_filter_view.set_dirty();
}
void BLERxView::set_parent_rect(const Rect new_parent_rect) {
View::set_parent_rect(new_parent_rect);
const Rect content_rect{0, header_height, new_parent_rect.width(), new_parent_rect.height() - header_height - switch_button_height};
recent_entries_view.set_parent_rect(content_rect);
recent_entry_detail_view.set_parent_rect(content_rect);
recent_entries_filter_view.set_parent_rect(content_rect);
}
BLERxView::~BLERxView() {
@@ -451,7 +773,7 @@ BLERxView::~BLERxView() {
baseband::shutdown();
}
void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry) {
void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry, ADV_PDU_TYPE pdu_type) {
std::string data_string;
int i;
@@ -468,6 +790,7 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry)
entry.packetData.size = packet->size;
entry.packetData.dataLen = packet->dataLen;
// Mac Address of sender.
entry.packetData.macAddress[0] = packet->macAddress[0];
entry.packetData.macAddress[1] = packet->macAddress[1];
entry.packetData.macAddress[2] = packet->macAddress[2];
@@ -476,77 +799,44 @@ void BLERxView::updateEntry(const BlePacketData* packet, BleRecentEntry& entry)
entry.packetData.macAddress[5] = packet->macAddress[5];
entry.numHits++;
entry.pduType = pdu_type;
entry.channelNumber = channel_number;
// Parse Data Section into buffer to be interpretted later.
for (int i = 0; i < packet->dataLen; i++) {
entry.packetData.data[i] = packet->data[i];
}
entry.nameString = "";
entry.include_name = check_name.value();
uint8_t currentByte = 0;
uint8_t length = 0;
uint8_t type = 0;
// Only parse name for advertisment packets and empty name entries
if ((pdu_type == ADV_IND || pdu_type == ADV_NONCONN_IND || pdu_type == SCAN_RSP || pdu_type == ADV_SCAN_IND) && entry.nameString.empty()) {
ADV_PDU_PAYLOAD_TYPE_0_2_4_6* advertiseData = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)entry.packetData.data;
bool stringFound = false;
uint8_t currentByte = 0;
uint8_t length = 0;
uint8_t type = 0;
for (currentByte = 0; (currentByte < entry.packetData.dataLen);) {
length = entry.packetData.data[currentByte++];
type = entry.packetData.data[currentByte++];
std::string decoded_data;
for (currentByte = 0; (currentByte < entry.packetData.dataLen);) {
length = advertiseData->Data[currentByte++];
type = advertiseData->Data[currentByte++];
// Subtract 1 because type is part of the length.
for (int i = 0; i < length - 1; i++) {
if (((type == 0x08) || (type == 0x09)) && !stringFound) {
entry.nameString += (char)entry.packetData.data[currentByte];
// Subtract 1 because type is part of the length.
for (int i = 0; i < length - 1; i++) {
if (type == 0x08 || type == 0x09) {
decoded_data += (char)advertiseData->Data[currentByte];
}
currentByte++;
}
if (!decoded_data.empty()) {
entry.nameString = std::move(decoded_data);
break;
}
currentByte++;
}
if (!entry.nameString.empty()) {
stringFound = true;
}
}
switch (options_sort.selected_index()) {
case 0:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.macAddress; }, true);
break;
case 1:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.numHits; }, false);
break;
case 2:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.dbValue; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.dbValue; }, true);
break;
case 3:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.timestamp; }, false);
break;
case 4:
sortEntriesBy(
recent, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
sortEntriesBy(
filterEntries, [](const BleRecentEntry& entry) { return entry.nameString; }, true);
break;
default:
break;
}
on_switch_table(filter);
// TODO: Crude hack, should be a more formal listener arrangement...
if (entry.key() == recent_entry_detail_view.entry().key()) {
recent_entry_detail_view.set_entry(entry);
} else if (pdu_type == ADV_DIRECT_IND || pdu_type == SCAN_REQ) {
ADV_PDU_PAYLOAD_TYPE_1_3* directed_mac_data = (ADV_PDU_PAYLOAD_TYPE_1_3*)entry.packetData.data;
reverse_byte_array(directed_mac_data->A1, 6);
}
}
+74 -15
View File
@@ -24,6 +24,8 @@
#ifndef __BLE_RX_APP_H__
#define __BLE_RX_APP_H__
#include "ble_tx_app.hpp"
#include "ui.hpp"
#include "ui_navigation.hpp"
#include "ui_receiver.hpp"
@@ -79,7 +81,11 @@ struct BleRecentEntry {
std::string timestamp;
std::string dataString;
std::string nameString;
bool include_name;
uint16_t numHits;
ADV_PDU_TYPE pduType;
uint8_t channelNumber;
bool entryFound;
BleRecentEntry()
: BleRecentEntry{0} {
@@ -93,7 +99,11 @@ struct BleRecentEntry {
timestamp{},
dataString{},
nameString{},
numHits{} {
include_name{},
numHits{},
pduType{},
channelNumber{},
entryFound{} {
}
Key key() const {
@@ -118,6 +128,10 @@ class BleRecentEntryDetailView : public View {
private:
NavigationView& nav_;
BleRecentEntry entry_{};
BLETxPacket build_packet();
void on_save_file(const std::string value, BLETxPacket packetToSave);
bool saveFile(const std::filesystem::path& path, BLETxPacket packetToSave);
std::string packetFileBuffer{};
static constexpr uint8_t total_data_lines{5};
@@ -128,16 +142,31 @@ class BleRecentEntryDetailView : public View {
{12 * 8, 0 * 16, 17 * 8, 16},
"-"};
Labels label_pdu_type{
{{0 * 8, 1 * 16}, "PDU Type:", Color::light_grey()}};
Text text_pdu_type{
{9 * 8, 1 * 16, 17 * 8, 16},
"-"};
Labels labels{
{{0 * 8, 2 * 16}, "Len", Color::light_grey()},
{{5 * 8, 2 * 16}, "Type", Color::light_grey()},
{{10 * 8, 2 * 16}, "Value", Color::light_grey()},
{{0 * 8, 3 * 16}, "Len", Color::light_grey()},
{{5 * 8, 3 * 16}, "Type", Color::light_grey()},
{{10 * 8, 3 * 16}, "Value", Color::light_grey()},
};
Button button_send{
{19, 224, 96, 24},
"Send"};
Button button_done{
{125, 224, 96, 24},
"Done"};
Button button_save{
{72, 264, 96, 24},
"Save"};
bool send_updates{false};
Rect draw_field(
@@ -161,9 +190,14 @@ class BLERxView : public View {
std::string title() const override { return "BLE RX"; };
private:
std::string build_line_str(BleRecentEntry entry);
void on_save_file(const std::string value);
bool saveFile(const std::filesystem::path& path);
void on_data(BlePacketData* packetData);
void on_switch_table(const std::string value);
void updateEntry(const BlePacketData* packet, BleRecentEntry& entry);
void on_filter_change(std::string value);
void on_timer();
void handle_entries_sort(uint8_t index);
void updateEntry(const BlePacketData* packet, BleRecentEntry& entry, ADV_PDU_TYPE pdu_type);
NavigationView& nav_;
RxRadioState radio_state_{
@@ -177,9 +211,16 @@ class BLERxView : public View {
uint8_t console_color{0};
uint32_t prev_value{0};
uint8_t channel_number = 37;
bool auto_channel = false;
int16_t timer_count{0};
int16_t timer_period{6}; // 100ms
std::string filterBuffer{};
std::string filter{};
std::string listFileBuffer{};
std::string headerStr = "Timestamp, MAC Address, Name, Packet Type, Data, Hits, dB, Channel";
uint16_t maxLineLength = 140;
static constexpr auto header_height = 3 * 16;
static constexpr auto switch_button_height = 3 * 16;
@@ -189,7 +230,8 @@ class BLERxView : public View {
5,
{{"Ch.37 ", 37},
{"Ch.38", 38},
{"Ch.39", 39}}};
{"Ch.39", 39},
{"Auto", 40}}};
RxFrequencyField field_frequency{
{6 * 8, 0 * 16},
@@ -223,21 +265,35 @@ class BLERxView : public View {
{"Name", 4}}};
Button button_filter{
{12 * 8, 3 * 8, 4 * 8, 16},
{11 * 8, 3 * 8, 4 * 8, 16},
"Filter"};
Checkbox check_log{
{20 * 8, 3 * 8},
{17 * 8, 3 * 8},
3,
"Log",
true};
Checkbox check_name{
{23 * 8, 3 * 8},
3,
"Name",
true};
Console console{
{0, 4 * 16, 240, 240}};
Button button_clear_list{
{2 * 8, 320 - (16 + 32), 7 * 8, 32},
"Clear"};
Button button_save_list{
{11 * 8, 320 - (16 + 32), 11 * 8, 32},
"Export CSV"};
Button button_switch{
{8 * 8, 16 * 16, 14 * 8, 2 * 16},
"Switch to Tx"};
{240 - 6 * 8, 320 - (16 + 32), 4 * 8, 32},
"Tx"};
std::string str_log{""};
bool logging{false};
@@ -245,7 +301,7 @@ class BLERxView : public View {
std::unique_ptr<BLELogger> logger{};
BleRecentEntries recent{};
BleRecentEntries filterEntries{};
BleRecentEntries tempList{};
const RecentEntriesColumns columns{{
{"Mac Address", 17},
@@ -253,10 +309,7 @@ class BLERxView : public View {
{"dB", 4},
}};
BleRecentEntry entry_{};
BleRecentEntriesView recent_entries_view{columns, recent};
BleRecentEntriesView recent_entries_filter_view{columns, filterEntries};
BleRecentEntryDetailView recent_entry_detail_view{nav_, entry_};
MessageHandlerRegistration message_handler_packet{
Message::ID::BlePacket,
@@ -264,6 +317,12 @@ class BLERxView : public View {
const auto message = static_cast<const BLEPacketMessage*>(p);
this->on_data(message->packet);
}};
MessageHandlerRegistration message_handler_frame_sync{
Message::ID::DisplayFrameSync,
[this](const Message* const) {
this->on_timer();
}};
};
} /* namespace ui */
+205 -91
View File
@@ -94,44 +94,6 @@ uint32_t stringToUint32(const std::string& str) {
return result;
}
void readUntil(File& file, char* result, std::size_t maxBufferSize, char delimiter) {
std::size_t bytesRead = 0;
while (true) {
char ch;
File::Result<File::Size> readResult = file.read(&ch, 1);
if (readResult.is_ok() && readResult.value() > 0) {
if (ch == delimiter) {
// Found a space character, stop reading
break;
} else if (bytesRead < maxBufferSize) {
// Append the character to the result if there's space
result[bytesRead++] = ch;
} else {
// Buffer is full, break to prevent overflow
break;
}
} else {
break; // End of file or error
}
}
// Null-terminate the result string
result[bytesRead] = '\0';
}
void generateRandomMacAddress(char* macAddress) {
const char hexDigits[] = "0123456789ABCDEF";
// Generate 12 random hexadecimal characters
for (int i = 0; i < 12; i++) {
int randomIndex = rand() % 16;
macAddress[i] = hexDigits[randomIndex];
}
macAddress[12] = '\0'; // Null-terminate the string
}
static std::uint64_t get_freq_by_channel_number(uint8_t channel_number) {
uint64_t freq_hz;
@@ -172,6 +134,30 @@ void BLETxView::file_error() {
nav_.display_modal("Error", "File read error.");
}
bool BLETxView::saveFile(const std::filesystem::path& path) {
File f;
auto error = f.create(path);
if (error)
return false;
for (uint32_t i = 0; i < num_packets; i++) {
std::string macAddressStr = packets[i].macAddress;
std::string advertisementDataStr = packets[i].advertisementData;
std::string packetCountStr = packets[i].packetCount;
std::string packetString = macAddressStr + ' ' + advertisementDataStr + ' ' + packetCountStr;
// Are we on the last line?
if (i != num_packets - 1) {
packetString += '\n';
}
f.write(packetString.c_str(), packetString.length());
}
return true;
}
void BLETxView::toggle() {
if (is_active()) {
stop();
@@ -181,92 +167,138 @@ void BLETxView::toggle() {
}
void BLETxView::start() {
baseband::run_image(portapack::spi_flash::image_tag_btle_tx);
transmitter_model.enable();
// Generate new random Mac Address.
generateRandomMacAddress(randomMac);
// If this is our first run, check file.
if (!is_active()) {
// Check if file is present before continuing.
File data_file;
auto error = data_file.open(file_path);
if (error) {
if (error && !file_override) {
file_error();
check_loop.set_value(false);
return;
} else {
// Send first or single packet.
packet_counter = packets[current_packet].packet_count;
progressbar.set_max(packets[current_packet].packet_count);
button_play.set_bitmap(&bitmap_stop);
baseband::set_btletx(channel_number, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType);
transmitter_model.enable();
is_running = true;
}
} else {
// Send next packet.
baseband::set_btletx(channel_number, random_mac ? randomMac : packets[current_packet].macAddress, packets[current_packet].advertisementData, pduType);
button_play.set_bitmap(&bitmap_stop);
is_running = true;
}
if ((packet_counter % 10) == 0) {
text_packets_sent.set(to_string_dec_uint(packet_counter));
char advertisementData[63] = {0};
strcpy(advertisementData, packets[current_packet].advertisementData);
// TODO: Make this a checkbox.
if (!markedBytes.empty()) {
for (size_t i = 0; i < strlen(advertisementData); i++) {
bool found = false;
auto it = std::find(markedBytes.begin(), markedBytes.end(), i);
if (it != markedBytes.end()) {
found = true;
}
if (found) {
int min = 0;
int max = 15;
int hexDigit = min + std::rand() % (max - min + 1);
// Map the random number to a hexadecimal digit
char randomHexChar = (hexDigit < 10) ? ('0' + hexDigit) : ('A' + hexDigit - 10);
advertisementData[i] = randomHexChar;
}
}
}
packet_counter--;
progressbar.set_value(packets[current_packet].packet_count - packet_counter);
// Setup next packet configuration.
progressbar.set_max(packets[current_packet].packet_count);
baseband::set_btletx(channel_number, random_mac ? randomMac : packets[current_packet].macAddress, advertisementData, pduType);
}
void BLETxView::stop() {
transmitter_model.disable();
baseband::shutdown();
progressbar.set_value(0);
button_play.set_bitmap(&bitmap_play);
check_loop.set_value(false);
current_packet = 0;
text_packets_sent.set(to_string_dec_uint(packets[0].packet_count));
packet_counter = packets[0].packet_count;
update_packet_display(packets[0]);
update_current_packet(packets[0], 0);
is_running = false;
}
void BLETxView::on_tx_progress(const bool done) {
if (done) {
void BLETxView::reset() {
transmitter_model.disable();
baseband::shutdown();
start();
}
// called each 1/60th of second, so 6 = 100ms
void BLETxView::on_timer() {
if (++timer_count == timer_period) {
timer_count = 0;
if (is_active()) {
// Reached end of current packet repeats.
if (packet_counter == 0) {
// Done sending all packets.
if (current_packet == (num_packets - 1)) {
current_packet = 0;
// If looping, restart from beginning.
if (check_loop.value()) {
current_packet = 0;
packet_counter = packets[current_packet].packet_count;
update_packet_display(packets[current_packet]);
update_current_packet(packets[current_packet], current_packet);
reset();
} else {
stop();
}
} else {
current_packet++;
packet_counter = packets[current_packet].packet_count;
update_packet_display(packets[current_packet]);
update_current_packet(packets[current_packet], current_packet);
reset();
}
} else {
if ((timer_count % timer_period) == 0) {
start();
}
reset();
}
}
}
if (++auto_channel_counter == auto_channel_period) {
auto_channel_counter = 0;
if (auto_channel) {
int min = 37;
int max = 39;
channel_number = min + std::rand() % (max - min + 1);
field_frequency.set_value(get_freq_by_channel_number(channel_number));
}
}
}
void BLETxView::on_tx_progress(const bool done) {
if (done) {
if (is_active()) {
if ((packet_counter % 10) == 0) {
text_packets_sent.set(to_string_dec_uint(packet_counter));
}
timer_count++;
packet_counter--;
progressbar.set_value(packets[current_packet].packet_count - packet_counter);
}
}
}
BLETxView::BLETxView(NavigationView& nav)
: nav_{nav} {
baseband::run_image(portapack::spi_flash::image_tag_btle_tx);
add_children({&button_open,
&text_filename,
&progressbar,
@@ -286,22 +318,36 @@ BLETxView::BLETxView(NavigationView& nav)
&label_mac_address,
&text_mac_address,
&label_data_packet,
&button_switch,
&console});
&label_data_index,
&text_data_index,
&dataEditView,
&button_save_packet,
&button_switch});
field_frequency.set_step(0);
ensure_directory(packet_save_path);
button_play.on_select = [this](ImageButton&) {
this->toggle();
};
options_channel.on_change = [this](size_t, int32_t i) {
// If we selected Auto don't do anything and Auto will handle changing.
if (i == 40) {
auto_channel = true;
return;
} else {
auto_channel = false;
}
field_frequency.set_value(get_freq_by_channel_number(i));
channel_number = i;
};
options_speed.on_change = [this](size_t, int32_t i) {
timer_period = i;
timer_count = 0;
};
options_adv_type.on_change = [this](size_t, int32_t i) {
@@ -309,14 +355,16 @@ BLETxView::BLETxView(NavigationView& nav)
};
options_speed.set_selected_index(0);
options_channel.set_selected_index(0);
options_adv_type.set_selected_index(0);
check_rand_mac.set_value(false);
check_rand_mac.on_select = [this](Checkbox&, bool v) {
random_mac = v;
};
button_open.on_select = [this, &nav](Button&) {
auto open_view = nav.push<FileLoadView>(".TXT");
button_open.on_select = [this](Button&) {
auto open_view = nav_.push<FileLoadView>(".TXT");
open_view->on_changed = [this](std::filesystem::path new_file_path) {
on_file_changed(new_file_path);
@@ -324,10 +372,52 @@ BLETxView::BLETxView(NavigationView& nav)
};
};
button_switch.on_select = [this, &nav](Button&) {
nav_.set_on_pop([this]() { nav_.push<BLERxView>(); });
nav_.pop();
button_save_packet.on_select = [this, &nav](Button&) {
packetFileBuffer = "";
text_prompt(
nav,
packetFileBuffer,
64,
[this](std::string& buffer) {
on_save_file(buffer);
});
};
button_switch.on_select = [&nav](Button&) {
nav.replace<BLERxView>();
};
dataEditView.on_select = [this] {
// Save last selected cursor.
cursor_pos.line = dataEditView.line();
cursor_pos.col = dataEditView.col();
uint16_t dataBytePos = ((dataEditView.line()) * 30) + dataEditView.col();
auto it = std::find(markedBytes.begin(), markedBytes.end(), dataBytePos);
if (it != markedBytes.end()) {
markedBytes.erase(it);
} else {
markedBytes.push_back(dataBytePos);
}
dataEditView.cursor_mark_selected();
text_data_index.set(to_string_dec_uint(dataBytePos));
};
}
BLETxView::BLETxView(
NavigationView& nav,
BLETxPacket packet)
: BLETxView(nav) {
packets[0] = packet;
update_current_packet(packets[0], 0);
num_packets = 1;
file_override = true;
}
void BLETxView::on_file_changed(const fs::path& new_file_path) {
@@ -353,11 +443,10 @@ void BLETxView::on_file_changed(const fs::path& new_file_path) {
uint64_t packetCountSize = strlen(packets[num_packets].packetCount);
packets[num_packets].packet_count = stringToUint32(packets[num_packets].packetCount);
packet_counter = packets[num_packets].packet_count;
// Verify Data.
if ((macAddressSize == mac_address_size_str) && (advertisementDataSize < max_packet_size_str) && (packetCountSize < max_packet_repeat_str) &&
hasValidHexPairs(packets[num_packets].macAddress, macAddressSize / 2) && hasValidHexPairs(packets[num_packets].advertisementData, advertisementDataSize / 2) && (packets[num_packets].packet_count > 0) && (packets[num_packets].packet_count < max_packet_repeat_count)) {
hasValidHexPairs(packets[num_packets].macAddress, macAddressSize / 2) && hasValidHexPairs(packets[num_packets].advertisementData, advertisementDataSize / 2) && (packets[num_packets].packet_count >= 1) && (packets[num_packets].packet_count < max_packet_repeat_count)) {
text_filename.set(truncate(file_path.filename().string(), 12));
} else {
@@ -374,21 +463,27 @@ void BLETxView::on_file_changed(const fs::path& new_file_path) {
} while (num_packets < max_num_packets);
update_packet_display(packets[0]);
update_current_packet(packets[0], 0);
}
}
void BLETxView::on_save_file(const std::string value) {
auto folder = packet_save_path.parent_path();
auto ext = packet_save_path.extension();
auto new_path = folder / value + ext;
saveFile(new_path);
}
void BLETxView::on_data(uint32_t value, bool is_data) {
std::string str_console = "";
if (is_data) {
str_console += (char)(value);
}
console.write(str_console);
}
void BLETxView::update_packet_display(BLETxPacket packet) {
void BLETxView::update_current_packet(BLETxPacket packet, uint32_t currentIndex) {
std::string formattedMacAddress = to_string_formatted_mac_address(packet.macAddress);
std::vector<std::string> strings = splitIntoStrings(packet.advertisementData);
@@ -399,20 +494,39 @@ void BLETxView::update_packet_display(BLETxPacket packet) {
text_mac_address.set(formattedMacAddress);
console.clear(true);
packet_counter = packet.packet_count;
current_packet = currentIndex;
dataFile.create(dataTempFilePath);
for (const std::string& str : strings) {
console.writeln(str);
dataFile.write(str.c_str(), str.size());
dataFile.write("\n", 1);
;
}
dataFile.~File();
auto result = FileWrapper::open(dataTempFilePath);
if (!result)
return;
dataFileWrapper = *std::move(result);
dataEditView.set_font_zoom(true);
dataEditView.set_file(*dataFileWrapper);
dataEditView.redraw(true, true);
}
void BLETxView::set_parent_rect(const Rect new_parent_rect) {
View::set_parent_rect(new_parent_rect);
const Rect content_rect{0, header_height, new_parent_rect.width(), new_parent_rect.height() - header_height - switch_button_height};
console.set_parent_rect(content_rect);
dataEditView.set_parent_rect(content_rect);
}
BLETxView::~BLETxView() {
delete_file(dataTempFilePath);
transmitter_model.disable();
baseband::shutdown();
}
+109 -60
View File
@@ -28,6 +28,7 @@
#include "ui_navigation.hpp"
#include "ui_receiver.hpp"
#include "ui_transmitter.hpp"
#include "ui_text_editor.hpp"
#include "ui_freq_field.hpp"
#include "ui_record_view.hpp"
#include "app_settings.hpp"
@@ -55,9 +56,48 @@ class BLELoggerTx {
namespace ui {
enum PKT_TYPE {
PKT_TYPE_INVALID_TYPE,
PKT_TYPE_RAW,
PKT_TYPE_DISCOVERY,
PKT_TYPE_IBEACON,
PKT_TYPE_ADV_IND,
PKT_TYPE_ADV_DIRECT_IND,
PKT_TYPE_ADV_NONCONN_IND,
PKT_TYPE_ADV_SCAN_IND,
PKT_TYPE_SCAN_REQ,
PKT_TYPE_SCAN_RSP,
PKT_TYPE_CONNECT_REQ,
PKT_TYPE_LL_DATA,
PKT_TYPE_LL_CONNECTION_UPDATE_REQ,
PKT_TYPE_LL_CHANNEL_MAP_REQ,
PKT_TYPE_LL_TERMINATE_IND,
PKT_TYPE_LL_ENC_REQ,
PKT_TYPE_LL_ENC_RSP,
PKT_TYPE_LL_START_ENC_REQ,
PKT_TYPE_LL_START_ENC_RSP,
PKT_TYPE_LL_UNKNOWN_RSP,
PKT_TYPE_LL_FEATURE_REQ,
PKT_TYPE_LL_FEATURE_RSP,
PKT_TYPE_LL_PAUSE_ENC_REQ,
PKT_TYPE_LL_PAUSE_ENC_RSP,
PKT_TYPE_LL_VERSION_IND,
PKT_TYPE_LL_REJECT_IND,
PKT_TYPE_NUM_PKT_TYPE
};
struct BLETxPacket {
char macAddress[13];
char advertisementData[63];
char packetCount[11];
uint32_t packet_count;
PKT_TYPE packetType;
};
class BLETxView : public View {
public:
BLETxView(NavigationView& nav);
BLETxView(NavigationView& nav, BLETxPacket packet);
~BLETxView();
void set_parent_rect(const Rect new_parent_rect) override;
@@ -69,23 +109,21 @@ class BLETxView : public View {
void toggle();
void start();
void stop();
void reset();
void handle_replay_thread_done(const uint32_t return_code);
void file_error();
bool saveFile(const std::filesystem::path& path);
std::string title() const override { return "BLE TX"; };
struct BLETxPacket {
char macAddress[13];
char advertisementData[63];
char packetCount[11];
uint32_t packet_count;
};
private:
void on_timer();
void on_data(uint32_t value, bool is_data);
void on_tx_progress(const bool done);
void on_file_changed(const std::filesystem::path& new_file_path);
void update_packet_display(BLETxPacket packet);
void on_save_file(const std::string value);
void on_tx_progress(const bool done);
void on_random_data_change(std::string value);
void update_current_packet(BLETxPacket packet, uint32_t currentIndex);
NavigationView& nav_;
TxRadioState radio_state_{
@@ -100,61 +138,49 @@ class BLETxView : public View {
uint32_t prev_value{0};
std::filesystem::path file_path{};
std::filesystem::path packet_save_path{u"BLETX/BLETX_????.TXT"};
uint8_t channel_number = 37;
bool auto_channel = false;
char randomMac[13] = "010203040506";
bool is_running = false;
uint64_t timer_count{0};
uint64_t timer_period{256};
int16_t timer_count{0};
int16_t timer_period{1};
int16_t auto_channel_counter = 0;
int16_t auto_channel_period{6};
bool repeatLoop = false;
uint32_t packet_counter{0};
uint32_t num_packets{0};
uint32_t current_packet{0};
bool random_mac = false;
bool file_override = false;
enum PKT_TYPE {
INVALID_TYPE,
RAW,
DISCOVERY,
IBEACON,
ADV_IND,
ADV_DIRECT_IND,
ADV_NONCONN_IND,
ADV_SCAN_IND,
SCAN_REQ,
SCAN_RSP,
CONNECT_REQ,
LL_DATA,
LL_CONNECTION_UPDATE_REQ,
LL_CHANNEL_MAP_REQ,
LL_TERMINATE_IND,
LL_ENC_REQ,
LL_ENC_RSP,
LL_START_ENC_REQ,
LL_START_ENC_RSP,
LL_UNKNOWN_RSP,
LL_FEATURE_REQ,
LL_FEATURE_RSP,
LL_PAUSE_ENC_REQ,
LL_PAUSE_ENC_RSP,
LL_VERSION_IND,
LL_REJECT_IND,
NUM_PKT_TYPE
};
typedef struct {
uint16_t line;
uint16_t col;
} CursorPos;
std::unique_ptr<FileWrapper> dataFileWrapper{};
File dataFile{};
std::filesystem::path dataTempFilePath{u"BLETX/dataFileTemp.TXT"};
std::vector<uint16_t> markedBytes{};
CursorPos cursor_pos{};
static constexpr uint8_t mac_address_size_str{12};
static constexpr uint8_t max_packet_size_str{62};
static constexpr uint8_t max_packet_repeat_str{10};
static constexpr uint32_t max_packet_repeat_count{UINT32_MAX};
static constexpr uint32_t max_num_packets{256};
static constexpr uint32_t max_num_packets{32};
BLETxPacket packets[max_num_packets];
PKT_TYPE pduType = {DISCOVERY};
PKT_TYPE pduType = {PKT_TYPE_DISCOVERY};
static constexpr auto header_height = 9 * 16;
static constexpr auto switch_button_height = 3 * 16;
static constexpr auto switch_button_height = 5 * 16;
Button button_open{
{0 * 8, 0 * 16, 10 * 8, 2 * 16},
@@ -199,29 +225,31 @@ class BLETxView : public View {
OptionsField options_speed{
{7 * 8, 6 * 8},
3,
{{"1 ", 256},
{"2 ", 128},
{"3 ", 64},
{"4 ", 32},
{"5 ", 16}}};
{{"1 ", 1}, // 16ms
{"2 ", 2}, // 32ms
{"3 ", 3}, // 48ms
{"4 ", 6}, // 100ms
{"5 ", 12}}}; // 200ms
OptionsField options_channel{
{11 * 8, 6 * 8},
5,
{{"Ch.37 ", 37},
{"Ch.38", 38},
{"Ch.39", 39}}};
{"Ch.39", 39},
{"Auto", 40}}};
OptionsField options_adv_type{
{17 * 8, 6 * 8},
14,
{{"DISCOVERY ", DISCOVERY},
{"ADV_IND", ADV_IND},
{"ADV_DIRECT", ADV_DIRECT_IND},
{"ADV_NONCONN", ADV_NONCONN_IND},
{"SCAN_REQ", SCAN_REQ},
{"SCAN_RSP", SCAN_RSP},
{"CONNECT_REQ", CONNECT_REQ}}};
{{"DISCOVERY ", PKT_TYPE_DISCOVERY},
{"ADV_IND", PKT_TYPE_ADV_IND},
{"ADV_DIRECT", PKT_TYPE_ADV_DIRECT_IND},
{"ADV_NONCONN", PKT_TYPE_ADV_NONCONN_IND},
{"ADV_SCAN_IND", PKT_TYPE_ADV_SCAN_IND},
{"SCAN_REQ", PKT_TYPE_SCAN_REQ},
{"SCAN_RSP", PKT_TYPE_SCAN_RSP},
{"CONNECT_REQ", PKT_TYPE_CONNECT_REQ}}};
Labels label_packet_index{
{{0 * 8, 10 * 8}, "Packet Index:", Color::light_grey()}};
@@ -231,7 +259,7 @@ class BLETxView : public View {
"-"};
Labels label_packets_sent{
{{0 * 8, 12 * 8}, "Packets Left:", Color::light_grey()}};
{{0 * 8, 12 * 8}, "Repeat Count:", Color::light_grey()}};
Text text_packets_sent{
{13 * 8, 6 * 16, 12 * 8, 16},
@@ -245,18 +273,33 @@ class BLETxView : public View {
"-"};
Labels label_data_packet{
{{0 * 8, 16 * 8}, "Packet Data:", Color::light_grey()}};
{{0 * 8, 8 * 16}, "Packet Data:", Color::light_grey()}};
Console console{
{0, 8 * 16, 240, 240}};
{0, 9 * 16, 240, 240}};
TextViewer dataEditView{
{0, 9 * 16, 240, 240}};
Labels label_data_index{
{{0 * 8, 28 * 8}, "Data Index:", Color::light_grey()}};
Text text_data_index{
{11 * 8, 14 * 16, 8 * 8, 16},
"-"};
Button button_save_packet{
{1 * 8, 16 * 16, 13 * 8, 2 * 16},
"Save Packet"};
Button button_switch{
{8 * 8, 16 * 16, 14 * 8, 2 * 16},
{16 * 8, 16 * 16, 13 * 8, 2 * 16},
"Switch to Rx"};
std::string str_log{""};
bool logging{true};
bool logging_done{false};
std::string packetFileBuffer{};
std::unique_ptr<BLELoggerTx> logger{};
@@ -273,6 +316,12 @@ class BLETxView : public View {
const auto message = *reinterpret_cast<const TXProgressMessage*>(p);
this->on_tx_progress(message.done);
}};
MessageHandlerRegistration message_handler_frame_sync{
Message::ID::DisplayFrameSync,
[this](const Message* const) {
this->on_timer();
}};
};
} /* namespace ui */
+12 -4
View File
@@ -65,9 +65,13 @@ std::string commodity_type(CommodityType value) {
} /* namespace ert */
void ERTLogger::on_packet(const ert::Packet& packet) {
void ERTLogger::on_packet(const ert::Packet& packet, const uint32_t target_frequency) {
const auto formatted = packet.symbols_formatted();
std::string entry = ert::format::type(packet.type()) + " " + formatted.data + "/" + formatted.errors;
// TODO: function doesn't take uint64_t, so when >= 1<<32, weirdness will ensue!
const auto target_frequency_str = to_string_dec_uint(target_frequency, 10);
std::string entry = target_frequency_str + " " + ert::format::type(packet.type()) + " " + formatted.data + "/" + formatted.errors;
log_file.write_entry(packet.received_at(), entry);
}
@@ -99,10 +103,12 @@ void RecentEntriesTable<ERTRecentEntries>::draw(
painter.draw_string(target_rect.location(), style, line);
}
ERTAppView::ERTAppView(NavigationView&) {
ERTAppView::ERTAppView(NavigationView& nav)
: nav_{nav} {
baseband::run_image(portapack::spi_flash::image_tag_ert);
add_children({
&field_frequency,
&field_rf_amp,
&field_lna,
&field_vga,
@@ -110,6 +116,8 @@ ERTAppView::ERTAppView(NavigationView&) {
&recent_entries_view,
});
field_frequency.set_step(1000000);
receiver_model.enable();
logger = std::make_unique<ERTLogger>();
@@ -134,7 +142,7 @@ void ERTAppView::set_parent_rect(const Rect new_parent_rect) {
void ERTAppView::on_packet(const ert::Packet& packet) {
if (logger) {
logger->on_packet(packet);
logger->on_packet(packet, receiver_model.target_frequency());
}
if (packet.crc_ok()) {
+7 -1
View File
@@ -24,6 +24,7 @@
#include "ui_navigation.hpp"
#include "ui_receiver.hpp"
#include "ui_freq_field.hpp"
#include "ui_rssi.hpp"
#include "ui_channel.hpp"
@@ -95,7 +96,7 @@ class ERTLogger {
return log_file.append(filename);
}
void on_packet(const ert::Packet& packet);
void on_packet(const ert::Packet& packet, const uint32_t target_frequency);
private:
LogFile log_file{};
@@ -126,6 +127,7 @@ class ERTAppView : public View {
ERTRecentEntries recent{};
std::unique_ptr<ERTLogger> logger{};
NavigationView& nav_;
RxRadioState radio_state_{
911600000 /* frequency */,
2500000 /* bandwidth */,
@@ -143,6 +145,10 @@ class ERTAppView : public View {
static constexpr auto header_height = 1 * 16;
RxFrequencyField field_frequency{
{5 * 8, 0 * 16},
nav_};
RFAmpField field_rf_amp{
{13 * 8, 0 * 16}};
+6 -11
View File
@@ -34,9 +34,6 @@ namespace tpms {
namespace format {
static bool use_kpa = true;
static bool use_celsius = true;
std::string type(Reading::Type type) {
return to_string_dec_uint(toUType(type), 2);
}
@@ -46,11 +43,11 @@ std::string id(TransponderID id) {
}
std::string pressure(Pressure pressure) {
return to_string_dec_int(use_kpa ? pressure.kilopascal() : pressure.psi(), 3);
return to_string_dec_int(units_psi ? pressure.psi() : pressure.kilopascal(), 3);
}
std::string temperature(Temperature temperature) {
return to_string_dec_int(use_celsius ? temperature.celsius() : temperature.fahrenheit(), 3);
return to_string_dec_int(units_fahr ? temperature.fahrenheit() : temperature.celsius(), 3);
}
std::string flags(Flags flags) {
@@ -165,18 +162,16 @@ TPMSAppView::TPMSAppView(NavigationView&) {
options_band.set_by_value(receiver_model.target_frequency());
options_pressure.on_change = [this](size_t, int32_t i) {
tpms::format::use_kpa = !i;
tpms::format::units_psi = (bool)i;
update_view();
};
options_pressure.set_selected_index(0, true);
options_pressure.set_selected_index(tpms::format::units_psi, true);
options_temperature.on_change = [this](size_t, int32_t i) {
tpms::format::use_celsius = !i;
tpms::format::units_fahr = (bool)i;
update_view();
};
options_temperature.set_selected_index(0, true);
options_temperature.set_selected_index(tpms::format::units_fahr, true);
logger = std::make_unique<TPMSLogger>();
if (logger) {
+21 -3
View File
@@ -37,6 +37,17 @@
#include "tpms_packet.hpp"
namespace tpms {
namespace format {
static bool units_psi{false};
static bool units_fahr{false};
} /* namespace format */
} /* namespace tpms */
namespace std {
} /* namespace std */
@@ -103,11 +114,17 @@ class TPMSAppView : public View {
private:
RxRadioState radio_state_{
315000000 /* frequency*/,
314950000 /* frequency*/,
1750000 /* bandwidth */,
2457600 /* sampling rate */};
app_settings::SettingsManager settings_{
"rx_tpms", app_settings::Mode::RX};
"rx_tpms",
app_settings::Mode::RX,
{
{"units_psi"sv, &tpms::format::units_psi},
{"units_fahr"sv, &tpms::format::units_fahr},
}};
MessageHandlerRegistration message_handler_packet{
Message::ID::TPMSPacket,
@@ -129,11 +146,12 @@ class TPMSAppView : public View {
{21 * 8, 5, 6 * 8, 4},
};
// "315 MHz" TPMS sensors transmit at either 314.9 or 315 MHz but we should pick up either
OptionsField options_band{
{0 * 8, 0 * 16},
3,
{
{"315", 315000000},
{"315", 314950000},
{"434", 433920000},
}};
@@ -37,7 +37,7 @@ void AboutView::update() {
break;
case 2:
console.writeln("NotherNgineer,zxkmm,u-foka");
console.writeln("Netro");
console.writeln("Netro,HTotoo");
console.writeln("");
break;
+353 -318
View File
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* Copyright (C) 2023 Kyle Reed
*
* This file is part of PortaPack.
*
@@ -20,21 +21,23 @@
* Boston, MA 02110-1301, USA.
*/
#include <strings.h>
#include <algorithm>
#include "ui_adsb_rx.hpp"
#include "ui_alphanum.hpp"
#include "rtc_time.hpp"
#include "string_format.hpp"
#include "baseband_api.hpp"
#include "portapack_persistent_memory.hpp"
#include "rtc_time.hpp"
#include "string_format.hpp"
using namespace portapack;
namespace ui {
bool ac_details_view_active{false};
static std::string get_map_tag(const AircraftRecentEntry& entry) {
return trimr(entry.callsign.empty() ? entry.icao_str : entry.callsign);
}
template <>
void RecentEntriesTable<AircraftRecentEntries>::draw(
@@ -43,23 +46,25 @@ void RecentEntriesTable<AircraftRecentEntries>::draw(
Painter& painter,
const Style& style) {
Color target_color;
auto entry_age = entry.age;
std::string entry_string;
// Color decay for flights not being updated anymore
if (entry_age < ADSB_CURRENT) {
entry_string = "";
target_color = Color::green();
} else if (entry_age < ADSB_RECENT) {
entry_string = STR_COLOR_LIGHT_GREY;
target_color = Color::light_grey();
} else {
entry_string = STR_COLOR_DARK_GREY;
target_color = Color::grey();
}
switch (entry.state) {
case ADSBAgeState::Invalid:
case ADSBAgeState::Current:
entry_string = "";
target_color = Color::green();
break;
case ADSBAgeState::Recent:
entry_string = STR_COLOR_LIGHT_GREY;
target_color = Color::light_grey();
break;
default:
entry_string = STR_COLOR_DARK_GREY;
target_color = Color::grey();
};
entry_string +=
(entry.callsign[0] != ' ' ? entry.callsign + " " : entry.icaoStr + " ") +
(entry.callsign.empty() ? entry.icao_str + " " : entry.callsign + " ") +
to_string_dec_uint((unsigned int)(entry.pos.altitude / 100), 4) +
to_string_dec_uint((unsigned int)entry.velo.speed, 4) +
to_string_dec_uint((unsigned int)(entry.amp >> 9), 4) + " " +
@@ -72,47 +77,59 @@ void RecentEntriesTable<AircraftRecentEntries>::draw(
entry_string);
if (entry.pos.valid)
painter.draw_bitmap(target_rect.location() + Point(8 * 8, 0), bitmap_target, target_color, style.background);
painter.draw_bitmap(target_rect.location() + Point(8 * 8, 0),
bitmap_target, target_color, style.background);
}
void ADSBLogger::log_str(std::string& logline) {
log_file.write_entry(logline);
/* ADSBLogger ********************************************/
void ADSBLogger::log(const ADSBLogEntry& log_entry) {
std::string log_line;
log_line.reserve(100);
log_line = log_entry.raw_data;
log_line += "ICAO:" + log_entry.icao;
if (!log_entry.callsign.empty())
log_line += " " + log_entry.callsign;
if (log_entry.pos.valid)
log_line += " Alt:" + to_string_dec_int(log_entry.pos.altitude) +
" Lat:" + to_string_decimal(log_entry.pos.latitude, 7) +
" Lon:" + to_string_decimal(log_entry.pos.longitude, 7);
if (log_entry.vel.valid)
log_line += " Type:" + to_string_dec_uint(log_entry.vel_type) +
" Hdg:" + to_string_dec_uint(log_entry.vel.heading) +
" Spd: " + to_string_dec_int(log_entry.vel.speed);
log_file.write_entry(log_line);
}
// Aircraft Details
void ADSBRxAircraftDetailsView::focus() {
button_close.focus();
}
ADSBRxAircraftDetailsView::~ADSBRxAircraftDetailsView() {
on_close_();
}
/* ADSBRxAircraftDetailsView *****************************/
ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
NavigationView& nav,
const AircraftRecentEntry& entry,
const std::function<void(void)> on_close)
: entry_copy(entry),
on_close_(on_close) {
add_children({&labels,
&text_icao_address,
&text_registration,
&text_manufacturer,
&text_model,
&text_type,
&text_number_of_engines,
&text_engine_type,
&text_owner,
&text_operator,
&button_close});
const AircraftRecentEntry& entry) {
add_children(
{&labels,
&text_icao_address,
&text_registration,
&text_manufacturer,
&text_model,
&text_type,
&text_number_of_engines,
&text_engine_type,
&text_owner,
&text_operator,
&button_close});
std::unique_ptr<ADSBLogger> logger{};
icao_code = to_string_hex(entry_copy.ICAO_address, 6);
text_icao_address.set(to_string_hex(entry_copy.ICAO_address, 6));
text_icao_address.set(entry.icao_str);
// Try getting the aircraft information from icao24.db
return_code = db.retrieve_aircraft_record(&aircraft_record, icao_code);
std::database db{};
std::database::AircraftDBRecord aircraft_record;
auto return_code = db.retrieve_aircraft_record(&aircraft_record, entry.icao_str);
switch (return_code) {
case DATABASE_RECORD_FOUND:
text_registration.set(aircraft_record.aircraft_registration);
@@ -120,6 +137,7 @@ ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
text_model.set(aircraft_record.aircraft_model);
text_owner.set(aircraft_record.aircraft_owner);
text_operator.set(aircraft_record.aircraft_operator);
// Check for ICAO type, e.g. L2J
if (strlen(aircraft_record.icao_type) == 3) {
switch (aircraft_record.icao_type[0]) {
@@ -142,7 +160,8 @@ ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
text_type.set("Tilt-wing aircraft");
break;
}
text_number_of_engines.set(std::string(1, aircraft_record.icao_type[1]));
text_number_of_engines.set(std::string{1, aircraft_record.icao_type[1]});
switch (aircraft_record.icao_type[2]) {
case 'P':
text_engine_type.set("Piston engine");
@@ -157,8 +176,8 @@ ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
text_engine_type.set("Electric engine");
break;
}
}
// Check for ICAO type designator
else if (strlen(aircraft_record.icao_type) == 4) {
if (strcmp(aircraft_record.icao_type, "SHIP") == 0)
@@ -179,77 +198,49 @@ ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
text_type.set("Powered parachute/paraplane");
}
break;
case DATABASE_NOT_FOUND:
text_manufacturer.set("No icao24.db file");
break;
}
button_close.on_select = [&nav](Button&) {
ac_details_view_active = false;
nav.pop();
};
};
// End of Aicraft details
void ADSBRxDetailsView::focus() {
button_see_map.focus();
}
void ADSBRxDetailsView::update(const AircraftRecentEntry& entry) {
entry_copy = entry;
uint32_t age = entry_copy.age;
if (age < 60)
text_last_seen.set(to_string_dec_uint(age) + " seconds ago");
else
text_last_seen.set(to_string_dec_uint(age / 60) + " minutes ago");
text_infos.set(entry_copy.info_string);
if (entry_copy.velo.heading < 360 && entry_copy.velo.speed >= 0) { // I don't like this but...
text_info2.set("Hdg:" + to_string_dec_uint(entry_copy.velo.heading) + " Spd:" + to_string_dec_int(entry_copy.velo.speed));
} else {
text_info2.set("");
}
text_frame_pos_even.set(to_string_hex_array(entry_copy.frame_pos_even.get_raw_data(), 14));
text_frame_pos_odd.set(to_string_hex_array(entry_copy.frame_pos_odd.get_raw_data(), 14));
if (send_updates) {
geomap_view->update_tag(trimr(entry.callsign[0] != ' ' ? entry.callsign : to_string_hex(entry.ICAO_address, 6)));
geomap_view->update_position(entry_copy.pos.latitude, entry_copy.pos.longitude, entry_copy.velo.heading, entry_copy.pos.altitude);
}
void ADSBRxAircraftDetailsView::focus() {
button_close.focus();
}
ADSBRxDetailsView::~ADSBRxDetailsView() {
ac_details_view_active = false;
on_close_();
}
/* ADSBRxDetailsView *************************************/
ADSBRxDetailsView::ADSBRxDetailsView(
NavigationView& nav,
const AircraftRecentEntry& entry,
const std::function<void(void)> on_close)
: entry_copy(entry),
on_close_(on_close) {
add_children({&labels,
&text_icao_address,
&text_callsign,
&text_last_seen,
&text_airline,
&text_country,
&text_infos,
&text_info2,
&text_frame_pos_even,
&text_frame_pos_odd,
&button_aircraft_details,
&button_see_map});
const AircraftRecentEntry& entry)
: entry_(entry) {
add_children(
{&labels,
&text_icao_address,
&text_callsign,
&text_last_seen,
&text_airline,
&text_country,
&text_infos,
&text_info2,
&text_frame_pos_even,
&text_frame_pos_odd,
&button_aircraft_details,
&button_see_map});
std::unique_ptr<ADSBLogger> logger{};
update(entry_copy);
// The following won't change for a given airborne aircraft.
// Try getting the airline's name from airlines.db.
// NB: Only works once callsign has been read and won't be updated.
std::database db;
std::database::AirlinesDBRecord airline_record;
std::string airline_code = entry_.callsign.substr(0, 3);
auto return_code = db.retrieve_airline_record(&airline_record, airline_code);
// The following won't (shouldn't !) change for a given airborne aircraft
// Try getting the airline's name from airlines.db
airline_code = entry_copy.callsign.substr(0, 3);
return_code = db.retrieve_airline_record(&airline_record, airline_code);
switch (return_code) {
case DATABASE_RECORD_FOUND:
text_airline.set(airline_record.airline);
@@ -260,34 +251,125 @@ ADSBRxDetailsView::ADSBRxDetailsView(
break;
}
text_callsign.set(entry_copy.callsign);
text_icao_address.set(to_string_hex(entry_copy.ICAO_address, 6));
text_icao_address.set(entry_.icao_str);
button_aircraft_details.on_select = [this, &nav](Button&) {
ac_details_view_active = true;
aircraft_details_view = nav.push<ADSBRxAircraftDetailsView>(entry_copy, [this]() { send_updates = false; });
send_updates = false;
aircraft_details_view_ = nav.push<ADSBRxAircraftDetailsView>(entry_);
nav.set_on_pop([this]() {
aircraft_details_view_ = nullptr;
refresh_ui();
});
};
button_see_map.on_select = [this, &nav](Button&) {
if (!send_updates) { // Prevent recursively launching the map
geomap_view = nav.push<GeoMapView>(
trimr(entry_copy.callsign[0] != ' ' ? entry_copy.callsign : entry_copy.icaoStr),
entry_copy.pos.altitude,
GeoPos::alt_unit::FEET,
entry_copy.pos.latitude,
entry_copy.pos.longitude,
entry_copy.velo.heading,
[this]() {
send_updates = false;
});
send_updates = true;
}
geomap_view_ = nav.push<GeoMapView>(
get_map_tag(entry_),
entry_.pos.altitude,
GeoPos::alt_unit::FEET,
entry_.pos.latitude,
entry_.pos.longitude,
entry_.velo.heading);
nav.set_on_pop([this]() {
geomap_view_ = nullptr;
refresh_ui();
});
};
refresh_ui();
};
void ADSBRxView::focus() {
field_vga.focus();
void ADSBRxDetailsView::focus() {
button_see_map.focus();
}
void ADSBRxDetailsView::update(const AircraftRecentEntry& entry) {
entry_ = entry;
if (aircraft_details_view_) {
// AC Details view is showing, nothing to update.
} else if (geomap_view_) {
// Map is showing, update the current item.
geomap_view_->update_tag(get_map_tag(entry_));
geomap_view_->update_position(entry.pos.latitude, entry.pos.longitude, entry.velo.heading, entry.pos.altitude);
} else {
// Details is showing, update details.
refresh_ui();
}
}
void ADSBRxDetailsView::clear_map_markers() {
if (geomap_view_)
geomap_view_->clear_markers();
}
bool ADSBRxDetailsView::add_map_marker(const AircraftRecentEntry& entry) {
// Map not shown, can't add markers.
if (!geomap_view_)
return false;
GeoMarker marker{};
marker.lon = entry.pos.longitude;
marker.lat = entry.pos.latitude;
marker.angle = entry.velo.heading;
marker.tag = get_map_tag(entry);
auto markerStored = geomap_view_->store_marker(marker);
return markerStored == MARKER_STORED;
}
void ADSBRxDetailsView::refresh_ui() {
auto age = entry_.age;
if (age < 60)
text_last_seen.set(to_string_dec_uint(age) + " seconds ago");
else
text_last_seen.set(to_string_dec_uint(age / 60) + " minutes ago");
text_callsign.set(entry_.callsign);
text_infos.set(entry_.info_string);
if (entry_.velo.heading < 360 && entry_.velo.speed >= 0)
text_info2.set("Hdg:" + to_string_dec_uint(entry_.velo.heading) +
" Spd:" + to_string_dec_int(entry_.velo.speed));
else
text_info2.set("");
text_frame_pos_even.set(to_string_hex_array(entry_.frame_pos_even.get_raw_data(), 14));
text_frame_pos_odd.set(to_string_hex_array(entry_.frame_pos_odd.get_raw_data(), 14));
}
/* ADSBRxView ********************************************/
ADSBRxView::ADSBRxView(NavigationView& nav) {
baseband::run_image(portapack::spi_flash::image_tag_adsb_rx);
add_children(
{&labels,
&field_lna,
&field_vga,
&field_rf_amp,
&rssi,
&recent_entries_view,
&status_frame,
&status_good_frame});
recent_entries_view.set_parent_rect({0, 16, 240, 272});
recent_entries_view.on_select = [this, &nav](const AircraftRecentEntry& entry) {
detail_key = entry.key();
details_view = nav.push<ADSBRxDetailsView>(entry);
nav.set_on_pop([this]() {
detail_key = AircraftRecentEntry::invalid_key;
details_view = nullptr;
});
};
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
on_tick_second();
};
logger = std::make_unique<ADSBLogger>();
logger->append(LOG_ROOT_DIR "/ADSB.TXT");
receiver_model.enable();
baseband::set_adsb();
}
ADSBRxView::~ADSBRxView() {
@@ -296,232 +378,185 @@ ADSBRxView::~ADSBRxView() {
baseband::shutdown();
}
AircraftRecentEntry ADSBRxView::find_or_create_entry(uint32_t ICAO_address) {
auto it = find(recent, ICAO_address);
// If not found
if (it == std::end(recent)) {
recent.emplace_front(ICAO_address); // Add it
it = find(recent, ICAO_address); // Find it again
}
return *it;
}
void ADSBRxView::replace_entry(AircraftRecentEntry& entry) {
uint32_t ICAO_address = entry.ICAO_address;
std::replace_if(
recent.begin(), recent.end(),
[ICAO_address](const AircraftRecentEntry& compEntry) { return ICAO_address == compEntry.ICAO_address; },
entry);
}
void ADSBRxView::remove_old_entries() {
auto it = recent.rbegin();
auto end = recent.rend();
while (it != end) {
if (it->age_state >= 4) {
std::advance(it, 1);
recent.erase(it.base());
} else {
break; // stop looking because the list is sorted
}
}
}
void ADSBRxView::sort_entries_by_state() {
// Sorting List pn age_state using lambda function as comparator
recent.sort([](const AircraftRecentEntry& left, const AircraftRecentEntry& right) { return (left.age_state < right.age_state); });
void ADSBRxView::focus() {
field_vga.focus();
}
void ADSBRxView::on_frame(const ADSBFrameMessage* message) {
logger = std::make_unique<ADSBLogger>();
rtc::RTC datetime;
std::string callsign;
std::string str_info;
std::string logentry;
auto frame = message->frame;
uint32_t ICAO_address = frame.get_ICAO_address();
status_frame.toggle();
if (frame.check_CRC() && ICAO_address) {
rtcGetTime(&RTCD1, &datetime);
auto entry = find_or_create_entry(ICAO_address);
frame.set_rx_timestamp(datetime.minute() * 60 + datetime.second());
entry.reset_age();
if (entry.hits == 0) {
entry.amp = message->amp; // Store amplitude on first hit
} else {
entry.amp = ((entry.amp * 15) + message->amp) >> 4; // Update smoothed amplitude on updates
// Bad frame, skip it.
if (!frame.check_CRC() || ICAO_address == 0)
return;
ADSBLogEntry log_entry;
status_good_frame.toggle();
rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime);
frame.set_rx_timestamp(datetime.minute() * 60 + datetime.second());
// NB: Reference to update entry in-place.
auto& entry = find_or_create_entry(ICAO_address);
entry.inc_hit();
entry.reset_age();
// Store smoothed amplitude on updates.
entry.amp = entry.hits == 0
? message->amp
: ((entry.amp * 15) + message->amp) >> 4;
log_entry.raw_data = to_string_hex_array(frame.get_raw_data(), 14);
log_entry.icao = entry.icao_str;
if (frame.get_DF() == DF_ADSB) {
uint8_t msg_type = frame.get_msg_type();
uint8_t msg_sub = frame.get_msg_sub();
uint8_t* raw_data = frame.get_raw_data();
// 4: // surveillance, altitude reply
if ((msg_type >= AIRCRAFT_ID_L) && (msg_type <= AIRCRAFT_ID_H)) {
entry.set_callsign(decode_frame_id(frame));
log_entry.callsign = entry.callsign;
}
entry.inc_hit();
if (logger) {
logentry += to_string_hex_array(frame.get_raw_data(), 14) + " ";
logentry += "ICAO:" + entry.icaoStr + " ";
}
// 9:
// 18: // Extended squitter/non-transponder
// 21: // Comm-B, identity reply
// 20: // Comm-B, altitude reply
else if (((msg_type >= AIRBORNE_POS_BARO_L) && (msg_type <= AIRBORNE_POS_BARO_H)) ||
((msg_type >= AIRBORNE_POS_GPS_L) && (msg_type <= AIRBORNE_POS_GPS_H))) {
entry.set_frame_pos(frame, raw_data[6] & 4);
log_entry.pos = entry.pos;
if (frame.get_DF() == DF_ADSB) {
uint8_t msg_type = frame.get_msg_type();
uint8_t msg_sub = frame.get_msg_sub();
uint8_t* raw_data = frame.get_raw_data();
if (entry.pos.valid) {
std::string str_info =
"Alt:" + to_string_dec_int(entry.pos.altitude) +
" Lat:" + to_string_decimal(entry.pos.latitude, 2) +
" Lon:" + to_string_decimal(entry.pos.longitude, 2);
// 4: // surveillance, altitude reply
if ((msg_type >= AIRCRAFT_ID_L) && (msg_type <= AIRCRAFT_ID_H)) {
callsign = decode_frame_id(frame);
entry.set_callsign(callsign);
if (logger) {
logentry += callsign + " ";
}
entry.set_info_string(std::move(str_info));
}
// 9:
// 18: { // Extended squitter/non-transponder
// 21: // Comm-B, identity reply
// 20: // Comm-B, altitude reply
else if (((msg_type >= AIRBORNE_POS_BARO_L) && (msg_type <= AIRBORNE_POS_BARO_H)) ||
((msg_type >= AIRBORNE_POS_GPS_L) && (msg_type <= AIRBORNE_POS_GPS_H))) {
entry.set_frame_pos(frame, raw_data[6] & 4);
if (entry.pos.valid) {
str_info = "Alt:" + to_string_dec_int(entry.pos.altitude) +
" Lat:" + to_string_decimal(entry.pos.latitude, 2) +
" Lon:" + to_string_decimal(entry.pos.longitude, 2);
entry.set_info_string(str_info);
if (logger) {
// printing the coordinates in the log file with more
// resolution, as we are not constrained by screen
// real estate there:
std::string log_info = "Alt:" + to_string_dec_int(entry.pos.altitude) +
" Lat:" + to_string_decimal(entry.pos.latitude, 7) +
" Lon:" + to_string_decimal(entry.pos.longitude, 7);
logentry += log_info + " ";
}
}
} else if (msg_type == AIRBORNE_VEL && msg_sub >= VEL_GND_SUBSONIC && msg_sub <= VEL_AIR_SUPERSONIC) {
entry.set_frame_velo(frame);
if (logger) {
logger->append(LOG_ROOT_DIR "/ADSB.TXT");
logentry += "Type:" + to_string_dec_uint(msg_sub) +
" Hdg:" + to_string_dec_uint(entry.velo.heading) +
" Spd: " + to_string_dec_int(entry.velo.speed);
}
}
replace_entry(entry);
} // frame.get_DF() == DF_ADSB
if (logger) {
logger->append(LOG_ROOT_DIR "/ADSB.TXT");
// will log each frame in format:
// 20171103100227 8DADBEEFDEADBEEFDEADBEEFDEADBEEF ICAO:nnnnnn callsign Alt:nnnnnn Latnnn.nn Lonnnn.nn
logger->log_str(logentry);
} else if (msg_type == AIRBORNE_VEL && msg_sub >= VEL_GND_SUBSONIC && msg_sub <= VEL_AIR_SUPERSONIC) {
entry.set_frame_velo(frame);
log_entry.vel = entry.velo;
log_entry.vel_type = msg_sub;
}
}
logger->log(log_entry);
}
void ADSBRxView::on_tick_second() {
if (recent.size() <= 16) { // Not many entries update everything (16 is one screen full)
updateDetailsAndMap(1);
updateRecentEntries();
} else if (updateState == 0) { // Even second
updateState = 1;
updateDetailsAndMap(2);
} else { // Odd second only performed when there are many entries
updateState = 0;
updateRecentEntries();
status_frame.reset();
status_good_frame.reset();
++tick_count;
++ticks_since_marker_refresh;
// Small list, update all at once.
if (recent.size() <= max_update_entries) {
update_recent_entries(/*age_delta*/ 1);
refresh_ui();
return;
}
// Too many items, split update work into two phases:
// Entry maintenance and UI update.
if ((tick_count & 1) == 0)
update_recent_entries(/*age_delta*/ 2);
else
refresh_ui();
}
void ADSBRxView::updateDetailsAndMap(int ageStep) {
ui::GeoMarker marker;
bool storeNewMarkers = false;
void ADSBRxView::refresh_ui() {
// There's only one ticks handler, but 3 UIs that need to be updated.
// This code will dispatch updates to the currently active view.
// NB: Temporarily pausing updates in rtc_timer_tick context when viewing AC Details screen (kludge for some Guru faults)
// TODO: More targeted blocking of updates in rtc_timer_tick when ADSB processes are running
if (ac_details_view_active)
return;
if (details_view) {
// The details view is showing, forward updates to that UI.
bool current_updated = false;
bool map_needs_update = false;
// Sort and truncate the entries, grouped, newest group first
sort_entries_by_state();
truncate_entries(recent);
remove_old_entries();
if (details_view->map_active()) {
// Is it time to clear and refresh the map's markers?
if (ticks_since_marker_refresh >= MARKER_UPDATE_SECONDS) {
map_needs_update = true;
ticks_since_marker_refresh = 0;
details_view->clear_map_markers();
}
} else {
// Refresh map immediately once active.
ticks_since_marker_refresh = MARKER_UPDATE_SECONDS;
}
// Calculate if it is time to update markers
if (send_updates && details_view && details_view->geomap_view) {
ticksSinceMarkerRefresh += ageStep;
if (ticksSinceMarkerRefresh >= MARKER_UPDATE_SECONDS) { // Update other aircraft every few seconds
storeNewMarkers = true;
ticksSinceMarkerRefresh = 0;
// Process the entries list.
for (const auto& entry : recent) {
// Found the entry being shown in details view. Update it.
if (entry.key() == detail_key) {
details_view->update(entry);
current_updated = true;
}
// NB: current entry also gets a marker so it shows up if map is panned.
if (map_needs_update && entry.pos.valid && entry.state <= ADSBAgeState::Recent) {
map_needs_update = details_view->add_map_marker(entry);
}
// Any work left to do?
if (current_updated && !map_needs_update)
break;
}
} else {
ticksSinceMarkerRefresh = MARKER_UPDATE_SECONDS; // Send the markers as soon as the geoview exists
}
// Increment age, and pass updates to the details and map
const bool otherMarkersCanBeSent = send_updates && storeNewMarkers && details_view && details_view->geomap_view; // Save retesting all of this
MapMarkerStored markerStored = MARKER_NOT_STORED;
if (otherMarkersCanBeSent) {
details_view->geomap_view->clear_markers();
}
// Loop through all entries
for (auto& entry : recent) {
entry.inc_age(ageStep);
// Only if there is a details view
if (send_updates && details_view) {
if (entry.key() == detailed_entry_key) // Check if the ICAO address match
{
details_view->update(entry);
}
// Store if the view is present and the list isn't full
// Note -- Storing the selected entry too, in case map panning occurs
if (otherMarkersCanBeSent && (markerStored != MARKER_LIST_FULL) && entry.pos.valid && (entry.age_state <= 2)) {
marker.lon = entry.pos.longitude;
marker.lat = entry.pos.latitude;
marker.angle = entry.velo.heading;
marker.tag = trimr(entry.callsign[0] != ' ' ? entry.callsign : entry.icaoStr);
markerStored = details_view->geomap_view->store_marker(marker);
}
}
} // Loop through all entries, if only to update the age
}
void ADSBRxView::updateRecentEntries() {
// Redraw the list of aircraft
if (!send_updates) {
// Main page is the top view. Redraw the entries view.
recent_entries_view.set_dirty();
}
}
ADSBRxView::ADSBRxView(NavigationView& nav) {
baseband::run_image(portapack::spi_flash::image_tag_adsb_rx);
add_children({&labels,
&field_lna,
&field_vga,
&field_rf_amp,
&rssi,
&recent_entries_view});
void ADSBRxView::update_recent_entries(int age_delta) {
for (auto& entry : recent)
entry.inc_age(age_delta);
recent_entries_view.set_parent_rect({0, 16, 240, 272});
recent_entries_view.on_select = [this, &nav](const AircraftRecentEntry& entry) {
detailed_entry_key = entry.key();
details_view = nav.push<ADSBRxDetailsView>(
entry,
[this]() {
send_updates = false;
});
send_updates = true;
};
// Sort and truncate the entries, grouped by state, newest first.
sort_entries_by_state();
truncate_entries(recent);
remove_expired_entries();
}
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
on_tick_second();
};
AircraftRecentEntry& ADSBRxView::find_or_create_entry(uint32_t ICAO_address) {
// Find ...
auto it = find(recent, ICAO_address);
if (it != recent.end())
return *it;
baseband::set_adsb();
// ... or Create.
return recent.emplace_front(ICAO_address);
}
receiver_model.enable();
void ADSBRxView::sort_entries_by_state() {
recent.sort([](const auto& left, const auto& right) {
return (left.state < right.state);
});
}
void ADSBRxView::remove_expired_entries() {
// NB: Assumes entried are sorted with oldest last.
auto it = recent.rbegin();
auto end = recent.rend();
// Find the first !expired entry from the back.
while (it != end) {
if (it->state != ADSBAgeState::Expired)
break;
std::advance(it, 1);
}
// Remove the range of expired items.
recent.erase(it.base(), recent.end());
}
} /* namespace ui */
+131 -103
View File
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* Copyright (C) 2023 Kyle Reed
*
* This file is part of PortaPack.
*
@@ -21,29 +22,23 @@
*/
#include "ui.hpp"
#include "ui_receiver.hpp"
#include "ui_geomap.hpp"
#include "string_format.hpp"
#include "file.hpp"
#include "database.hpp"
#include "recent_entries.hpp"
#include "log_file.hpp"
#include "adsb.hpp"
#include "message.hpp"
#include "app_settings.hpp"
#include "radio_state.hpp"
#include "crc.hpp"
#include "database.hpp"
#include "file.hpp"
#include "log_file.hpp"
#include "message.hpp"
#include "radio_state.hpp"
#include "recent_entries.hpp"
#include "string_format.hpp"
using namespace adsb;
namespace ui {
#define ADSB_CURRENT 10 // Seconds
#define ADSB_RECENT 30 // Seconds
#define ADSB_REMOVE 300 // Used for removing old entries
#define AIRCRAFT_ID_L 1 // aircraft ID message type (lowest type id)
#define AIRCRAFT_ID_H 4 // aircraft ID message type (highest type id)
@@ -66,8 +61,26 @@ namespace ui {
#define VEL_AIR_SUBSONIC 3
#define VEL_AIR_SUPERSONIC 4
#define O_E_FRAME_TIMEOUT 20 // timeout between odd and even frames
#define O_E_FRAME_TIMEOUT 20 // timeout between odd and even frames
#define MARKER_UPDATE_SECONDS 5 // "other" map marker redraw interval
/* Thresholds (in seconds) that define the transition between ages. */
struct ADSBAgeLimit {
static constexpr int Current = 10;
static constexpr int Recent = 30;
static constexpr int Expired = 300;
};
/* Age states used for sorting and drawing recent entries. */
enum class ADSBAgeState : uint8_t {
Invalid,
Current,
Recent,
Old,
Expired,
};
/* Data extracted from ADSB frames. */
struct AircraftRecentEntry {
using Key = uint32_t;
@@ -76,30 +89,30 @@ struct AircraftRecentEntry {
uint32_t ICAO_address{};
uint16_t hits{0};
uint16_t age_state{1};
uint32_t age{0};
ADSBAgeState state{ADSBAgeState::Invalid};
uint32_t age{0}; // In seconds
uint32_t amp{0};
adsb_pos pos{false, 0, 0, 0};
adsb_vel velo{false, 0, 999, 0};
ADSBFrame frame_pos_even{};
ADSBFrame frame_pos_odd{};
std::string icaoStr{" "};
std::string callsign{" "};
std::string info_string{""};
std::string icao_str{};
std::string callsign{};
std::string info_string{};
AircraftRecentEntry(
const uint32_t ICAO_address)
AircraftRecentEntry(const uint32_t ICAO_address)
: ICAO_address{ICAO_address} {
this->icaoStr = to_string_hex(ICAO_address, 6);
this->icao_str = to_string_hex(ICAO_address, 6);
}
/* RecentEntries helpers expect a "key" on every item. */
Key key() const {
return ICAO_address;
}
void set_callsign(std::string& new_callsign) {
callsign = new_callsign;
void set_callsign(std::string new_callsign) {
callsign = std::move(new_callsign);
}
void inc_hit() {
@@ -122,8 +135,8 @@ struct AircraftRecentEntry {
velo = decode_frame_velo(frame);
}
void set_info_string(std::string& new_info_string) {
info_string = new_info_string;
void set_info_string(std::string new_info_string) {
info_string = std::move(new_info_string);
}
void reset_age() {
@@ -132,59 +145,59 @@ struct AircraftRecentEntry {
void inc_age(int delta) {
age += delta;
if (age < ADSB_CURRENT) {
age_state = pos.valid ? 0 : 1;
} else if (age < ADSB_RECENT) {
age_state = 2;
} else if (age < ADSB_REMOVE) {
age_state = 3;
} else {
age_state = 4;
}
if (age < ADSBAgeLimit::Current)
state = pos.valid ? ADSBAgeState::Invalid
: ADSBAgeState::Current;
else if (age < ADSBAgeLimit::Recent)
state = ADSBAgeState::Recent;
else if (age < ADSBAgeLimit::Expired)
state = ADSBAgeState::Old;
else
state = ADSBAgeState::Expired;
}
};
// NB: uses std::list underneath so assuming refs are NOT invalidated.
using AircraftRecentEntries = RecentEntries<AircraftRecentEntry>;
/* Holds data for logging. */
struct ADSBLogEntry {
std::string raw_data{};
std::string icao{};
std::string callsign{};
adsb_pos pos{};
adsb_vel vel{};
uint8_t vel_type{};
};
// TODO: Make logging optional.
/* Logs entries to a log file. */
class ADSBLogger {
public:
Optional<File::Error> append(const std::filesystem::path& filename) {
return log_file.append(filename);
}
void log_str(std::string& logline);
void log(const ADSBLogEntry& log_entry);
private:
LogFile log_file{};
};
/* Shows detailed information about an aircraft. */
class ADSBRxAircraftDetailsView : public View {
public:
ADSBRxAircraftDetailsView(NavigationView&, const AircraftRecentEntry& entry, const std::function<void(void)> on_close);
~ADSBRxAircraftDetailsView();
ADSBRxAircraftDetailsView(const ADSBRxAircraftDetailsView&) = delete;
ADSBRxAircraftDetailsView(ADSBRxAircraftDetailsView&&) = delete;
ADSBRxAircraftDetailsView& operator=(const ADSBRxAircraftDetailsView&) = delete;
ADSBRxAircraftDetailsView& operator=(ADSBRxAircraftDetailsView&&) = delete;
ADSBRxAircraftDetailsView(
NavigationView&,
const AircraftRecentEntry& entry);
void focus() override;
void update(const AircraftRecentEntry& entry);
std::string title() const override { return "AC Details"; };
AircraftRecentEntry get_current_entry() { return entry_copy; }
std::database::AircraftDBRecord aircraft_record = {};
std::string title() const override { return "AC Details"; }
private:
AircraftRecentEntry entry_copy{0};
std::function<void(void)> on_close_{};
bool send_updates{false};
std::database db = {};
std::string icao_code = "";
int return_code = 0;
Labels labels{
{{0 * 8, 1 * 16}, "ICAO:", Color::light_grey()},
{{0 * 8, 2 * 16}, "Registration:", Color::light_grey()},
@@ -237,36 +250,34 @@ class ADSBRxAircraftDetailsView : public View {
"Back"};
};
/* Shows detailed information about an aircraft's flight. */
class ADSBRxDetailsView : public View {
public:
ADSBRxDetailsView(NavigationView&, const AircraftRecentEntry& entry, const std::function<void(void)> on_close);
~ADSBRxDetailsView();
ADSBRxDetailsView(NavigationView&, const AircraftRecentEntry& entry);
ADSBRxDetailsView(const ADSBRxDetailsView&) = delete;
ADSBRxDetailsView(ADSBRxDetailsView&&) = delete;
ADSBRxDetailsView& operator=(const ADSBRxDetailsView&) = delete;
ADSBRxDetailsView& operator=(ADSBRxDetailsView&&) = delete;
void focus() override;
void update(const AircraftRecentEntry& entry);
std::string title() const override { return "Details"; };
/* Calls forwarded to map view if shown. */
bool map_active() const { return geomap_view_; }
void clear_map_markers();
/* Adds a marker for the entry to the map. Returns true on success. */
bool add_map_marker(const AircraftRecentEntry& entry);
AircraftRecentEntry get_current_entry() { return entry_copy; }
std::database::AirlinesDBRecord airline_record = {};
GeoMapView* geomap_view{nullptr};
std::string title() const override { return "Details"; }
private:
AircraftRecentEntry entry_copy{0};
std::function<void(void)> on_close_{};
ADSBRxAircraftDetailsView* aircraft_details_view{nullptr};
bool send_updates{false};
std::database db = {};
std::string airline_code = "";
int return_code = 0;
void refresh_ui();
GeoMapView* geomap_view_{nullptr};
ADSBRxAircraftDetailsView* aircraft_details_view_{nullptr};
// NB: Keeping a copy so that it doesn't end up dangling
// if removed from the recent entries list.
AircraftRecentEntry entry_{AircraftRecentEntry::invalid_key};
Labels labels{
{{0 * 8, 1 * 16}, "ICAO:", Color::light_grey()},
@@ -321,6 +332,7 @@ class ADSBRxDetailsView : public View {
"See on map"};
};
/* Main ADSB application view and message dispatch. */
class ADSBRxView : public View {
public:
ADSBRxView(NavigationView& nav);
@@ -332,46 +344,52 @@ class ADSBRxView : public View {
ADSBRxView& operator=(ADSBRxView&&) = delete;
void focus() override;
std::string title() const override { return "ADS-B RX"; };
void replace_entry(AircraftRecentEntry& entry);
void remove_old_entries();
AircraftRecentEntry find_or_create_entry(uint32_t ICAO_address);
void sort_entries_by_state();
private:
RxRadioState radio_state_{
1090000000 /* frequency */,
2500000 /* bandwidth */,
2000000 /* sampling rate */,
1'090'000'000 /* frequency */,
2'500'000 /* bandwidth */,
2'000'000 /* sampling rate */,
ReceiverModel::Mode::SpectrumAnalysis};
app_settings::SettingsManager settings_{
"rx_adsb", app_settings::Mode::RX};
std::unique_ptr<ADSBLogger> logger{};
/* Event Handlers */
void on_frame(const ADSBFrameMessage* message);
void on_tick_second();
int updateState = {0};
void updateRecentEntries();
void updateDetailsAndMap(int ageStep);
#define MARKER_UPDATE_SECONDS (5)
int ticksSinceMarkerRefresh{MARKER_UPDATE_SECONDS - 1};
const RecentEntriesColumns columns{{{"ICAO/Call", 9},
{"Lvl", 3},
{"Spd", 3},
{"Amp", 3},
{"Hit", 3},
{"Age", 4}}};
AircraftRecentEntries recent{};
RecentEntriesView<RecentEntries<AircraftRecentEntry>> recent_entries_view{columns, recent};
void refresh_ui();
SignalToken signal_token_tick_second{};
uint8_t tick_count = 0;
uint16_t ticks_since_marker_refresh{MARKER_UPDATE_SECONDS};
/* Max number of entries that can be updated in a single pass.
* 16 is one screen of recent entries. */
static constexpr uint8_t max_update_entries = 16;
/* Recent Entries */
const RecentEntriesColumns columns{
{{"ICAO/Call", 9},
{"Lvl", 3},
{"Spd", 3},
{"Amp", 3},
{"Hit", 3},
{"Age", 4}}};
AircraftRecentEntries recent{};
RecentEntriesView<AircraftRecentEntries> recent_entries_view{columns, recent};
/* Entry Management */
void update_recent_entries(int age_delta);
AircraftRecentEntry& find_or_create_entry(uint32_t ICAO_address);
void sort_entries_by_state();
void remove_expired_entries();
/* The key of the entry in the details view if shown. */
AircraftRecentEntry::Key detail_key{AircraftRecentEntry::invalid_key};
ADSBRxDetailsView* details_view{nullptr};
uint32_t detailed_entry_key{0};
bool send_updates{false};
Labels labels{
{{0 * 8, 0 * 8}, "LNA: VGA: AMP:", Color::light_grey()}};
@@ -386,7 +404,17 @@ class ADSBRxView : public View {
{18 * 8, 0 * 16}};
RSSI rssi{
{20 * 8, 4, 10 * 8, 8},
{20 * 8, 4, 10 * 7, 8},
};
ActivityDot status_frame{
{screen_width - 3, 5, 2, 2},
Color::white(),
};
ActivityDot status_good_frame{
{screen_width - 3, 9, 2, 2},
Color::green(),
};
MessageHandlerRegistration message_handler_frame{
+114 -52
View File
@@ -124,8 +124,8 @@ void TemperatureWidget::paint(Painter& painter) {
}
TemperatureWidget::temperature_t TemperatureWidget::temperature(const sample_t sensor_value) const {
/*It seems to be a temperature difference of 25C*/
return -40 + (sensor_value * 4.31) + 25; // max2837 datasheet temp 25ºC has sensor value: 15
// Scaling is different for MAX2837 vs MAX2839 so it's now done in the respective chip-specific module
return sensor_value;
}
std::string TemperatureWidget::temperature_str(const temperature_t temperature) const {
@@ -159,11 +159,8 @@ void TemperatureView::focus() {
/* RegistersWidget *******************************************************/
RegistersWidget::RegistersWidget(
RegistersWidgetConfig&& config,
std::function<uint32_t(const size_t register_number)>&& reader)
: Widget{},
config(std::move(config)),
reader(std::move(reader)) {
RegistersWidgetConfig&& config)
: Widget{}, config(std::move(config)), page_number(0) {
}
void RegistersWidget::update() {
@@ -180,7 +177,7 @@ void RegistersWidget::paint(Painter& painter) {
void RegistersWidget::draw_legend(const Coord left, Painter& painter) {
const auto pos = screen_pos();
for (size_t i = 0; i < config.registers_count; i += config.registers_per_row()) {
for (uint32_t i = 0; i < config.registers_count; i += config.registers_per_row()) {
const Point offset{
left, static_cast<int>((i / config.registers_per_row()) * row_height)};
@@ -197,12 +194,12 @@ void RegistersWidget::draw_values(
Painter& painter) {
const auto pos = screen_pos();
for (size_t i = 0; i < config.registers_count; i++) {
for (uint32_t i = 0; i < config.registers_count; i++) {
const Point offset = {
static_cast<int>(left + config.legend_width() + 8 + (i % config.registers_per_row()) * (config.value_width() + 8)),
static_cast<int>((i / config.registers_per_row()) * row_height)};
const auto value = reader(i);
const auto value = reg_read(i);
const auto text = to_string_hex(value, config.value_length());
painter.draw_string(
@@ -212,19 +209,61 @@ void RegistersWidget::draw_values(
}
}
uint32_t RegistersWidget::reg_read(const uint32_t register_number) {
if (register_number < config.registers_count) {
switch (config.chip_type) {
case CT_PMEM:
return portapack::persistent_memory::pmem_data_word((page_number * config.registers_count + register_number) / 4) >> (register_number % 4 * 8);
case CT_RFFC5072:
return radio::debug::first_if::register_read(register_number);
case CT_MAX283X:
return radio::debug::second_if::register_read(register_number);
case CT_SI5351:
return portapack::clock_generator.read_register(register_number);
case CT_AUDIO:
return audio::debug::reg_read(register_number);
}
}
return 0xFFFF;
}
void RegistersWidget::reg_write(const uint32_t register_number, const uint32_t value) {
if (register_number < config.registers_count) {
switch (config.chip_type) {
case CT_PMEM:
break;
case CT_RFFC5072:
radio::debug::first_if::register_write(register_number, value);
break;
case CT_MAX283X:
radio::debug::second_if::register_write(register_number, value);
break;
case CT_SI5351:
portapack::clock_generator.write_register(register_number, value);
break;
case CT_AUDIO:
audio::debug::reg_write(register_number, value);
break;
}
}
}
/* RegistersView *********************************************************/
RegistersView::RegistersView(
NavigationView& nav,
const std::string& title,
RegistersWidgetConfig&& config,
std::function<uint32_t(const size_t register_number)>&& reader)
: registers_widget{std::move(config), std::move(reader)} {
RegistersWidgetConfig&& config)
: registers_widget{std::move(config)} {
add_children({
&text_title,
&registers_widget,
&button_update,
&button_done,
&labels,
&field_write_reg_num,
&field_write_data_val,
&button_write,
});
button_update.on_select = [this](Button&) {
@@ -237,6 +276,20 @@ RegistersView::RegistersView(
text_title.set_parent_rect({(240 - static_cast<int>(title.size()) * 8) / 2, 16,
static_cast<int>(title.size()) * 8, 16});
text_title.set(title);
field_write_reg_num.on_change = [this](SymField&) {
field_write_data_val.set_value(this->registers_widget.reg_read(field_write_reg_num.to_integer()));
field_write_data_val.set_dirty();
};
const auto value = registers_widget.reg_read(0);
field_write_data_val.set_value(value);
button_write.set_style(&Styles::red);
button_write.on_select = [this](Button&) {
this->registers_widget.reg_write(field_write_reg_num.to_integer(), field_write_data_val.to_integer());
this->registers_widget.update();
};
}
void RegistersView::focus() {
@@ -376,18 +429,10 @@ DebugPeripheralsMenuView::DebugPeripheralsMenuView(NavigationView& nav) {
const char* max283x = hackrf_r9 ? "MAX2839" : "MAX2837";
const char* si5351x = hackrf_r9 ? "Si5351A" : "Si5351C";
add_items({
{"RFFC5072", ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav]() { nav.push<RegistersView>(
"RFFC5072", RegistersWidgetConfig{31, 16},
[](const size_t register_number) { return radio::debug::first_if::register_read(register_number); }); }},
{max283x, ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav, max283x]() { nav.push<RegistersView>(
max283x, RegistersWidgetConfig{32, 10},
[](const size_t register_number) { return radio::debug::second_if::register_read(register_number); }); }},
{si5351x, ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav, si5351x]() { nav.push<RegistersView>(
si5351x, RegistersWidgetConfig{96, 8},
[](const size_t register_number) { return portapack::clock_generator.read_register(register_number); }); }},
{audio::debug::codec_name(), ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav]() { nav.push<RegistersView>(
audio::debug::codec_name(), RegistersWidgetConfig{audio::debug::reg_count(), audio::debug::reg_bits()},
[](const size_t register_number) { return audio::debug::reg_read(register_number); }); }},
{"RFFC5072", ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav]() { nav.push<RegistersView>("RFFC5072", RegistersWidgetConfig{CT_RFFC5072, 31, 16}); }},
{max283x, ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav, max283x]() { nav.push<RegistersView>(max283x, RegistersWidgetConfig{CT_MAX283X, 32, 10}); }},
{si5351x, ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav, si5351x]() { nav.push<RegistersView>(si5351x, RegistersWidgetConfig{CT_SI5351, 96, 8}); }},
{audio::debug::codec_name(), ui::Color::dark_cyan(), &bitmap_icon_peripherals_details, [&nav]() { nav.push<RegistersView>(audio::debug::codec_name(), RegistersWidgetConfig{CT_AUDIO, audio::debug::reg_count(), audio::debug::reg_bits()}); }},
});
set_max_rows(2); // allow wider buttons
}
@@ -402,9 +447,10 @@ DebugMenuView::DebugMenuView(NavigationView& nav) {
{"Buttons Test", ui::Color::dark_cyan(), &bitmap_icon_controls, [&nav]() { nav.push<DebugControlsView>(); }},
{"Debug Dump", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { portapack::persistent_memory::debug_dump(); }},
{"M0 Stack Dump", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { stack_dump(); }},
{"Memory", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { nav.push<DebugMemoryView>(); }},
{"P.Memory", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { nav.push<DebugPmemView>(); }},
{"Memory Dump", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { nav.push<DebugMemoryDumpView>(); }},
{"Memory Usage", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { nav.push<DebugMemoryView>(); }},
{"Peripherals", ui::Color::dark_cyan(), &bitmap_icon_peripherals, [&nav]() { nav.push<DebugPeripheralsMenuView>(); }},
{"Pers. Memory", ui::Color::dark_cyan(), &bitmap_icon_memory, [&nav]() { nav.push<DebugPmemView>(); }},
//{ "Radio State", ui::Color::white(), nullptr, [&nav](){ nav.push<NotImplementedView>(); } },
{"SD Card", ui::Color::dark_cyan(), &bitmap_icon_sdcard, [&nav]() { nav.push<SDCardDebugView>(); }},
{"Temperature", ui::Color::dark_cyan(), &bitmap_icon_temperature, [&nav]() { nav.push<TemperatureView>(); }},
@@ -418,30 +464,53 @@ DebugMenuView::DebugMenuView(NavigationView& nav) {
set_max_rows(2); // allow wider buttons
}
/* DebugPmemView *********************************************************/
/* DebugMemoryDumpView *********************************************************/
uint32_t pmem_checksum(volatile const uint32_t data[63]) {
CRC<32> crc{0x04c11db7, 0xffffffff, 0xffffffff};
for (size_t i = 0; i < 63; i++) {
const auto word = data[i];
crc.process_byte((word >> 0) & 0xff);
crc.process_byte((word >> 8) & 0xff);
crc.process_byte((word >> 16) & 0xff);
crc.process_byte((word >> 24) & 0xff);
}
return crc.checksum();
DebugMemoryDumpView::DebugMemoryDumpView(NavigationView& nav) {
add_children({
&button_dump,
&button_read,
&button_write,
&button_done,
&labels,
&field_starting_address,
&field_byte_count,
&field_rw_address,
&field_data_value,
});
button_done.on_select = [&nav](Button&) { nav.pop(); };
button_dump.on_select = [this](Button&) {
if (field_byte_count.to_integer() != 0)
memory_dump((uint32_t*)field_starting_address.to_integer(), ((uint32_t)field_byte_count.to_integer() + 3) / 4, false);
};
button_read.on_select = [this](Button&) {
field_data_value.set_value(*(uint32_t*)field_rw_address.to_integer());
field_data_value.set_dirty();
};
button_write.set_style(&Styles::red);
button_write.on_select = [this](Button&) {
*(uint32_t*)field_rw_address.to_integer() = (uint32_t)field_data_value.to_integer();
};
}
DebugPmemView::DebugPmemView(NavigationView& nav)
: data{*reinterpret_cast<pmem_data*>(memory::map::backup_ram.base())}, registers_widget(RegistersWidgetConfig{page_size, 8}, std::bind(&DebugPmemView::registers_widget_feed, this, std::placeholders::_1)) {
static_assert(sizeof(pmem_data) == memory::map::backup_ram.size());
void DebugMemoryDumpView::focus() {
button_done.focus();
}
/* DebugPmemView *********************************************************/
DebugPmemView::DebugPmemView(NavigationView& nav)
: registers_widget(RegistersWidgetConfig{CT_PMEM, page_size, 8}) {
add_children({&text_page, &registers_widget, &text_checksum, &text_checksum2, &button_ok});
registers_widget.set_parent_rect({0, 32, 240, 192});
text_checksum.set("Size: " + to_string_dec_uint(portapack::persistent_memory::data_size(), 3) + " CRC: " + to_string_hex(data.check_value, 8));
text_checksum2.set("Calculated CRC: " + to_string_hex(pmem_checksum(data.regfile), 8));
text_checksum.set("Size: " + to_string_dec_uint(portapack::persistent_memory::data_size(), 3) + " CRC: " + to_string_hex(portapack::persistent_memory::pmem_stored_checksum(), 8));
text_checksum2.set("Calculated CRC: " + to_string_hex(portapack::persistent_memory::pmem_calculated_checksum(), 8));
button_ok.on_select = [&nav](Button&) {
nav.pop();
@@ -451,7 +520,7 @@ DebugPmemView::DebugPmemView(NavigationView& nav)
}
bool DebugPmemView::on_encoder(const EncoderEvent delta) {
page = std::max(0l, std::min((int32_t)page_max, page + delta));
registers_widget.set_page(std::max(0ul, std::min((uint32_t)page_count - 1, registers_widget.page() + delta)));
update();
@@ -463,17 +532,10 @@ void DebugPmemView::focus() {
}
void DebugPmemView::update() {
text_page.set(to_string_hex(page_size * page, 2) + "+");
text_page.set(to_string_hex(registers_widget.page() * page_size, 2) + "+");
registers_widget.update();
}
uint32_t DebugPmemView::registers_widget_feed(const size_t register_number) {
if (page_size * page + register_number >= memory::map::backup_ram.size()) {
return 0xff;
}
return data.regfile[(page_size * page + register_number) / 4] >> (register_number % 4 * 8);
}
/* DebugScreenTest ****************************************************/
DebugScreenTest::DebugScreenTest(NavigationView& nav)
+95 -26
View File
@@ -48,8 +48,8 @@ class DebugMemoryView : public View {
private:
Text text_title{
{96, 96, 48, 16},
"Memory",
{72, 96, 96, 16},
"Memory Usage",
};
Text text_label_m0_core_free{
@@ -131,9 +131,18 @@ class TemperatureView : public View {
"Done"};
};
typedef enum {
CT_PMEM,
CT_RFFC5072,
CT_MAX283X,
CT_SI5351,
CT_AUDIO,
} chip_type_t;
struct RegistersWidgetConfig {
size_t registers_count;
size_t register_bits;
chip_type_t chip_type;
uint32_t registers_count;
uint32_t register_bits;
constexpr size_t legend_length() const {
return (registers_count >= 0x10) ? 2 : 1;
@@ -174,17 +183,21 @@ struct RegistersWidgetConfig {
class RegistersWidget : public Widget {
public:
RegistersWidget(
RegistersWidgetConfig&& config,
std::function<uint32_t(const size_t register_number)>&& reader);
RegistersWidget(RegistersWidgetConfig&& config);
void update();
void paint(Painter& painter) override;
uint32_t reg_read(const uint32_t register_number);
void reg_write(const uint32_t register_number, const uint32_t value);
void set_page(int32_t value) { page_number = value; }
uint32_t page(void) { return page_number; }
private:
const RegistersWidgetConfig config;
const std::function<uint32_t(const size_t register_number)> reader;
uint32_t page_number;
static constexpr size_t row_height = 16;
@@ -194,11 +207,7 @@ class RegistersWidget : public Widget {
class RegistersView : public View {
public:
RegistersView(
NavigationView& nav,
const std::string& title,
RegistersWidgetConfig&& config,
std::function<uint32_t(const size_t register_number)>&& reader);
RegistersView(NavigationView& nav, const std::string& title, RegistersWidgetConfig&& config);
void focus();
@@ -208,12 +217,30 @@ class RegistersView : public View {
RegistersWidget registers_widget;
Button button_update{
{16, 256, 96, 24},
{16, 280, 96, 24},
"Update"};
Button button_done{
{128, 256, 96, 24},
{128, 280, 96, 24},
"Done"};
Button button_write{
{144, 248, 80, 20},
"Write"};
Labels labels{
{{1 * 8, 248}, "Reg:", Color::light_grey()},
{{8 * 8, 248}, "Data:", Color::light_grey()}};
SymField field_write_reg_num{
{5 * 8, 248},
2,
SymField::Type::Hex};
SymField field_write_data_val{
{13 * 8, 248},
4,
SymField::Type::Hex};
};
class ControlsSwitchesWidget : public Widget {
@@ -274,6 +301,58 @@ class DebugControlsView : public View {
"Done"};
};
class DebugMemoryDumpView : public View {
public:
DebugMemoryDumpView(NavigationView& nav);
void focus() override;
std::string title() const override { return "Memory Dump"; };
private:
Button button_dump{
{72, 4 * 16, 96, 24},
"Dump"};
Button button_read{
{16, 11 * 16, 96, 24},
"Read"};
Button button_write{
{128, 11 * 16, 96, 24},
"Write"};
Button button_done{
{128, 240, 96, 24},
"Done"};
Labels labels{
{{5 * 8, 1 * 16}, "Dump Range to File", Color::yellow()},
{{0 * 8, 2 * 16}, "Starting Address: 0x", Color::light_grey()},
{{0 * 8, 3 * 16}, "Byte Count: 0x", Color::light_grey()},
{{3 * 8, 8 * 16}, "Read/Write Single Word", Color::yellow()},
{{0 * 8, 9 * 16}, "Memory Address: 0x", Color::light_grey()},
{{0 * 8, 10 * 16}, "Data Value: 0x", Color::light_grey()}};
SymField field_starting_address{
{20 * 8, 2 * 16},
8,
SymField::Type::Hex};
SymField field_byte_count{
{20 * 8, 3 * 16},
8,
SymField::Type::Hex};
SymField field_rw_address{
{20 * 8, 9 * 16},
8,
SymField::Type::Hex};
SymField field_data_value{
{20 * 8, 10 * 16},
8,
SymField::Type::Hex};
};
class DebugPmemView : public View {
public:
DebugPmemView(NavigationView& nav);
@@ -282,17 +361,8 @@ class DebugPmemView : public View {
std::string title() const override { return "P.Mem Debug"; }
private:
struct pmem_data {
uint32_t regfile[63];
uint32_t check_value;
};
static constexpr uint8_t page_size{96}; // Must be multiply of 4 otherwise bit shifting for register view wont work properly
static constexpr uint8_t page_max{(portapack::memory::map::backup_ram.size() + page_size - 1) / page_size - 1};
int32_t page{0};
volatile const pmem_data& data;
static constexpr uint8_t page_count{(portapack::memory::map::backup_ram.size() + page_size - 1) / page_size};
Text text_page{{16, 16, 208, 16}};
@@ -307,7 +377,6 @@ class DebugPmemView : public View {
};
void update();
uint32_t registers_widget_feed(const size_t register_number);
};
class DebugScreenTest : public View {
+8 -1
View File
@@ -156,7 +156,7 @@ void FileManBaseView::load_directory_contents(const fs::path& dir_path) {
for (const auto& entry : fs::directory_iterator(dir_path, u"*")) {
// Hide files starting with '.' (hidden / tmp).
if (is_hidden_file(entry.path()))
if (!show_hidden_files && is_hidden_file(entry.path()))
continue;
if (fs::is_regular_file(entry.status())) {
@@ -568,6 +568,7 @@ FileManagerView::FileManagerView(
&button_open_notepad,
&button_rename_timestamp,
&button_open_iq_trim,
&button_show_hidden_files,
});
menu_view.on_highlight = [this]() {
@@ -658,6 +659,12 @@ FileManagerView::FileManagerView(
} else
nav_.display_modal("IQ Trim", "Not a capture file.");
};
button_show_hidden_files.on_select = [this]() {
show_hidden_files = !show_hidden_files;
button_show_hidden_files.set_color(show_hidden_files ? Color::green() : Color::dark_grey());
reload_current();
};
}
} // namespace ui
+8
View File
@@ -104,6 +104,8 @@ class FileManBaseView : public View {
std::vector<fileman_entry> entry_list{};
std::vector<uint32_t> saved_index_stack{};
bool show_hidden_files{false};
Labels labels{
{{0, 0}, "Path:", Color::light_grey()}};
@@ -278,6 +280,12 @@ class FileManagerView : public FileManBaseView {
{},
&bitmap_icon_trim,
Color::orange()};
NewButton button_show_hidden_files{
{13 * 8, 34 * 8, 4 * 8, 32},
{},
&bitmap_icon_hide,
Color::dark_grey()};
};
} /* namespace ui */
+11 -18
View File
@@ -142,31 +142,24 @@ LevelView::LevelView(NavigationView& nav)
}
void LevelView::on_statistics_update(const ChannelStatistics& statistics) {
static int last_max_db = -1000;
static int last_min_rssi = -1000;
static int last_avg_rssi = -1000;
static int last_max_rssi = -1000;
static int16_t last_max_db = -1000;
static int16_t last_min_rssi = -1000;
static int16_t last_avg_rssi = -1000;
static int16_t last_max_rssi = -1000;
rssi_graph.add_values(rssi.get_min(), rssi.get_avg(), rssi.get_max(), statistics.max_db);
bool refresh_db = false;
bool refresh_rssi = false;
// refresh db
if (last_max_db != statistics.max_db) {
refresh_db = true;
}
if (last_min_rssi != rssi.get_min() || last_avg_rssi != rssi.get_avg() || last_max_rssi != rssi.get_max()) {
refresh_rssi = true;
}
if (refresh_db) {
last_max_db = statistics.max_db;
freq_stats_db.set("Power: " + to_string_dec_int(statistics.max_db) + " db");
}
if (refresh_rssi) {
last_min_rssi = rssi.get_min();
last_avg_rssi = rssi.get_avg();
last_max_rssi = rssi.get_max();
freq_stats_rssi.set("RSSI: " + to_string_dec_int(rssi.get_min()) + "/" + to_string_dec_int(rssi.get_avg()) + "/" + to_string_dec_int(rssi.get_max()) + ",dt: " + to_string_dec_int(rssi.get_delta()));
// refresh rssi
if (last_min_rssi != rssi_graph.get_graph_min() || last_avg_rssi != rssi_graph.get_graph_avg() || last_max_rssi != rssi_graph.get_graph_max()) {
last_min_rssi = rssi_graph.get_graph_min();
last_avg_rssi = rssi_graph.get_graph_avg();
last_max_rssi = rssi_graph.get_graph_max();
freq_stats_rssi.set("RSSI: " + to_string_dec_int(last_min_rssi) + "/" + to_string_dec_int(last_avg_rssi) + "/" + to_string_dec_int(last_max_rssi) + ",dt: " + to_string_dec_int(rssi_graph.get_graph_delta()));
}
} /* on_statistic_updates */
+42 -22
View File
@@ -56,13 +56,23 @@ static msg_t ookthread_fn(void* arg) {
v = (symbol < 2) ? 1 : 0; // TX on for dot or dash, off for pause
delay = morse_symbols[symbol];
gpio_og_tx.write(v);
if (hackrf_r9) { // r9 has a common PIN 54 for MODE CONTROL TX / RX, <=r6 has two independent MODE CONTROL pins (TX and RX)
gpio_r9_rx.write(!v); // in hackrf r9 opposite logic "0" means tx , "1" rx = no tx)
} else {
gpio_og_tx.write(v); // in hackrf <=r6, "1" means tx , "0" no tx.
}
arg_c->on_tx_progress(i, false);
chThdSleepMilliseconds(delay * arg_c->time_unit_ms);
}
gpio_og_tx.write(0); // Ensure TX is off
if (hackrf_r9) { // r9 has a common PIN 54 for MODE CONTROL TX / RX, <=r6 has two independent MODE CONTROL pins (TX and RX)
gpio_r9_rx.write(1); // Hackrf_r9 , common pin, rx="1" (we ensure TX is off) / tx="0"
} else {
gpio_og_tx.write(0); // Hackrf <=r6 independent TX / RX pins ,now touching the tx pin ==> Ensure TX is off
}
arg_c->on_tx_progress(0, true);
chThdExit(0);
@@ -121,9 +131,9 @@ bool MorseView::start_tx() {
transmitter_model.set_baseband_bandwidth(1'750'000); // Min TX LPF .already tested in FM morse max tone 9,999k , max dev 150khz
transmitter_model.enable();
if (modulation == CW) {
if (mode_cw) {
ookthread = chThdCreateStatic(ookthread_wa, sizeof(ookthread_wa), NORMALPRIO + 10, ookthread_fn, this);
} else if (modulation == FM) {
} else {
baseband::set_tones_config(transmitter_model.channel_bandwidth(), 0, symbol_count, false, false);
}
@@ -133,8 +143,8 @@ bool MorseView::start_tx() {
void MorseView::update_tx_duration() {
uint32_t duration_ms;
time_unit_ms = 1200 / field_speed.value();
symbol_count = morse_encode(message, time_unit_ms, field_tone.value(), &time_units);
time_unit_ms = 1200 / speed;
symbol_count = morse_encode(message, time_unit_ms, tone, &time_units);
if (symbol_count) {
duration_ms = time_units * time_unit_ms;
@@ -175,7 +185,7 @@ void MorseView::on_loop_progress(const uint32_t progress, const bool done) {
}
void MorseView::set_foxhunt(size_t i) {
message = foxhunt_codes[i];
message = foxhunt_codes[i - 1];
buffer = message.c_str();
text_message.set(message);
update_tx_duration();
@@ -188,7 +198,7 @@ MorseView::MorseView(
add_children({&labels,
&checkbox_foxhunt,
&options_foxhunt,
&field_foxhunt,
&field_speed,
&field_tone,
&options_modulation,
@@ -200,36 +210,46 @@ MorseView::MorseView(
&tx_view});
// Default settings
field_speed.set_value(15); // 15wps
field_tone.set_value(700); // 700Hz FM tone
options_modulation.set_selected_index(0); // CW mode
options_loop.set_selected_index(0); // Off
field_speed.set_value(speed);
field_tone.set_value(tone);
options_modulation.set_by_value(mode_cw);
options_loop.set_by_value(loop);
checkbox_foxhunt.set_value(foxhunt_mode);
field_foxhunt.set_value(foxhunt_code);
checkbox_foxhunt.on_select = [this](Checkbox&, bool value) {
foxhunt_mode = value;
if (foxhunt_mode)
set_foxhunt(options_foxhunt.selected_index_value());
set_foxhunt(foxhunt_code);
};
options_foxhunt.on_change = [this](size_t i, int32_t) {
field_foxhunt.on_change = [this](int32_t value) {
foxhunt_code = value;
if (foxhunt_mode)
set_foxhunt(i);
set_foxhunt(foxhunt_code);
};
options_modulation.on_change = [this](size_t i, int32_t) {
modulation = (modulation_t)i;
};
options_loop.on_change = [this](size_t i, uint32_t n) {
options_modulation.on_change = [this](size_t i, int32_t value) {
(void)i; // avoid unused warning
loop = n;
mode_cw = (bool)value;
};
field_speed.on_change = [this](int32_t) {
options_loop.on_change = [this](size_t i, uint32_t value) {
(void)i; // avoid unused warning
loop = value;
};
field_speed.on_change = [this](int32_t value) {
speed = value;
update_tx_duration();
};
field_tone.on_change = [this](int32_t value) {
tone = value;
};
button_message.on_select = [this, &nav](Button&) {
this->on_set_text(nav);
};
+25 -24
View File
@@ -65,7 +65,6 @@ class MorseView : public View {
private:
NavigationView& nav_;
std::string buffer{"PORTAPACK"};
std::string message{};
uint32_t time_units{0};
@@ -74,14 +73,25 @@ class MorseView : public View {
1750000 /* bandwidth */,
1536000 /* sampling rate */
};
app_settings::SettingsManager settings_{
"tx_morse", app_settings::Mode::TX};
enum modulation_t {
CW = 0,
FM = 1
};
modulation_t modulation{CW};
std::string buffer{"PORTAPACK"};
bool mode_cw{true};
bool foxhunt_mode{false};
uint32_t foxhunt_code{1};
uint32_t speed{15};
uint32_t tone{700};
app_settings::SettingsManager settings_{
"tx_morse",
app_settings::Mode::TX,
{
{"message"sv, &buffer},
{"foxhunt"sv, &foxhunt_mode},
{"foxhunt_code"sv, &foxhunt_code},
{"speed"sv, &speed},
{"tone"sv, &tone},
{"mode_cw"sv, &mode_cw},
{"loop"sv, &loop},
}};
bool start_tx();
void update_tx_duration();
@@ -90,7 +100,6 @@ class MorseView : public View {
Thread* ookthread{nullptr};
Thread* loopthread{nullptr};
bool foxhunt_mode{false};
bool run{false};
Labels labels{
@@ -104,20 +113,12 @@ class MorseView : public View {
{4 * 8, 16},
8,
"Foxhunt:"};
OptionsField options_foxhunt{
NumberField field_foxhunt{
{17 * 8, 16 + 4},
7,
{{"1 (MOE)", 0},
{"2 (MOI)", 1},
{"3 (MOS)", 2},
{"4 (MOH)", 3},
{"5 (MO5)", 4},
{"6 (MON)", 5},
{"7 (MOD)", 6},
{"8 (MOB)", 7},
{"9 (MO6)", 8},
{"X (MO) ", 9},
{"T (S) ", 10}}};
2,
{1, 11},
1,
' '};
NumberField field_speed{
{10 * 8, 6 * 8},
@@ -136,8 +137,8 @@ class MorseView : public View {
OptionsField options_modulation{
{15 * 8, 10 * 8},
2,
{{"CW", 0},
{"FM", 1}}};
{{"CW", true},
{"FM", false}}};
OptionsField options_loop{
{9 * 8, 12 * 8},
+9 -9
View File
@@ -481,10 +481,9 @@ ReconView::ReconView(NavigationView& nav)
button_remove.on_select = [this](ButtonWithEncoder&) {
handle_remove_current_item();
// TODO: refresh UI.
timer = 0;
freq_lock = 0;
recon_redraw();
};
button_remove.on_change = [this]() {
@@ -567,7 +566,7 @@ ReconView::ReconView(NavigationView& nav)
};
button_restart.on_select = [this](Button&) {
frequency_file_load(true);
frequency_file_load();
if (frequency_list.size() > 0) {
if (fwd) {
button_dir.set_text("FW>");
@@ -614,7 +613,7 @@ ReconView::ReconView(NavigationView& nav)
button_scanner_mode.set_text("SCAN");
button_remove.set_text("DELETE");
}
frequency_file_load(true);
frequency_file_load();
if (autostart) {
recon_resume();
} else {
@@ -642,7 +641,7 @@ ReconView::ReconView(NavigationView& nav)
load_persisted_settings();
ui_settings.save();
frequency_file_load(false);
frequency_file_load();
freqlist_cleared_for_ui_action = false;
if (autostart) {
@@ -709,7 +708,7 @@ ReconView::ReconView(NavigationView& nav)
delete_file(freq_file_path);
}
frequency_file_load(false); /* do not stop all at start */
frequency_file_load(); /* do not stop all at start */
if (autostart) {
recon_resume();
} else {
@@ -718,7 +717,7 @@ ReconView::ReconView(NavigationView& nav)
recon_redraw();
}
void ReconView::frequency_file_load(bool) {
void ReconView::frequency_file_load() {
if (field_mode.selected_index_value() != SPEC_MODULATION)
audio::output::stop();
@@ -784,7 +783,7 @@ void ReconView::on_statistics_update(const ChannelStatistics& statistics) {
// hack to reload the list if it was cleared by going into CONFIG
if (freqlist_cleared_for_ui_action) {
if (!manual_mode) {
frequency_file_load(false);
frequency_file_load();
}
if (autostart && !user_pause) {
recon_resume();
@@ -1214,11 +1213,12 @@ void ReconView::handle_remove_current_item() {
if (frequency_list.size() > 0) {
current_index = clip<int32_t>(current_index, 0u, frequency_list.size() - 1);
text_cycle.set_text(to_string_dec_uint(current_index + 1, 3));
entry = current_entry();
freq = entry.frequency_a;
} else {
current_index = 0;
text_cycle.set_text(" ");
}
update_description();
}
+3 -3
View File
@@ -81,7 +81,7 @@ class ReconView : public View {
void show_max(bool refresh_display = false);
void recon_pause();
void recon_resume();
void frequency_file_load(bool stop_all_before = false);
void frequency_file_load();
void on_statistics_update(const ChannelStatistics& statistics);
void on_index_delta(int32_t v);
void on_stepper_delta(int32_t v);
@@ -154,8 +154,8 @@ class ReconView : public View {
uint16_t last_nb_match{999};
uint16_t last_freq_lock{999};
size_t last_list_size{0};
int8_t last_rssi_min{-127};
int8_t last_rssi_med{-127};
int8_t last_rssi_min{127};
int8_t last_rssi_med{0};
int8_t last_rssi_max{-127};
int32_t last_index{-1};
int64_t last_freq{0};
+33
View File
@@ -343,6 +343,38 @@ void SetUIView::focus() {
button_save.focus();
}
/* SetSDCardView *********************************************/
SetSDCardView::SetSDCardView(NavigationView& nav) {
add_children({&labels,
&checkbox_sdcard_speed,
&button_test_sdcard_high_speed,
&text_sdcard_test_status,
&button_save,
&button_cancel});
checkbox_sdcard_speed.set_value(pmem::config_sdcard_high_speed_io());
button_test_sdcard_high_speed.on_select = [&nav, this](Button&) {
pmem::set_config_sdcard_high_speed_io(true, false);
text_sdcard_test_status.set("!! HIGH SPEED MODE ON !!");
};
button_save.on_select = [&nav, this](Button&) {
pmem::set_config_sdcard_high_speed_io(checkbox_sdcard_speed.value(), true);
send_system_refresh();
nav.pop();
};
button_cancel.on_select = [&nav, this](Button&) {
nav.pop();
};
}
void SetSDCardView::focus() {
button_save.focus();
}
/* SetConverterSettingsView ******************************/
SetConverterSettingsView::SetConverterSettingsView(NavigationView& nav) {
@@ -636,6 +668,7 @@ SettingsMenuView::SettingsMenuView(NavigationView& nav) {
{"QR Code", ui::Color::dark_cyan(), &bitmap_icon_qr_code, [&nav]() { nav.push<SetQRCodeView>(); }},
{"Radio", ui::Color::dark_cyan(), &bitmap_icon_options_radio, [&nav]() { nav.push<SetRadioView>(); }},
{"User Interface", ui::Color::dark_cyan(), &bitmap_icon_options_ui, [&nav]() { nav.push<SetUIView>(); }},
{"SD Card", ui::Color::dark_cyan(), &bitmap_icon_sdcard, [&nav]() { nav.push<SetSDCardView>(); }},
});
set_max_rows(2); // allow wider buttons
}
+37 -2
View File
@@ -306,8 +306,43 @@ class SetUIView : public View {
Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel",
};
"Cancel"};
};
class SetSDCardView : public View {
public:
SetSDCardView(NavigationView& nav);
void focus() override;
std::string title() const override { return "SD Card"; };
private:
Labels labels{
// 01234567890123456789012345678
{{1 * 8, 120 - 48}, " HIGH SPEED SDCARD IO ", Color::light_grey()},
{{1 * 8, 120 - 32}, " May or may not work !! ", Color::light_grey()}};
Checkbox checkbox_sdcard_speed{
{2 * 8, 120},
20,
"enable high speed IO"};
Button button_test_sdcard_high_speed{
{2 * 8, 152, 27 * 8, 32},
"TEST BUTTON (NO PMEM SAVE)"};
Text text_sdcard_test_status{
{2 * 8, 198, 28 * 8, 16},
""};
Button button_save{
{2 * 8, 16 * 16, 12 * 8, 32},
"Save"};
Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel"};
};
class SetConverterSettingsView : public View {
+69 -2
View File
@@ -72,6 +72,11 @@ void TextViewer::paint(Painter& painter) {
paint_state_.redraw_text = false;
}
if (paint_state_.redraw_marked) {
paint_marked(painter);
paint_state_.redraw_marked = false;
}
paint_cursor(painter);
}
@@ -118,8 +123,9 @@ bool TextViewer::on_encoder(EncoderEvent delta) {
return updated;
}
void TextViewer::redraw(bool redraw_text) {
void TextViewer::redraw(bool redraw_text, bool redraw_marked) {
paint_state_.redraw_text = redraw_text;
paint_state_.redraw_marked = redraw_marked;
set_dirty();
}
@@ -140,6 +146,27 @@ void TextViewer::cursor_end() {
redraw();
}
void TextViewer::cursor_set(uint16_t line, uint16_t col) {
cursor_.line = line;
cursor_.col = col;
}
void TextViewer::cursor_mark_selected() {
LineColPair newMarker = std::make_pair(cursor_.line, cursor_.col);
auto it = std::find(pairedVector.begin(), pairedVector.end(), newMarker);
if (it != pairedVector.end()) {
pairedVector.erase(it);
} else {
pairedVector.push_back(newMarker);
}
// Mark pending change.
cursor_.mark_change = false;
redraw();
}
uint16_t TextViewer::line_length() {
return file_->line_length(cursor_.line);
}
@@ -247,12 +274,52 @@ void TextViewer::paint_cursor(Painter& painter) {
};
if (paint_state_.line != UINT32_MAX) // only XOR old cursor if it still appears on the screen
xor_cursor(paint_state_.line, paint_state_.col);
{
// Only reset previous cursor if we aren't marking.
if (paint_state_.mark_change) {
xor_cursor(paint_state_.line, paint_state_.col);
}
}
xor_cursor(cursor_.line, cursor_.col);
paint_state_.line = cursor_.line;
paint_state_.col = cursor_.col;
paint_state_.mark_change = cursor_.mark_change;
// Reset marking and wait for new change.
cursor_.mark_change = true;
}
void TextViewer::paint_marked(Painter& painter) {
auto xor_cursor = [this, &painter](int32_t line, uint16_t col) {
int cursor_width = char_width + 1;
int x = (col - paint_state_.first_col) * char_width - 1;
if (x < 0) { // cursor is one pixel narrower when in left column
cursor_width--;
x = 0;
}
int y = screen_rect().top() + (line - paint_state_.first_line) * char_height;
// Converting one row at a time to reduce buffer size
auto pbuf8 = cursor_.pixel_buffer8;
auto pbuf = cursor_.pixel_buffer;
for (auto col = 0; col < char_height; col++) {
// Annoyingly, read_pixels uses a 24-bit pixel format vs draw_pixels which uses 16-bit
portapack::display.read_pixels({x, y + col, cursor_width, 1}, pbuf8, cursor_width);
for (auto i = 0; i < cursor_width; i++)
pbuf[i] = Color(pbuf8[i].r, pbuf8[i].g, pbuf8[i].b).v ^ 0xFFFF;
portapack::display.draw_pixels({x, y + col, cursor_width, 1}, pbuf, cursor_width);
}
};
auto it = pairedVector.begin();
while (it != pairedVector.end()) {
LineColPair entry = (LineColPair)*it;
xor_cursor(entry.first, entry.second);
it++;
}
}
void TextViewer::reset_file(FileWrapper* file) {
+11 -1
View File
@@ -59,7 +59,7 @@ class TextViewer : public Widget {
bool on_key(KeyEvent key) override;
bool on_encoder(EncoderEvent delta) override;
void redraw(bool redraw_text = false);
void redraw(bool redraw_text = false, bool redraw_marked = false);
void set_file(FileWrapper& file) { reset_file(&file); }
void clear_file() { reset_file(); }
@@ -71,6 +71,11 @@ class TextViewer : public Widget {
void cursor_home();
void cursor_end();
void cursor_set(uint16_t line, uint16_t col);
void cursor_mark_selected();
typedef std::pair<uint16_t, uint16_t> LineColPair;
std::vector<LineColPair> pairedVector{};
// Gets the length of the current line.
uint16_t line_length();
@@ -97,6 +102,7 @@ class TextViewer : public Widget {
void paint_text(Painter& painter, uint32_t line, uint16_t col);
void paint_cursor(Painter& painter);
void paint_marked(Painter& painter);
void reset_file(FileWrapper* file = nullptr);
@@ -111,6 +117,8 @@ class TextViewer : public Widget {
uint32_t first_line{};
uint16_t first_col{};
bool redraw_text{true};
bool redraw_marked{false};
bool mark_change{true};
} paint_state_{};
struct {
@@ -121,6 +129,8 @@ class TextViewer : public Widget {
// Pixel buffer used for cursor XOR'ing - Max cursor width = Max char width + 1
ColorRGB888 pixel_buffer8[ui::char_width + 1]{};
Color pixel_buffer[ui::char_width + 1]{};
bool mark_change{true};
} cursor_{};
};
@@ -0,0 +1,220 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ui_weatherstation.hpp"
#include "modems.hpp"
#include "audio.hpp"
#include "baseband_api.hpp"
#include "string_format.hpp"
#include "portapack_persistent_memory.hpp"
using namespace portapack;
using namespace ui;
namespace ui {
void WeatherRecentEntryDetailView::update_data() {
// set text elements
text_type.set(WeatherView::getWeatherSensorTypeName((FPROTO_WEATHER_SENSOR)entry_.sensorType));
text_id.set("0x" + to_string_hex(entry_.id));
text_temp.set(weather_units_fahr ? to_string_decimal((entry_.temp * 9 / 5) + 32, 1) + STR_DEGREES_F : to_string_decimal(entry_.temp, 2) + STR_DEGREES_C);
text_hum.set(to_string_dec_uint(entry_.humidity) + "%");
text_ch.set(to_string_dec_uint(entry_.channel));
text_batt.set(to_string_dec_uint(entry_.battery_low) + " " + ((entry_.battery_low == 0) ? "OK" : "LOW"));
text_age.set(to_string_dec_uint(entry_.age) + " sec");
}
WeatherRecentEntryDetailView::WeatherRecentEntryDetailView(NavigationView& nav, const WeatherRecentEntry& entry)
: nav_{nav},
entry_{entry} {
add_children({&button_done,
&text_type,
&text_id,
&text_temp,
&text_hum,
&text_ch,
&text_batt,
&text_age,
&labels});
button_done.on_select = [&nav](const ui::Button&) {
nav.pop();
};
update_data();
}
void WeatherRecentEntryDetailView::focus() {
button_done.focus();
}
void WeatherView::focus() {
field_frequency.focus();
}
WeatherView::WeatherView(NavigationView& nav)
: nav_{nav} {
add_children({&rssi,
&field_rf_amp,
&field_lna,
&field_vga,
&field_frequency,
&options_temperature,
&button_clear_list,
&recent_entries_view});
baseband::run_image(portapack::spi_flash::image_tag_weather);
button_clear_list.on_select = [this](Button&) {
recent.clear();
recent_entries_view.set_dirty();
};
field_frequency.set_step(100000);
options_temperature.on_change = [this](size_t, int32_t i) {
weather_units_fahr = (bool)i;
recent_entries_view.set_dirty();
};
options_temperature.set_selected_index(weather_units_fahr, false);
const Rect content_rect{0, header_height, screen_width, screen_height - header_height};
recent_entries_view.set_parent_rect(content_rect);
recent_entries_view.on_select = [this](const WeatherRecentEntry& entry) {
nav_.push<WeatherRecentEntryDetailView>(entry);
};
baseband::set_weather();
receiver_model.enable();
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
on_tick_second();
};
}
void WeatherView::on_tick_second() {
for (auto& entry : recent) {
entry.inc_age(1);
}
recent_entries_view.set_dirty();
}
void WeatherView::on_data(const WeatherDataMessage* data) {
WeatherRecentEntry key{data->sensorType, data->id, data->temp, data->humidity, data->channel, data->battery_low};
auto matching_recent = find(recent, key.key());
if (matching_recent != std::end(recent)) {
// Found within. Move to front of list, increment counter.
(*matching_recent).reset_age();
recent.push_front(*matching_recent);
recent.erase(matching_recent);
} else {
recent.emplace_front(key);
truncate_entries(recent, 64);
}
recent_entries_view.set_dirty();
}
WeatherView::~WeatherView() {
rtc_time::signal_tick_second -= signal_token_tick_second;
receiver_model.disable();
baseband::shutdown();
}
const char* WeatherView::getWeatherSensorTypeName(FPROTO_WEATHER_SENSOR type) {
switch (type) {
case FPW_NexusTH:
return "NexusTH";
case FPW_Acurite592TXR:
return "Acurite592TXR";
case FPW_Acurite606TX:
return "Acurite606TX";
case FPW_Acurite609TX:
return "Acurite609TX";
case FPW_Ambient:
return "Ambient";
case FPW_AuriolAhfl:
return "AuriolAhfl";
case FPW_AuriolTH:
return "AuriolTH";
case FPW_GTWT02:
return "GT-WT02";
case FPW_GTWT03:
return "GT-WT03";
case FPW_INFACTORY:
return "InFactory";
case FPW_LACROSSETX:
return "LaCrosse TX";
case FPW_LACROSSETX141thbv2:
return "LaCrosse TX141THBv2";
case FPW_OREGON2:
return "Oregon2";
case FPW_OREGON3:
return "Oregon3";
case FPW_OREGONv1:
return "OregonV1";
case FPW_THERMOPROTX4:
return "ThermoPro TX4";
case FPW_TX_8300:
return "TX 8300";
case FPW_WENDOX_W6726:
return "Wendox W6726";
case FPW_Acurite986:
return "Acurite986";
case FPW_Invalid:
default:
return "Unknown";
}
}
std::string WeatherView::pad_string_with_spaces(int snakes) {
std::string paddedStr(snakes, ' ');
return paddedStr;
}
template <>
void RecentEntriesTable<ui::WeatherRecentEntries>::draw(
const Entry& entry,
const Rect& target_rect,
Painter& painter,
const Style& style) {
std::string line{};
line.reserve(30);
line = WeatherView::getWeatherSensorTypeName((FPROTO_WEATHER_SENSOR)entry.sensorType);
if (line.length() < 10) {
line += WeatherView::pad_string_with_spaces(10 - line.length());
} else {
line = truncate(line, 10);
}
std::string temp = (weather_units_fahr ? to_string_decimal((entry.temp * 9 / 5) + 32, 1) : to_string_decimal(entry.temp, 1));
std::string humStr = to_string_dec_uint(entry.humidity) + "%";
std::string chStr = to_string_dec_uint(entry.channel);
std::string ageStr = to_string_dec_uint(entry.age);
line += WeatherView::pad_string_with_spaces(6 - temp.length()) + temp;
line += WeatherView::pad_string_with_spaces(5 - humStr.length()) + humStr;
line += WeatherView::pad_string_with_spaces(4 - chStr.length()) + chStr;
line += WeatherView::pad_string_with_spaces(4 - ageStr.length()) + ageStr;
line.resize(target_rect.width() / 8, ' ');
painter.draw_string(target_rect.location(), style, line);
}
} // namespace ui
@@ -0,0 +1,193 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __UI_WEATHER_H__
#define __UI_WEATHER_H__
#include "ui.hpp"
#include "ui_navigation.hpp"
#include "ui_receiver.hpp"
#include "ui_freq_field.hpp"
#include "app_settings.hpp"
#include "radio_state.hpp"
#include "utility.hpp"
#include "recent_entries.hpp"
#include "../baseband/fprotos/weathertypes.hpp"
using namespace ui;
namespace ui {
static bool weather_units_fahr{false};
struct WeatherRecentEntry {
using Key = uint64_t;
static constexpr Key invalid_key = 0x0fffffff; // todo calc the invalid all
uint8_t sensorType = FPW_Invalid;
uint32_t id = 0xFFFFFFFF;
float temp = -273.0f;
uint8_t humidity = 0xFF;
uint8_t battery_low = 0xFF;
uint8_t channel = 0xFF;
uint16_t age = 0; // updated on each seconds, show how long the signal was last seen
WeatherRecentEntry() {}
WeatherRecentEntry(
uint8_t sensorType,
uint32_t id,
float temp,
uint8_t humidity,
uint8_t channel,
uint8_t battery_low = 0xff)
: sensorType{sensorType},
id{id},
temp{temp},
humidity{humidity},
battery_low{battery_low},
channel{channel} {
}
Key key() const {
return (((static_cast<uint64_t>(temp * 10) & 0xFFFF) << 48) ^ static_cast<uint64_t>(id) << 24) |
(static_cast<uint64_t>(sensorType) & 0xFF) << 16 |
(static_cast<uint64_t>(humidity) & 0xFF) << 8 |
(static_cast<uint64_t>(battery_low) & 0xF) << 4 |
(static_cast<uint64_t>(channel) & 0xF);
}
void inc_age(int delta) {
if (UINT16_MAX - delta > age) age += delta;
}
void reset_age() {
age = 0;
}
};
using WeatherRecentEntries = RecentEntries<WeatherRecentEntry>;
using WeatherRecentEntriesView = RecentEntriesView<WeatherRecentEntries>;
class WeatherView : public View {
public:
WeatherView(NavigationView& nav);
~WeatherView();
void focus() override;
std::string title() const override { return "Weather"; };
static const char* getWeatherSensorTypeName(FPROTO_WEATHER_SENSOR type);
static std::string pad_string_with_spaces(int snakes);
private:
void on_tick_second();
void on_data(const WeatherDataMessage* data);
NavigationView& nav_;
RxRadioState radio_state_{
433'920'000 /* frequency */,
1'750'000 /* bandwidth */,
2'000'000 /* sampling rate */,
ReceiverModel::Mode::AMAudio};
app_settings::SettingsManager settings_{
"rx_weather",
app_settings::Mode::RX,
{
{"units_fahr"sv, &weather_units_fahr},
}};
WeatherRecentEntries recent{};
OptionsField options_temperature{
{10 * 8, 0 * 16},
2,
{{STR_DEGREES_C, 0},
{STR_DEGREES_F, 1}}};
RFAmpField field_rf_amp{
{13 * 8, 0 * 16}};
LNAGainField field_lna{
{15 * 8, 0 * 16}};
VGAGainField field_vga{
{18 * 8, 0 * 16}};
RSSI rssi{
{21 * 8, 0, 6 * 8, 4}};
RxFrequencyField field_frequency{
{0 * 8, 0 * 16},
nav_};
SignalToken signal_token_tick_second{};
Button button_clear_list{
{0, 16, 7 * 8, 32},
"Clear"};
static constexpr auto header_height = 3 * 16;
const RecentEntriesColumns columns{{
{"Type", 10},
{"Temp", 5},
{"Hum", 4},
{"Ch", 3},
{"Age", 3},
}};
WeatherRecentEntriesView recent_entries_view{columns, recent};
MessageHandlerRegistration message_handler_packet{
Message::ID::WeatherData,
[this](Message* const p) {
const auto message = static_cast<const WeatherDataMessage*>(p);
this->on_data(message);
}};
};
class WeatherRecentEntryDetailView : public View {
public:
WeatherRecentEntryDetailView(NavigationView& nav, const WeatherRecentEntry& entry);
void update_data();
void focus() override;
private:
NavigationView& nav_;
WeatherRecentEntry entry_{};
Text text_type{{0 * 8, 1 * 16, 15 * 8, 16}, "?"};
Text text_id{{6 * 8, 2 * 16, 10 * 8, 16}, "?"};
Text text_temp{{6 * 8, 3 * 16, 8 * 8, 16}, "?"};
Text text_hum{{11 * 8, 4 * 16, 6 * 8, 16}, "?"};
Text text_ch{{11 * 8, 5 * 16, 6 * 8, 16}, "?"};
Text text_batt{{11 * 8, 6 * 16, 6 * 8, 16}, "?"};
Text text_age{{11 * 8, 7 * 16, 6 * 8, 16}, "?"};
Labels labels{
{{0 * 8, 0 * 16}, "Weather station type:", Color::light_grey()},
{{0 * 8, 2 * 16}, "Id: ", Color::light_grey()},
{{0 * 8, 3 * 16}, "Temp:", Color::light_grey()},
{{0 * 8, 4 * 16}, "Humidity:", Color::light_grey()},
{{0 * 8, 5 * 16}, "Channel:", Color::light_grey()},
{{0 * 8, 6 * 16}, "Battery:", Color::light_grey()},
{{0 * 8, 7 * 16}, "Age:", Color::light_grey()},
};
Button button_done{
{screen_width - 96 - 4, screen_height - 32 - 12, 96, 32},
"Done"};
};
} // namespace ui
#endif /*__UI_WEATHER_H__*/
+4
View File
@@ -240,6 +240,10 @@ uint32_t reg_read(const size_t register_number) {
return audio_codec->reg_read(register_number);
}
void reg_write(const size_t register_number, uint32_t value) {
audio_codec->reg_write(register_number, value);
}
std::string codec_name() {
return audio_codec->name();
}
+2
View File
@@ -59,6 +59,7 @@ class Codec {
virtual size_t reg_count() const = 0;
virtual size_t reg_bits() const = 0;
virtual uint32_t reg_read(const size_t register_number) = 0;
virtual void reg_write(const size_t register_number, const uint32_t value) = 0;
};
namespace output {
@@ -107,6 +108,7 @@ namespace debug {
size_t reg_count();
uint32_t reg_read(const size_t register_number);
void reg_write(const size_t register_number, uint32_t value);
std::string codec_name();
size_t reg_bits();
+6 -2
View File
@@ -278,8 +278,7 @@ void set_pocsag() {
}
void set_adsb() {
const ADSBConfigureMessage message{
1};
const ADSBConfigureMessage message{};
send_message(&message);
}
@@ -320,6 +319,11 @@ void set_spectrum_painter_config(const uint16_t width, const uint16_t height, bo
send_message(&message);
}
void set_weather() {
const WeatherRxConfigureMessage message{};
send_message(&message);
}
static bool baseband_image_running = false;
void run_image(const spi_flash::image_tag_t image_tag) {
+1
View File
@@ -88,6 +88,7 @@ void set_spectrum(const size_t sampling_rate, const size_t trigger);
void set_siggen_tone(const uint32_t tone);
void set_siggen_config(const uint32_t bw, const uint32_t shape, const uint32_t duration);
void set_spectrum_painter_config(const uint16_t width, const uint16_t height, bool update, int32_t bw);
void set_weather();
void request_beep();
void run_image(const portapack::spi_flash::image_tag_t image_tag);
+38
View File
@@ -5683,6 +5683,44 @@ static constexpr Bitmap bitmap_icon_trim{
{16, 16},
bitmap_icon_trim_data};
static constexpr uint8_t bitmap_icon_hide_data[] = {
0x00,
0x00,
0x00,
0x40,
0x00,
0x20,
0xE0,
0x17,
0x18,
0x18,
0xC4,
0x27,
0x62,
0x42,
0x21,
0x85,
0xA1,
0x84,
0x62,
0x46,
0xA4,
0x23,
0x18,
0x18,
0xE8,
0x07,
0x04,
0x00,
0x02,
0x00,
0x00,
0x00,
};
static constexpr Bitmap bitmap_icon_hide{
{16, 16},
bitmap_icon_hide_data};
} /* namespace ui */
#endif /*__BITMAP_HPP__*/
+1
View File
@@ -21,6 +21,7 @@
*/
#include <string>
#include <cstdint>
#ifndef __DE_BRUIJN_H__
#define __DE_BRUIJN_H__
+18 -13
View File
@@ -258,38 +258,43 @@ void draw_stack_dump() {
}
}
// Disk I/O in this function doesn't work after a fault
// Using the stack while dumping the stack isn't ideal, but hopefully anything imporant is still on the call stack.
bool stack_dump() {
uint32_t num_words = &__process_stack_end__ - &__process_stack_base__;
return memory_dump(&__process_stack_base__, num_words, true);
}
bool memory_dump(uint32_t* addr_start, uint32_t num_words, bool stack_flag) {
Painter painter;
std::string debug_dir = "DEBUG";
std::filesystem::path filename{};
File stack_dump_file{};
File dump_file{};
bool error;
std::string str;
uint32_t* addr_end;
uint32_t* p;
int n{0};
bool data_found{false};
make_new_directory(debug_dir);
filename = next_filename_matching_pattern(debug_dir + "/STACK_DUMP_????.TXT");
filename = next_filename_matching_pattern(debug_dir + "/" + (stack_flag ? "STACK" : "MEMORY") + "_DUMP_????.TXT");
error = filename.empty();
if (!error)
error = stack_dump_file.create(filename) != 0;
error = dump_file.create(filename) != 0;
if (error) {
painter.draw_string({16, 320 - 32}, ui::Styles::red, "ERROR DUMPING " + filename.filename().string() + "!");
return false;
}
for (p = &__process_stack_base__; p < &__process_stack_end__; p++) {
addr_end = addr_start + num_words;
for (p = addr_start; p < addr_end; p++) {
// skip past unused stack words
if (!data_found) {
if (stack_flag && !data_found) {
if (*p == CRT0_STACKS_FILL_PATTERN)
continue;
else {
data_found = true;
auto stack_space_left = p - &__process_stack_base__;
stack_dump_file.write_line(to_string_hex((uint32_t)&__process_stack_base__, 8) + ": Unused words " + to_string_dec_uint(stack_space_left));
auto stack_space_left = p - addr_start;
dump_file.write_line(to_string_hex((uint32_t)addr_start, 8) + ": Unused words " + to_string_dec_uint(stack_space_left));
// align subsequent lines to start on 16-byte boundaries
p -= (stack_space_left & 3);
@@ -299,20 +304,20 @@ bool stack_dump() {
// write address
if (n++ == 0) {
str = to_string_hex((uint32_t)p, 8) + ":";
stack_dump_file.write(str.data(), 9);
dump_file.write(str.data(), 9);
}
// write stack dword
str = " " + to_string_hex(*p, 8);
stack_dump_file.write(str.data(), 9);
dump_file.write(str.data(), 9);
if (n == 4) {
stack_dump_file.write("\r\n", 2);
dump_file.write("\r\n", 2);
n = 0;
}
}
painter.draw_string({16, 320 - 32}, ui::Styles::green, filename.filename().string() + " dumped!");
painter.draw_string({0, 320 - 16}, ui::Styles::green, filename.filename().string() + " dumped!");
return true;
}
+1
View File
@@ -48,5 +48,6 @@ inline uint32_t get_free_stack_space() {
}
bool stack_dump();
bool memory_dump(uint32_t* addr_start, uint32_t num_words, bool stack_flag);
#endif /*__DEBUG_H__*/
+6 -3
View File
@@ -321,7 +321,7 @@ void MAX2837::set_rx_buff_vcm(const size_t v) {
flush();
}
reg_t MAX2837::temp_sense() {
int8_t MAX2837::temp_sense() {
if (!_map.r.rx_top.ts_en) {
_map.r.rx_top.ts_en = 1;
flush_one(Register::RX_TOP);
@@ -334,12 +334,15 @@ reg_t MAX2837::temp_sense() {
halPolledDelay(ticks_for_temperature_sense_adc_conversion);
const auto value = read(Register::TEMP_SENSE);
/*
* Conversion to degrees C determined by testing - does not match data sheet.
*/
reg_t value = read(Register::TEMP_SENSE) & 0x1F;
_map.r.rx_top.ts_adc_trigger = 0;
flush_one(Register::RX_TOP);
return value;
return std::min(127, (int)(value * 4.31 - 40)); // reg value is 0 to 31; possible return range is -40 C to 127 C
}
} // namespace max2837
+2 -3
View File
@@ -833,9 +833,10 @@ class MAX2837 : public MAX283x {
void set_vco_bias(const size_t v);
void set_rx_buff_vcm(const size_t v) override;
reg_t temp_sense() override;
int8_t temp_sense() override;
reg_t read(const address_t reg_num) override;
void write(const address_t reg_num, const reg_t value) override;
private:
spi::arbiter::Target& _target;
@@ -845,8 +846,6 @@ class MAX2837 : public MAX283x {
void flush_one(const Register reg);
void write(const address_t reg_num, const reg_t value);
void write(const Register reg, const reg_t value);
reg_t read(const Register reg);
+4 -5
View File
@@ -351,7 +351,7 @@ void MAX2839::set_rx_buff_vcm(const size_t v) {
flush();
}
reg_t MAX2839::temp_sense() {
int8_t MAX2839::temp_sense() {
if (!_map.r.rx_top_2.ts_en) {
_map.r.rx_top_2.ts_en = 1;
flush_one(Register::RX_TOP_2);
@@ -365,15 +365,14 @@ reg_t MAX2839::temp_sense() {
halPolledDelay(ticks_for_temperature_sense_adc_conversion);
/*
* Things look very similar to MAX2837, so this probably works, but the
* MAX2839 data sheet does not describe the TEMP_SENSE register contents.
* Conversion to degrees C determined by testing - does not match data sheet.
*/
const auto value = read(Register::TEMP_SENSE);
reg_t value = read(Register::TEMP_SENSE) & 0x1F;
_map.r.rx_top_2.ts_adc_trigger = 0;
flush_one(Register::RX_TOP_2);
return value;
return std::min(127, (int)(value * 4.31 - 40)); // reg value is 0 to 31; possible return range is -40 C to 127 C
}
} // namespace max2839
+2 -3
View File
@@ -692,9 +692,10 @@ class MAX2839 : public MAX283x {
void set_rx_lo_iq_calibration(const size_t v) override;
void set_rx_buff_vcm(const size_t v) override;
reg_t temp_sense() override;
int8_t temp_sense() override;
reg_t read(const address_t reg_num) override;
void write(const address_t reg_num, const reg_t value) override;
private:
spi::arbiter::Target& _target;
@@ -704,8 +705,6 @@ class MAX2839 : public MAX283x {
void flush_one(const Register reg);
void write(const address_t reg_num, const reg_t value);
void write(const Register reg, const reg_t value);
reg_t read(const Register reg);
+2 -1
View File
@@ -131,9 +131,10 @@ class MAX283x {
virtual void set_rx_lo_iq_calibration(const size_t v);
virtual void set_rx_buff_vcm(const size_t v);
virtual reg_t temp_sense();
virtual int8_t temp_sense();
virtual reg_t read(const address_t reg_num);
virtual void write(const address_t reg_num, const reg_t value);
};
} // namespace max283x
+1 -2
View File
@@ -841,6 +841,7 @@ class RFFC507x {
void set_gpo1(const bool new_value);
reg_t read(const address_t reg_num);
void write(const address_t reg_num, const reg_t value);
private:
spi::SPI _bus{};
@@ -848,8 +849,6 @@ class RFFC507x {
RegisterMap _map{default_hackrf_one};
DirtyRegisters<Register, reg_count> _dirty{};
void write(const address_t reg_num, const reg_t value);
void write(const Register reg, const reg_t value);
reg_t read(const Register reg);
+2
View File
@@ -186,6 +186,8 @@ int main(void) {
sdcStop(&SDCD1);
portapack::shutdown();
} else {
config_mode_clear();
}
m4_init(portapack::spi_flash::image_tag_hackrf, portapack::memory::map::m4_code_hackrf, true);
+14
View File
@@ -467,6 +467,20 @@ bool init() {
i2c0.start(i2c_config_fast_clock);
chThdSleepMilliseconds(10);
/* Check if portapack is attached by checking if any of the two audio chips is present. */
systime_t timeout = 50;
uint8_t wm8731_reset_command[] = {0x0f, 0x00};
if (i2c0.transmit(0x1a /* wm8731 */, wm8731_reset_command, 2, timeout) == false) {
audio_codec_ak4951.reset();
uint8_t ak4951_init_command[] = {0x00, 0x00};
i2c0.transmit(0x12 /* ak4951 */, ak4951_init_command, 2, timeout);
chThdSleepMilliseconds(10);
if (i2c0.transmit(0x12 /* ak4951 */, ak4951_init_command, 2, timeout) == false) {
shutdown_base();
return false;
}
}
touch::adc::init();
controls_init();
chThdSleepMilliseconds(10);
+2
View File
@@ -20,6 +20,8 @@
* Boston, MA 02110-1301, USA.
*/
#include <cstdint>
#include "aprs.hpp"
#include "ax25.hpp"
+11 -3
View File
@@ -61,7 +61,7 @@ static constexpr SPIConfig ssp_config_max283x = {
.ssport = gpio_max283x_select.port(),
.sspad = gpio_max283x_select.pad(),
.cr0 =
CR0_CLOCKRATE(ssp_scr(ssp1_pclk_f, ssp1_cpsr, max283x_spi_f)) | CR0_FRFSPI | CR0_DSS16BIT,
CR0_CLOCKRATE(ssp_scr(ssp1_pclk_f, ssp1_cpsr, max283x_spi_f) + 1) | CR0_FRFSPI | CR0_DSS16BIT,
.cpsr = ssp1_cpsr,
};
@@ -287,6 +287,10 @@ uint32_t register_read(const size_t register_number) {
return radio::first_if.read(register_number);
}
void register_write(const size_t register_number, uint32_t value) {
radio::first_if.write(register_number, value);
}
} /* namespace first_if */
namespace second_if {
@@ -295,8 +299,12 @@ uint32_t register_read(const size_t register_number) {
return radio::second_if->read(register_number);
}
uint8_t temp_sense() {
return radio::second_if->temp_sense() & 0x1f;
void register_write(const size_t register_number, uint32_t value) {
radio::second_if->write(register_number, value);
}
int8_t temp_sense() {
return radio::second_if->temp_sense();
}
} /* namespace second_if */
+3 -1
View File
@@ -65,15 +65,17 @@ namespace debug {
namespace first_if {
uint32_t register_read(const size_t register_number);
void register_write(const size_t register_number, uint32_t value);
} /* namespace first_if */
namespace second_if {
uint32_t register_read(const size_t register_number);
void register_write(const size_t register_number, uint32_t value);
// TODO: This belongs somewhere else.
uint8_t temp_sense();
int8_t temp_sense();
} /* namespace second_if */
+24 -8
View File
@@ -24,13 +24,13 @@
#include "ui_widget.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <list>
#include <utility>
#include <functional>
#include <iterator>
#include <algorithm>
#include <list>
#include <utility>
template <class Entry>
using RecentEntries = std::list<Entry>;
@@ -42,6 +42,13 @@ typename ContainerType::const_iterator find(const ContainerType& entries, const
[key](typename ContainerType::const_reference e) { return e.key() == key; });
}
template <typename ContainerType, typename Key>
typename ContainerType::iterator find(ContainerType& entries, const Key key) {
return std::find_if(
std::begin(entries), std::end(entries),
[key](typename ContainerType::const_reference e) { return e.key() == key; });
}
template <typename ContainerType>
static void truncate_entries(ContainerType& entries, const size_t entries_max = 64) {
while (entries.size() > entries_max) {
@@ -96,19 +103,28 @@ void sortEntriesBy(ContainerType& entries, KeySelector keySelector, SortOrder as
}
template <typename ContainerType, typename KeySelector>
void removeEntriesWithoutKey(ContainerType& entries, ContainerType& filteredEntries, KeySelector keySelector) {
void resetFilteredEntries(ContainerType& entries, KeySelector keySelector) {
// Clear the filteredEntries container
filteredEntries.clear();
auto it = entries.begin();
while (it != entries.end()) {
if (!keySelector(*it)) {
filteredEntries.emplace_back(*it); // Add a new entry to filteredEntries
if (keySelector(*it)) {
entries.erase(it); // Add a new entry to filteredEntries
}
++it; // Move to the next element, outside of the if block
}
}
template <typename ContainerType, typename MemberPtr, typename KeyValue>
void setAllMembersToValue(ContainerType& entries, MemberPtr memberPtr, const KeyValue& keyValue) {
for (auto& entry : entries) {
// Check if the member specified by memberPtr is equal to keyValue
if (entry.*memberPtr != keyValue) {
// Update the member with keyValue
entry.*memberPtr = keyValue;
}
}
}
namespace ui {
using RecentEntriesColumn = std::pair<std::string, size_t>;
+42 -2
View File
@@ -311,13 +311,13 @@ std::string to_string_file_size(uint32_t file_size) {
return to_string_dec_uint(file_size) + suffix[suffix_index];
}
std::string to_string_mac_address(const uint8_t* macAddress, uint8_t length) {
std::string to_string_mac_address(const uint8_t* macAddress, uint8_t length, bool noColon) {
std::string string;
string += to_string_hex(macAddress[0], 2);
for (int i = 1; i < length; i++) {
string += ":" + to_string_hex(macAddress[i], 2);
string += noColon ? to_string_hex(macAddress[i], 2) : ":" + to_string_hex(macAddress[i], 2);
}
return string;
@@ -337,6 +337,46 @@ std::string to_string_formatted_mac_address(const char* macAddress) {
return formattedAddress;
}
void generateRandomMacAddress(char* macAddress) {
const char hexDigits[] = "0123456789ABCDEF";
// Generate 12 random hexadecimal characters
for (int i = 0; i < 12; i++) {
int randomIndex = rand() % 16;
macAddress[i] = hexDigits[randomIndex];
}
macAddress[12] = '\0'; // Null-terminate the string
}
uint64_t readUntil(File& file, char* result, std::size_t maxBufferSize, char delimiter) {
std::size_t bytesRead = 0;
while (true) {
char ch;
File::Result<File::Size> readResult = file.read(&ch, 1);
if (readResult.is_ok() && readResult.value() > 0) {
if (ch == delimiter) {
// Found a space character, stop reading
break;
} else if (bytesRead < maxBufferSize) {
// Append the character to the result if there's space
result[bytesRead++] = ch;
} else {
// Buffer is full, break to prevent overflow
break;
}
} else {
break; // End of file or error
}
}
// Null-terminate the result string
result[bytesRead] = '\0';
return bytesRead;
}
std::string unit_auto_scale(double n, const uint32_t base_unit, uint32_t precision) {
const uint32_t powers_of_ten[5] = {1, 10, 100, 1000, 10000};
std::string string{""};
+4 -1
View File
@@ -77,8 +77,11 @@ std::string to_string_FAT_timestamp(const FATTimestamp& timestamp);
std::string to_string_file_size(uint32_t file_size);
// Converts Mac Address to string.
std::string to_string_mac_address(const uint8_t* macAddress, uint8_t length);
std::string to_string_mac_address(const uint8_t* macAddress, uint8_t length, bool noColon);
std::string to_string_formatted_mac_address(const char* macAddress);
void generateRandomMacAddress(char* macAddress);
uint64_t readUntil(File& file, char* result, std::size_t maxBufferSize, char delimiter);
/* Scales 'n' to be a value less than 1000. 'base_unit' is the index of the unit from
* 'unit_prefix' that 'n' is in initially. 3 is the index of the '1s' unit. */
+1 -1
View File
@@ -29,7 +29,7 @@
class TemperatureLogger {
public:
using sample_t = uint8_t;
using sample_t = int8_t;
void second_tick();
+41 -2
View File
@@ -30,6 +30,10 @@
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(x) ((x) > 0 ? (x) : -(x))
// flag value for graph min
// If it does not change, then all graph min values are is zero
#define GRAPH_MIN_ALL_ZERO_FLAG 666
namespace ui {
RSSI::RSSI(
@@ -209,17 +213,48 @@ void RSSI::on_statistics_update(const RSSIStatistics& statistics) {
set_dirty();
}
int16_t RSSIGraph::get_graph_min() {
return graph_min_;
}
int16_t RSSIGraph::get_graph_avg() {
return graph_avg_;
}
int16_t RSSIGraph::get_graph_max() {
return graph_max_;
}
int16_t RSSIGraph::get_graph_delta() {
return graph_max_ - graph_min_;
}
void RSSIGraph::paint(Painter& painter) {
const auto r = screen_rect();
RSSIGraph_entry& prev_entry = graph_list[0];
int xpos = 0, prev_xpos = r.width();
if (graph_list.size() == 0)
return;
int xpos = 0, prev_xpos = r.width();
RSSIGraph_entry& prev_entry = graph_list[0];
graph_min_ = GRAPH_MIN_ALL_ZERO_FLAG; // if it stays at that value the whole graphlist min are zero
graph_max_ = 0;
int avg_sum = 0;
for (int n = 1; (unsigned)n <= graph_list.size(); n++) {
xpos = (r.width() * (graph_list.size() - n)) / graph_list.size();
int size = abs(xpos - prev_xpos);
RSSIGraph_entry& entry = graph_list[n - 1];
// stats
if (entry.rssi_min != 0 && entry.rssi_min < graph_min_) {
graph_min_ = entry.rssi_min;
}
if (entry.rssi_max > graph_max_) {
graph_max_ = entry.rssi_max;
}
avg_sum += entry.rssi_avg;
// black
const Rect r0{r.right() - prev_xpos, r.top(), size, r.height()};
painter.fill_rectangle(
@@ -293,6 +328,10 @@ void RSSIGraph::paint(Painter& painter) {
prev_entry = entry;
prev_xpos = xpos;
}
graph_avg_ = (avg_sum / graph_list.size());
// hack to only set to 0 if all graphlist min values are 0
if (graph_min_ == GRAPH_MIN_ALL_ZERO_FLAG)
graph_min_ = 0;
}
/*void RSSIGraph::paintOld(Painter& painter) {
+9
View File
@@ -49,6 +49,7 @@ class RSSI : public Widget {
: RSSI{{}, {}} {
}
// get last used/received min/avg/max/delta
int16_t get_min();
int16_t get_avg();
int16_t get_max();
@@ -113,8 +114,16 @@ class RSSIGraph : public Widget {
void on_hide() override;
void on_show() override;
// get whole graph_list min/avg/max/delta
int16_t get_graph_min();
int16_t get_graph_avg();
int16_t get_graph_max();
int16_t get_graph_delta();
private:
int16_t graph_min_ = 0;
int16_t graph_avg_ = 0;
int16_t graph_max_ = 0;
uint16_t nb_columns_before_hide = 16;
uint16_t nb_columns = 16;
RSSIGraphList graph_list{};
+7
View File
@@ -79,6 +79,7 @@
#include "ui_tone_search.hpp"
#include "ui_touchtunes.hpp"
#include "ui_view_wav.hpp"
#include "ui_weatherstation.hpp"
#include "ui_whipcalc.hpp"
#include "ui_external_items_menu_loader.hpp"
@@ -86,6 +87,7 @@
#include "ais_app.hpp"
#include "analog_audio_app.hpp"
#include "analog_tv_app.hpp"
#include "ble_comm_app.hpp"
#include "ble_rx_app.hpp"
#include "ble_tx_app.hpp"
#include "capture_app.hpp"
@@ -170,6 +172,9 @@ SystemStatusView::SystemStatusView(
pmem::load_persistent_settings_from_file();
}
// force apply of selected sdcard speed override at UI startup
pmem::set_config_sdcard_high_speed_io(pmem::config_sdcard_high_speed_io(), false);
button_back.id = -1; // Special ID used by FocusManager
title.set_style(&Styles::bg_dark_grey);
@@ -551,6 +556,7 @@ ReceiversMenuView::ReceiversMenuView(NavigationView& nav) {
{"APRS", Color::green(), &bitmap_icon_aprs, [&nav]() { nav.push<APRSRXView>(); }},
{"Audio", Color::green(), &bitmap_icon_speaker, [&nav]() { nav.push<AnalogAudioView>(); }},
//{"BTLE", Color::yellow(), &bitmap_icon_btle, [&nav]() { nav.push<BTLERxView>(); }},
//{"BLE Comm", ui::Color::orange(), &bitmap_icon_btle, [&nav]() { nav.push<BLECommView>(); }},
{"BLE Rx", Color::green(), &bitmap_icon_btle, [&nav]() { nav.push<BLERxView>(); }},
{"ERT Meter", Color::green(), &bitmap_icon_ert, [&nav]() { nav.push<ERTAppView>(); }},
{"Level", Color::green(), &bitmap_icon_options_radio, [&nav]() { nav.push<LevelView>(); }},
@@ -560,6 +566,7 @@ ReceiversMenuView::ReceiversMenuView(NavigationView& nav) {
{"Recon", Color::green(), &bitmap_icon_scanner, [&nav]() { nav.push<ReconView>(); }},
{"Search", Color::yellow(), &bitmap_icon_search, [&nav]() { nav.push<SearchView>(); }},
{"TPMS Cars", Color::green(), &bitmap_icon_tpms, [&nav]() { nav.push<TPMSAppView>(); }},
{"Weather", Color::yellow(), &bitmap_icon_lge, [&nav]() { nav.push<WeatherView>(); }},
// {"FSK RX", Color::yellow(), &bitmap_icon_remote, [&nav]() { nav.push<FskxRxMainView>(); }},
// {"DMR", Color::dark_grey(), &bitmap_icon_dmr, [&nav](){ nav.push<NotImplementedView>(); }},
// {"SIGFOX", Color::dark_grey(), &bitmap_icon_fox, [&nav](){ nav.push<NotImplementedView>(); }},
+8
View File
@@ -550,6 +550,14 @@ set(MODE_CPPSRC
)
DeclareTargets(PWFM wfm_audio)
### Weather Stations
set(MODE_CPPSRC
proc_weather.cpp
)
DeclareTargets(PWTH weather)
### Flash Utility
include(${CHIBIOS_PORTAPACK}/os/various/fatfs_bindings/fatfs.cmake)
@@ -0,0 +1,155 @@
#ifndef __FPROTO_Acurite_592TXR_H__
#define __FPROTO_Acurite_592TXR_H__
#include "weatherbase.hpp"
typedef enum {
Acurite_592TXRDecoderStepReset = 0,
Acurite_592TXRDecoderStepCheckPreambule,
Acurite_592TXRDecoderStepSaveDuration,
Acurite_592TXRDecoderStepCheckDuration,
} Acurite_592TXRDecoderStep;
class FProtoWeatherAcurite592TXR : public FProtoWeatherBase {
public:
FProtoWeatherAcurite592TXR() {
sensorType = FPW_Acurite592TXR;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case Acurite_592TXRDecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short * 3) <
te_delta * 2)) {
parser_step = Acurite_592TXRDecoderStepCheckPreambule;
te_last = duration;
header_count = 0;
}
break;
case Acurite_592TXRDecoderStepCheckPreambule:
if (level) {
te_last = duration;
} else {
if ((DURATION_DIFF(
te_last, te_short * 3) <
te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 3) <
te_delta * 2)) {
// Found preambule
header_count++;
} else if ((header_count > 2) && (header_count < 5)) {
if ((DURATION_DIFF(te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_long) < te_delta)) {
decode_data = 0;
decode_count_bit = 0;
subghz_protocol_blocks_add_bit(0);
parser_step = Acurite_592TXRDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(
te_last, te_long) <
te_delta) &&
(DURATION_DIFF(duration, te_short) <
te_delta)) {
decode_data = 0;
decode_count_bit = 0;
subghz_protocol_blocks_add_bit(1);
parser_step = Acurite_592TXRDecoderStepSaveDuration;
} else {
parser_step = Acurite_592TXRDecoderStepReset;
}
} else {
parser_step = Acurite_592TXRDecoderStepReset;
}
}
break;
case Acurite_592TXRDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = Acurite_592TXRDecoderStepCheckDuration;
} else {
parser_step = Acurite_592TXRDecoderStepReset;
}
break;
case Acurite_592TXRDecoderStepCheckDuration:
if (!level) {
if (duration >= ((uint32_t)te_short * 5)) {
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_acurite_592txr_check_crc()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_acurite_592txr_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
parser_step = Acurite_592TXRDecoderStepReset;
break;
} else if (
(DURATION_DIFF(
te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_long) <
te_delta)) {
subghz_protocol_blocks_add_bit(0);
parser_step = Acurite_592TXRDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(
te_last, te_long) <
te_delta) &&
(DURATION_DIFF(duration, te_short) <
te_delta)) {
subghz_protocol_blocks_add_bit(1);
parser_step = Acurite_592TXRDecoderStepSaveDuration;
} else {
parser_step = Acurite_592TXRDecoderStepReset;
}
} else {
parser_step = Acurite_592TXRDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 200;
uint32_t te_long = 400;
uint32_t te_delta = 90;
uint32_t min_count_bit_for_found = 56;
bool ws_protocol_acurite_592txr_check_crc() {
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 48),
static_cast<uint8_t>(decode_data >> 40),
static_cast<uint8_t>(decode_data >> 32),
static_cast<uint8_t>(decode_data >> 24),
static_cast<uint8_t>(decode_data >> 16),
static_cast<uint8_t>(decode_data >> 8)};
if ((subghz_protocol_blocks_add_bytes(msg, 6) ==
(uint8_t)(decode_data & 0xFF)) &&
(!subghz_protocol_blocks_parity_bytes(&msg[2], 4))) {
return true;
} else {
return false;
}
}
void ws_protocol_acurite_592txr_remote_controller() {
uint8_t channel_[] = {3, 0, 2, 1};
uint8_t channel_raw = ((data >> 54) & 0x03);
channel = channel_[channel_raw];
id = (data >> 40) & 0x3FFF;
battery_low = !((data >> 38) & 1);
humidity = (data >> 24) & 0x7F;
uint16_t temp_raw = ((data >> 9) & 0xF80) | ((data >> 8) & 0x7F);
temp = ((float)(temp_raw)-1000) / 10.0f;
btn = WS_NO_BTN;
}
};
#endif
@@ -0,0 +1,110 @@
#ifndef __FPROTO_Acurite_606TX_H__
#define __FPROTO_Acurite_606TX_H__
#include "weatherbase.hpp"
typedef enum {
Acurite_606TXDecoderStepReset = 0,
Acurite_606TXDecoderStepSaveDuration,
Acurite_606TXDecoderStepCheckDuration,
} Acurite_606TXDecoderStep;
class FProtoWeatherAcurite606TX : public FProtoWeatherBase {
public:
FProtoWeatherAcurite606TX() {
sensorType = FPW_Acurite606TX;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case Acurite_606TXDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 17) < te_delta * 8)) {
// Found syncPrefix
parser_step = Acurite_606TXDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case Acurite_606TXDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = Acurite_606TXDecoderStepCheckDuration;
} else {
parser_step = Acurite_606TXDecoderStepReset;
}
break;
case Acurite_606TXDecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(te_last, te_short) <
te_delta) {
if ((DURATION_DIFF(duration, te_short) <
te_delta) ||
(duration > te_long * 3)) {
// Found syncPostfix
parser_step = Acurite_606TXDecoderStepReset;
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_acurite_606tx_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_acurite_606tx_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta * 2) {
subghz_protocol_blocks_add_bit(0);
parser_step = Acurite_606TXDecoderStepSaveDuration;
} else if (
DURATION_DIFF(duration, te_long * 2) < te_delta * 4) {
subghz_protocol_blocks_add_bit(1);
parser_step = Acurite_606TXDecoderStepSaveDuration;
} else {
parser_step = Acurite_606TXDecoderStepReset;
}
} else {
parser_step = Acurite_606TXDecoderStepReset;
}
} else {
parser_step = Acurite_606TXDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 32;
void ws_protocol_acurite_606tx_remote_controller() {
id = (data >> 24) & 0xFF;
battery_low = (data >> 23) & 1;
channel = WS_NO_CHANNEL;
if (!((data >> 19) & 1)) {
temp = (float)((data >> 8) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 8) & 0x07FF) + 1) / -10.0f;
}
btn = WS_NO_BTN;
humidity = WS_NO_HUMIDITY;
}
bool ws_protocol_acurite_606tx_check() {
if (!decode_data) return false;
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 24),
static_cast<uint8_t>(decode_data >> 16),
static_cast<uint8_t>(decode_data >> 8)};
uint8_t crc = subghz_protocol_blocks_lfsr_digest8(msg, 3, 0x98, 0xF1);
return (crc == (decode_data & 0xFF));
}
};
#endif
@@ -0,0 +1,112 @@
#ifndef __FPROTO_Acurite_609TX_H__
#define __FPROTO_Acurite_609TX_H__
#include "weatherbase.hpp"
typedef enum {
Acurite_609TXCDecoderStepReset = 0,
Acurite_609TXCDecoderStepSaveDuration,
Acurite_609TXCDecoderStepCheckDuration,
} Acurite_609TXCDecoderStep;
class FProtoWeatherAcurite609TX : public FProtoWeatherBase {
public:
FProtoWeatherAcurite609TX() {
sensorType = FPW_Acurite609TX;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case Acurite_609TXCDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 17) <
te_delta * 8)) {
// Found syncPrefix
parser_step = Acurite_609TXCDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case Acurite_609TXCDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = Acurite_609TXCDecoderStepCheckDuration;
} else {
parser_step = Acurite_609TXCDecoderStepReset;
}
break;
case Acurite_609TXCDecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(te_last, te_short) <
te_delta) {
if ((DURATION_DIFF(duration, te_short) <
te_delta) ||
(duration > te_long * 3)) {
// Found syncPostfix
parser_step = Acurite_609TXCDecoderStepReset;
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_acurite_609txc_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_acurite_609txc_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta * 2) {
subghz_protocol_blocks_add_bit(0);
parser_step = Acurite_609TXCDecoderStepSaveDuration;
} else if (
DURATION_DIFF(duration, te_long * 2) <
te_delta * 4) {
subghz_protocol_blocks_add_bit(1);
parser_step = Acurite_609TXCDecoderStepSaveDuration;
} else {
parser_step = Acurite_609TXCDecoderStepReset;
}
} else {
parser_step = Acurite_609TXCDecoderStepReset;
}
} else {
parser_step = Acurite_609TXCDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 32;
void ws_protocol_acurite_609txc_remote_controller() {
id = (data >> 32) & 0xFF;
battery_low = (data >> 31) & 1;
channel = WS_NO_CHANNEL;
// Temperature in Celsius is encoded as a 12 bit integer value
// multiplied by 10 using the 4th - 6th nybbles (bytes 1 & 2)
// negative values are recovered by sign extend from int16_t.
int16_t temp_raw =
(int16_t)(((data >> 12) & 0xf000) | ((data >> 16) << 4));
temp = (temp_raw >> 4) * 0.1f;
humidity = (data >> 8) & 0xff;
btn = WS_NO_BTN;
}
bool ws_protocol_acurite_609txc_check() {
if (!decode_data) return false;
uint8_t crc = (uint8_t)(decode_data >> 32) +
(uint8_t)(decode_data >> 24) +
(uint8_t)(decode_data >> 16) +
(uint8_t)(decode_data >> 8);
return (crc == (decode_data & 0xFF));
}
};
#endif
+138
View File
@@ -0,0 +1,138 @@
#ifndef __FPROTO_Acurite_986_H__
#define __FPROTO_Acurite_986_H__
#include "weatherbase.hpp"
typedef enum {
Acurite_986DecoderStepReset = 0,
Acurite_986DecoderStepSync1,
Acurite_986DecoderStepSync2,
Acurite_986DecoderStepSync3,
Acurite_986DecoderStepSaveDuration,
Acurite_986DecoderStepCheckDuration,
} Acurite_986DecoderStep;
class FProtoWeatherAcurite986 : public FProtoWeatherBase {
public:
FProtoWeatherAcurite986() {
sensorType = FPW_Acurite986;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case Acurite_986DecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_long) < te_delta * 15)) {
// Found 1st sync bit
parser_step = Acurite_986DecoderStepSync1;
decode_data = 0;
decode_count_bit = 0;
}
break;
case Acurite_986DecoderStepSync1:
if (DURATION_DIFF(duration, te_long) < te_delta * 15) {
if (!level) {
parser_step = Acurite_986DecoderStepSync2;
}
} else {
parser_step = Acurite_986DecoderStepReset;
}
break;
case Acurite_986DecoderStepSync2:
if (DURATION_DIFF(duration, te_long) < te_delta * 15) {
if (!level) {
parser_step = Acurite_986DecoderStepSync3;
}
} else {
parser_step = Acurite_986DecoderStepReset;
}
break;
case Acurite_986DecoderStepSync3:
if (DURATION_DIFF(duration, te_long) < te_delta * 15) {
if (!level) {
parser_step = Acurite_986DecoderStepSaveDuration;
}
} else {
parser_step = Acurite_986DecoderStepReset;
}
break;
case Acurite_986DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = Acurite_986DecoderStepCheckDuration;
} else {
parser_step = Acurite_986DecoderStepReset;
}
break;
case Acurite_986DecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(duration, te_short) <
te_delta * 10) {
if (duration < te_short) {
subghz_protocol_blocks_add_bit(0);
parser_step = Acurite_986DecoderStepSaveDuration;
} else {
subghz_protocol_blocks_add_bit(1);
parser_step = Acurite_986DecoderStepSaveDuration;
}
} else {
// Found syncPostfix
parser_step = Acurite_986DecoderStepReset;
if ((decode_count_bit == min_count_bit_for_found) && ws_protocol_acurite_986_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_acurite_986_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
}
} else {
parser_step = Acurite_986DecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 800;
uint32_t te_long = 1750;
uint32_t te_delta = 50;
uint32_t min_count_bit_for_found = 40;
void ws_protocol_acurite_986_remote_controller() {
int temp;
id = subghz_protocol_blocks_reverse_key(data >> 24, 8);
id = (id << 8) | subghz_protocol_blocks_reverse_key(data >> 16, 8);
battery_low = (data >> 14) & 1;
channel = ((data >> 15) & 1) + 1;
temp = subghz_protocol_blocks_reverse_key(data >> 32, 8);
if (temp & 0x80) {
temp = -(temp & 0x7F);
}
temp = locale_fahrenheit_to_celsius((float)temp);
btn = WS_NO_BTN;
humidity = WS_NO_HUMIDITY;
}
bool ws_protocol_acurite_986_check() {
if (!decode_data) return false;
uint8_t msg[] = {
(uint8_t)(decode_data >> 32),
(uint8_t)(decode_data >> 24),
(uint8_t)(decode_data >> 16),
(uint8_t)(decode_data >> 8)};
uint8_t crc = subghz_protocol_blocks_crc8(msg, 4, 0x07, 0x00);
return (crc == (decode_data & 0xFF));
}
};
#endif
+88
View File
@@ -0,0 +1,88 @@
#ifndef __FPROTO_Ambient_H__
#define __FPROTO_Ambient_H__
#include "weatherbase.hpp"
#define AMBIENT_WEATHER_PACKET_HEADER_1 0xFFD440000000000 // 0xffd45 .. 0xffd46
#define AMBIENT_WEATHER_PACKET_HEADER_2 0x001440000000000 // 0x00145 .. 0x00146
#define AMBIENT_WEATHER_PACKET_HEADER_MASK 0xFFFFC0000000000
class FProtoWeatherAmbient : public FProtoWeatherBase {
public:
FProtoWeatherAmbient() {
sensorType = FPW_Ambient;
}
void feed(bool level, uint32_t duration) {
ManchesterEvent event = ManchesterEventReset;
if (!level) {
if (DURATION_DIFF(duration, te_short) < te_delta) {
event = ManchesterEventShortLow;
} else if (
DURATION_DIFF(duration, te_long) < te_delta * 2) {
event = ManchesterEventLongLow;
}
} else {
if (DURATION_DIFF(duration, te_short) < te_delta) {
event = ManchesterEventShortHigh;
} else if (
DURATION_DIFF(duration, te_long) < te_delta * 2) {
event = ManchesterEventLongHigh;
}
}
if (event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(manchester_saved_state, event, &manchester_saved_state, &data);
if (data_ok) {
decode_data = (decode_data << 1) | !data;
}
if (((decode_data & AMBIENT_WEATHER_PACKET_HEADER_MASK) == AMBIENT_WEATHER_PACKET_HEADER_1) ||
((decode_data & AMBIENT_WEATHER_PACKET_HEADER_MASK) == AMBIENT_WEATHER_PACKET_HEADER_2)) {
if (ws_protocol_ambient_weather_check_crc()) {
data = decode_data;
data_count_bit = min_count_bit_for_found;
ws_protocol_ambient_weather_remote_controller();
if (callback) callback(this);
decode_data = 0;
decode_count_bit = 0;
}
}
} else {
decode_data = 0;
decode_count_bit = 0;
manchester_advance(manchester_saved_state, ManchesterEventReset, &manchester_saved_state, NULL);
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 1000;
uint32_t te_delta = 120;
uint32_t min_count_bit_for_found = 48;
bool ws_protocol_ambient_weather_check_crc() {
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 40),
static_cast<uint8_t>(decode_data >> 32),
static_cast<uint8_t>(decode_data >> 24),
static_cast<uint8_t>(decode_data >> 16),
static_cast<uint8_t>(decode_data >> 8)};
uint8_t crc = subghz_protocol_blocks_lfsr_digest8(msg, 5, 0x98, 0x3e) ^ 0x64;
return (crc == (uint8_t)(decode_data & 0xFF));
}
void ws_protocol_ambient_weather_remote_controller() {
id = (data >> 32) & 0xFF;
battery_low = (data >> 31) & 1;
channel = ((data >> 28) & 0x07) + 1;
temp = locale_fahrenheit_to_celsius(((float)((data >> 16) & 0x0FFF) - 400.0f) / 10.0f);
humidity = (data >> 8) & 0xFF;
btn = WS_NO_BTN;
}
};
#endif
+121
View File
@@ -0,0 +1,121 @@
#ifndef __FPROTO_AuriolAhfl_H__
#define __FPROTO_AuriolAhfl_H__
#include "weatherbase.hpp"
#define AURIOL_AHFL_CONST_DATA 0b0100
typedef enum {
auriol_AHFLDecoderStepReset = 0,
auriol_AHFLDecoderStepSaveDuration,
auriol_AHFLDecoderStepCheckDuration,
} auriol_AHFLDecoderStep;
class FProtoWeatherAuriolAhfl : public FProtoWeatherBase {
public:
FProtoWeatherAuriolAhfl() {
sensorType = FPW_AuriolAhfl;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case auriol_AHFLDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 18) <
te_delta)) {
// Found syncPrefix
parser_step = auriol_AHFLDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case auriol_AHFLDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = auriol_AHFLDecoderStepCheckDuration;
} else {
parser_step = auriol_AHFLDecoderStepReset;
}
break;
case auriol_AHFLDecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(te_last, te_short) <
te_delta) {
if (DURATION_DIFF(duration, te_short * 18) <
te_delta * 8) {
// Found syncPostfix
parser_step = auriol_AHFLDecoderStepReset;
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_auriol_ahfl_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_auriol_ahfl_remote_controller();
if (callback) {
callback(this);
}
} else if (decode_count_bit == 1) {
parser_step = auriol_AHFLDecoderStepSaveDuration;
}
decode_data = 0;
decode_count_bit = 0;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta * 2) {
subghz_protocol_blocks_add_bit(0);
parser_step = auriol_AHFLDecoderStepSaveDuration;
} else if (
DURATION_DIFF(duration, te_long * 2) <
te_delta * 4) {
subghz_protocol_blocks_add_bit(1);
parser_step = auriol_AHFLDecoderStepSaveDuration;
} else {
parser_step = auriol_AHFLDecoderStepReset;
}
} else {
parser_step = auriol_AHFLDecoderStepReset;
}
} else {
parser_step = auriol_AHFLDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 42;
void ws_protocol_auriol_ahfl_remote_controller() {
id = data >> 34;
battery_low = (data >> 33) & 1;
btn = (data >> 32) & 1;
channel = ((data >> 30) & 0x3) + 1;
if (!((data >> 29) & 1)) {
temp = (float)((data >> 18) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 18) & 0x07FF) + 1) / -10.0f;
}
humidity = (data >> 11) & 0x7F;
}
bool ws_protocol_auriol_ahfl_check() {
uint8_t type = (decode_data >> 6) & 0x0F;
if (type != AURIOL_AHFL_CONST_DATA) {
// Fail const data check
return false;
}
uint64_t payload = decode_data >> 6;
// Checksum is the last 6 bits of data
uint8_t checksum_received = decode_data & 0x3F;
uint8_t checksum_calculated = 0;
for (uint8_t i = 0; i < 9; i++) {
checksum_calculated += (payload >> (i * 4)) & 0xF;
}
return checksum_received == checksum_calculated;
}
};
#endif
+115
View File
@@ -0,0 +1,115 @@
#ifndef __FPROTO_AuriolTH_H__
#define __FPROTO_AuriolTH_H__
#include "weatherbase.hpp"
#define AURIOL_TH_CONST_DATA 0b1110
typedef enum {
auriol_THDecoderStepReset = 0,
auriol_THDecoderStepSaveDuration,
auriol_THDecoderStepCheckDuration,
} auriol_THDecoderStep;
class FProtoWeatherAuriolTh : public FProtoWeatherBase {
public:
FProtoWeatherAuriolTh() {
sensorType = FPW_AuriolTH;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case auriol_THDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 8) < te_delta)) {
// Found sync
parser_step = auriol_THDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case auriol_THDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = auriol_THDecoderStepCheckDuration;
} else {
parser_step = auriol_THDecoderStepReset;
}
break;
case auriol_THDecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(duration, te_short * 8) <
te_delta) {
// Found sync
parser_step = auriol_THDecoderStepReset;
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_auriol_th_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_auriol_th_remote_controller();
if (callback) callback(this);
parser_step = auriol_THDecoderStepCheckDuration;
}
decode_data = 0;
decode_count_bit = 0;
break;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_short * 2) <
te_delta)) {
subghz_protocol_blocks_add_bit(0);
parser_step = auriol_THDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_short * 4) <
te_delta * 2)) {
subghz_protocol_blocks_add_bit(1);
parser_step = auriol_THDecoderStepSaveDuration;
} else {
parser_step = auriol_THDecoderStepReset;
}
} else {
parser_step = auriol_THDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 37;
void ws_protocol_auriol_th_remote_controller() {
id = (data >> 31) & 0xFF;
battery_low = ((data >> 30) & 1);
channel = ((data >> 25) & 0x03) + 1;
btn = WS_NO_BTN;
if (!((data >> 23) & 1)) {
temp = (float)((data >> 13) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 13) & 0x07FF) + 1) / -10.0f;
}
humidity = (data >> 1) & 0x7F;
}
bool ws_protocol_auriol_th_check() {
uint8_t type = (decode_data >> 8) & 0x0F;
if ((type == AURIOL_TH_CONST_DATA) && ((decode_data >> 4) != 0xffffffff)) {
return true;
} else {
return false;
}
return true;
}
};
#endif
+121
View File
@@ -0,0 +1,121 @@
#ifndef __FPROTO_GTWT02_H__
#define __FPROTO_GTWT02_H__
#include "weatherbase.hpp"
#define AURIOL_AHFL_CONST_DATA 0b0100
typedef enum {
GT_WT02DecoderStepReset = 0,
GT_WT02DecoderStepSaveDuration,
GT_WT02DecoderStepCheckDuration,
} GT_WT02DecoderStep;
class FProtoWeatherGTWT02 : public FProtoWeatherBase {
public:
FProtoWeatherGTWT02() {
sensorType = FPW_GTWT02;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case GT_WT02DecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 18) <
te_delta * 8)) {
// Found syncPrefix
parser_step = GT_WT02DecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case GT_WT02DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = GT_WT02DecoderStepCheckDuration;
} else {
parser_step = GT_WT02DecoderStepReset;
}
break;
case GT_WT02DecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(te_last, te_short) <
te_delta) {
if (DURATION_DIFF(duration, te_short * 18) <
te_delta * 8) {
// Found syncPostfix
parser_step = GT_WT02DecoderStepReset;
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_gt_wt_02_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_gt_wt_02_remote_controller();
if (callback) callback(this);
} else if (decode_count_bit == 1) {
parser_step = GT_WT02DecoderStepSaveDuration;
}
decode_data = 0;
decode_count_bit = 0;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta * 2) {
subghz_protocol_blocks_add_bit(0);
parser_step = GT_WT02DecoderStepSaveDuration;
} else if (
DURATION_DIFF(duration, te_long * 2) <
te_delta * 4) {
subghz_protocol_blocks_add_bit(1);
parser_step = GT_WT02DecoderStepSaveDuration;
} else {
parser_step = GT_WT02DecoderStepReset;
}
} else {
parser_step = GT_WT02DecoderStepReset;
}
} else {
parser_step = GT_WT02DecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 37;
void ws_protocol_gt_wt_02_remote_controller() {
id = (data >> 29) & 0xFF;
battery_low = (data >> 28) & 1;
btn = (data >> 27) & 1;
channel = ((data >> 25) & 0x3) + 1;
if (!((data >> 24) & 1)) {
temp = (float)((data >> 13) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 13) & 0x07FF) + 1) / -10.0f;
}
humidity = (data >> 6) & 0x7F;
if (humidity <= 10) // actually the sensors sends 10 below working range of 20%
humidity = 0;
else if (humidity > 90) // actually the sensors sends 110 above working range of 90%
humidity = 100;
}
bool ws_protocol_gt_wt_02_check() {
if (!decode_data) return false;
uint8_t sum = (decode_data >> 5) & 0xe;
uint64_t temp_data = decode_data >> 9;
for (uint8_t i = 0; i < 7; i++) {
sum += (temp_data >> (i * 4)) & 0xF;
}
return ((uint8_t)(decode_data & 0x3F) == (sum & 0x3F));
}
};
#endif
+159
View File
@@ -0,0 +1,159 @@
#ifndef __FPROTO_GTWT03_H__
#define __FPROTO_GTWT03_H__
#include "weatherbase.hpp"
#define AURIOL_AHFL_CONST_DATA 0b0100
typedef enum {
GT_WT03DecoderStepReset = 0,
GT_WT03DecoderStepCheckPreambule,
GT_WT03DecoderStepSaveDuration,
GT_WT03DecoderStepCheckDuration,
} GT_WT03DecoderStep;
class FProtoWeatherGTWT03 : public FProtoWeatherBase {
public:
FProtoWeatherGTWT03() {
sensorType = FPW_GTWT03;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case GT_WT03DecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short * 3) <
te_delta * 2)) {
parser_step = GT_WT03DecoderStepCheckPreambule;
te_last = duration;
header_count = 0;
}
break;
case GT_WT03DecoderStepCheckPreambule:
if (level) {
te_last = duration;
} else {
if ((DURATION_DIFF(te_last, te_short * 3) < te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 3) < te_delta * 2)) {
// Found preambule
header_count++;
} else if (header_count == 4) {
if ((DURATION_DIFF(te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_long) < te_delta)) {
decode_data = 0;
decode_count_bit = 0;
subghz_protocol_blocks_add_bit(0);
parser_step = GT_WT03DecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_long) < te_delta) &&
(DURATION_DIFF(duration, te_short) < te_delta)) {
decode_data = 0;
decode_count_bit = 0;
subghz_protocol_blocks_add_bit(1);
parser_step = GT_WT03DecoderStepSaveDuration;
} else {
parser_step = GT_WT03DecoderStepReset;
}
} else {
parser_step = GT_WT03DecoderStepReset;
}
}
break;
case GT_WT03DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = GT_WT03DecoderStepCheckDuration;
} else {
parser_step = GT_WT03DecoderStepReset;
}
break;
case GT_WT03DecoderStepCheckDuration:
if (!level) {
if (((DURATION_DIFF(te_last, te_short * 3) < te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 3) < te_delta * 2))) {
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_gt_wt_03_check_crc()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_gt_wt_03_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
header_count = 1;
parser_step = GT_WT03DecoderStepCheckPreambule;
break;
} else if (
(DURATION_DIFF(te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_long) < te_delta)) {
subghz_protocol_blocks_add_bit(0);
parser_step = GT_WT03DecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_long) < te_delta) &&
(DURATION_DIFF(duration, te_short) < te_delta)) {
subghz_protocol_blocks_add_bit(1);
parser_step = GT_WT03DecoderStepSaveDuration;
} else {
parser_step = GT_WT03DecoderStepReset;
}
} else {
parser_step = GT_WT03DecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 285;
uint32_t te_long = 570;
uint32_t te_delta = 120;
uint32_t min_count_bit_for_found = 41;
void ws_protocol_gt_wt_03_remote_controller() {
id = data >> 33;
humidity = (data >> 25) & 0xFF;
if (humidity <= 10) { // actually the sensors sends 10 below working range of 20%
humidity = 0;
} else if (humidity > 95) { // actually the sensors sends 110 above working range of 90%
humidity = 100;
}
battery_low = (data >> 24) & 1;
btn = (data >> 23) & 1;
channel = ((data >> 21) & 0x03) + 1;
if (!((data >> 20) & 1)) {
temp = (float)((data >> 9) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 9) & 0x07FF) + 1) / -10.0f;
}
}
bool ws_protocol_gt_wt_03_check_crc() {
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 33),
static_cast<uint8_t>(decode_data >> 25),
static_cast<uint8_t>(decode_data >> 17),
static_cast<uint8_t>(decode_data >> 9)};
uint8_t sum = 0;
for (unsigned k = 0; k < sizeof(msg); ++k) {
uint8_t data = msg[k];
uint16_t key = 0x3100;
for (int i = 7; i >= 0; --i) {
// XOR key into sum if data bit is set
if ((data >> i) & 1) sum ^= key & 0xff;
// roll the key right
key = (key >> 1);
}
}
return ((sum ^ (uint8_t)((decode_data >> 1) & 0xFF)) == 0x2D);
}
};
#endif
+138
View File
@@ -0,0 +1,138 @@
#ifndef __FPROTO_Infactory_H__
#define __FPROTO_Infactory_H__
#include "weatherbase.hpp"
typedef enum {
InfactoryDecoderStepReset = 0,
InfactoryDecoderStepCheckPreambule,
InfactoryDecoderStepSaveDuration,
InfactoryDecoderStepCheckDuration,
} InfactoryDecoderStep;
class FProtoWeatherInfactory : public FProtoWeatherBase {
public:
FProtoWeatherInfactory() {
sensorType = FPW_INFACTORY;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case InfactoryDecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short * 2) <
te_delta * 2)) {
parser_step = InfactoryDecoderStepCheckPreambule;
te_last = duration;
header_count = 0;
}
break;
case InfactoryDecoderStepCheckPreambule:
if (level) {
te_last = duration;
} else {
if ((DURATION_DIFF(te_last, te_short * 2) <
te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 2) <
te_delta * 2)) {
// Found preambule
header_count++;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_short * 16) <
te_delta * 8)) {
// Found syncPrefix
if (header_count > 3) {
parser_step = InfactoryDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
} else {
parser_step = InfactoryDecoderStepReset;
}
}
break;
case InfactoryDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = InfactoryDecoderStepCheckDuration;
} else {
parser_step = InfactoryDecoderStepReset;
}
break;
case InfactoryDecoderStepCheckDuration:
if (!level) {
if (duration >= ((uint32_t)te_short * 30)) {
// Found syncPostfix
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_infactory_check_crc()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_infactory_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
parser_step = InfactoryDecoderStepReset;
break;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_long) <
te_delta * 2)) {
subghz_protocol_blocks_add_bit(0);
parser_step = InfactoryDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_long * 2) <
te_delta * 4)) {
subghz_protocol_blocks_add_bit(1);
parser_step = InfactoryDecoderStepSaveDuration;
} else {
parser_step = InfactoryDecoderStepReset;
}
} else {
parser_step = InfactoryDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 40;
bool ws_protocol_infactory_check_crc() {
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 32),
static_cast<uint8_t>(((decode_data >> 24) & 0x0F) | (decode_data & 0x0F) << 4),
static_cast<uint8_t>(decode_data >> 16),
static_cast<uint8_t>(decode_data >> 8),
static_cast<uint8_t>(decode_data)};
uint8_t crc =
subghz_protocol_blocks_crc4(msg, 4, 0x13, 0); // Koopmann 0x9, CCITT-4; FP-4; ITU-T G.704
crc ^= msg[4] >> 4; // last nibble is only XORed
return (crc == ((decode_data >> 28) & 0x0F));
}
void ws_protocol_infactory_remote_controller() {
id = data >> 32;
battery_low = (data >> 26) & 1;
btn = WS_NO_BTN;
temp =
locale_fahrenheit_to_celsius(((float)((data >> 12) & 0x0FFF) - 900.0f) / 10.0f);
humidity =
(((data >> 8) & 0x0F) * 10) + ((data >> 4) & 0x0F); // BCD, 'A0'=100%rH
channel = data & 0x03;
}
};
#endif
+163
View File
@@ -0,0 +1,163 @@
#ifndef __FPROTO_LACROSSE_TX_H__
#define __FPROTO_LACROSSE_TX_H__
#include "weatherbase.hpp"
#define LACROSSE_TX_GAP 1000
#define LACROSSE_TX_BIT_SIZE 44
#define LACROSSE_TX_SUNC_PATTERN 0x0A000000000
#define LACROSSE_TX_SUNC_MASK 0x0F000000000
#define LACROSSE_TX_MSG_TYPE_TEMP 0x00
#define LACROSSE_TX_MSG_TYPE_HUM 0x0E
typedef enum {
LaCrosse_TXDecoderStepReset = 0,
LaCrosse_TXDecoderStepCheckPreambule,
LaCrosse_TXDecoderStepSaveDuration,
LaCrosse_TXDecoderStepCheckDuration,
} LaCrosse_TXDecoderStep;
class FProtoWeatherLaCrosseTx : public FProtoWeatherBase {
public:
FProtoWeatherLaCrosseTx() {
sensorType = FPW_LACROSSETX;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case LaCrosse_TXDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, LACROSSE_TX_GAP) < te_delta * 2)) {
parser_step = LaCrosse_TXDecoderStepCheckPreambule;
header_count = 0;
}
break;
case LaCrosse_TXDecoderStepCheckPreambule:
if (level) {
if ((DURATION_DIFF(duration, te_short) < te_delta) &&
(header_count > 1)) {
parser_step = LaCrosse_TXDecoderStepCheckDuration;
decode_data = 0;
decode_count_bit = 0;
te_last = duration;
} else if (duration > (te_long * 2)) {
parser_step = LaCrosse_TXDecoderStepReset;
}
} else {
if (DURATION_DIFF(duration, LACROSSE_TX_GAP) < te_delta * 2) {
te_last = duration;
header_count++;
} else {
parser_step = LaCrosse_TXDecoderStepReset;
}
}
break;
case LaCrosse_TXDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = LaCrosse_TXDecoderStepCheckDuration;
} else {
parser_step = LaCrosse_TXDecoderStepReset;
}
break;
case LaCrosse_TXDecoderStepCheckDuration:
if (!level) {
if (duration > LACROSSE_TX_GAP * 3) {
if (DURATION_DIFF(te_last, te_short) < te_delta) {
subghz_protocol_blocks_add_bit(1);
parser_step = LaCrosse_TXDecoderStepSaveDuration;
} else if (
DURATION_DIFF(te_last, te_long) < te_delta) {
subghz_protocol_blocks_add_bit(0);
parser_step = LaCrosse_TXDecoderStepSaveDuration;
}
if ((decode_data & LACROSSE_TX_SUNC_MASK) ==
LACROSSE_TX_SUNC_PATTERN) {
if (ws_protocol_lacrosse_tx_check_crc()) {
data = decode_data;
data_count_bit = LACROSSE_TX_BIT_SIZE;
ws_protocol_lacrosse_tx_remote_controller();
if (callback) callback(this);
}
}
decode_data = 0;
decode_count_bit = 0;
header_count = 0;
parser_step = LaCrosse_TXDecoderStepReset;
break;
} else if (
(DURATION_DIFF(te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, LACROSSE_TX_GAP) < te_delta * 2)) {
subghz_protocol_blocks_add_bit(1);
parser_step = LaCrosse_TXDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_long) < te_delta) &&
(DURATION_DIFF(duration, LACROSSE_TX_GAP) < te_delta * 2)) {
subghz_protocol_blocks_add_bit(0);
parser_step = LaCrosse_TXDecoderStepSaveDuration;
} else {
parser_step = LaCrosse_TXDecoderStepReset;
}
} else {
parser_step = LaCrosse_TXDecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 550;
uint32_t te_long = 1300;
uint32_t te_delta = 120;
uint32_t min_count_bit_for_found = 40;
void ws_protocol_lacrosse_tx_remote_controller() {
uint8_t msg_type = (data >> 32) & 0x0F;
id = (((data >> 28) & 0x0F) << 3) | (((data >> 24) & 0x0F) >> 1);
float msg_value = (float)((data >> 20) & 0x0F) * 10.0f +
(float)((data >> 16) & 0x0F) +
(float)((data >> 12) & 0x0F) * 0.1f;
if (msg_type == LACROSSE_TX_MSG_TYPE_TEMP) { //-V1051
temp = msg_value - 50.0f;
humidity = WS_NO_HUMIDITY;
} else if (msg_type == LACROSSE_TX_MSG_TYPE_HUM) {
// ToDo for verification, records are needed with sensors maintaining temperature and temperature for this standard
humidity = (uint8_t)msg_value;
} else {
// furi_crash("WS: WSProtocolLaCrosse_TX incorrect msg_type.");
}
btn = WS_NO_BTN;
battery_low = WS_NO_BATT;
channel = WS_NO_CHANNEL;
}
bool ws_protocol_lacrosse_tx_check_crc() {
if (!decode_data) return false;
uint8_t msg[] = {
static_cast<uint8_t>((decode_data >> 36) & 0x0F),
static_cast<uint8_t>((decode_data >> 32) & 0x0F),
static_cast<uint8_t>((decode_data >> 28) & 0x0F),
static_cast<uint8_t>((decode_data >> 24) & 0x0F),
static_cast<uint8_t>((decode_data >> 20) & 0x0F),
static_cast<uint8_t>((decode_data >> 16) & 0x0F),
static_cast<uint8_t>((decode_data >> 12) & 0x0F),
static_cast<uint8_t>((decode_data >> 8) & 0x0F),
static_cast<uint8_t>((decode_data >> 4) & 0x0F)};
uint8_t crc = subghz_protocol_blocks_add_bytes(msg, 9);
return ((crc & 0x0F) == (decode_data & 0x0F));
}
};
#endif
@@ -0,0 +1,135 @@
#ifndef __FPROTO_LACROSSE_TX141thbv2_H__
#define __FPROTO_LACROSSE_TX141thbv2_H__
#include "weatherbase.hpp"
#define LACROSSE_TX141TH_BV2_BIT_COUNT 41
typedef enum {
LaCrosse_TX141THBv2DecoderStepReset = 0,
LaCrosse_TX141THBv2DecoderStepCheckPreambule,
LaCrosse_TX141THBv2DecoderStepSaveDuration,
LaCrosse_TX141THBv2DecoderStepCheckDuration,
} LaCrosse_TX141THBv2DecoderStep;
class FProtoWeatherLaCrosseTx141thbv2 : public FProtoWeatherBase {
public:
FProtoWeatherLaCrosseTx141thbv2() {
sensorType = FPW_LACROSSETX141thbv2;
}
void feed(bool level, uint32_t duration) {
switch (parser_step) {
case LaCrosse_TX141THBv2DecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short * 4) < te_delta * 2)) {
parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
te_last = duration;
header_count = 0;
}
break;
case LaCrosse_TX141THBv2DecoderStepCheckPreambule:
if (level) {
te_last = duration;
} else {
if ((DURATION_DIFF(te_last, te_short * 4) < te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 4) < te_delta * 2)) {
// Found preambule
header_count++;
} else if (header_count == 4) {
if (ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(te_last, duration)) {
decode_data = decode_data & 1;
decode_count_bit = 1;
parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
} else {
parser_step = LaCrosse_TX141THBv2DecoderStepReset;
}
} else {
parser_step = LaCrosse_TX141THBv2DecoderStepReset;
}
}
break;
case LaCrosse_TX141THBv2DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = LaCrosse_TX141THBv2DecoderStepCheckDuration;
} else {
parser_step = LaCrosse_TX141THBv2DecoderStepReset;
}
break;
case LaCrosse_TX141THBv2DecoderStepCheckDuration:
if (!level) {
if (((DURATION_DIFF(te_last, te_short * 3) < te_delta * 2) &&
(DURATION_DIFF(duration, te_short * 4) < te_delta * 2))) {
if ((decode_count_bit == min_count_bit_for_found) ||
(decode_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT)) {
if (ws_protocol_lacrosse_tx141thbv2_check_crc()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_lacrosse_tx141thbv2_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 0;
header_count = 1;
parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
break;
}
} else if (ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(te_last, duration)) {
parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
} else {
parser_step = LaCrosse_TX141THBv2DecoderStepReset;
}
} else {
parser_step = LaCrosse_TX141THBv2DecoderStepReset;
}
break;
}
}
protected:
uint32_t te_short = 208;
uint32_t te_long = 417;
uint32_t te_delta = 120;
uint32_t min_count_bit_for_found = 40;
bool ws_protocol_lacrosse_tx141thbv2_check_crc() {
if (!decode_data) return false;
uint64_t data = decode_data;
if (decode_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT) {
data >>= 1;
}
uint8_t msg[] = {static_cast<uint8_t>(data >> 32), static_cast<uint8_t>(data >> 24), static_cast<uint8_t>(data >> 16), static_cast<uint8_t>(data >> 8)};
uint8_t crc = subghz_protocol_blocks_lfsr_digest8_reflect(msg, 4, 0x31, 0xF4);
return (crc == (data & 0xFF));
}
void ws_protocol_lacrosse_tx141thbv2_remote_controller() {
if (data_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT) {
data >>= 1;
}
id = data >> 32;
battery_low = (data >> 31) & 1;
btn = (data >> 30) & 1;
channel = ((data >> 28) & 0x03) + 1;
temp = ((float)((data >> 16) & 0x0FFF) - 500.0f) / 10.0f;
humidity = (data >> 8) & 0xFF;
}
bool ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(uint32_t te_last, uint32_t te_current) {
bool ret = false;
if (DURATION_DIFF(te_last + te_current, te_short + te_long) < te_delta * 2) {
if (te_last > te_current) {
subghz_protocol_blocks_add_bit(1);
} else {
subghz_protocol_blocks_add_bit(0);
}
ret = true;
}
return ret;
}
};
#endif
+121
View File
@@ -0,0 +1,121 @@
#ifndef __FPROTO_NEXUSTH_H__
#define __FPROTO_NEXUSTH_H__
#include "weatherbase.hpp"
// For the state machine
typedef enum {
Nexus_THDecoderStepReset = 0,
Nexus_THDecoderStepSaveDuration,
Nexus_THDecoderStepCheckDuration,
} Nexus_THDecoderStep;
// we will look for this value
#define NEXUS_TH_CONST_DATA 0b1111
class FProtoWeatherNexusTH : public FProtoWeatherBase {
public:
FProtoWeatherNexusTH() {
// must set it's value from the "weathertypes.hpp". getWeatherSensorTypeName() will work with this.
sensorType = FPW_NexusTH;
}
// Here we will got a level and duration. eg HIGH (true) for 500. This function must be as fast as possible, to keep the core happy.
// From this function we will call the "callback", that is responsible for transmitting the parsed data to the ui. Before calling it, we must populate the base class's variables, like temp, humidity, ...
void feed(bool level, uint32_t duration) override {
switch (parser_step) {
case Nexus_THDecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 8) <
te_delta * 4)) {
// Found sync
parser_step = Nexus_THDecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case Nexus_THDecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = Nexus_THDecoderStepCheckDuration;
} else {
parser_step = Nexus_THDecoderStepReset;
}
break;
case Nexus_THDecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(duration, te_short * 8) < te_delta * 4) {
// Found sync
parser_step = Nexus_THDecoderStepReset;
if ((decode_count_bit == min_count_bit_for_found) &&
ws_protocol_nexus_th_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_nexus_th_remote_controller();
if (callback) callback((FProtoWeatherBase*)this);
parser_step = Nexus_THDecoderStepCheckDuration;
}
decode_data = 0;
decode_count_bit = 0;
break;
} else if ((DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_short * 2) < te_delta * 2)) {
subghz_protocol_blocks_add_bit(0);
parser_step = Nexus_THDecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_short * 4) < te_delta * 4)) {
subghz_protocol_blocks_add_bit(1);
parser_step = Nexus_THDecoderStepSaveDuration;
} else {
parser_step = Nexus_THDecoderStepReset;
}
} else {
parser_step = Nexus_THDecoderStepReset;
}
break;
}
}
protected:
// timing values
uint32_t te_short = 490;
uint32_t te_long = 1980;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 36;
// sanity check
bool ws_protocol_nexus_th_check() {
uint8_t type = (decode_data >> 8) & 0x0F;
if ((type == NEXUS_TH_CONST_DATA) && ((decode_data >> 4) != 0xffffffff)) {
return true;
} else {
return false;
}
return true;
}
// fill the base class's variables before calling the callback
void ws_protocol_nexus_th_remote_controller() {
id = (data >> 28) & 0xFF;
battery_low = !((data >> 27) & 1);
channel = ((data >> 24) & 0x03) + 1;
btn = WS_NO_BTN;
if (!((data >> 23) & 1)) {
temp = (float)((data >> 12) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 12) & 0x07FF) + 1) / -10.0f;
}
humidity = data & 0xFF;
if (humidity > 95)
humidity = 95;
else if (humidity < 20)
humidity = 20;
}
};
#endif
+248
View File
@@ -0,0 +1,248 @@
#ifndef __FPROTO_OREGON2_H__
#define __FPROTO_OREGON2_H__
#include "weatherbase.hpp"
#define OREGON2_PREAMBLE_BITS 19
#define OREGON2_PREAMBLE_MASK 0b1111111111111111111
#define OREGON2_SENSOR_ID(d) (((d) >> 16) & 0xFFFF)
#define OREGON2_CHECKSUM_BITS 8
// 15 ones + 0101 (inverted A)
#define OREGON2_PREAMBLE 0b1111111111111110101
// bit indicating the low battery
#define OREGON2_FLAG_BAT_LOW 0x4
/// Documentation for Oregon Scientific protocols can be found here:
/// http://wmrx00.sourceforge.net/Arduino/OregonScientific-RF-Protocols.pdf
// Sensors ID
#define ID_THGR122N 0x1d20
#define ID_THGR968 0x1d30
#define ID_BTHR918 0x5d50
#define ID_BHTR968 0x5d60
#define ID_RGR968 0x2d10
#define ID_THR228N 0xec40
#define ID_THN132N 0xec40 // same as THR228N but different packet size
#define ID_RTGN318 0x0cc3 // warning: id is from 0x0cc3 and 0xfcc3
#define ID_RTGN129 0x0cc3 // same as RTGN318 but different packet size
#define ID_THGR810 0xf824 // This might be ID_THGR81, but what's true is lost in (git) history
#define ID_THGR810a 0xf8b4 // unconfirmed version
#define ID_THN802 0xc844
#define ID_PCR800 0x2914
#define ID_PCR800a 0x2d14 // Different PCR800 ID - AU version I think
#define ID_WGR800 0x1984
#define ID_WGR800a 0x1994 // unconfirmed version
#define ID_WGR968 0x3d00
#define ID_UV800 0xd874
#define ID_THN129 0xcc43 // THN129 Temp only
#define ID_RTHN129 0x0cd3 // RTHN129 Temp, clock sensors
#define ID_RTHN129_1 0x9cd3
#define ID_RTHN129_2 0xacd3
#define ID_RTHN129_3 0xbcd3
#define ID_RTHN129_4 0xccd3
#define ID_RTHN129_5 0xdcd3
#define ID_BTHGN129 0x5d53 // Baro, Temp, Hygro sensor
#define ID_UVR128 0xec70
#define ID_THGR328N 0xcc23 // Temp & Hygro sensor similar to THR228N with 5 channel instead of 3
#define ID_RTGR328N_1 0xdcc3 // RTGR328N_[1-5] RFclock(date &time)&Temp&Hygro sensor
#define ID_RTGR328N_2 0xccc3
#define ID_RTGR328N_3 0xbcc3
#define ID_RTGR328N_4 0xacc3
#define ID_RTGR328N_5 0x9cc3
#define ID_RTGR328N_6 0x8ce3 // RTGR328N_6&7 RFclock(date &time)&Temp&Hygro sensor like THGR328N
#define ID_RTGR328N_7 0x8ae3
typedef enum {
Oregon2DecoderStepReset = 0,
Oregon2DecoderStepFoundPreamble,
Oregon2DecoderStepVarData,
} Oregon2DecoderStep;
class FProtoWeatherOregon2 : public FProtoWeatherBase {
public:
FProtoWeatherOregon2() {
sensorType = FPW_OREGON2;
}
void feed(bool level, uint32_t duration) override {
// oregon v2.1 signal is inverted
ManchesterEvent event = level_and_duration_to_event(!level, duration);
bool data;
// low-level bit sequence decoding
if (event == ManchesterEventReset) {
parser_step = Oregon2DecoderStepReset;
have_bit = false;
decode_data = 0UL;
decode_count_bit = 0;
}
if (manchester_advance(manchester_saved_state, event, &manchester_saved_state, &data)) {
if (have_bit) {
if (!prev_bit && data) {
subghz_protocol_blocks_add_bit(1);
} else if (prev_bit && !data) {
subghz_protocol_blocks_add_bit(0);
} else {
ws_protocol_decoder_oregon2_reset();
}
have_bit = false;
} else {
prev_bit = data;
have_bit = true;
}
}
switch (parser_step) {
case Oregon2DecoderStepReset:
// waiting for fixed oregon2 preamble
if (decode_count_bit >= OREGON2_PREAMBLE_BITS &&
((decode_data & OREGON2_PREAMBLE_MASK) == OREGON2_PREAMBLE)) {
parser_step = Oregon2DecoderStepFoundPreamble;
decode_count_bit = 0;
decode_data = 0UL;
}
break;
case Oregon2DecoderStepFoundPreamble:
// waiting for fixed oregon2 data
if (decode_count_bit == 32) {
data = decode_data;
data_count_bit = decode_count_bit;
decode_data = 0UL;
decode_count_bit = 0;
// reverse nibbles in decoded data
data = (data & 0x55555555) << 1 |
(data & 0xAAAAAAAA) >> 1;
data = (data & 0x33333333) << 2 |
(data & 0xCCCCCCCC) >> 2;
ws_oregon2_decode_const_data();
var_bits =
oregon2_sensor_id_var_bits(OREGON2_SENSOR_ID(data));
if (!var_bits) {
// sensor is not supported, stop decoding, but showing the decoded fixed part
parser_step = Oregon2DecoderStepReset;
if (callback) callback(this);
} else {
parser_step = Oregon2DecoderStepVarData;
}
}
break;
case Oregon2DecoderStepVarData:
// waiting for variable (sensor-specific data)
if (decode_count_bit == (uint32_t)var_bits + OREGON2_CHECKSUM_BITS) {
var_data = decode_data & 0xFFFFFFFF;
// reverse nibbles in var data
var_data = (var_data & 0x55555555) << 1 |
(var_data & 0xAAAAAAAA) >> 1;
var_data = (var_data & 0x33333333) << 2 |
(var_data & 0xCCCCCCCC) >> 2;
ws_oregon2_decode_var_data(OREGON2_SENSOR_ID(data), var_data >> OREGON2_CHECKSUM_BITS);
parser_step = Oregon2DecoderStepReset;
if (callback) callback(this);
}
break;
}
}
protected:
// timing values
uint32_t te_short = 500;
uint32_t te_long = 1000;
uint32_t te_delta = 200;
uint32_t min_count_bit_for_found = 32;
bool have_bit = false;
bool prev_bit = 0;
uint8_t var_bits{0};
uint32_t var_data{0};
void ws_protocol_decoder_oregon2_reset() {
parser_step = Oregon2DecoderStepReset;
decode_data = 0UL;
decode_count_bit = 0;
manchester_advance(manchester_saved_state, ManchesterEventReset, &manchester_saved_state, NULL);
have_bit = false;
var_data = 0;
var_bits = 0;
}
ManchesterEvent level_and_duration_to_event(bool level, uint32_t duration) {
bool is_long = false;
if (DURATION_DIFF(duration, te_long) < te_delta) {
is_long = true;
} else if (DURATION_DIFF(duration, te_short) < te_delta) {
is_long = false;
} else {
return ManchesterEventReset;
}
if (level)
return is_long ? ManchesterEventLongHigh : ManchesterEventShortHigh;
else
return is_long ? ManchesterEventLongLow : ManchesterEventShortLow;
}
uint8_t oregon2_sensor_id_var_bits(uint16_t sensor_id) {
switch (sensor_id) {
case ID_THR228N:
case ID_RTHN129_1:
case ID_RTHN129_2:
case ID_RTHN129_3:
case ID_RTHN129_4:
case ID_RTHN129_5:
return 16;
case ID_THGR122N:
return 24;
default:
return 0;
}
}
void ws_oregon2_decode_const_data() {
id = OREGON2_SENSOR_ID(data);
uint8_t ch_bits = (data >> 12) & 0xF;
channel = 1;
while (ch_bits > 1) {
channel++;
ch_bits >>= 1;
}
battery_low = (data & OREGON2_FLAG_BAT_LOW) ? 1 : 0;
}
uint16_t bcd_decode_short(uint32_t data) {
return (data & 0xF) * 10 + ((data >> 4) & 0xF);
}
float ws_oregon2_decode_temp(uint32_t data) {
int32_t temp_val;
temp_val = bcd_decode_short(data >> 4);
temp_val *= 10;
temp_val += (data >> 12) & 0xF;
if (data & 0xF) temp_val = -temp_val;
return (float)temp_val / 10.0;
}
void ws_oregon2_decode_var_data(uint16_t sensor_id, uint32_t data) {
switch (sensor_id) {
case ID_THR228N:
case ID_RTHN129_1:
case ID_RTHN129_2:
case ID_RTHN129_3:
case ID_RTHN129_4:
case ID_RTHN129_5:
temp = ws_oregon2_decode_temp(data);
humidity = WS_NO_HUMIDITY;
return;
case ID_THGR122N:
humidity = bcd_decode_short(data);
temp = ws_oregon2_decode_temp(data >> 8);
return;
default:
break;
}
}
};
#endif
+173
View File
@@ -0,0 +1,173 @@
#ifndef __FPROTO_OREGON3_H__
#define __FPROTO_OREGON3_H__
#include "weatherbase.hpp"
#define OREGON3_PREAMBLE_BITS 28
#define OREGON3_PREAMBLE_MASK 0b1111111111111111111111111111
// 24 ones + 0101 (inverted A)
#define OREGON3_PREAMBLE 0b1111111111111111111111110101
// Fixed part contains:
// - Sensor type: 16 bits
// - Channel: 4 bits
// - ID (changes when batteries are changed): 8 bits
// - Battery status: 4 bits
#define OREGON3_FIXED_PART_BITS (16 + 4 + 8 + 4)
#define OREGON3_SENSOR_ID(d) (((d) >> 16) & 0xFFFF)
#define OREGON3_CHECKSUM_BITS 8
// bit indicating the low battery
#define OREGON3_FLAG_BAT_LOW 0x4
/// Documentation for Oregon Scientific protocols can be found here:
/// https://www.osengr.org/Articles/OS-RF-Protocols-IV.pdf
// Sensors ID
#define ID_THGR221 0xf824
typedef enum {
Oregon3DecoderStepReset = 0,
Oregon3DecoderStepFoundPreamble,
Oregon3DecoderStepVarData,
} Oregon3DecoderStep;
class FProtoWeatherOregon3 : public FProtoWeatherBase {
public:
FProtoWeatherOregon3() {
sensorType = FPW_OREGON3;
}
void feed(bool level, uint32_t duration) override {
ManchesterEvent event = level_and_duration_to_event(!level, duration);
// low-level bit sequence decoding
if (event == ManchesterEventReset) {
parser_step = Oregon3DecoderStepReset;
prev_bit = false;
decode_data = 0UL;
decode_count_bit = 0;
}
if (manchester_advance(
manchester_saved_state, event, &manchester_saved_state, &prev_bit)) {
subghz_protocol_blocks_add_bit(prev_bit);
}
switch (parser_step) {
case Oregon3DecoderStepReset:
// waiting for fixed oregon3 preamble
if (decode_count_bit >= OREGON3_PREAMBLE_BITS &&
((decode_data & OREGON3_PREAMBLE_MASK) == OREGON3_PREAMBLE)) {
parser_step = Oregon3DecoderStepFoundPreamble;
decode_count_bit = 0;
decode_data = 0UL;
}
break;
case Oregon3DecoderStepFoundPreamble:
// waiting for fixed oregon3 data
if (decode_count_bit == OREGON3_FIXED_PART_BITS) {
data = decode_data;
data_count_bit = decode_count_bit;
decode_data = 0UL;
decode_count_bit = 0;
// reverse nibbles in decoded data as oregon v3.0 is LSB first
data = (data & 0x55555555) << 1 |
(data & 0xAAAAAAAA) >> 1;
data = (data & 0x33333333) << 2 |
(data & 0xCCCCCCCC) >> 2;
ws_oregon3_decode_const_data();
var_bits =
oregon3_sensor_id_var_bits(OREGON3_SENSOR_ID(data));
if (!var_bits) {
// sensor is not supported, stop decoding
parser_step = Oregon3DecoderStepReset;
} else {
parser_step = Oregon3DecoderStepVarData;
}
}
break;
case Oregon3DecoderStepVarData:
// waiting for variable (sensor-specific data)
if (decode_count_bit == (uint32_t)var_bits + OREGON3_CHECKSUM_BITS) {
var_data = decode_data & 0xFFFFFFFFFFFFFFFF;
// reverse nibbles in var data
var_data = (var_data & 0x5555555555555555) << 1 |
(var_data & 0xAAAAAAAAAAAAAAAA) >> 1;
var_data = (var_data & 0x3333333333333333) << 2 |
(var_data & 0xCCCCCCCCCCCCCCCC) >> 2;
ws_oregon3_decode_var_data(OREGON3_SENSOR_ID(data), var_data >> OREGON3_CHECKSUM_BITS);
parser_step = Oregon3DecoderStepReset;
if (callback) callback(this);
}
break;
}
}
protected:
// timing values
uint32_t te_short = 500;
uint32_t te_long = 1100;
uint32_t te_delta = 300;
uint32_t min_count_bit_for_found = 32;
bool prev_bit = false;
uint8_t var_bits{0};
uint64_t var_data{0};
ManchesterEvent level_and_duration_to_event(bool level, uint32_t duration) {
bool is_long = false;
if (DURATION_DIFF(duration, te_long) < te_delta) {
is_long = true;
} else if (DURATION_DIFF(duration, te_short) < te_delta) {
is_long = false;
} else {
return ManchesterEventReset;
}
if (level)
return is_long ? ManchesterEventLongHigh : ManchesterEventShortHigh;
else
return is_long ? ManchesterEventLongLow : ManchesterEventShortLow;
}
uint8_t oregon3_sensor_id_var_bits(uint16_t sensor_id) {
switch (sensor_id) {
case ID_THGR221:
// nibbles: temp + hum + '0'
return (4 + 2 + 1) * 4;
default:
return 0;
}
}
void ws_oregon3_decode_const_data() {
id = OREGON3_SENSOR_ID(data);
channel = (data >> 12) & 0xF;
battery_low = (data & OREGON3_FLAG_BAT_LOW) ? 1 : 0;
}
uint16_t ws_oregon3_bcd_decode_short(uint32_t data) {
return (data & 0xF) * 10 + ((data >> 4) & 0xF);
}
float ws_oregon3_decode_temp(uint32_t data) {
int32_t temp_val;
temp_val = ws_oregon3_bcd_decode_short(data >> 4);
temp_val *= 10;
temp_val += (data >> 12) & 0xF;
if (data & 0xF) temp_val = -temp_val;
return (float)temp_val / 10.0;
}
void ws_oregon3_decode_var_data(uint16_t sensor_id, uint32_t data) {
switch (sensor_id) {
case ID_THGR221:
default:
humidity = ws_oregon3_bcd_decode_short(data >> 4);
temp = ws_oregon3_decode_temp(data >> 12);
break;
}
}
};
#endif
+176
View File
@@ -0,0 +1,176 @@
#ifndef __FPROTO_OREGONv1_H__
#define __FPROTO_OREGONv1_H__
#include "weatherbase.hpp"
typedef enum {
Oregon_V1DecoderStepReset = 0,
Oregon_V1DecoderStepFoundPreamble,
Oregon_V1DecoderStepParse,
} Oregon_V1DecoderStep;
#define OREGON_V1_HEADER_OK 0xFF
class FProtoWeatherOregonV1 : public FProtoWeatherBase {
public:
FProtoWeatherOregonV1() {
sensorType = FPW_OREGONv1;
}
void feed(bool level, uint32_t duration) override {
ManchesterEvent event = ManchesterEventReset;
switch (parser_step) {
case Oregon_V1DecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short) <
te_delta)) {
parser_step = Oregon_V1DecoderStepFoundPreamble;
te_last = duration;
header_count = 0;
}
break;
case Oregon_V1DecoderStepFoundPreamble:
if (level) {
// keep high levels, if they suit our durations
if ((DURATION_DIFF(duration, te_short) <
te_delta) ||
(DURATION_DIFF(duration, te_short * 4) <
te_delta)) {
te_last = duration;
} else {
parser_step = Oregon_V1DecoderStepReset;
}
} else if (
// checking low levels
(DURATION_DIFF(duration, te_short) <
te_delta) &&
(DURATION_DIFF(te_last, te_short) <
te_delta)) {
// Found header
header_count++;
} else if (
(DURATION_DIFF(duration, te_short * 3) <
te_delta) &&
(DURATION_DIFF(te_last, te_short) <
te_delta)) {
// check header
if (header_count > 7) {
header_count = OREGON_V1_HEADER_OK;
}
} else if (
(header_count == OREGON_V1_HEADER_OK) &&
(DURATION_DIFF(te_last, te_short * 4) <
te_delta)) {
// found all the necessary patterns
decode_data = 0;
decode_count_bit = 1;
manchester_advance(
manchester_saved_state,
ManchesterEventReset,
&manchester_saved_state,
NULL);
parser_step = Oregon_V1DecoderStepParse;
if (duration < te_short * 4) {
first_bit = 1;
} else {
first_bit = 0;
}
} else {
parser_step = Oregon_V1DecoderStepReset;
}
break;
case Oregon_V1DecoderStepParse:
if (level) {
if (DURATION_DIFF(duration, te_short) <
te_delta) {
event = ManchesterEventShortHigh;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta) {
event = ManchesterEventLongHigh;
} else {
parser_step = Oregon_V1DecoderStepReset;
}
} else {
if (DURATION_DIFF(duration, te_short) <
te_delta) {
event = ManchesterEventShortLow;
} else if (
DURATION_DIFF(duration, te_long) <
te_delta) {
event = ManchesterEventLongLow;
} else if (duration >= ((uint32_t)te_long * 2)) {
if (decode_count_bit ==
min_count_bit_for_found) {
if (first_bit) {
decode_data = ~decode_data | (1 << 31);
}
if (ws_protocol_oregon_v1_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_oregon_v1_remote_controller();
if (callback) callback(this);
}
}
decode_data = 0;
decode_count_bit = 0;
manchester_advance(
manchester_saved_state,
ManchesterEventReset,
&manchester_saved_state,
NULL);
} else {
parser_step = Oregon_V1DecoderStepReset;
}
}
if (event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
manchester_saved_state, event, &manchester_saved_state, &data);
if (data_ok) {
decode_data = (decode_data << 1) | !data;
decode_count_bit++;
}
}
break;
}
}
protected:
// timing values
uint32_t te_short = 1465;
uint32_t te_long = 2930;
uint32_t te_delta = 350;
uint32_t min_count_bit_for_found = 32;
uint8_t first_bit{0};
bool ws_protocol_oregon_v1_check() {
if (!decode_data) return false;
uint64_t data = subghz_protocol_blocks_reverse_key(decode_data, 32);
uint16_t crc = (data & 0xff) + ((data >> 8) & 0xff) + ((data >> 16) & 0xff);
crc = (crc & 0xff) + ((crc >> 8) & 0xff);
return (crc == ((data >> 24) & 0xFF));
}
void ws_protocol_oregon_v1_remote_controller() {
uint64_t data2 = subghz_protocol_blocks_reverse_key(data, 32);
id = data2 & 0xFF;
channel = ((data2 >> 6) & 0x03) + 1;
float temp_raw =
((data2 >> 8) & 0x0F) * 0.1f + ((data2 >> 12) & 0x0F) + ((data2 >> 16) & 0x0F) * 10.0f;
if (!((data2 >> 21) & 1)) {
temp = temp_raw;
} else {
temp = -temp_raw;
}
battery_low = !((data2 >> 23) & 1ULL);
btn = WS_NO_BTN;
humidity = WS_NO_HUMIDITY;
}
};
#endif
@@ -0,0 +1,110 @@
#ifndef __FPROTO_THERMOPROTX4_H__
#define __FPROTO_THERMOPROTX4_H__
#include "weatherbase.hpp"
#define THERMO_PRO_TX4_TYPE_1 0b1001
#define THERMO_PRO_TX4_TYPE_2 0b0110
typedef enum {
ThermoPRO_TX4DecoderStepReset = 0,
ThermoPRO_TX4DecoderStepSaveDuration,
ThermoPRO_TX4DecoderStepCheckDuration,
} ThermoPRO_TX4DecoderStep;
class FProtoWeatherThermoProTx4 : public FProtoWeatherBase {
public:
FProtoWeatherThermoProTx4() {
sensorType = FPW_THERMOPROTX4;
}
void feed(bool level, uint32_t duration) override {
switch (parser_step) {
case ThermoPRO_TX4DecoderStepReset:
if ((!level) && (DURATION_DIFF(duration, te_short * 18) < te_delta * 10)) {
// Found sync
parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 0;
}
break;
case ThermoPRO_TX4DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
} else {
parser_step = ThermoPRO_TX4DecoderStepReset;
}
break;
case ThermoPRO_TX4DecoderStepCheckDuration:
if (!level) {
if (DURATION_DIFF(duration, te_short * 18) < te_delta * 10) {
// Found sync
parser_step = ThermoPRO_TX4DecoderStepReset;
if ((decode_count_bit == min_count_bit_for_found) &&
ws_protocol_thermopro_tx4_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_thermopro_tx4_remote_controller();
if (callback) callback(this);
parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
}
decode_data = 0;
decode_count_bit = 0;
break;
} else if (
(DURATION_DIFF(
te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_long) < te_delta * 2)) {
subghz_protocol_blocks_add_bit(0);
parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
} else if (
(DURATION_DIFF(
te_last, te_short) < te_delta) &&
(DURATION_DIFF(duration, te_long * 2) < te_delta * 4)) {
subghz_protocol_blocks_add_bit(1);
parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
} else {
parser_step = ThermoPRO_TX4DecoderStepReset;
}
} else {
parser_step = ThermoPRO_TX4DecoderStepReset;
}
break;
}
}
protected:
// timing values
uint32_t te_short = 500;
uint32_t te_long = 2000;
uint32_t te_delta = 150;
uint32_t min_count_bit_for_found = 37;
bool ws_protocol_thermopro_tx4_check() {
uint8_t type = decode_data >> 33;
if ((type == THERMO_PRO_TX4_TYPE_1) || (type == THERMO_PRO_TX4_TYPE_2)) {
return true;
} else {
return false;
}
}
void ws_protocol_thermopro_tx4_remote_controller() {
id = (data >> 25) & 0xFF;
battery_low = (data >> 24) & 1;
btn = (data >> 23) & 1;
channel = ((data >> 21) & 0x03) + 1;
if (!((data >> 20) & 1)) {
temp = (float)((data >> 9) & 0x07FF) / 10.0f;
} else {
temp = (float)((~(data >> 9) & 0x07FF) + 1) / -10.0f;
}
humidity = (data >> 1) & 0xFF;
}
};
#endif
+141
View File
@@ -0,0 +1,141 @@
#ifndef __FPROTO_TX8300_H__
#define __FPROTO_TX8300_H__
#include "weatherbase.hpp"
typedef enum {
TX_8300DecoderStepReset = 0,
TX_8300DecoderStepCheckPreambule,
TX_8300DecoderStepSaveDuration,
TX_8300DecoderStepCheckDuration,
} TX_8300DecoderStep;
#define TX_8300_PACKAGE_SIZE 32
class FProtoWeatherTX8300 : public FProtoWeatherBase {
public:
FProtoWeatherTX8300() {
sensorType = FPW_TX_8300;
}
void feed(bool level, uint32_t duration) override {
switch (parser_step) {
case TX_8300DecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short * 2) < te_delta)) {
parser_step = TX_8300DecoderStepCheckPreambule;
}
break;
case TX_8300DecoderStepCheckPreambule:
if ((!level) && ((DURATION_DIFF(duration, te_short * 2) < te_delta) ||
(DURATION_DIFF(duration, te_short * 3) < te_delta))) {
parser_step = TX_8300DecoderStepSaveDuration;
decode_data = 0;
decode_count_bit = 1;
package_1 = 0;
package_2 = 0;
} else {
parser_step = TX_8300DecoderStepReset;
}
break;
case TX_8300DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = TX_8300DecoderStepCheckDuration;
} else {
parser_step = TX_8300DecoderStepReset;
}
break;
case TX_8300DecoderStepCheckDuration:
if (!level) {
if (duration >= ((uint32_t)te_short * 5)) {
// Found syncPostfix
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_tx_8300_check_crc()) {
data = package_1;
data_count_bit = decode_count_bit;
ws_protocol_tx_8300_remote_controller();
if (callback) callback(this);
}
decode_data = 0;
decode_count_bit = 1;
parser_step = TX_8300DecoderStepReset;
break;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_long) <
te_delta * 2)) {
subghz_protocol_blocks_add_bit(1);
parser_step = TX_8300DecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_short) <
te_delta)) {
subghz_protocol_blocks_add_bit(0);
parser_step = TX_8300DecoderStepSaveDuration;
} else {
parser_step = TX_8300DecoderStepReset;
}
if (decode_count_bit == TX_8300_PACKAGE_SIZE) {
package_1 = decode_data;
decode_data = 0;
} else if (decode_count_bit == TX_8300_PACKAGE_SIZE * 2) {
package_2 = decode_data;
decode_data = 0;
}
} else {
parser_step = TX_8300DecoderStepReset;
}
break;
}
}
protected:
// timing values
uint32_t te_short = 1940;
uint32_t te_long = 3880;
uint32_t te_delta = 250;
uint32_t min_count_bit_for_found = 72;
uint32_t package_1{0};
uint32_t package_2{0};
bool ws_protocol_tx_8300_check_crc() {
if (!package_2) return false;
if (package_1 != ~package_2) return false;
uint16_t x = 0;
uint16_t y = 0;
for (int i = 0; i < 32; i += 4) {
x += (package_1 >> i) & 0x0F;
y += (package_1 >> i) & 0x05;
}
uint8_t crc = (~x & 0xF) << 4 | (~y & 0xF);
return (crc == (decode_data & 0xFF));
}
void ws_protocol_tx_8300_remote_controller() {
humidity = (((data >> 28) & 0x0F) * 10) + ((data >> 24) & 0x0F);
btn = WS_NO_BTN;
if (!((data >> 22) & 0x03))
battery_low = 0;
else
battery_low = 1;
channel = (data >> 20) & 0x03;
id = (data >> 12) & 0x7F;
float temp_raw = ((data >> 8) & 0x0F) * 10.0f + ((data >> 4) & 0x0F) +
(data & 0x0F) * 0.1f;
if (!((data >> 19) & 1)) {
temp = temp_raw;
} else {
temp = -temp_raw;
}
}
};
#endif
@@ -0,0 +1,156 @@
#ifndef __FPROTO_WENDOX_W6726_H__
#define __FPROTO_WENDOX_W6726_H__
#include "weatherbase.hpp"
typedef enum {
WendoxW6726DecoderStepReset = 0,
WendoxW6726DecoderStepCheckPreambule,
WendoxW6726DecoderStepSaveDuration,
WendoxW6726DecoderStepCheckDuration,
} WendoxW6726DecoderStep;
class FProtoWeatherWendoxW6726 : public FProtoWeatherBase {
public:
FProtoWeatherWendoxW6726() {
sensorType = FPW_WENDOX_W6726;
}
void feed(bool level, uint32_t duration) override {
switch (parser_step) {
case WendoxW6726DecoderStepReset:
if ((level) && (DURATION_DIFF(duration, te_short) <
te_delta)) {
parser_step = WendoxW6726DecoderStepCheckPreambule;
te_last = duration;
header_count = 0;
}
break;
case WendoxW6726DecoderStepCheckPreambule:
if (level) {
te_last = duration;
} else {
if ((DURATION_DIFF(te_last, te_short) <
te_delta * 1) &&
(DURATION_DIFF(duration, te_long) <
te_delta * 2)) {
header_count++;
} else if ((header_count > 4) && (header_count < 12)) {
if ((DURATION_DIFF(
te_last, te_long) <
te_delta * 2) &&
(DURATION_DIFF(duration, te_short) <
te_delta)) {
decode_data = 0;
decode_count_bit = 0;
subghz_protocol_blocks_add_bit(1);
parser_step = WendoxW6726DecoderStepSaveDuration;
} else {
parser_step = WendoxW6726DecoderStepReset;
}
} else {
parser_step = WendoxW6726DecoderStepReset;
}
}
break;
case WendoxW6726DecoderStepSaveDuration:
if (level) {
te_last = duration;
parser_step = WendoxW6726DecoderStepCheckDuration;
} else {
parser_step = WendoxW6726DecoderStepReset;
}
break;
case WendoxW6726DecoderStepCheckDuration:
if (!level) {
if (duration >
te_short + te_long) {
if (DURATION_DIFF(te_last, te_short) < te_delta) {
subghz_protocol_blocks_add_bit(0);
parser_step = WendoxW6726DecoderStepSaveDuration;
} else if (
DURATION_DIFF(te_last, te_long) < te_delta * 2) {
subghz_protocol_blocks_add_bit(1);
parser_step = WendoxW6726DecoderStepSaveDuration;
} else {
parser_step = WendoxW6726DecoderStepReset;
}
if ((decode_count_bit ==
min_count_bit_for_found) &&
ws_protocol_wendox_w6726_check()) {
data = decode_data;
data_count_bit = decode_count_bit;
ws_protocol_wendox_w6726_remote_controller();
if (callback) callback(this);
}
parser_step = WendoxW6726DecoderStepReset;
} else if (
(DURATION_DIFF(te_last, te_short) <
te_delta) &&
(DURATION_DIFF(duration, te_long) <
te_delta * 3)) {
subghz_protocol_blocks_add_bit(0);
parser_step = WendoxW6726DecoderStepSaveDuration;
} else if (
(DURATION_DIFF(te_last, te_long) <
te_delta * 2) &&
(DURATION_DIFF(duration, te_short) <
te_delta)) {
subghz_protocol_blocks_add_bit(1);
parser_step = WendoxW6726DecoderStepSaveDuration;
} else {
parser_step = WendoxW6726DecoderStepReset;
}
} else {
parser_step = WendoxW6726DecoderStepReset;
}
break;
}
}
protected:
// timing values
uint32_t te_short = 1955;
uint32_t te_long = 5865;
uint32_t te_delta = 300;
uint32_t min_count_bit_for_found = 29;
bool ws_protocol_wendox_w6726_check() {
if (!decode_data) return false;
uint8_t msg[] = {
static_cast<uint8_t>(decode_data >> 28),
static_cast<uint8_t>(decode_data >> 20),
static_cast<uint8_t>(decode_data >> 12),
static_cast<uint8_t>(decode_data >> 4)};
uint8_t crc = subghz_protocol_blocks_crc4(msg, 4, 0x9, 0xD);
return (crc == (decode_data & 0x0F));
}
void ws_protocol_wendox_w6726_remote_controller() {
id = (data >> 24) & 0xFF;
battery_low = (data >> 6) & 1;
channel = WS_NO_CHANNEL;
if (((data >> 23) & 1)) {
temp = (float)(((data >> 14) & 0x1FF) + 12) / 10.0f;
} else {
temp = (float)((~(data >> 14) & 0x1FF) + 1 - 12) / -10.0f;
}
if (temp < -50.0f) {
temp = -50.0f;
} else if (temp > 70.0f) {
temp = 70.0f;
}
btn = WS_NO_BTN;
humidity = WS_NO_HUMIDITY;
}
};
#endif
+230
View File
@@ -0,0 +1,230 @@
/*
Base class for all weather protocols.
This and most of the weather protocols uses code from Flipper XTreme codebase ( https://github.com/Flipper-XFW/Xtreme-Firmware/tree/dev/lib/subghz ). Thanks for their work!
For comments in a protocol implementation check w-nexus-th.hpp
*/
#ifndef __FPROTO_BASE_H__
#define __FPROTO_BASE_H__
#define bit_read(value, bit) (((value) >> (bit)) & 0x01)
#include "weathertypes.hpp"
#include <string>
// default walues to indicate 'no value'
#define WS_NO_ID 0xFFFFFFFF
#define WS_NO_BATT 0xFF
#define WS_NO_HUMIDITY 0xFF
#define WS_NO_CHANNEL 0xFF
#define WS_NO_BTN 0xFF
#define WS_NO_TEMPERATURE -273.0f
#define DURATION_DIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
typedef enum {
ManchesterStateStart1 = 0,
ManchesterStateMid1 = 1,
ManchesterStateMid0 = 2,
ManchesterStateStart0 = 3
} ManchesterState;
typedef enum {
ManchesterEventShortLow = 0,
ManchesterEventShortHigh = 2,
ManchesterEventLongLow = 4,
ManchesterEventLongHigh = 6,
ManchesterEventReset = 8
} ManchesterEvent;
class FProtoWeatherBase;
typedef void (*SubGhzProtocolDecoderBaseRxCallback)(FProtoWeatherBase* instance);
class FProtoWeatherBase {
public:
FProtoWeatherBase() {}
virtual ~FProtoWeatherBase() {}
virtual void feed(bool level, uint32_t duration) = 0; // need to be implemented on each protocol handler.
void setCallback(SubGhzProtocolDecoderBaseRxCallback cb) { callback = cb; } // this is called when there is a hit.
uint8_t getSensorType() { return sensorType; }
uint32_t getSensorId() { return id; }
float getTemp() { return temp; }
uint8_t getHumidity() { return humidity; }
uint8_t getBattLow() { return battery_low; }
uint32_t getTimestamp() { return timestamp; }
uint8_t getChannel() { return channel; }
uint8_t getButton() { return btn; }
protected:
// Helper functions to keep it as compatible with flipper as we can, so adding new protos will be easy.
void subghz_protocol_blocks_add_bit(uint8_t bit) {
decode_data = decode_data << 1 | bit;
decode_count_bit++;
}
uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t size) {
uint32_t result = 0;
for (size_t i = 0; i < size; ++i) {
result += message[i];
}
return (uint8_t)result;
}
uint8_t subghz_protocol_blocks_parity8(uint8_t byte) {
byte ^= byte >> 4;
byte &= 0xf;
return (0x6996 >> byte) & 1;
}
uint8_t subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t size) {
uint8_t result = 0;
for (size_t i = 0; i < size; ++i) {
result ^= subghz_protocol_blocks_parity8(message[i]);
}
return result;
}
uint8_t subghz_protocol_blocks_lfsr_digest8(
uint8_t const message[],
size_t size,
uint8_t gen,
uint8_t key) {
uint8_t sum = 0;
for (size_t byte = 0; byte < size; ++byte) {
uint8_t data = message[byte];
for (int i = 7; i >= 0; --i) {
// XOR key into sum if data bit is set
if ((data >> i) & 1) sum ^= key;
// roll the key right (actually the LSB is dropped here)
// and apply the gen (needs to include the dropped LSB as MSB)
if (key & 1)
key = (key >> 1) ^ gen;
else
key = (key >> 1);
}
}
return sum;
}
float locale_fahrenheit_to_celsius(float temp_f) {
return (temp_f - 32.f) / 1.8f;
}
bool manchester_advance(
ManchesterState state,
ManchesterEvent event,
ManchesterState* next_state,
bool* data) {
bool result = false;
ManchesterState new_state;
if (event == ManchesterEventReset) {
new_state = manchester_reset_state;
} else {
new_state = (ManchesterState)(transitions[state] >> event & 0x3);
if (new_state == state) {
new_state = manchester_reset_state;
} else {
if (new_state == ManchesterStateMid0) {
if (data) *data = false;
result = true;
} else if (new_state == ManchesterStateMid1) {
if (data) *data = true;
result = true;
}
}
}
*next_state = new_state;
return result;
}
uint8_t subghz_protocol_blocks_crc4(
uint8_t const message[],
size_t size,
uint8_t polynomial,
uint8_t init) {
uint8_t remainder = init << 4; // LSBs are unused
uint8_t poly = polynomial << 4;
uint8_t bit;
while (size--) {
remainder ^= *message++;
for (bit = 0; bit < 8; bit++) {
if (remainder & 0x80) {
remainder = (remainder << 1) ^ poly;
} else {
remainder = (remainder << 1);
}
}
}
return remainder >> 4 & 0x0f; // discard the LSBs
}
uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
uint8_t const message[],
size_t size,
uint8_t gen,
uint8_t key) {
uint8_t sum = 0;
// Process message from last byte to first byte (reflected)
for (int byte = size - 1; byte >= 0; --byte) {
uint8_t data = message[byte];
// Process individual bits of each byte (reflected)
for (uint8_t i = 0; i < 8; ++i) {
// XOR key into sum if data bit is set
if ((data >> i) & 1) {
sum ^= key;
}
// roll the key left (actually the LSB is dropped here)
// and apply the gen (needs to include the dropped lsb as MSB)
if (key & 0x80)
key = (key << 1) ^ gen;
else
key = (key << 1);
}
}
return sum;
}
uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t bit_count) {
uint64_t reverse_key = 0;
for (uint8_t i = 0; i < bit_count; i++) {
reverse_key = reverse_key << 1 | bit_read(key, i);
}
return reverse_key;
}
uint8_t subghz_protocol_blocks_crc8(
uint8_t const message[],
size_t size,
uint8_t polynomial,
uint8_t init) {
uint8_t remainder = init;
for (size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte];
for (uint8_t bit = 0; bit < 8; ++bit) {
if (remainder & 0x80) {
remainder = (remainder << 1) ^ polynomial;
} else {
remainder = (remainder << 1);
}
}
}
return remainder;
}
// General weather data holder
uint8_t sensorType = FPW_Invalid;
uint32_t id = WS_NO_ID;
float temp = WS_NO_TEMPERATURE;
uint8_t humidity = WS_NO_HUMIDITY;
uint8_t battery_low = WS_NO_BATT;
uint32_t timestamp = 0;
uint8_t channel = WS_NO_CHANNEL;
uint8_t btn = WS_NO_BTN;
// inner logic stuff, also for flipper compatibility. //todo revork a bit, so won't have dupes (decode_data + data, ..), but check if any of the protos uses it in the same time or not. (shouldn't)
SubGhzProtocolDecoderBaseRxCallback callback = NULL;
uint16_t header_count = 0;
uint8_t parser_step = 0;
uint32_t te_last = 0;
uint64_t data = 0;
uint32_t data_count_bit = 0;
uint64_t decode_data = 0;
uint32_t decode_count_bit = 0;
ManchesterState manchester_saved_state = ManchesterStateMid1;
static const ManchesterState manchester_reset_state = ManchesterStateMid1;
static inline const uint8_t transitions[] = {0b00000001, 0b10010001, 0b10011011, 0b11111011};
};
#endif
@@ -0,0 +1,79 @@
/*
This is the protocol list handler. It holds an instance of all known protocols.
So include here the .hpp, and add a new element to the protos vector in the constructor. That's all you need to do here if you wanna add a new proto.
@htotoo
*/
#include "w-nexus-th.hpp"
#include "w-acurite592txr.hpp"
#include "w-acurite606tx.hpp"
#include "w-acurite609tx.hpp"
#include "w-ambient.hpp"
#include "w-auriol-ahfl.hpp"
#include "w-auriol-th.hpp"
#include "w-gt-wt-02.hpp"
#include "w-gt-wt-03.hpp"
#include "w-infactory.hpp"
#include "w-lacrosse-tx.hpp"
#include "w-lacrosse-tx141thbv2.hpp"
#include "w-oregon2.hpp"
#include "w-oregon3.hpp"
#include "w-oregonv1.hpp"
#include "w-thermoprotx4.hpp"
#include "w-tx8300.hpp"
#include "w-wendox-w6726.hpp"
#include "w-acurite986.hpp"
#include <vector>
#include <memory>
#include "portapack_shared_memory.hpp"
#ifndef __FPROTO_PROTOLIST_H__
#define __FPROTO_PROTOLIST_H__
class WeatherProtos {
public:
WeatherProtos() {
// add protos
protos.push_back(std::make_unique<FProtoWeatherNexusTH>()); // 1
protos.push_back(std::make_unique<FProtoWeatherAcurite592TXR>()); // 2
protos.push_back(std::make_unique<FProtoWeatherAcurite606TX>()); // 3
protos.push_back(std::make_unique<FProtoWeatherAcurite609TX>()); // 4
protos.push_back(std::make_unique<FProtoWeatherAmbient>()); // 5
protos.push_back(std::make_unique<FProtoWeatherAuriolAhfl>()); // 6
protos.push_back(std::make_unique<FProtoWeatherAuriolTh>()); // 7
protos.push_back(std::make_unique<FProtoWeatherGTWT02>()); // 8
protos.push_back(std::make_unique<FProtoWeatherGTWT03>()); // 9
protos.push_back(std::make_unique<FProtoWeatherInfactory>()); // 10
protos.push_back(std::make_unique<FProtoWeatherLaCrosseTx>()); // 11
protos.push_back(std::make_unique<FProtoWeatherLaCrosseTx141thbv2>()); // 12
protos.push_back(std::make_unique<FProtoWeatherOregon2>()); // 13
protos.push_back(std::make_unique<FProtoWeatherOregon3>()); // 14
protos.push_back(std::make_unique<FProtoWeatherOregonV1>()); // 15
protos.push_back(std::make_unique<FProtoWeatherThermoProTx4>()); // 16
protos.push_back(std::make_unique<FProtoWeatherTX8300>()); // 17
protos.push_back(std::make_unique<FProtoWeatherWendoxW6726>()); // 18
protos.push_back(std::make_unique<FProtoWeatherAcurite986>()); // 19
// set callback for them
for (const auto& obj : protos) {
obj->setCallback(callbackTarget);
}
}
static void callbackTarget(FProtoWeatherBase* instance) {
WeatherDataMessage packet_message{instance->getSensorType(), instance->getSensorId(),
instance->getTemp(), instance->getHumidity(), instance->getBattLow(),
instance->getChannel(), instance->getButton()};
shared_memory.application_queue.push(packet_message);
}
void feed(bool level, uint32_t duration) {
for (const auto& obj : protos) {
obj->feed(level, duration);
}
}
protected:
std::vector<std::unique_ptr<FProtoWeatherBase>> protos{};
};
#endif
@@ -0,0 +1,34 @@
#ifndef __FPROTO_WEATHERTYPES_H__
#define __FPROTO_WEATHERTYPES_H__
/*
Define known protocols.
These values must be present on the protocol's constructor, like FProtoWeatherAcurite592TXR() { sensorType = FPW_Acurite592TXR; }
Also it must have a switch-case element in the getWeatherSensorTypeName() function, to display it's name.
*/
enum FPROTO_WEATHER_SENSOR {
FPW_Invalid = 0,
FPW_NexusTH = 1,
FPW_Acurite592TXR = 2,
FPW_Acurite606TX = 3,
FPW_Acurite609TX = 4,
FPW_Ambient = 5,
FPW_AuriolAhfl = 6,
FPW_AuriolTH = 7,
FPW_GTWT02 = 8,
FPW_GTWT03 = 9,
FPW_INFACTORY = 10,
FPW_LACROSSETX = 11,
FPW_LACROSSETX141thbv2 = 12,
FPW_OREGON2 = 13,
FPW_OREGON3 = 14,
FPW_OREGONv1 = 15,
FPW_THERMOPROTX4 = 16,
FPW_TX_8300 = 17,
FPW_WENDOX_W6726 = 18,
FPW_Acurite986 = 19
};
#endif
+51 -51
View File
@@ -20,6 +20,8 @@
* Boston, MA 02110-1301, USA.
*/
// https://www.icao.int/SAM/Documents/2015-SEMAUTOM/Ses4%20Presentation%20CUBA_ADSB.pdf
#include "proc_adsbrx.hpp"
#include "portapack_shared_memory.hpp"
#include "sine_table_int8.hpp"
@@ -31,29 +33,27 @@
using namespace adsb;
void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
int8_t re, im;
uint32_t mag;
uint32_t c;
uint8_t bit, byte{};
// This is called at 2M/2048 = 977Hz
// One pulse = 500ns = 2 samples
// One bit = 2 pulses = 1us = 4 samples
// Each sample is 500ns.
// One bit is 2 samples == 1us.
// Bit value is the transition between samples.
// i.e. lo->hi == 0, hi->lo == 1
if (!configured) return;
uint8_t bit = 0;
uint8_t byte = 0;
for (size_t i = 0; i < buffer.count; i++) {
// Compute sample's magnitude
re = (int32_t)buffer.p[i].real();
im = (int32_t)buffer.p[i].imag();
mag = ((uint32_t)(re * re) + (uint32_t)(im * im));
// Compute sample's magnitude.
int8_t re = buffer.p[i].real();
int8_t im = buffer.p[i].imag();
uint16_t mag = (re * re) + (im * im);
if (decoding) {
// Decode
// 1 bit lasts 2 samples
if (sample_count & 1) {
if (bit_count >= msgLen) {
// 1 bit == 2 samples, transition defines bit value.
if ((sample_count & 1) == 1) {
if (bit_count >= msg_len) {
const ADSBFrameMessage message(frame, amp);
shared_memory.application_queue.push(message);
decoding = false;
@@ -65,40 +65,42 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
byte = bit | (byte << 1);
bit_count++;
// Perform checks at the end of the first byte
if (!(bit_count & 7)) {
// Store the byte
// Every 8th bit...
if ((bit_count & 0x7) == 0) {
// Store the byte.
frame.push_byte(byte);
// Check at the end of the first byte of the message
uint8_t df = (byte >> 3);
if ((bit_count == 8) && !(df & 0x10)) {
msgLen = 56; // DFs 16 or greater are long 112. DFs 15 or less are short 56.
// Perform additional check on the first byte.
if (bit_count == 8) {
// Abandon all frames that aren't DF17 or DF18 extended squitters.
uint8_t df = (byte >> 3);
if (df != 17 && df != 18) {
decoding = false;
bit = (prev_mag > mag) ? 1 : 0;
frame.clear();
}
}
}
}
// Abondon all frames that arent DF17 or DF18 extended squitters
if ((bit_count == 8) && !((df == 17) || (df == 18))) {
decoding = false;
bit = (prev_mag > mag) ? 1 : 0;
frame.clear();
}
} // last bit of a byte
} // Second sample of each bit
sample_count++;
}
// Continue looking for preamble even if in a packet
// switch if new preamble is higher magnitude
// Continue looking for preamble, even if in a packet.
// Switch if new preamble is higher magnitude.
// Shift the preamble
for (c = 0; c < (ADSB_PREAMBLE_LENGTH); c++) {
// Shift the preamble.
for (uint8_t c = 0; c < ADSB_PREAMBLE_LENGTH; c++) {
shifter[c] = shifter[c + 1];
}
shifter[ADSB_PREAMBLE_LENGTH] = mag;
// First check of relations between the first 10 samples
// representing a valid preamble. We don't even investigate further
// if this simple test is not passed
// First check of relations between the first 12 samples
// representing a valid preamble. We don't even investigate
// further if this simple test is not passed.
// Preamble is 8us - or 16 samples.
// 0123456789ABCDEF
// _-_-____-_-_____
if (shifter[0] < shifter[1] &&
shifter[1] > shifter[2] &&
shifter[2] < shifter[3] &&
@@ -113,32 +115,30 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
// The samples between the two spikes must be < than the average
// of the high spikes level. We don't test bits too near to
// the high levels as signals can be out of phase so part of the
// energy can be in the near samples
int32_t thisAmp = (shifter[1] + shifter[3] + shifter[8] + shifter[10]);
uint32_t high = thisAmp / 9;
if (
shifter[5] < high &&
// energy can be in the near samples.
int32_t this_amp = (shifter[1] + shifter[3] + shifter[8] + shifter[10]);
uint32_t high = this_amp / 9; // TBD: Why 9?
if (shifter[5] < high &&
shifter[6] < high &&
// Similarly samples in the range 11-13 must be low, as it is the
// space between the preamble and real data. Again we don't test
// bits too near to high levels, see above
// bits too near to high levels, see above.
shifter[12] < high &&
shifter[13] < high &&
shifter[14] < high) {
if ((decoding == false) || // New preamble
((decoding == true) && (thisAmp > amp))) // Higher power than existing packet
if ((decoding == false) || // New preamble
((decoding == true) && (this_amp > amp))) // Higher power than existing packet
{
decoding = true;
msgLen = 112;
amp = thisAmp;
amp = this_amp;
sample_count = 0;
bit_count = 0;
frame.clear();
}
} // 4 & 5 low and 11-14 low
} // Check for preamble pattern
}
}
// Store mag for next time
// Store mag for next time.
prev_mag = mag;
}
}
+8 -10
View File
@@ -39,21 +39,19 @@ class ADSBRXProcessor : public BasebandProcessor {
void on_message(const Message* const message) override;
private:
static constexpr size_t baseband_fs = 2000000;
static constexpr size_t baseband_fs = 2'000'000;
static constexpr size_t msg_len = 112;
ADSBFrame frame{};
bool configured{false};
bool decoding{false};
uint32_t prev_mag{0};
size_t bit_count{0}, sample_count{0};
size_t msgLen{112};
uint32_t shifter[ADSB_PREAMBLE_LENGTH + 1];
bool decoding{};
bool preamble{}, active{};
uint16_t bit_pos{0};
uint8_t cur_bit{0};
uint32_t sample{0};
int32_t re{}, im{};
int32_t amp{0};
size_t bit_count{0};
size_t sample_count{0};
uint32_t shifter[ADSB_PREAMBLE_LENGTH + 1];
/* NB: Threads should be the last members in the class definition. */
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
+3 -3
View File
@@ -339,6 +339,9 @@ void BTLETxProcessor::execute(const buffer_c8_t& buffer) {
if (sample_count > length) {
configured = false;
sample_count = 0;
txprogress_message.done = true;
shared_memory.application_queue.push(txprogress_message);
} else {
// Real and imaginary was already calculated in gen_sample_from_phy_bit.
// It was processed from each data bit, run through a Gaussian Filter, and then ran through sin and cos table to get each IQ bit.
@@ -363,9 +366,6 @@ void BTLETxProcessor::execute(const buffer_c8_t& buffer) {
buffer.p[i] = {re, im};
}
}
txprogress_message.done = true;
shared_memory.application_queue.push(txprogress_message);
}
void BTLETxProcessor::on_message(const Message* const message) {
+109 -245
View File
@@ -95,32 +95,7 @@ void BTLERxProcessor::scramble_byte(uint8_t* byte_in, int num_byte, const uint8_
}
}
// void BTLERxProcessor::demod_byte(int num_byte, uint8_t *out_byte)
//{
// int i, j;
// int I0, Q0, I1, Q1;
// uint8_t bit_decision;
// int sample_idx = 0;
// for (i = 0; i < num_byte; i++)
// {
// out_byte[i] = 0;
// for (j = 0; j < 8; j++)
// {
// I0 = dst_buffer.p[sample_idx].real();
// Q0 = dst_buffer.p[sample_idx].imag();
// I1 = dst_buffer.p[sample_idx + 1].real();
// Q1 = dst_buffer.p[sample_idx + 1].imag();
// bit_decision = (I0 * Q1 - I1 * Q0) > 0 ? 1 : 0;
// out_byte[i] = out_byte[i] | (bit_decision << j);
// sample_idx += SAMPLE_PER_SYMBOL;;}
//}
int BTLERxProcessor::parse_adv_pdu_payload_byte(uint8_t* payload_byte, int num_payload_byte, ADV_PDU_TYPE pdu_type, void* adv_pdu_payload) {
int BTLERxProcessor::verify_payload_byte(int num_payload_byte, ADV_PDU_TYPE pdu_type) {
// Should at least have 6 bytes for the MAC Address.
// Also ensuring that there is at least 1 byte of data.
if (num_payload_byte <= 6) {
@@ -129,178 +104,26 @@ int BTLERxProcessor::parse_adv_pdu_payload_byte(uint8_t* payload_byte, int num_p
}
if (pdu_type == ADV_IND || pdu_type == ADV_NONCONN_IND || pdu_type == SCAN_RSP || pdu_type == ADV_SCAN_IND) {
payload_type_0_2_4_6 = (ADV_PDU_PAYLOAD_TYPE_0_2_4_6*)adv_pdu_payload;
macAddress[0] = payload_byte[5];
macAddress[1] = payload_byte[4];
macAddress[2] = payload_byte[3];
macAddress[3] = payload_byte[2];
macAddress[4] = payload_byte[1];
macAddress[5] = payload_byte[0];
memcpy(payload_type_0_2_4_6->Data, payload_byte + 6, num_payload_byte - 6);
}
// Only processing advertisments for right now.
else {
return 0;
} else if (pdu_type == ADV_DIRECT_IND || pdu_type == SCAN_REQ) {
if (num_payload_byte != 12) {
// printf("Error: Payload length %d bytes. Need to be 12 for PDU Type %s!\n", num_payload_byte, ADV_PDU_TYPE_STR[pdu_type]);
return -1;
}
} else if (pdu_type == CONNECT_REQ) {
if (num_payload_byte != 34) {
// printf("Error: Payload length %d bytes. Need to be 34 for PDU Type %s!\n", num_payload_byte, ADV_PDU_TYPE_STR[pdu_type]);
return -1;
}
} else {
return -1;
}
// else if (pdu_type == ADV_DIRECT_IND || pdu_type == SCAN_REQ)
// {
// if (num_payload_byte != 12)
// {
// //printf("Error: Payload length %d bytes. Need to be 12 for PDU Type %s!\n", num_payload_byte, ADV_PDU_TYPE_STR[pdu_type]);
// return(-1);
// }
// payload_type_1_3 = (ADV_PDU_PAYLOAD_TYPE_1_3 *)adv_pdu_payload;
// //AdvA = reorder_bytes_str( payload_bytes(1 : (2*6)) );
// macAddress[0] = payload_byte[5];
// macAddress[1] = payload_byte[4];
// macAddress[2] = payload_byte[3];
// macAddress[3] = payload_byte[2];
// macAddress[4] = payload_byte[1];
// macAddress[5] = payload_byte[0];
// //InitA = reorder_bytes_str( payload_bytes((2*6+1):end) );
// payload_type_1_3->A1[0] = payload_byte[11];
// payload_type_1_3->A1[1] = payload_byte[10];
// payload_type_1_3->A1[2] = payload_byte[9];
// payload_type_1_3->A1[3] = payload_byte[8];
// payload_type_1_3->A1[4] = payload_byte[7];
// payload_type_1_3->A1[5] = payload_byte[6];
// //payload_parse_result_str = ['AdvA:' AdvA ' InitA:' InitA];
// }
// else if (pdu_type == CONNECT_REQ)
// {
// if (num_payload_byte != 34)
// {
// //printf("Error: Payload length %d bytes. Need to be 34 for PDU Type %s!\n", num_payload_byte, ADV_PDU_TYPE_STR[pdu_type]);
// return(-1);
// }
// payload_type_5 = (ADV_PDU_PAYLOAD_TYPE_5 *)adv_pdu_payload;
// //InitA = reorder_bytes_str( payload_bytes(1 : (2*6)) );
// macAddress[0] = payload_byte[5];
// macAddress[1] = payload_byte[4];
// macAddress[2] = payload_byte[3];
// macAddress[3] = payload_byte[2];
// macAddress[4] = payload_byte[1];
// macAddress[5] = payload_byte[0];
// //AdvA = reorder_bytes_str( payload_bytes((2*6+1):(2*6+2*6)) );
// payload_type_5->AdvA[0] = payload_byte[11];
// payload_type_5->AdvA[1] = payload_byte[10];
// payload_type_5->AdvA[2] = payload_byte[9];
// payload_type_5->AdvA[3] = payload_byte[8];
// payload_type_5->AdvA[4] = payload_byte[7];
// payload_type_5->AdvA[5] = payload_byte[6];
// //AA = reorder_bytes_str( payload_bytes((2*6+2*6+1):(2*6+2*6+2*4)) );
// payload_type_5->AA[0] = payload_byte[15];
// payload_type_5->AA[1] = payload_byte[14];
// payload_type_5->AA[2] = payload_byte[13];
// payload_type_5->AA[3] = payload_byte[12];
// //CRCInit = payload_bytes((2*6+2*6+2*4+1):(2*6+2*6+2*4+2*3));
// payload_type_5->CRCInit = ( payload_byte[16] );
// payload_type_5->CRCInit = ( (payload_type_5->CRCInit << 8) | payload_byte[17] );
// payload_type_5->CRCInit = ( (payload_type_5->CRCInit << 8) | payload_byte[18] );
// //WinSize = payload_bytes((2*6+2*6+2*4+2*3+1):(2*6+2*6+2*4+2*3+2*1));
// payload_type_5->WinSize = payload_byte[19];
// //WinOffset = reorder_bytes_str( payload_bytes((2*6+2*6+2*4+2*3+2*1+1):(2*6+2*6+2*4+2*3+2*1+2*2)) );
// payload_type_5->WinOffset = ( payload_byte[21] );
// payload_type_5->WinOffset = ( (payload_type_5->WinOffset << 8) | payload_byte[20] );
// //Interval = reorder_bytes_str( payload_bytes((2*6+2*6+2*4+2*3+2*1+2*2+1):(2*6+2*6+2*4+2*3+2*1+2*2+2*2)) );
// payload_type_5->Interval = ( payload_byte[23] );
// payload_type_5->Interval = ( (payload_type_5->Interval << 8) | payload_byte[22] );
// //Latency = reorder_bytes_str( payload_bytes((2*6+2*6+2*4+2*3+2*1+2*2+2*2+1):(2*6+2*6+2*4+2*3+2*1+2*2+2*2+2*2)) );
// payload_type_5->Latency = ( payload_byte[25] );
// payload_type_5->Latency = ( (payload_type_5->Latency << 8) | payload_byte[24] );
// //Timeout = reorder_bytes_str( payload_bytes((2*6+2*6+2*4+2*3+2*1+2*2+2*2+2*2+1):(2*6+2*6+2*4+2*3+2*1+2*2+2*2+2*2+2*2)) );
// payload_type_5->Timeout = ( payload_byte[27] );
// payload_type_5->Timeout = ( (payload_type_5->Timeout << 8) | payload_byte[26] );
// //ChM = reorder_bytes_str( payload_bytes((2*6+2*6+2*4+2*3+2*1+2*2+2*2+2*2+2*2+1):(2*6+2*6+2*4+2*3+2*1+2*2+2*2+2*2+2*2+2*5)) );
// payload_type_5->ChM[0] = payload_byte[32];
// payload_type_5->ChM[1] = payload_byte[31];
// payload_type_5->ChM[2] = payload_byte[30];
// payload_type_5->ChM[3] = payload_byte[29];
// payload_type_5->ChM[4] = payload_byte[28];
// //tmp_bits = payload_bits((end-7) : end);
// //Hop = num2str( bi2de(tmp_bits(1:5), 'right-msb') );
// //SCA = num2str( bi2de(tmp_bits(6:end), 'right-msb') );
// payload_type_5->Hop = (payload_byte[33]&0x1F);
// payload_type_5->SCA = ((payload_byte[33]>>5)&0x07);
// }
// else
// {
// //TODO: Handle Unknown PDU.
// payload_type_R = (ADV_PDU_PAYLOAD_TYPE_R *)adv_pdu_payload;
// memcpy(payload_type_R->payload_byte, payload_byte, num_payload_byte);
// return(-1);
// }
return 0;
}
void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
if (!configured) return;
// Pulled this implementation from channel_stats_collector.c to time slice a specific packet's dB.
uint32_t max_squared = 0;
void* src_p = buffer.p;
while (src_p < &buffer.p[buffer.count]) {
const uint32_t sample = *__SIMD32(src_p)++;
const uint32_t mag_sq = __SMUAD(sample, sample);
if (mag_sq > max_squared) {
max_squared = mag_sq;
}
}
const float max_squared_f = max_squared;
const int32_t max_dB = mag2_to_dbv_norm(max_squared_f * (1.0f / (32768.0f * 32768.0f)));
decim_0.execute(buffer, dst_buffer);
feed_channel_stats(dst_buffer);
const buffer_c8_t iq_buffer{
buffer.p,
buffer.count,
baseband_fs};
// process++;
// if ((process % 50) != 0) return;
// 4Mhz 2048 samples
//--------------Variable Defines---------------------------------//
int i, sp, j = 0;
int I0, Q0, I1, Q1 = 0;
int k, p, phase_idx = 0;
int num_demod_byte = 0;
bool unequal_flag;
const int demod_buf_len = LEN_DEMOD_BUF_ACCESS; // For AA
int demod_buf_offset = 0;
void BTLERxProcessor::handleBeginState() {
int num_symbol_left = dst_buffer.count / SAMPLE_PER_SYMBOL; // One buffer sample consist of I and Q.
int symbols_eaten = 0;
int hit_idx = (-1);
//--------------Start Parsing For Access Address---------------//
static uint8_t demod_buf_access[SAMPLE_PER_SYMBOL][LEN_DEMOD_BUF_ACCESS];
@@ -310,33 +133,38 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
uint32_t accesssAddress = 0;
// Filling up addressBits with the access address we are looking to find.
for (i = 0; i < 32; i++) {
for (int i = 0; i < 32; i++) {
accessAddrBits[i] = 0x01 & uint32_tmp;
uint32_tmp = (uint32_tmp >> 1);
}
const int demod_buf_len = LEN_DEMOD_BUF_ACCESS; // For AA
int demod_buf_offset = 0;
int hit_idx = (-1);
bool unequal_flag = false;
memset(demod_buf_access, 0, SAMPLE_PER_SYMBOL * demod_buf_len);
for (i = 0; i < num_symbol_left * SAMPLE_PER_SYMBOL; i += SAMPLE_PER_SYMBOL) {
sp = ((demod_buf_offset - demod_buf_len + 1) & (demod_buf_len - 1));
for (int i = 0; i < num_symbol_left * SAMPLE_PER_SYMBOL; i += SAMPLE_PER_SYMBOL) {
int sp = ((demod_buf_offset - demod_buf_len + 1) & (demod_buf_len - 1));
for (j = 0; j < SAMPLE_PER_SYMBOL; j++) {
for (int j = 0; j < SAMPLE_PER_SYMBOL; j++) {
// Sample and compare with the adjacent next sample.
I0 = dst_buffer.p[i + j].real();
Q0 = dst_buffer.p[i + j].imag();
I1 = dst_buffer.p[i + j + 1].real();
Q1 = dst_buffer.p[i + j + 1].imag();
int I0 = dst_buffer.p[i + j].real();
int Q0 = dst_buffer.p[i + j].imag();
int I1 = dst_buffer.p[i + j + 1].real();
int Q1 = dst_buffer.p[i + j + 1].imag();
phase_idx = j;
int phase_idx = j;
demod_buf_access[phase_idx][demod_buf_offset] = (I0 * Q1 - I1 * Q0) > 0 ? 1 : 0;
k = sp;
int k = sp;
unequal_flag = false;
accesssAddress = 0;
for (p = 0; p < demod_buf_len; p++) {
for (int p = 0; p < demod_buf_len; p++) {
if (demod_buf_access[phase_idx][k] != accessAddrBits[p]) {
unequal_flag = true;
hit_idx = (-1);
@@ -368,13 +196,15 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
symbols_eaten += hit_idx;
symbols_eaten += (8 * NUM_ACCESS_ADDR_BYTE * SAMPLE_PER_SYMBOL); // move to beginning of PDU header
symbols_eaten += (8 * NUM_ACCESS_ADDR_BYTE * SAMPLE_PER_SYMBOL); // move to the beginning of PDU header
num_symbol_left = num_symbol_left - symbols_eaten;
//--------------Start PDU Header Parsing-----------------------//
parseState = Parse_State_PDU_Header;
}
num_demod_byte = 2; // PDU header has 2 octets
void BTLERxProcessor::handlePDUHeaderState() {
int num_demod_byte = 2; // PDU header has 2 octets
symbols_eaten += 8 * num_demod_byte * SAMPLE_PER_SYMBOL;
@@ -382,22 +212,19 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
return;
}
// //Demod the PDU Header
uint8_t bit_decision;
// Jump back down to the beginning of PDU header.
sample_idx = symbols_eaten - (8 * num_demod_byte * SAMPLE_PER_SYMBOL);
// Jump back down to beginning of PDU header.
int sample_idx = symbols_eaten - (8 * num_demod_byte * SAMPLE_PER_SYMBOL);
packet_index = 0;
uint16_t packet_index = 0;
for (i = 0; i < num_demod_byte; i++) {
for (int i = 0; i < num_demod_byte; i++) {
rb_buf[packet_index] = 0;
for (j = 0; j < 8; j++) {
I0 = dst_buffer.p[sample_idx].real();
Q0 = dst_buffer.p[sample_idx].imag();
I1 = dst_buffer.p[sample_idx + 1].real();
Q1 = dst_buffer.p[sample_idx + 1].imag();
for (int j = 0; j < 8; j++) {
int I0 = dst_buffer.p[sample_idx].real();
int Q0 = dst_buffer.p[sample_idx].imag();
int I1 = dst_buffer.p[sample_idx + 1].real();
int Q1 = dst_buffer.p[sample_idx + 1].imag();
bit_decision = (I0 * Q1 - I1 * Q0) > 0 ? 1 : 0;
rb_buf[packet_index] = rb_buf[packet_index] | (bit_decision << j);
@@ -408,39 +235,39 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
packet_index++;
}
// demod_byte(num_demod_byte, rb_buf);
scramble_byte(rb_buf, num_demod_byte, scramble_table[channel_number], rb_buf);
uint8_t pdu_type = (ADV_PDU_TYPE)(rb_buf[0] & 0x0F);
pdu_type = (ADV_PDU_TYPE)(rb_buf[0] & 0x0F);
// uint8_t tx_add = ((rb_buf[0] & 0x40) != 0);
// uint8_t rx_add = ((rb_buf[0] & 0x80) != 0);
uint8_t payload_len = (rb_buf[1] & 0x3F);
payload_len = (rb_buf[1] & 0x3F);
// Not valid Advertise Payload.
// Not a valid Advertise Payload.
if ((payload_len < 6) || (payload_len > 37)) {
parseState = Parse_State_Begin;
return;
} else {
parseState = Parse_State_PDU_Payload;
}
}
//--------------Start Payload Parsing--------------------------//
num_demod_byte = (payload_len + 3);
void BTLERxProcessor::handlePDUPayloadState() {
int i;
int num_demod_byte = (payload_len + 3);
symbols_eaten += 8 * num_demod_byte * SAMPLE_PER_SYMBOL;
if (symbols_eaten > (int)dst_buffer.count) {
return;
}
// sample_idx = symbols_eaten - (8 * num_demod_byte * SAMPLE_PER_SYMBOL);
for (i = 0; i < num_demod_byte; i++) {
rb_buf[packet_index] = 0;
for (j = 0; j < 8; j++) {
I0 = dst_buffer.p[sample_idx].real();
Q0 = dst_buffer.p[sample_idx].imag();
I1 = dst_buffer.p[sample_idx + 1].real();
Q1 = dst_buffer.p[sample_idx + 1].imag();
for (int j = 0; j < 8; j++) {
int I0 = dst_buffer.p[sample_idx].real();
int Q0 = dst_buffer.p[sample_idx].imag();
int I1 = dst_buffer.p[sample_idx + 1].real();
int Q1 = dst_buffer.p[sample_idx + 1].imag();
bit_decision = (I0 * Q1 - I1 * Q0) > 0 ? 1 : 0;
rb_buf[packet_index] = rb_buf[packet_index] | (bit_decision << j);
@@ -451,12 +278,8 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
packet_index++;
}
// demod_byte(num_demod_byte, rb_buf + 2);
scramble_byte(rb_buf + 2, num_demod_byte, scramble_table[channel_number] + 2, rb_buf + 2);
//--------------Start CRC Checking-----------------------------//
// Check CRC
bool crc_flag = crc_check(rb_buf, payload_len + 2, crc_init_internal);
// pkt_count++;
@@ -466,7 +289,7 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
// Checking CRC and excluding Reserved PDU types.
if (pdu_type < RESERVED0 && !crc_flag) {
if (parse_adv_pdu_payload_byte(rb_buf + 2, payload_len, (ADV_PDU_TYPE)pdu_type, (void*)(&adv_pdu_payload)) == 0) {
if (verify_payload_byte(payload_len, (ADV_PDU_TYPE)pdu_type) == 0) {
sendPacket = true;
}
@@ -477,12 +300,12 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
blePacketData.type = pdu_type;
blePacketData.size = payload_len;
blePacketData.macAddress[0] = macAddress[0];
blePacketData.macAddress[1] = macAddress[1];
blePacketData.macAddress[2] = macAddress[2];
blePacketData.macAddress[3] = macAddress[3];
blePacketData.macAddress[4] = macAddress[4];
blePacketData.macAddress[5] = macAddress[5];
blePacketData.macAddress[0] = rb_buf[7];
blePacketData.macAddress[1] = rb_buf[6];
blePacketData.macAddress[2] = rb_buf[5];
blePacketData.macAddress[3] = rb_buf[4];
blePacketData.macAddress[4] = rb_buf[3];
blePacketData.macAddress[5] = rb_buf[2];
// Skip Header Byte and MAC Address
uint8_t startIndex = 8;
@@ -498,6 +321,48 @@ void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
shared_memory.application_queue.push(data_message);
}
}
parseState = Parse_State_Begin;
}
void BTLERxProcessor::execute(const buffer_c8_t& buffer) {
if (!configured) return;
// Pulled this implementation from channel_stats_collector.c to time slice a specific packet's dB.
uint32_t max_squared = 0;
void* src_p = buffer.p;
while (src_p < &buffer.p[buffer.count]) {
const uint32_t sample = *__SIMD32(src_p)++;
const uint32_t mag_sq = __SMUAD(sample, sample);
if (mag_sq > max_squared) {
max_squared = mag_sq;
}
}
const float max_squared_f = max_squared;
max_dB = mag2_to_dbv_norm(max_squared_f * (1.0f / (32768.0f * 32768.0f)));
// 4Mhz 2048 samples
// Decimated by 4 to achieve 2048/4 = 512 samples at 1 sample per symbol.
decim_0.execute(buffer, dst_buffer);
feed_channel_stats(dst_buffer);
symbols_eaten = 0;
// Handle parsing based on parseState
if (parseState == Parse_State_Begin) {
handleBeginState();
}
if (parseState == Parse_State_PDU_Header) {
handlePDUHeaderState();
}
if (parseState == Parse_State_PDU_Payload) {
handlePDUPayloadState();
}
}
void BTLERxProcessor::on_message(const Message* const message) {
@@ -507,8 +372,7 @@ void BTLERxProcessor::on_message(const Message* const message) {
void BTLERxProcessor::configure(const BTLERxConfigureMessage& message) {
channel_number = message.channel_number;
decim_0.configure(taps_200k_wfm_decim_0.taps);
demod.configure(48000, 5000);
decim_0.configure(taps_BTLE_1M_PHY_decim_0.taps);
configured = true;
+23 -32
View File
@@ -48,6 +48,12 @@ class BTLERxProcessor : public BasebandProcessor {
static constexpr uint32_t DEFAULT_ACCESS_ADDR{0x8E89BED6};
static constexpr int NUM_ACCESS_ADDR_BYTE{4};
enum Parse_State {
Parse_State_Begin = 0,
Parse_State_PDU_Header,
Parse_State_PDU_Payload
};
enum ADV_PDU_TYPE {
ADV_IND = 0,
ADV_DIRECT_IND = 1,
@@ -70,28 +76,6 @@ class BTLERxProcessor : public BasebandProcessor {
uint8_t macAddress[6];
int checksumReceived = 0;
struct ADV_PDU_PAYLOAD_TYPE_0_2_4_6 {
uint8_t Data[31];
};
struct ADV_PDU_PAYLOAD_TYPE_1_3 {
uint8_t A1[6];
};
struct ADV_PDU_PAYLOAD_TYPE_5 {
uint8_t AdvA[6];
uint8_t AA[4];
uint32_t CRCInit;
uint8_t WinSize;
uint16_t WinOffset;
uint16_t Interval;
uint16_t Latency;
uint16_t Timeout;
uint8_t ChM[5];
uint8_t Hop;
uint8_t SCA;
};
struct ADV_PDU_PAYLOAD_TYPE_R {
uint8_t payload_byte[40];
};
@@ -109,15 +93,19 @@ class BTLERxProcessor : public BasebandProcessor {
void scramble_byte(uint8_t* byte_in, int num_byte, const uint8_t* scramble_table_byte, uint8_t* byte_out);
// void demod_byte(int num_byte, uint8_t *out_byte);
int parse_adv_pdu_payload_byte(uint8_t* payload_byte, int num_payload_byte, ADV_PDU_TYPE pdu_type, void* adv_pdu_payload);
int verify_payload_byte(int num_payload_byte, ADV_PDU_TYPE pdu_type);
std::array<complex16_t, 1024> dst{};
void handleBeginState();
void handlePDUHeaderState();
void handlePDUPayloadState();
std::array<complex16_t, 512> dst{};
const buffer_c16_t dst_buffer{
dst.data(),
dst.size()};
static constexpr int RB_SIZE = 2048;
uint8_t rb_buf[2048];
static constexpr int RB_SIZE = 512;
uint8_t rb_buf[RB_SIZE];
dsp::decimate::FIRC8xR16x24FS4Decim4 decim_0{};
@@ -131,18 +119,21 @@ class BTLERxProcessor : public BasebandProcessor {
bool configured{false};
BlePacketData blePacketData{};
Parse_State parseState{Parse_State_Begin};
uint16_t packet_index{0};
int sample_idx{0};
int symbols_eaten{0};
uint8_t bit_decision{0};
uint8_t payload_len{0};
uint8_t pdu_type{0};
int32_t max_dB{0};
/* NB: Threads should be the last members in the class definition. */
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
RSSIThread rssi_thread{};
void configure(const BTLERxConfigureMessage& message);
ADV_PDU_PAYLOAD_TYPE_0_2_4_6* payload_type_0_2_4_6 = nullptr;
ADV_PDU_PAYLOAD_TYPE_1_3* payload_type_1_3 = nullptr;
ADV_PDU_PAYLOAD_TYPE_5* payload_type_5 = nullptr;
ADV_PDU_PAYLOAD_TYPE_R* payload_type_R = nullptr;
ADV_PDU_PAYLOAD_TYPE_R adv_pdu_payload = {0};
// clang-format off
// Scramble table definition
const uint8_t scramble_table[40][42] =
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "proc_weather.hpp"
#include "portapack_shared_memory.hpp"
#include "event_m4.hpp"
void WeatherProcessor::execute(const buffer_c8_t& buffer) {
if (!configured) return;
for (size_t i = 0; i < buffer.count; i++) {
int8_t re = buffer.p[i].real();
int8_t im = buffer.p[i].imag();
uint32_t mag = ((uint32_t)re * (uint32_t)re) + ((uint32_t)im * (uint32_t)im);
bool meashl = (mag > threshold);
tm += mag;
if (meashl == currentHiLow && currentDuration < 10'000'000) // allow pass 'end' signal
{
if (currentDuration < UINT32_MAX) currentDuration += usperTick;
} else { // called on change, so send the last duration and dir.
protoList.feed(currentHiLow, currentDuration / 1000);
currentDuration = usperTick;
currentHiLow = meashl;
}
}
cnt += buffer.count;
if (cnt > 30'000) {
threshold = (tm / cnt) / 2;
cnt = 0;
tm = 0;
if (threshold < 50) threshold = 50;
if (threshold > 1700) threshold = 1700;
}
}
void WeatherProcessor::on_message(const Message* const message) {
if (message->id == Message::ID::WeatherRxConfigure)
configure(*reinterpret_cast<const WeatherRxConfigureMessage*>(message));
}
void WeatherProcessor::configure(const WeatherRxConfigureMessage& message) {
(void)message;
configured = true;
}
int main() {
EventDispatcher event_dispatcher{std::make_unique<WeatherProcessor>()};
event_dispatcher.run();
return 0;
}
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
Creator: @htotoo
*/
#ifndef __PROC_WEATHER_H__
#define __PROC_WEATHER_H__
#include "baseband_processor.hpp"
#include "baseband_thread.hpp"
#include "rssi_thread.hpp"
#include "message.hpp"
#include "fprotos/weatherprotos.hpp"
class WeatherProcessor : public BasebandProcessor {
public:
void execute(const buffer_c8_t& buffer) override;
void on_message(const Message* const message) override;
private:
static constexpr uint32_t usperTick = 500; // we nees ms to has to divide by 1000
static constexpr size_t baseband_fs = 1'750'000;
uint32_t currentDuration = 0;
uint32_t threshold = 0x0630; // will overwrite after the first iteration
bool currentHiLow = false;
bool configured{false};
// for debug
uint32_t cnt = 0;
uint32_t tm = 0;
WeatherProtos protoList{}; // holds all the protocols we can parse
void configure(const WeatherRxConfigureMessage& message);
/* NB: Threads should be the last members in the class definition. */
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
RSSIThread rssi_thread{};
};
#endif /*__PROC_WEATHER_H__*/
File diff suppressed because it is too large Load Diff
+4 -3
View File
@@ -205,11 +205,12 @@
#ifdef __cplusplus
extern "C" {
#endif
void halInit(void);
void halInit(void);
#if HAL_IMPLEMENTS_COUNTERS
bool_t halIsCounterWithin(halrtcnt_t start, halrtcnt_t end);
void halPolledDelay(halrtcnt_t ticks);
bool_t halIsCounterWithin(halrtcnt_t start, halrtcnt_t end);
void halPolledDelay(halrtcnt_t ticks);
#endif /* HAL_IMPLEMENTS_COUNTERS */
void sdio_cclk_set(const size_t divider_value);
#ifdef __cplusplus
}
#endif
+1
View File
@@ -25,6 +25,7 @@
#include <cstring>
#include <string>
#include <cstdint>
namespace adsb {
+4
View File
@@ -862,6 +862,10 @@ class AK4951 : public audio::Codec {
return read(reg_address);
}
void reg_write(const size_t reg_address, const uint32_t value) override {
write(reg_address, value);
}
private:
I2C& bus;
const I2C::address_t bus_address;
+78
View File
@@ -1264,4 +1264,82 @@ static constexpr fir_taps_real<16> taps_200k_decim_1 = {
}},
};
// BTLE RX decimation filters ////////////////////////////////////////////////
// Default BTLE filter, it is supporting 1M PHY.
// IFIR image-reject filter: fs=4000000, pass=430000, stop=825000, decim=4, fout=1000000
// 1M PHY, This one it is for the classic bluetooth , (BW = 1Mhz : +-500k, channel space is 2Mhz)
// The traditional transmission of 1 Mbit in the Bluetooth Basic Rate was renamed 1M PHY
static constexpr fir_taps_real<24> taps_BTLE_1M_PHY_decim_0 = {
.low_frequency_normalized = -430000.0f / 4000000.0f,
.high_frequency_normalized = 430000.0f / 4000000.0f,
.transition_normalized = 395000.0f / 4000000.0f,
.taps = {{
12,
57,
112,
83,
-139,
-531,
-813,
-507,
766,
2916,
5255,
6788,
6788,
5255,
2916,
766,
-507,
-813,
-531,
-139,
83,
112,
57,
12,
}},
};
// IFIR image-reject filter: fs=4000000, pass=920000, stop=1350000, decim=4, fout=1000000
// Alternative filter, Note : in local test, it improves slightly the sensitivity compared to above filter, but it should have aliasing if co-adjacent channels.
// Then , we leave that filter in the code ,as experimental , but it should not be set up as default one.
// It may work well, in areas where we just receive few signals, without adjacent channels and weak far away signals.
// 2M PHY , Bluetooth 5 has introduced a new transmission mode with a doubled symbol rate.
// Bluetooth LE has been traditionally transmitting 1 bit per symbol so that theoretically the data rate doubles as well. (BW 2Mhz : +-1Mhz, channel space 2Mhz)
static constexpr fir_taps_real<24> taps_BTLE_2M_PHY_decim_0 = {
.low_frequency_normalized = -920000.0f / 4000000.0f,
.high_frequency_normalized = 920000.0f / 4000000.0f,
.transition_normalized = 430000.0f / 4000000.0f,
.taps = {{
-8,
-20,
42,
81,
-142,
-234,
371,
573,
-884,
-1414,
2573,
8062,
8062,
2573,
-1414,
-884,
573,
371,
-234,
-142,
81,
42,
-20,
-8,
}},
};
#endif /*__DSP_FIR_TAPS_H__*/
+62 -6
View File
@@ -115,6 +115,8 @@ class Message {
FSKRxConfigure = 58,
BlePacket = 58,
BTLETxConfigure = 59,
WeatherRxConfigure = 60,
WeatherData = 61,
MAX
};
@@ -403,6 +405,28 @@ class AFSKDataMessage : public Message {
uint32_t value;
};
struct ADV_PDU_PAYLOAD_TYPE_0_2_4_6 {
uint8_t Data[31];
};
struct ADV_PDU_PAYLOAD_TYPE_1_3 {
uint8_t A1[6];
};
struct ADV_PDU_PAYLOAD_TYPE_5 {
uint8_t AdvA[6];
uint8_t AA[4];
uint32_t CRCInit;
uint8_t WinSize;
uint16_t WinOffset;
uint16_t Interval;
uint16_t Latency;
uint16_t Timeout;
uint8_t ChM[5];
uint8_t Hop;
uint8_t SCA;
};
struct BlePacketData {
int max_dB;
uint8_t type;
@@ -1089,13 +1113,9 @@ class APRSPacketMessage : public Message {
class ADSBConfigureMessage : public Message {
public:
constexpr ADSBConfigureMessage(
const uint32_t test)
: Message{ID::ADSBConfigure},
test(test) {
constexpr ADSBConfigureMessage()
: Message{ID::ADSBConfigure} {
}
const uint32_t test;
};
class JammerConfigureMessage : public Message {
@@ -1218,4 +1238,40 @@ class SpectrumPainterBufferConfigureResponseMessage : public Message {
SpectrumPainterFIFO* fifo{nullptr};
};
class WeatherRxConfigureMessage : public Message {
public:
constexpr WeatherRxConfigureMessage()
: Message{ID::WeatherRxConfigure} {
// todoh give some more info
}
};
class WeatherDataMessage : public Message {
public:
constexpr WeatherDataMessage(
uint8_t sensorType = 0,
uint32_t id = 0xFFFFFFFF,
float temp = -273.0f,
uint8_t humidity = 0xFF,
uint8_t battery_low = 0xFF,
uint8_t channel = 0xFF,
uint8_t btn = 0xFF)
: Message{ID::WeatherData},
sensorType{sensorType},
id{id},
temp{temp},
humidity{humidity},
battery_low{battery_low},
channel{channel},
btn{btn} {
}
uint8_t sensorType = 0;
uint32_t id = 0xFFFFFFFF;
float temp = -273.0f;
uint8_t humidity = 0xFF;
uint8_t battery_low = 0xFF;
uint8_t channel = 0xFF;
uint8_t btn = 0xFF;
};
#endif /*__MESSAGE_H__*/
@@ -40,6 +40,7 @@
#include <utility>
#include <ch.h>
#include <hal.h>
using namespace std;
@@ -143,7 +144,7 @@ struct misc_config_t {
bool mute_audio : 1;
bool disable_speaker : 1;
bool config_disable_external_tcxo : 1;
bool UNUSED_3 : 1;
bool config_sdcard_high_speed_io : 1;
bool UNUSED_4 : 1;
bool UNUSED_5 : 1;
bool UNUSED_6 : 1;
@@ -345,6 +346,19 @@ struct backup_ram_t {
check_value = compute_check_value();
copy(*this, dst);
}
/* Access functions for DebugPmemView */
uint32_t pmem_data_word(uint32_t index) {
return (index > sizeof(regfile) / sizeof(uint32_t)) ? 0xFFFFFFFF : regfile[index];
}
uint32_t pmem_stored_checksum(void) {
return check_value;
}
uint32_t pmem_calculated_checksum(void) {
return compute_check_value();
}
};
static_assert(sizeof(backup_ram_t) == memory::map::backup_ram.size());
@@ -381,6 +395,8 @@ void defaults() {
set_recon_update_ranges_when_recon(true);
set_recon_load_hamradios(true);
set_recon_match_mode(0);
set_config_sdcard_high_speed_io(false, true);
}
void init() {
@@ -413,6 +429,18 @@ void persist() {
} /* namespace cache */
uint32_t pmem_data_word(uint32_t index) {
return backup_ram->pmem_data_word(index);
}
uint32_t pmem_stored_checksum(void) {
return backup_ram->pmem_stored_checksum();
}
uint32_t pmem_calculated_checksum(void) {
return backup_ram->pmem_calculated_checksum();
}
rf::Frequency target_frequency() {
rf::tuning_range.reset_if_outside(data->target_frequency, target_frequency_reset_value);
return data->target_frequency;
@@ -556,6 +584,10 @@ bool config_disable_external_tcxo() {
return data->misc_config.config_disable_external_tcxo;
}
bool config_sdcard_high_speed_io() {
return data->misc_config.config_sdcard_high_speed_io;
}
bool stealth_mode() {
return data->ui_config.stealth_mode;
}
@@ -621,6 +653,19 @@ void set_config_disable_external_tcxo(bool v) {
data->misc_config.config_disable_external_tcxo = v;
}
void set_config_sdcard_high_speed_io(bool v, bool save) {
if (v) {
/* 200MHz / (2 * 2) = 50MHz */
/* TODO: Adjust SCU pin configurations: pull-up/down, slew, glitch filter? */
sdio_cclk_set(2);
} else {
/* 200MHz / (2 * 4) = 25MHz */
sdio_cclk_set(4);
}
if (save)
data->misc_config.config_sdcard_high_speed_io = v;
}
void set_stealth_mode(bool v) {
data->ui_config.stealth_mode = v;
}
@@ -990,6 +1035,7 @@ bool debug_dump() {
pmem_dump_file.write_line("misc_config config_audio_mute: " + to_string_dec_int(config_audio_mute()));
pmem_dump_file.write_line("misc_config config_speaker_disable: " + to_string_dec_int(config_speaker_disable()));
pmem_dump_file.write_line("ui_config config_disable_external_tcxo: " + to_string_dec_uint(config_disable_external_tcxo()));
pmem_dump_file.write_line("ui_config config_sdcard_high_speed_io: " + to_string_dec_uint(config_sdcard_high_speed_io()));
// receiver_model
pmem_dump_file.write_line("\n[Receiver Model]");
@@ -174,6 +174,7 @@ uint8_t config_cpld();
void set_config_cpld(uint8_t i);
bool config_disable_external_tcxo();
bool config_sdcard_high_speed_io();
bool config_splash();
bool config_converter();
bool config_updown_converter();
@@ -193,6 +194,7 @@ void set_load_app_settings(bool v);
void set_save_app_settings(bool v);
void set_show_bigger_qr_code(bool v);
void set_config_disable_external_tcxo(bool v);
void set_config_sdcard_high_speed_io(bool v, bool save);
void set_config_splash(bool v);
bool config_converter();
bool config_updown_converter();
@@ -281,6 +283,10 @@ bool should_use_sdcard_for_pmem();
int save_persistent_settings_to_file();
int load_persistent_settings_from_file();
uint32_t pmem_data_word(uint32_t index);
uint32_t pmem_stored_checksum(void);
uint32_t pmem_calculated_checksum(void);
size_t data_size();
bool debug_dump();
+2
View File
@@ -112,6 +112,8 @@ constexpr image_tag_t image_tag_tones{'P', 'T', 'O', 'N'};
constexpr image_tag_t image_tag_flash_utility{'P', 'F', 'U', 'T'};
constexpr image_tag_t image_tag_usb_sd{'P', 'U', 'S', 'B'};
constexpr image_tag_t image_tag_weather{'P', 'W', 'T', 'H'};
constexpr image_tag_t image_tag_noop{'P', 'N', 'O', 'P'};
constexpr image_tag_t image_tag_hackrf{'H', 'R', 'F', '1'};
+4 -2
View File
@@ -30,6 +30,8 @@
#include <utility>
#include <vector>
#define pow2(x) ((x) * (x))
namespace ui {
Widget* FocusManager::focus_widget() const {
@@ -138,12 +140,12 @@ static int32_t rect_distances(
switch (direction) {
case KeyEvent::Right:
case KeyEvent::Left:
return ((std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1) ^ 3) * sqrt((on_axis_distance + 1));
return pow2(std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1) * (on_axis_distance + 1);
break;
case KeyEvent::Up:
case KeyEvent::Down:
return (sqrt(std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1)) * ((on_axis_distance + 1) ^ 3);
return (std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1) * pow2(on_axis_distance + 1);
break;
default:
+22
View File
@@ -587,6 +587,28 @@ void ProgressBar::paint(Painter& painter) {
painter.draw_rectangle(sr, s.foreground);
}
/* ActivityDot ***********************************************************/
ActivityDot::ActivityDot(
Rect parent_rect,
Color color)
: Widget{parent_rect},
_color{color} {}
void ActivityDot::paint(Painter& painter) {
painter.fill_rectangle(screen_rect(), _on ? _color : Color::grey());
}
void ActivityDot::toggle() {
_on = !_on;
set_dirty();
}
void ActivityDot::reset() {
_on = false;
set_dirty();
}
/* Console ***************************************************************/
Console::Console(
+14
View File
@@ -317,6 +317,20 @@ class ProgressBar : public Widget {
uint32_t _max = 100;
};
/* A simple status indicator that can be used to indicate activity. */
class ActivityDot : public Widget {
public:
ActivityDot(Rect parent_rect, Color color);
void paint(Painter& painter) override;
void toggle();
void reset();
private:
bool _on{false};
Color _color;
};
class Console : public Widget {
public:
Console(Rect parent_rect);
+7
View File
@@ -116,6 +116,8 @@ bool WM8731::write(const Register reg) {
}
bool WM8731::write(const address_t reg_address, const reg_t value) {
map.w[reg_address] = value; // Save data written in case this fn is called from Debug->Peripherals app
const uint16_t word = (reg_address << 9) | value;
const std::array<uint8_t, 2> values{
static_cast<uint8_t>(word >> 8),
@@ -124,10 +126,15 @@ bool WM8731::write(const address_t reg_address, const reg_t value) {
return bus.transmit(bus_address, values.data(), values.size());
}
/* WM8731 is a write-only device; the read function only returns the value we last wrote */
uint32_t WM8731::reg_read(const size_t reg_address) {
return map.w[reg_address];
}
void WM8731::reg_write(const size_t reg_address, uint32_t value) {
write(reg_address, value);
}
void WM8731::write(const LeftLineIn value) {
map.r.left_line_in = value;
write(Register::LeftLineIn);
+1
View File
@@ -405,6 +405,7 @@ class WM8731 : public audio::Codec {
}
uint32_t reg_read(const size_t reg_address) override;
void reg_write(const size_t reg_address, uint32_t value) override;
private:
I2C& bus;

Some files were not shown because too many files have changed in this diff Show More