Skip to content

Commit

Permalink
iButton
Browse files Browse the repository at this point in the history
  • Loading branch information
pr3y committed Jan 10, 2025
1 parent 1e0a6f7 commit 0f3510b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/core/menu_items/OthersMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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(); }},
};
Expand Down
105 changes: 105 additions & 0 deletions src/modules/others/ibutton.cpp
Original file line number Diff line number Diff line change
@@ -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");
}

5 changes: 5 additions & 0 deletions src/modules/others/ibutton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <OneWire.h>

void setup_ibutton();
void write_ibutton();
void read_ibutton();

0 comments on commit 0f3510b

Please sign in to comment.