From 0f3510b5217ff452d3492d083098ffaac0e3cf35 Mon Sep 17 00:00:00 2001 From: pr3y Date: Fri, 10 Jan 2025 03:01:45 -0300 Subject: [PATCH] iButton --- platformio.ini | 1 + src/core/menu_items/OthersMenu.cpp | 2 + src/modules/others/ibutton.cpp | 105 +++++++++++++++++++++++++++++ src/modules/others/ibutton.h | 5 ++ 4 files changed, 113 insertions(+) create mode 100644 src/modules/others/ibutton.cpp create mode 100644 src/modules/others/ibutton.h diff --git a/platformio.ini b/platformio.ini index a3b9c506..a8ccf480 100644 --- a/platformio.ini +++ b/platformio.ini @@ -90,6 +90,7 @@ lib_deps = Bodmer/JPEGDecoder https://github.com/bmorcelli/SmartRC-CC1101-Driver-Lib/ ducktape=https://github.com/bmorcelli/duktape/releases/download/2.7.0-lite/duktape-2.7.0.zip + paulstoffregen/OneWire@^2.3.8 ;https://github.com/eadmaster/rtl_433_ESP ;jackjansen/esp32_idf5_https_server_compat tobozo/ESP32-PSRamFS diff --git a/src/core/menu_items/OthersMenu.cpp b/src/core/menu_items/OthersMenu.cpp index 6022a494..b01f7dc8 100644 --- a/src/core/menu_items/OthersMenu.cpp +++ b/src/core/menu_items/OthersMenu.cpp @@ -9,6 +9,7 @@ #include "modules/others/timer.h" #include "modules/others/clicker.h" #include "modules/others/bad_usb.h" +#include "modules/others/ibutton.h" void OthersMenu::optionsMenu() { options = { @@ -30,6 +31,7 @@ void OthersMenu::optionsMenu() { #if !defined(ARDUINO_M5STACK_ARDUINO_M5STACK_CORE) && !defined(ARDUINO_M5STACK_ARDUINO_M5STACK_CORE2) {"Interpreter", [=]() { run_bjs_script(); }}, #endif + {"iButton", [=]() { setup_ibutton(); }}, {"Timer", [=]() { Timer(); }}, {"Main Menu", [=]() { backToMenu(); }}, }; diff --git a/src/modules/others/ibutton.cpp b/src/modules/others/ibutton.cpp new file mode 100644 index 00000000..909c4aa0 --- /dev/null +++ b/src/modules/others/ibutton.cpp @@ -0,0 +1,105 @@ +// thanks geo-tp for the base of this +#include "ibutton.h" +#include "core/display.h" +#include "core/mykeyboard.h" + +#define ONE_WIRE_BUS 0 + +OneWire oneWire(ONE_WIRE_BUS); +byte buffer[8]; + +void setup_ibutton() { + tft.fillScreen(TFT_BLACK); + tft.setTextColor(TFT_WHITE); + tft.setTextSize(2); + displayTextLine("Waiting iButton..."); + delay(100); + + for(;;){ + if (check(EscPress)){ + returnToMenu=true; + break; + } + // iButton is plugged + if (oneWire.reset() != 0) { + // Main Button is pressed + if (check(SelPress)) { + write_ibutton(); + } else { + read_ibutton(); + } + } + delay(500); + } +} + +void write_byte_rw1990(byte data) { + for (int data_bit = 0; data_bit < 8; data_bit++) { + delay(25); + oneWire.write_bit(~data); + data >>= 1; + } +} + +void write_ibutton() { + tft.setCursor(52,102); + tft.print("Wait..."); + + oneWire.reset(); // Reset bus + oneWire.skip(); // Skip rom check + delay(20); + + // Step 1 : Prepare for writing + oneWire.write(0xD1); // Start write command + oneWire.reset(); // Reset bus + + // Step 2 : Write the ID + oneWire.write(0xD5); // Write ID command + for (byte i = 0; i < 8; i++) { + write_byte_rw1990(buffer[i]); // Write each byte + } + oneWire.reset(); // Reset bus + + // Step 3 : Finalise + oneWire.write(0xD1); // End of write command + oneWire.reset(); // Reset bus + + // Display end of copy + tft.fillScreen(TFT_BLACK); + tft.setCursor(90,50); + tft.setTextSize(2); + displayTextLine("COPIED"); + tft.setCursor(40,80); + tft.print("Release button"); + + delay(3000); + + tft.fillScreen(TFT_BLACK); + tft.setCursor(10,60); + displayTextLine("Waiting iButton..."); +} + +void read_ibutton() { + oneWire.write(0x33); // Read ID command + oneWire.read_bytes(buffer, 8); // Read ID + + // Display iButton + tft.fillScreen(TFT_BLACK); + tft.setTextSize(2); + tft.setCursor(55,20); + tft.println("iButton ID: "); + + // Dislay ID + tft.setTextSize(1.7); + tft.setCursor(12, 57); + for (byte i = 0; i < 8; i++) { + tft.print(buffer[i], HEX); + tft.print(" "); + } + + // Display copy infos + tft.setCursor(55,85); + tft.setTextSize(1.5); + tft.println("Hold OK to copy"); +} + diff --git a/src/modules/others/ibutton.h b/src/modules/others/ibutton.h new file mode 100644 index 00000000..afaa02bf --- /dev/null +++ b/src/modules/others/ibutton.h @@ -0,0 +1,5 @@ +#include + +void setup_ibutton(); +void write_ibutton(); +void read_ibutton();