import time
from machine import Pin, I2C
import ssd1306
from rotary_irq_esp import RotaryIRQ
# Timestamp edit variable
tm = (2026, 1, 4, 21, 0, 0, 0, 0)
edit_ts = time.mktime(tm[:6] + (0, 0))
# --- OLED setup ---
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# --- encoder setup ---
clk = Pin(32, Pin.IN, Pin.PULL_UP)
dt = Pin(33, Pin.IN, Pin.PULL_UP)
def callback(pin):
print("IRQ fired! CLK:", clk.value(), "DT:", dt.value())
# Attach IRQ to CLK pin (rising + falling)
clk.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=callback)
while True:
time.sleep(0.1)
# Check raw pin values
print("Initial CLK:", clk.value(), "DT:", dt.value())
# --- Create rotary encoder ---
rot = RotaryIRQ(
pin_num_clk=32,
pin_num_dt=33,
min_val=0, # example range
max_val=100,
incr=1
)
# --- Callback for rotary events ---
def rotary_handler(value):
global edit_ts, last_rot_value
delta = value - last_rot_value
edit_ts += delta * 3600 # 1 hour per step
last_rot_value = value
print("Rotary step:", delta, "-> edit_ts:", edit_ts) # 🔹 debug
# Save original timestamp
# tm_ts = edit_ts
rot.add_listener(rotary_handler)
# --- Idle loop ---
while True:
y, m, d, h, mi, s, _, _ = time.localtime(edit_ts)
# Display basic info
oled.fill(0)
oled.text("Time (edit_ts):", 0, 0)
oled.text("{:02d}-{:02d}-{:04d}".format(d, m, y), 0, 20)
oled.text("{:02d}:{:02d}:{:02d}".format(h, mi, s), 0, 35)
oled.show()
# Also print to console
print("Display:", "{:02d}-{:02d}-{:04d} {:02d}:{:02d}:{:02d}".format(d, m, y, h, mi, s))
time.sleep(0.2)