Skip to content

Commit

Permalink
Add initial IR support
Browse files Browse the repository at this point in the history
  • Loading branch information
andreock committed Sep 9, 2024
1 parent 2c02dfc commit 1a8e3f8
Show file tree
Hide file tree
Showing 18 changed files with 545 additions and 9 deletions.
10 changes: 8 additions & 2 deletions include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

#define BATTERY_MONITOR 2

#define IR_RECEIVER_PIN 6
#define IR_EMITTER_PIN 14

#elif ARDUINO_NANO_ESP32

// Define buttons pins
Expand All @@ -70,9 +73,9 @@
#define SD_CARD_SCK D13

// SX1276(SubGHZ)
#define SX1276_DIO1 D3
#define SX1276_DIO1 D0
#define SX1276_NSS D7
#define SX1276_DIO2 D8
#define SX1276_DIO2 D1
#define SX1276_MISO D9

// PN532(NFC)
Expand All @@ -81,4 +84,7 @@

#define BATTERY_MONITOR A0

#define IR_RECEIVER_PIN D8
#define IR_EMITTER_PIN D3

#endif
116 changes: 116 additions & 0 deletions lib/UI/navigation/IR/IRNavigation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

#include "fm.hpp"
#include "gui.hpp"
#include "../../../../include/pins.h"
#include "pages/IR/IRMainPage.hpp"
#include "pages/IR/IREmulateRC.hpp"
#include "pages/IR/IRRecordSignalPage.hpp"
#include "pages/FileBrowser/FileBrowserPage.hpp"
#include "../navigation.hpp"
#include "ir_attacks.hpp"
#include "ArduinoJson.h"
#include "posixsd.hpp"
#include "sdcard_helper.hpp"

static Gui *gui;

static IRMainPage *ir_main_page = nullptr;
IRRecordSignalPage *ir_record_signal_page = nullptr;
static IREmulateRC *ir_emulate_rc = nullptr;
static IrFramework *ir_framework = nullptr;
static FileBrowserPage *file_browser_page = nullptr;

void goto_ir_gui() {
gui->reset();
ir_main_page = new IRMainPage(3, 0, 1, gui->get_screen(), gui);
gui->set_current_page(ir_main_page);
}

void ir_goto_home() {
init_main_gui();
ir_main_page = nullptr;
delete ir_framework;
ir_framework = nullptr;
}

void goto_ir_record_signal_page() {
gui->reset();
ir_record_signal_page = new IRRecordSignalPage(1, 1, 0, gui->get_screen(), gui);
gui->set_current_page(ir_record_signal_page);
ir_record_signal(ir_record_signal_page, ir_framework);
}

void save_record_to_sd() {
Serial.println("Saving to SD");
RecordedSignal signal = ir_framework->get_decoded_signal();
JsonDocument doc;
doc["protocol"] = signal.protocol;
doc["address"] = signal.address;
doc["command"] = signal.command;
doc["number_of_bits"] = signal.numberOfBits;
doc["extra"] = signal.extra;
doc["decoded_raw_data"] = signal.decodedRawData;
doc["raw_len"] = signal.raw_len;
doc["flags"] = signal.flags;
JsonArray raw_data = doc["raw_data"].to<JsonArray>();
for (size_t i = 0; i < signal.raw_len; i++)
{
raw_data.add(signal.decodedRawDataArray[i]);
}
doc.shrinkToFit();
File res = open("/IR/signals/" + (String)millis() + ".json", "w");
serializeJsonPretty(doc, res);
res.close();
goto_ir_gui();
}

#include <list>

static std::list<string> ir_signal_files;
void send_signal(const char *path) {
ir_send_signal(ir_framework, ("/IR/signals/" + (String)path).c_str());
}

static std::list<string> ir_rc_files;
JsonDocument rc_signals;

void goto_ir_rc_emulator(const char *path) {
gui->reset();
File rc_file = open("/IR/signal_rc/" + (String)path, "r");
deserializeJson(rc_signals, rc_file);
const char* cmds[rc_signals.size()];
for (size_t i = 0; i < rc_signals.size(); i++)
{
cmds[i] = rc_signals[i]["name"].as<const char*>();
}
ir_emulate_rc = new IREmulateRC(rc_signals.size(), 0,1, gui->get_screen(), gui);
ir_emulate_rc->display(cmds, rc_signals.size());
gui->set_current_page(ir_emulate_rc, false);
}

void goto_ir_rc_browser() {
gui->reset();
ir_rc_files = list_dir(open("/IR/signal_rc", "r"));
file_browser_page = new FileBrowserPage(ir_rc_files.size() + 1, 1, 1 , gui->get_screen(), gui);
file_browser_page->display("IR RC Emulator", &ir_rc_files, goto_ir_rc_emulator, goto_ir_gui);
gui->set_current_page(file_browser_page, false);
}

void emulate_ir_rc() {
size_t current_signal_index = ir_emulate_rc->get_current_element();
JsonDocument current_signal = rc_signals[current_signal_index];
ir_send_signal(ir_framework, current_signal);
}

void goto_ir_send() {
gui->reset();
ir_signal_files = list_dir(open("/IR/signals", "r"));
file_browser_page = new FileBrowserPage(ir_signal_files.size() + 1, 1, 1,gui->get_screen(), gui);
file_browser_page->display("IR File Browser", &ir_signal_files, send_signal, goto_ir_gui);
gui->set_current_page(file_browser_page, false);
}

void init_ir_navigation(Gui *_gui) {
gui = _gui;
ir_framework = new IrFramework(IR_RECEIVER_PIN, IR_EMITTER_PIN);
}
12 changes: 12 additions & 0 deletions lib/UI/navigation/IR/IRNavigation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "gui.hpp"

void goto_ir_gui();
void ir_goto_home();
void goto_lg_ac_gui();
void lg_ac_controller_go_back();
void init_ir_navigation(Gui *_gui);
void goto_ir_record_signal_page();
void save_record_to_sd();
void goto_ir_send();
void goto_ir_rc_browser();
void emulate_ir_rc();
12 changes: 6 additions & 6 deletions lib/UI/navigation/navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@
#include "NFC/NFCNavigation.hpp"
#include "buttons/btn_routines.hpp"
#include "gui.hpp"
#include "IR/IRNavigation.hpp"
#include "network_attacks/network_attacks_navigation.hpp"
#include "wifi/wifi_navigation.hpp"
#include "../../../include/debug.h"

#define WIFI_MODULE_POS 0
#define BLE_MODULE_POS 1
#define BADUSB_MODULE_POS 2
#define SUBGHZ_MODULE_POS 3
#define NFC_MODULE_POS 4
#define NETWORK_ATTACKS_MODULE_POS 6

static Gui *gui;

Expand Down Expand Up @@ -64,6 +59,11 @@ void init_nfc_ui() {
goto_nfc_gui();
}

void init_ir_ui() {
init_ir_navigation(gui);
goto_ir_gui();
}

void init_network_attacks_ui() {
init_network_attacks_navigation(gui);
goto_net_attacks_gui();
Expand Down
2 changes: 2 additions & 0 deletions lib/UI/navigation/navigation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ void init_subghz_ui();

void init_nfc_ui();

void init_ir_ui();

void init_network_attacks_ui();
#endif
44 changes: 44 additions & 0 deletions lib/UI/pages/IR/IREmulateRC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "IREmulateRC.hpp"
#include "style.h"
#include "../../navigation/IR/IRNavigation.hpp"

void IREmulateRC::display(const char* cmd[], size_t count)
{
grid = new Grid(screen, 2, 4);
for (size_t i = 0; i < count; i++)
{
grid->add(new RectText(screen, cmd[i], HOME_TEXT_SIZE, HOME_TEXT_COLOR, HOME_ICON_HEIGHT,
HOME_ICON_RADIUS, HOME_ICON_COLOR, emulate_ir_rc));
}
go_back = new List(screen, "Go back", 2, ST77XX_WHITE, 20, ST77XX_BLACK, goto_ir_gui);
grid->add(go_back);
grid->set_pos(0, 10);
grid->set_space_between(10);
grid->set_padding(0, 20);
grid->display();
grid->set_selected(0, true);
}

void IREmulateRC::right() {
if(position_limit <= 4)
return;
if (current_position >= 0 && current_position < position_limit) {
grid->set_selected(current_position, false);
current_position++;
}
grid->set_selected(current_position, true);
}

void IREmulateRC::left() {
if(position_limit <= 4)
return;
if (current_position > 0 && current_position <= position_limit) {
grid->set_selected(current_position, false);
current_position--;
}
grid->set_selected(current_position, true);
}

void IREmulateRC::display() {

}
46 changes: 46 additions & 0 deletions lib/UI/pages/IR/IREmulateRC.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* 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, version 3.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../Page.hpp"
#include "Grid.hpp"
#include "List.hpp"
#include "widgets/RectText.hpp"

#ifndef IR_EMULATE_RC_PAGE_H
#define IR_EMULATE_RC_PAGE_H

class IREmulateRC : public Page {
private:
List *go_back;

public:
IREmulateRC(uint8_t _position_limit, uint8_t _lower_limit,
uint8_t _position_increment, GFXForms *screen, Gui *_gui) : Page(_position_limit, _lower_limit, _position_increment, screen, _gui) {};
~IREmulateRC() {};
void display(const char* cmd[], size_t count);
void display();
void set_selected(int pos, bool status) {
grid->set_selected(pos, status);
};
void click(int pos, void callback()) { grid->click(pos, callback); };
void left();
void right();
size_t get_current_element() { return grid->get_selected(); };
};

#endif
44 changes: 44 additions & 0 deletions lib/UI/pages/IR/IRMainPage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* 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, version 3.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/

#include "IRMainPage.hpp"

#include "../../i18n.hpp"
#include "../../navigation/IR/IRNavigation.hpp"
#include "gui.hpp"

IRMainPage::~IRMainPage() {}

void mock() {

}

void IRMainPage::display() {
grid = new Grid(screen, 4, 1);
record_signal = new List(screen, "Record signal", 2, ST77XX_WHITE, 20, ST77XX_BLACK, goto_ir_record_signal_page);
emulate_signal = new List(screen, "Emulate signal", 2, ST77XX_WHITE, 20, ST77XX_BLACK, goto_ir_send);
emulate_rc = new List(screen, "Emulate RC", 2, ST77XX_WHITE, 20, ST77XX_BLACK, goto_ir_rc_browser);
go_back = new List(screen, "Go back", 2, ST77XX_WHITE, 20, ST77XX_BLACK, ir_goto_home);

grid->add(record_signal);
grid->add(emulate_signal);
grid->add(emulate_rc);
grid->add(go_back);
grid->set_selected(0, true);
grid->display();
}
46 changes: 46 additions & 0 deletions lib/UI/pages/IR/IRMainPage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* 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, version 3.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../Page.hpp"
#include "Grid.hpp"
#include "List.hpp"

#ifndef IR_MAIN_PAGE_H
#define IR_MAIN_PAGE_H

class IRMainPage : public Page {
private:
List *record_signal;
List *emulate_signal;
List *emulate_list;
List *emulate_rc;
List *go_back;

public:
IRMainPage(uint8_t _position_limit, uint8_t _lower_limit,
uint8_t _position_increment, GFXForms *screen, Gui *_gui) : Page(_position_limit, _lower_limit, _position_increment, screen, _gui) {};
~IRMainPage();
void display();

void set_selected(int pos, bool status) {
grid->set_selected(pos, status);
};
void click(int pos, void callback()) { grid->click(pos, callback); };
};

#endif
Loading

0 comments on commit 1a8e3f8

Please sign in to comment.