From 3537e9a1bb318cf44667f39e55b08e4734198285 Mon Sep 17 00:00:00 2001 From: andreock Date: Fri, 3 Jan 2025 02:10:42 +0100 Subject: [PATCH] Add Settings page --- lib/SubGHZ/SubGHZ.cpp | 40 ++++++ lib/SubGHZ/SubGHZ.hpp | 18 +++ .../Settings/SettingsNavigation.cpp | 46 +++++++ .../Settings/SettingsNavigation.hpp | 22 ++++ lib/UI/navigation/SubGHZ/SubGHZNavigation.cpp | 31 +++++ lib/UI/navigation/SubGHZ/SubGHZNavigation.hpp | 18 ++- lib/UI/pages/Settings/SettingsPage.cpp | 115 ++++++++++++++++++ lib/UI/pages/Settings/SettingsPage.hpp | 83 +++++++++++++ lib/UI/pages/main_page/MainPage.cpp | 4 +- lib/power_management/DeepSleep.cpp | 14 ++- lib/power_management/DeepSleep.hpp | 5 +- src/main.cpp | 3 +- 12 files changed, 392 insertions(+), 7 deletions(-) create mode 100644 lib/UI/navigation/Settings/SettingsNavigation.cpp create mode 100644 lib/UI/navigation/Settings/SettingsNavigation.hpp create mode 100644 lib/UI/pages/Settings/SettingsPage.cpp create mode 100644 lib/UI/pages/Settings/SettingsPage.hpp diff --git a/lib/SubGHZ/SubGHZ.cpp b/lib/SubGHZ/SubGHZ.cpp index f435bf8..f1b485e 100644 --- a/lib/SubGHZ/SubGHZ.cpp +++ b/lib/SubGHZ/SubGHZ.cpp @@ -1,3 +1,20 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + #include "SubGHZ.hpp" #include @@ -96,6 +113,29 @@ void SubGHZ::init_receive() { radio.receiveDirect(); } +int16_t SubGHZ::get_chip_version() { + int16_t version = 0; +#ifndef CC1101_SUBGHZ + SPI2.begin(SX1276_SCK, SX1276_MISO, SX1276_MOSI, _csn); + if(radio.beginFSK() == RADIOLIB_ERR_NONE) { + version = radio.getChipVersion(); + } +#else + SPI2.begin(CC1101_SCK, CC1101_MISO, CC1101_MOSI, _csn); + digitalWrite(CC1101_CS, HIGH); + + int state = radio.begin(315); + if (state != RADIOLIB_ERR_NONE) { + Serial.print("Error initializing CC1101"); + Serial.printf("Status: %i\n", state); + } else { + Serial.println("Initialized correctly"); + version = radio.getChipVersion(); + } +#endif + return version; +} + void SubGHZ::stop_receive() { #ifdef ESP32S3_DEVKITC_BOARD radio.clearDio1Action(); diff --git a/lib/SubGHZ/SubGHZ.hpp b/lib/SubGHZ/SubGHZ.hpp index 7dd11d3..bebd751 100644 --- a/lib/SubGHZ/SubGHZ.hpp +++ b/lib/SubGHZ/SubGHZ.hpp @@ -1,3 +1,20 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + #ifndef SUBGHZ_LIBRARY_H #define SUBGHZ_LIBRARY_H @@ -53,6 +70,7 @@ class SubGHZ { SignalStrength scan_frequency(float freq, float bw = 200.0F, float deviation = 47.0F); SignalStrength scan_frequency(); + int16_t get_chip_version(); void rec_signal(std::vector *orig_signal); void send_signal(float freq, Modulation modulation, byte *payload, size_t length); diff --git a/lib/UI/navigation/Settings/SettingsNavigation.cpp b/lib/UI/navigation/Settings/SettingsNavigation.cpp new file mode 100644 index 0000000..a698a55 --- /dev/null +++ b/lib/UI/navigation/Settings/SettingsNavigation.cpp @@ -0,0 +1,46 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + +#include "../../pages/Settings/SettingsPage.hpp" +#include "gui.hpp" +#include "../navigation.hpp" +#include "../../navigation/SubGHZ/SubGHZNavigation.hpp" + +static SettingsPage *settings = nullptr; +static Gui *gui = nullptr; + +void goto_back_to_home() { + init_main_gui(); +} + +void goto_settings_ui() { + String subghz_rev = get_subghz_chip_revision(); + size_t pos_limit = SD.cardType() != CARD_NONE ? 11 : 9; + size_t lower_limit = SD.cardType() != CARD_NONE ? 5 : 3; + #if !defined(WAKEUP_PIN) + pos_limit--; + lower_limit--; + #endif + settings = new SettingsPage(pos_limit, lower_limit, 1, gui->get_screen(), gui); + gui->reset(); + settings->display(subghz_rev); + gui->set_current_page(settings, false); +} + +void init_settings_navigation(Gui *_gui) { + gui = _gui; +} \ No newline at end of file diff --git a/lib/UI/navigation/Settings/SettingsNavigation.hpp b/lib/UI/navigation/Settings/SettingsNavigation.hpp new file mode 100644 index 0000000..d614ba7 --- /dev/null +++ b/lib/UI/navigation/Settings/SettingsNavigation.hpp @@ -0,0 +1,22 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + +#include "gui.hpp" + +void goto_settings_ui(); +void init_settings_navigation(Gui *_gui); +void goto_back_to_home(); \ No newline at end of file diff --git a/lib/UI/navigation/SubGHZ/SubGHZNavigation.cpp b/lib/UI/navigation/SubGHZ/SubGHZNavigation.cpp index aaa8426..e1f5bb8 100644 --- a/lib/UI/navigation/SubGHZ/SubGHZNavigation.cpp +++ b/lib/UI/navigation/SubGHZ/SubGHZNavigation.cpp @@ -1,3 +1,20 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + #include #include "../../../../include/debug.h" @@ -176,3 +193,17 @@ void init_subghz_navigation(Gui *_gui) { CC1101_IO0, CC1101_IO2); #endif } + +String get_subghz_chip_revision() { +#ifndef CC1101_SUBGHZ + subghz_module = new SubGHZ(SD_CARD_SCK, SX1276_MISO, SD_CARD_MOSI, SX1276_NSS, + SX1276_DIO1, SX1276_DIO2); +#else + subghz_module = new SubGHZ(SD_CARD_SCK, CC1101_MISO, SD_CARD_MOSI, CC1101_CS, + CC1101_IO0, CC1101_IO2); +#endif + String version = ""; + version = subghz_module->get_chip_version(); + delete subghz_module; + return version; +} diff --git a/lib/UI/navigation/SubGHZ/SubGHZNavigation.hpp b/lib/UI/navigation/SubGHZ/SubGHZNavigation.hpp index 0006f64..d30237f 100644 --- a/lib/UI/navigation/SubGHZ/SubGHZNavigation.hpp +++ b/lib/UI/navigation/SubGHZ/SubGHZNavigation.hpp @@ -1,3 +1,19 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ #ifndef SUBGHZ_NAVIGATION_H #define SUBGHZ_NAVIGATION_H @@ -22,5 +38,5 @@ void set_subghz_sender_freq(float freq); void set_subghz_sender_bandwidth(float bandwidth); void set_subghz_sender_deviation(float deviation); void set_subghz_sender_modulation(int modulation); - +String get_subghz_chip_revision(); #endif diff --git a/lib/UI/pages/Settings/SettingsPage.cpp b/lib/UI/pages/Settings/SettingsPage.cpp new file mode 100644 index 0000000..7766bf9 --- /dev/null +++ b/lib/UI/pages/Settings/SettingsPage.cpp @@ -0,0 +1,115 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + +#include "SettingsPage.hpp" +#include "../../i18n.hpp" +#include "../../navigation/NFC/NFCNavigation.hpp" +#include "../../navigation/Settings/SettingsNavigation.hpp" +#include "gui.hpp" +#include "../../../../include/device_info.h" +#include "battery_monitor.hpp" +#include "DeepSleep.hpp" + +extern String fw_md5; // From main.cpp + +SettingsPage::~SettingsPage() { + delete version; + delete chip; + delete board_widget; + delete heap_usage; + delete battery; + if(sd_card_installed()) { + delete sd_card; + delete sd_card_size; + } + delete nfc_version; + delete subghz; +#if defined(WAKEUP_PIN) + delete deep_sleep; +#endif + delete reboot; + delete go_back; +} + +void SettingsPage::display(String subghz_rev) { + bool _sd_card_installed = sd_card_installed(); + + grid = new Grid(screen, _sd_card_installed ? 12 : 9, 1); + grid->set_y_spacing(10); + + version = new Text(screen, ST77XX_WHITE, (String)"Version: " + VERSION + " " + fw_md5.substring(0, 6)); + String board = ""; +#ifdef ESP32S3_DEVKITC_BOARD + board = "ESP32-S3 DevKitC"; +#elif ARDUINO_NANO_ESP32 + board = "Arduino Nano ESP32"; +#elif LILYGO_T_EMBED_CC1101 + board = "LilyGo T-Embed CC1101"; +#else + board = "Unknown"; +#endif + board_widget = new Text(screen, ST77XX_WHITE, board); + heap_usage = new Text(screen, ST77XX_WHITE, "Heap: " +(String)((ESP.getHeapSize() - ESP.getFreeHeap()) / (1024)) + "/" + (ESP.getHeapSize() / (1024)) + "KB"); + chip = new Text(screen, ST77XX_WHITE, (String)"Chip: " + DEVICE + " " + ESP.getChipRevision() + " " + ESP.getCpuFreqMHz() + " MHz"); + battery = new Text(screen, ST77XX_WHITE, (String)"Battery: " + read_battery_level() + "%"); + + if(_sd_card_installed) { + sd_card = new Text(screen, ST77XX_WHITE, "SD Card type: " + get_current_sd_type()); + sd_card_size = new Text(screen, ST77XX_WHITE, (String)"SD Card usage: " + (float)SD.usedBytes() / (1024 * 1024 * 1024) + "/" + SD.cardSize() / (1024 * 1024 * 1024) + "GB"); + } + + nfc_version = new Text(screen, ST77XX_WHITE, "PN532 version: " + get_current_pn532_version()); + + #ifdef CC1101_SUBGHZ + String subghz_module = "CC1101"; + #else + String subghz_module = "SX1276"; + #endif + subghz = new Text(screen, ST77XX_WHITE, "SubGHZ module: " + subghz_module + " 0x" + subghz_rev); + +#if defined(WAKEUP_PIN) + deep_sleep = new List(screen, "Deep Sleep", 2, ST77XX_WHITE, 20, ST77XX_BLACK, go_deep_sleep); +#endif + reboot = new List(screen, "Reboot", 2, ST77XX_WHITE, 20, ST77XX_BLACK, [] () { ESP.restart(); }); + go_back = new List(screen, "Go back", 2, ST77XX_WHITE, 20, ST77XX_BLACK, goto_back_to_home); + + grid->add(version); + grid->add(chip); + grid->add(board_widget); + grid->add(heap_usage); + grid->add(battery); + + if(_sd_card_installed) { + grid->add(sd_card); + grid->add(sd_card_size); + } + + grid->add(nfc_version); + grid->add(subghz); + +#if defined(WAKEUP_PIN) + grid->add(deep_sleep); +#endif + grid->add(reboot); + grid->add(go_back); + size_t lower_limit = _sd_card_installed ? 5 : 3; + #if !defined(WAKEUP_PIN) + lower_limit--; + #endif + grid->set_selected(lower_limit, true); + grid->display(); +} \ No newline at end of file diff --git a/lib/UI/pages/Settings/SettingsPage.hpp b/lib/UI/pages/Settings/SettingsPage.hpp new file mode 100644 index 0000000..f5ff5e4 --- /dev/null +++ b/lib/UI/pages/Settings/SettingsPage.hpp @@ -0,0 +1,83 @@ +/* + * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or + * https://capibarazero.github.io/). Copyright (c) 2025 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 . + */ + +#include "../Page.hpp" +#include "Grid.hpp" +#include "List.hpp" +#include "SD.h" + +#ifndef SETTINGS_PAGE_H +#define SETTINGS_PAGE_H + +class SettingsPage : public Page { + private: + Text *version; + Text *chip; + Text *board_widget; + Text *heap_usage; + Text *battery; + Text *sd_card; + Text *sd_card_size; + Text *nfc_version; + Text *subghz; + List *deep_sleep; + List *reboot; + List *go_back; + List *empty; + + bool sd_card_installed() { + return SD.cardType() != CARD_NONE; + } + + String get_current_sd_type() { + uint8_t cardType = SD.cardType(); + switch (cardType) { + case CARD_NONE: + return "None"; + break; + case CARD_MMC: + return "MMC"; + break; + case CARD_SD: + return "SDSC"; + break; + case CARD_SDHC: + return "SDHC"; + break; + default: + return "Unknown"; + break; + } + } + public: + SettingsPage(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) { + }; + ~SettingsPage(); + void display() {}; + void display(String subghz_rev); + void click(int pos, void callback()) { + grid->click(pos, callback); + }; + void set_selected(int pos, bool status) { + grid->set_pos(pos, status); + }; +}; + +#endif \ No newline at end of file diff --git a/lib/UI/pages/main_page/MainPage.cpp b/lib/UI/pages/main_page/MainPage.cpp index bfb2070..0c73f93 100644 --- a/lib/UI/pages/main_page/MainPage.cpp +++ b/lib/UI/pages/main_page/MainPage.cpp @@ -1,6 +1,6 @@ /* * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or - * https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale. + * https://capibarazero.github.io/). Copyright (c) 2025 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 @@ -62,7 +62,7 @@ void MainPage::display() { settings = new RectText(screen, english_words->at(SETTINGS_HOME_KEY), HOME_TEXT_SIZE, HOME_TEXT_COLOR, HOME_ICON_HEIGHT, - HOME_ICON_RADIUS, HOME_ICON_COLOR, section_not_ready); + HOME_ICON_RADIUS, HOME_ICON_COLOR, init_settings_ui); grid = new Grid(screen, 2, 4); diff --git a/lib/power_management/DeepSleep.cpp b/lib/power_management/DeepSleep.cpp index 98b109d..206f62f 100644 --- a/lib/power_management/DeepSleep.cpp +++ b/lib/power_management/DeepSleep.cpp @@ -1,6 +1,6 @@ /* * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or - * https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale. + * https://capibarazero.github.io/). Copyright (c) 2025 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 @@ -28,6 +28,18 @@ static void IRAM_ATTR standby() { ESP_EXT1_WAKEUP_ANY_LOW); esp_deep_sleep_start(); } + +#endif + +#if defined(WAKEUP_PIN) + +void go_deep_sleep() { // For usage in settings + digitalWrite(15, LOW); // Power off CC1101 and LED + esp_sleep_enable_ext1_wakeup(GPIO_BITMASK(WAKEUP_PIN), + ESP_EXT1_WAKEUP_ANY_LOW); + esp_deep_sleep_start(); +} + #endif #if defined(STANDBY_BTN) && defined(WAKEUP_PIN) diff --git a/lib/power_management/DeepSleep.hpp b/lib/power_management/DeepSleep.hpp index bc2920e..05ee3bb 100644 --- a/lib/power_management/DeepSleep.hpp +++ b/lib/power_management/DeepSleep.hpp @@ -1,6 +1,6 @@ /* * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or - * https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale. + * https://capibarazero.github.io/). Copyright (c) 2025 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 @@ -15,4 +15,5 @@ * along with this program. If not, see . */ -void enable_deep_sleep(); \ No newline at end of file +void enable_deep_sleep(); +void go_deep_sleep(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5ff1ccb..de2e943 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ /* * This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or - * https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale. + * https://capibarazero.github.io/). Copyright (c) 2025 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 @@ -42,6 +42,7 @@ Gui *main_gui; Adafruit_ST7789 *display; GFXForms *screen; SPIClass display_spi; +String fw_md5 = ESP.getSketchMD5(); // Calculate MD5 at startup(save time when loading settings) #ifdef ARDUINO_NANO_ESP32 Peripherals_Arduino_Nano_ESP32 peripherals = Peripherals_Arduino_Nano_ESP32();