From ff1fc3b181f1231fe2a05763f1b8180f93715bc4 Mon Sep 17 00:00:00 2001 From: Rennan Cockles Date: Thu, 3 Oct 2024 22:18:17 -0300 Subject: [PATCH 1/3] new connect menu --- src/core/display.cpp | 16 ++++++------- src/core/display.h | 8 +++---- src/core/main_menu.cpp | 1 + src/core/main_menu.h | 10 ++++---- src/core/menu_items/ConnectMenu.cpp | 36 +++++++++++++++++++++++++++++ src/core/menu_items/ConnectMenu.h | 18 +++++++++++++++ 6 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 src/core/menu_items/ConnectMenu.cpp create mode 100644 src/core/menu_items/ConnectMenu.h diff --git a/src/core/display.cpp b/src/core/display.cpp index 6d346605..6265583a 100644 --- a/src/core/display.cpp +++ b/src/core/display.cpp @@ -88,27 +88,27 @@ void displayRedStripe(String text, uint16_t fgcolor, uint16_t bgcolor) { tft.println(text); } -void displayError(String txt) { +void displayError(String txt, bool waitKeyPress) { #ifndef HAS_SCREEN Serial.println("ERR: " + txt); return; #endif displayRedStripe(txt); delay(200); - while(!checkAnyKeyPress()) delay(100); + while(waitKeyPress && !checkAnyKeyPress()) delay(100); } -void displayWarning(String txt) { +void displayWarning(String txt, bool waitKeyPress) { #ifndef HAS_SCREEN Serial.println("WARN: " + txt); return; #endif displayRedStripe(txt, TFT_BLACK,TFT_YELLOW); delay(200); - while(!checkAnyKeyPress()) delay(100); + while(waitKeyPress && !checkAnyKeyPress()) delay(100); } -void displayInfo(String txt) { +void displayInfo(String txt, bool waitKeyPress) { #ifndef HAS_SCREEN Serial.println("INFO: " + txt); return; @@ -116,10 +116,10 @@ void displayInfo(String txt) { // todo: add newlines to txt if too long displayRedStripe(txt, TFT_WHITE, TFT_BLUE); delay(200); - while(!checkAnyKeyPress()) delay(100); + while(waitKeyPress && !checkAnyKeyPress()) delay(100); } -void displaySuccess(String txt) { +void displaySuccess(String txt, bool waitKeyPress) { #ifndef HAS_SCREEN Serial.println("SUCCESS: " + txt); return; @@ -127,7 +127,7 @@ void displaySuccess(String txt) { // todo: add newlines to txt if too long displayRedStripe(txt, TFT_WHITE, TFT_DARKGREEN); delay(200); - while(!checkAnyKeyPress()) delay(100); + while(waitKeyPress && !checkAnyKeyPress()) delay(100); } void setPadCursor(int16_t padx, int16_t pady) { diff --git a/src/core/display.h b/src/core/display.h index 8402a6c7..e84db5ab 100644 --- a/src/core/display.h +++ b/src/core/display.h @@ -23,10 +23,10 @@ void setTftDisplay(int x = 0, int y = 0, uint16_t fc = tft.textcolor, int size = void displayRedStripe(String text, uint16_t fgcolor = TFT_WHITE, uint16_t bgcolor = TFT_RED); -void displayError(String txt); // Faixa vermelha -void displayWarning(String txt);// Faixa amarela -void displayInfo(String txt); // Faixa Azul -void displaySuccess(String txt);// Faixa Verde +void displayError(String txt, bool waitKeyPress = false); // Faixa vermelha +void displayWarning(String txt, bool waitKeyPress = false);// Faixa amarela +void displayInfo(String txt, bool waitKeyPress = false); // Faixa Azul +void displaySuccess(String txt, bool waitKeyPress = false);// Faixa Verde void setPadCursor(int16_t padx=1, int16_t pady=0); diff --git a/src/core/main_menu.cpp b/src/core/main_menu.cpp index 2a7c95b3..a8f97dfb 100644 --- a/src/core/main_menu.cpp +++ b/src/core/main_menu.cpp @@ -19,6 +19,7 @@ MainMenu::MainMenu() { #endif &othersMenu, &clockMenu, + &connectMenu, &configMenu, }; diff --git a/src/core/main_menu.h b/src/core/main_menu.h index dbabb593..04b7ee7c 100644 --- a/src/core/main_menu.h +++ b/src/core/main_menu.h @@ -6,29 +6,31 @@ #include "menu_items/BleMenu.h" #include "menu_items/ClockMenu.h" #include "menu_items/ConfigMenu.h" +#include "menu_items/ConnectMenu.h" #include "menu_items/FMMenu.h" #include "menu_items/IRMenu.h" +#include "menu_items/NRF24.h" #include "menu_items/OthersMenu.h" #include "menu_items/RFIDMenu.h" #include "menu_items/RFMenu.h" -#include "menu_items/WifiMenu.h" #include "menu_items/ScriptsMenu.h" -#include "menu_items/NRF24.h" +#include "menu_items/WifiMenu.h" class MainMenu { public: BleMenu bleMenu; ClockMenu clockMenu; + ConnectMenu connectMenu; ConfigMenu configMenu; FMMenu fmMenu; IRMenu irMenu; + NRF24Menu nrf24Menu; OthersMenu othersMenu; RFIDMenu rfidMenu; RFMenu rfMenu; - WifiMenu wifiMenu; ScriptsMenu scriptsMenu; - NRF24Menu nrf24Menu; + WifiMenu wifiMenu; MainMenu(); ~MainMenu(); diff --git a/src/core/menu_items/ConnectMenu.cpp b/src/core/menu_items/ConnectMenu.cpp new file mode 100644 index 00000000..dea1f379 --- /dev/null +++ b/src/core/menu_items/ConnectMenu.cpp @@ -0,0 +1,36 @@ + +#include "ConnectMenu.h" +#include "core/display.h" +#include "core/settings.h" +#include "core/connect.h" +#include "core/wifi_common.h" + + +void ConnectMenu::optionsMenu() { + options = { + {"Send File", [=]() { DeviceConnection().sendFile(); }}, + {"Receive File", [=]() { DeviceConnection().receiveFile(); }}, + {"Main Menu", [=]() { backToMenu(); }}, + }; + + delay(200); + loopOptions(options,false,true,getName()); +} + +String ConnectMenu::getName() { + return _name; +} + +void ConnectMenu::draw() { + tft.fillRect(iconX,iconY,80,80,BGCOLOR); + + tft.fillCircle(15+iconX, 40+iconY, 7, FGCOLOR); + + tft.fillCircle(55+iconX, 20+iconY, 7, FGCOLOR); + tft.fillCircle(65+iconX, 40+iconY, 7, FGCOLOR); + tft.fillCircle(55+iconX, 60+iconY, 7, FGCOLOR); + + tft.drawLine(15+iconX, 40+iconY, 55+iconX, 20+iconY, FGCOLOR); + tft.drawLine(15+iconX, 40+iconY, 65+iconX, 40+iconY, FGCOLOR); + tft.drawLine(15+iconX, 40+iconY, 55+iconX, 60+iconY, FGCOLOR); +} \ No newline at end of file diff --git a/src/core/menu_items/ConnectMenu.h b/src/core/menu_items/ConnectMenu.h new file mode 100644 index 00000000..b92f5487 --- /dev/null +++ b/src/core/menu_items/ConnectMenu.h @@ -0,0 +1,18 @@ +#ifndef __CONNECT_MENU_H__ +#define __CONNECT_MENU_H__ + +#include "MenuItemInterface.h" + + +class ConnectMenu : public MenuItemInterface { +public: + void optionsMenu(void); + void draw(void); + String getName(void); + +private: + String _name = "Connect"; + +}; + +#endif From 4589b1956991064cdd2cf9fe8e1d5ad875b01ef2 Mon Sep 17 00:00:00 2001 From: Rennan Cockles Date: Thu, 3 Oct 2024 23:39:19 -0300 Subject: [PATCH 2/3] device connection module --- src/core/connect.cpp | 297 +++++++++++++++++++++++++++++++++++++++++++ src/core/connect.h | 71 +++++++++++ 2 files changed, 368 insertions(+) create mode 100644 src/core/connect.cpp create mode 100644 src/core/connect.h diff --git a/src/core/connect.cpp b/src/core/connect.cpp new file mode 100644 index 00000000..088b1006 --- /dev/null +++ b/src/core/connect.cpp @@ -0,0 +1,297 @@ +/** + * @file connect.cpp + * @author Rennan Cockles (https://github.com/rennancockles) + * @brief Connection between devices + * @version 0.1 + * @date 2024-10-02 + */ + + +#include "connect.h" +#include +#include +#include +#include "core/mykeyboard.h" +#include "core/display.h" + + +DeviceConnection::Status espnowSendStatus; +std::vector recvQueue; + + +DeviceConnection::DeviceConnection() {} +DeviceConnection::~DeviceConnection() { esp_now_deinit(); } + + +void DeviceConnection::sendFile() { + drawMainBorderWithTitle("SEND FILE"); + + sendStatus = CONNECTING; + WiFi.mode(WIFI_STA); + + if (esp_now_init() != ESP_OK) { + displayError("Error initializing share"); + returnToMenu=true; + delay(1000); + return; + } + + if (!setupPeer()) { + displayError("Failed to add peer"); + returnToMenu=true; + delay(1000); + return; + } + + File file = selectFile(); + if (!file) { + displayError("Error selecting file"); + returnToMenu=true; + delay(1000); + return; + } + + FileMessage message = createFileMessage(file); + + esp_now_register_send_cb(onDataSent); + + esp_err_t response; + sendStatus = STARTED; + espnowSendStatus = STARTED; + + drawMainBorderWithTitle("SEND FILE"); + padprintln(""); + padprintln("Sending..."); + + delay(100); + + while (file.available()) { + if (espnowSendStatus == FAILED) sendStatus = FAILED; + + if (checkEscPress()) sendStatus = ABORTED; + + if (sendStatus == ABORTED || sendStatus == FAILED) { + message.done = true; + message.dataSize = 0; + esp_now_send(broadcastAddress, (uint8_t*)&message, sizeof(message)); + displayError("Error sending file"); + break; + } + + size_t bytesRead = file.readBytes(message.data, PAGE_BYTES); + message.dataSize = bytesRead; + message.bytesSent = min(message.bytesSent+bytesRead, message.totalBytes); + message.done = message.bytesSent == message.totalBytes; + + response = esp_now_send(broadcastAddress, (uint8_t*)&message, sizeof(message)); + if (response != ESP_OK) { + Serial.printf("Send file response: %s\n", esp_err_to_name(response)); + sendStatus = FAILED; + } + + progressHandler(file.position(), file.size(), "Sending..."); + delay(100); + } + + if (message.bytesSent == message.totalBytes) displaySuccess("File sent"); + + file.close(); + esp_now_unregister_send_cb(); + returnToMenu=true; + delay(1000); +} + + +void DeviceConnection::receiveFile() { + drawMainBorderWithTitle("RECEIVE FILE"); + padprintln(""); + padprintln("Waiting..."); + + recvFileName = ""; + recvQueue = {}; + recvStatus = CONNECTING; + WiFi.mode(WIFI_STA); + + if (esp_now_init() != ESP_OK) { + displayError("Error initializing share"); + delay(1000); + return; + } + + esp_now_register_recv_cb(onDataRecv); + + delay(100); + + while(1) { + if (checkEscPress()) recvStatus = ABORTED; + + if (recvStatus == ABORTED || recvStatus == FAILED) { + displayError("Error receiving file"); + break; + } + if (recvStatus == SUCCESS) { + displaySuccess("File received"); + break; + } + + if (!recvQueue.empty()) { + FileMessage recvFileMessage = recvQueue.front(); + recvQueue.erase(recvQueue.begin()); + + progressHandler(recvFileMessage.bytesSent, recvFileMessage.totalBytes, "Receiving..."); + + if (!appendToFile(recvFileMessage)) { + recvStatus = FAILED; + Serial.println("Append file fail"); + } + if (recvFileMessage.done) { + Serial.println("Recv done"); + recvStatus = ( + recvFileMessage.bytesSent == recvFileMessage.totalBytes + ? SUCCESS + : FAILED + ); + } + } + + delay(100); + } + + esp_now_unregister_recv_cb(); + delay(1000); + + if (recvStatus == SUCCESS) { + drawMainBorderWithTitle("RECEIVE FILE"); + padprintln(""); + padprintln("File received: "); + padprintln(recvFileName); + padprintln("\n"); + padprintln("Press any key to leave"); + while(!checkAnyKeyPress()) { delay(80); } + } +} + + +bool DeviceConnection::setupPeer() { + peerInfo = {}; + peerInfo.channel = 0; + peerInfo.encrypt = false; + memcpy(peerInfo.peer_addr, broadcastAddress, 6); + + Serial.print("Adding peer: "); + for (byte i = 0; i < 6; i++) { + if (i>0) Serial.print(":"); + Serial.print(broadcastAddress[i] < 0x10 ? "0" : ""); + Serial.print(broadcastAddress[i], HEX); + } + Serial.println(); + + esp_err_t add_peer_resp = esp_now_add_peer(&peerInfo); + // Serial.printf("Add peer response: 0X%x\n", add_peer_resp); + + return add_peer_resp == ESP_OK; +} + + +File DeviceConnection::selectFile() { + String filename; + FS *fs=&LittleFS; + setupSdCard(); + if(sdcardMounted) { + options = { + {"Sd Card", [&](){ fs=&SD; }}, + {"LittleFs", [&](){ fs=&LittleFS; }}, + }; + delay(200); + loopOptions(options); + delay(200); + } + filename = loopSD(*fs,true); + + File file = fs->open(filename, FILE_READ); + return file; +} + + +DeviceConnection::FileMessage DeviceConnection::createFileMessage(File file) { + FileMessage message; + String path = String(file.path()); + + message.filename = file.name(); + message.filepath = path.substring(0, path.lastIndexOf("/")); + message.totalBytes = file.size(); + message.bytesSent = 0; + message.done = false; + + return message; +} + + +bool DeviceConnection::appendToFile(DeviceConnection::FileMessage fileMessage) { + FS *fs; + if(!getFsStorage(fs)) return false; + + if (recvFileName == "") createFilename(fs, fileMessage); + + File file = (*fs).open(recvFileName, FILE_APPEND); + if(!file) return false; + + file.write((const uint8_t*)fileMessage.data, fileMessage.dataSize); + file.close(); + + return true; +} + + +void DeviceConnection::createFilename(FS *fs, DeviceConnection::FileMessage fileMessage) { + String filename = fileMessage.filename.substring(0,fileMessage.filename.lastIndexOf(".")); + String ext = fileMessage.filename.substring(fileMessage.filename.lastIndexOf(".")); + + Serial.println("Creating file"); + Serial.print("Path: ");Serial.println(fileMessage.filepath); + Serial.print("Name: ");Serial.println(filename); + Serial.print("Ext: ");Serial.println(ext); + + if (!(*fs).exists(fileMessage.filepath)) (*fs).mkdir(fileMessage.filepath); + if ((*fs).exists(fileMessage.filepath+"/"+filename+ext)) { + int i = 1; + filename += "_"; + while((*fs).exists(fileMessage.filepath+"/" + filename + String(i) + ext)) i++; + filename += String(i); + } + + recvFileName = fileMessage.filepath+"/" + filename + ext; +} + + +void onDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) { + if (status == ESP_NOW_SEND_SUCCESS) { + espnowSendStatus = DeviceConnection::SUCCESS; + Serial.println("ESPNOW send success"); + } else { + espnowSendStatus = DeviceConnection::FAILED; + Serial.println("ESPNOW send fail"); + } +} + + +void onDataRecv(const uint8_t* mac, const uint8_t* incomingData, int len) { + DeviceConnection::FileMessage recvFileMessage; + memcpy(&recvFileMessage, incomingData, sizeof(recvFileMessage)); + + Serial.print("Name: "); + Serial.println(recvFileMessage.filename); + Serial.print("Path: "); + Serial.println(recvFileMessage.filepath); + Serial.print("Bytes sent: "); + Serial.println(recvFileMessage.bytesSent); + Serial.print("Total bytes: "); + Serial.println(recvFileMessage.totalBytes); + Serial.print("Done: "); + Serial.println(recvFileMessage.done); + // Serial.println("Data: "); + // Serial.write((const uint8_t*)recvFileMessage.data, recvFileMessage.dataSize); + Serial.println("\n-----"); + + recvQueue.push_back(recvFileMessage); +} diff --git a/src/core/connect.h b/src/core/connect.h new file mode 100644 index 00000000..b328f5b6 --- /dev/null +++ b/src/core/connect.h @@ -0,0 +1,71 @@ +/** + * @file connect.h + * @author Rennan Cockles (https://github.com/rennancockles) + * @brief Connection between devices + * @version 0.1 + * @date 2024-10-02 + */ + + +#ifndef __DEVICE_CONNECTION_H__ +#define __DEVICE_CONNECTION_H__ + +#include +#include "core/globals.h" + +#define PAGE_BYTES 150 + + +class DeviceConnection { +public: + enum Status { + CONNECTING, + STARTED, + FAILED, + SUCCESS, + ABORTED, + }; + + typedef struct { + String filename; + String filepath; + size_t totalBytes; + size_t bytesSent; + char data[PAGE_BYTES]; + size_t dataSize; + bool done; + } FileMessage; + + ///////////////////////////////////////////////////////////////////////////////////// + // Constructor + ///////////////////////////////////////////////////////////////////////////////////// + DeviceConnection(); + ~DeviceConnection(); + + ///////////////////////////////////////////////////////////////////////////////////// + // Operations + ///////////////////////////////////////////////////////////////////////////////////// + void sendFile(); + void receiveFile(); + +private: + Status recvStatus; + Status sendStatus; + String recvFileName; + uint8_t broadcastAddress[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + esp_now_peer_info_t peerInfo; + + ///////////////////////////////////////////////////////////////////////////////////// + // Helpers + ///////////////////////////////////////////////////////////////////////////////////// + bool setupPeer(); + File selectFile(); + FileMessage createFileMessage(File file); + bool appendToFile(FileMessage fileMessage); + void createFilename(FS *fs, FileMessage fileMessage); +}; + +void onDataSent(const uint8_t* mac_addr, esp_now_send_status_t status); +void onDataRecv(const uint8_t* mac, const uint8_t* incomingData, int len); + +#endif From f7f4b393033c41c42641cf90a0884686e2b088a6 Mon Sep 17 00:00:00 2001 From: Rennan Cockles Date: Thu, 3 Oct 2024 23:39:47 -0300 Subject: [PATCH 3/3] small adjustments --- src/core/menu_items/BleMenu.cpp | 9 ++++----- src/core/menu_items/RFIDMenu.cpp | 6 +++--- src/core/menu_items/RFMenu.cpp | 6 +++--- src/core/menu_items/WifiMenu.cpp | 1 - 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/core/menu_items/BleMenu.cpp b/src/core/menu_items/BleMenu.cpp index 7c1cfd53..37b81dce 100644 --- a/src/core/menu_items/BleMenu.cpp +++ b/src/core/menu_items/BleMenu.cpp @@ -3,17 +3,16 @@ #include "core/display.h" #include "modules/ble/ble_spam.h" #include "modules/ble/ble_common.h" -#include "modules/NRF24/nrf_jammer.h" #include "modules/ble/bad_ble.h" void BleMenu::optionsMenu() { options.clear(); - if(BLEConnected) options.push_back({"Disconnect", [=]() { - BLEDevice::deinit(); - BLEConnected=false; + if(BLEConnected) options.push_back({"Disconnect", [=]() { + BLEDevice::deinit(); + BLEConnected=false; if(Ask_for_restart==1) Ask_for_restart=2; // Sets the variable to ask for restart; }}); - + options.push_back({"Media Cmds", [=]() { ble_MediaCommands(); }}); #if !defined(LITE_VERSION) // options.push_back({"BLE Beacon", [=]() { ble_test(); }}); diff --git a/src/core/menu_items/RFIDMenu.cpp b/src/core/menu_items/RFIDMenu.cpp index 6abd3d0a..2696a3c3 100644 --- a/src/core/menu_items/RFIDMenu.cpp +++ b/src/core/menu_items/RFIDMenu.cpp @@ -18,9 +18,9 @@ void RFIDMenu::optionsMenu() { delay(200); String txt = "RFID"; - if(RfidModule==0) txt+=" (RFID2)"; - else if(RfidModule==1) txt+=" (PN532-I2C)"; - else if(RfidModule==2) txt+=" (PN532-SPI)"; + if(RfidModule==M5_RFID2_MODULE) txt+=" (RFID2)"; + else if(RfidModule==PN532_I2C_MODULE) txt+=" (PN532-I2C)"; + else if(RfidModule==PN532_SPI_MODULE) txt+=" (PN532-SPI)"; loopOptions(options,false,true,txt); } diff --git a/src/core/menu_items/RFMenu.cpp b/src/core/menu_items/RFMenu.cpp index 032d9050..7d068c8d 100644 --- a/src/core/menu_items/RFMenu.cpp +++ b/src/core/menu_items/RFMenu.cpp @@ -16,9 +16,9 @@ void RFMenu::optionsMenu() { delay(200); String txt = "Radio Frequency"; - if(RfModule) txt+=" (CC1101)"; // Indicates if CC1101 is connected - else txt+=" Tx: " + String(RfTx) + " Rx: " + String(RfRx); - + if(RfModule==CC1101_SPI_MODULE) txt+=" (CC1101)"; // Indicates if CC1101 is connected + else txt+=" Tx: " + String(RfTx) + " Rx: " + String(RfRx); + loopOptions(options,false,true,txt); } diff --git a/src/core/menu_items/WifiMenu.cpp b/src/core/menu_items/WifiMenu.cpp index 7bee7727..10069d21 100644 --- a/src/core/menu_items/WifiMenu.cpp +++ b/src/core/menu_items/WifiMenu.cpp @@ -9,7 +9,6 @@ #include "modules/wifi/sniffer.h" #include "modules/wifi/wardriving.h" #include "modules/wifi/wifi_atks.h" -#include "modules/NRF24/nrf_jammer.h" #ifndef LITE_VERSION #include "modules/pwnagotchi/pwnagotchi.h"