From 5078a194aaa2d9761659aebc33d33b4a5375cac2 Mon Sep 17 00:00:00 2001 From: Incursio Hack - Ruan Rocha <114836217+IncursioHack@users.noreply.github.com> Date: Thu, 9 May 2024 18:02:58 -0300 Subject: [PATCH] Inclusion of the RF433R and the Spectrum option. On Spectrum it is possible to visualize the signal wave, this code was adapted for Cardputer and was based on the @aat440hz project. --- src/main.cpp | 5 ++-- src/rf.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rf.h | 16 +++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/rf.cpp create mode 100644 src/rf.h diff --git a/src/main.cpp b/src/main.cpp index a2697cac2..26056a775 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,7 @@ TFT_eSprite draw = TFT_eSprite(&tft); #include "evil_portal.h" #include "clients.h" #include "arp.h" +#include "rf.h" #ifdef CARDPUTER @@ -200,7 +201,7 @@ void loop() { options = { {"Scan/copy", [=]() { displayRedStripe("Scan/Copy"); }}, {"Replay", [=]() { displayRedStripe("Replay"); }}, - {"Spectrum", [=]() { displayRedStripe("Spectrum"); }}, + {"Spectrum", [=]() { rf_spectrum(); }}, //@IncursioHack {"Main Menu", [=]() { backToMenu(); }}, }; delay(200); @@ -209,7 +210,7 @@ void loop() { break; case 3: // RFID options = { - {"Scan/copy", [=]() { rfid_setup(); }}, + {"Scan/copy", [=]() { rfid_setup(); }}, //@IncursioHack {"Replay", [=]() { displayRedStripe("Replay"); }}, {"Main Menu", [=]() { backToMenu(); }}, }; diff --git a/src/rf.cpp b/src/rf.cpp new file mode 100644 index 000000000..23c8b5620 --- /dev/null +++ b/src/rf.cpp @@ -0,0 +1,79 @@ +//@IncursioHack - github.com/IncursioHack + +#include "rf.h" +#include "globals.h" +#include "mykeyboard.h" +#include "display.h" +#include "PCA9554.h" +#include "sd_functions.h" +#include + +// Cria um objeto PCA9554 com o endereço I2C do PCA9554PW +// PCA9554 extIo1(pca9554pw_address); + +#define RMT_RX_CHANNEL RMT_CHANNEL_6 +#define RMT_BLOCK_NUM 1 + +#define RMT_CLK_DIV 80 /*!< RMT counter clock divider */ +#define RMT_1US_TICKS (80000000 / RMT_CLK_DIV / 1000000) +#define RMT_1MS_TICKS (RMT_1US_TICKS * 1000) + +#define SIGNAL_STRENGTH_THRESHOLD 1500 // Adjust this threshold as needed + +#define DISPLAY_HEIGHT 130 // Height of the display area for the waveform +#define DISPLAY_WIDTH 240 // Width of the display area +#define LINE_WIDTH 2 // Adjust line width as needed + +void initRMT() { + rmt_config_t rxconfig; + rxconfig.rmt_mode = RMT_MODE_RX; + rxconfig.channel = RMT_RX_CHANNEL; + rxconfig.gpio_num = gpio_num_t(GROVE_SCL); + rxconfig.mem_block_num = RMT_BLOCK_NUM; + rxconfig.clk_div = RMT_CLK_DIV; + rxconfig.rx_config.filter_en = true; + rxconfig.rx_config.filter_ticks_thresh = 200 * RMT_1US_TICKS; + rxconfig.rx_config.idle_threshold = 3 * RMT_1MS_TICKS; + + ESP_ERROR_CHECK(rmt_config(&rxconfig)); + ESP_ERROR_CHECK(rmt_driver_install(rxconfig.channel, 2048, 0)); +} + +void rf_spectrum() { //@IncursioHack - https://github.com/IncursioHack ----thanks @aat440hz - RF433ANY-M5Cardputer + + tft.fillScreen(TFT_BLACK); + tft.setTextSize(1); + tft.println(""); + tft.println(" RF433 - Spectrum"); + pinMode(GROVE_SCL, INPUT); + initRMT(); + + RingbufHandle_t rb = nullptr; + rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb); + rmt_rx_start(RMT_RX_CHANNEL, true); + while (rb) { + size_t rx_size = 0; + rmt_item32_t* item = (rmt_item32_t*)xRingbufferReceive(rb, &rx_size, 500); + if (item != nullptr) { + if (rx_size != 0) { + // Clear the display area + tft.fillRect(0, 20, DISPLAY_WIDTH, DISPLAY_HEIGHT, TFT_BLACK); + // Draw waveform based on signal strength + for (size_t i = 0; i < rx_size; i++) { + int lineHeight = map(item[i].duration0 + item[i].duration1, 0, SIGNAL_STRENGTH_THRESHOLD, 0, DISPLAY_HEIGHT/2); + int lineX = map(i, 0, rx_size - 1, 0, DISPLAY_WIDTH - 1); // Map i to within the display width + // Ensure drawing coordinates stay within the box bounds + int startY = constrain(20 + DISPLAY_HEIGHT / 2 - lineHeight / 2, 20, 20 + DISPLAY_HEIGHT); + int endY = constrain(20 + DISPLAY_HEIGHT / 2 + lineHeight / 2, 20, 20 + DISPLAY_HEIGHT); + tft.drawLine(lineX, startY, lineX, endY, TFT_PURPLE); + } + } + vRingbufferReturnItem(rb, (void*)item); + } + } + // Checks para sair do while + + rmt_rx_stop(RMT_RX_CHANNEL); + delay(10); + +} \ No newline at end of file diff --git a/src/rf.h b/src/rf.h new file mode 100644 index 000000000..0868311df --- /dev/null +++ b/src/rf.h @@ -0,0 +1,16 @@ +// @IncursioHack - https://github.com/IncursioHack +#include +#include +#include "PCA9554.h" // Biblioteca para PCA9554 + +// Define o endereço I2C do PCA9554PW +const int pca9554pw_address = 0x27; + +// Cria um objeto PCA9554 com o endereço I2C do PCA9554PW +extern PCA9554 extIo1; + +// Define os pinos para o receptor e transmissor +const int PCA9554RSX_PIN = 4; +const int PCA9554TRX_PIN = 0; + +void rf_spectrum(); \ No newline at end of file