from machine import Pin, I2C, deepsleep
import ssd1306
from machine import RTC
import time
import random
rtc = RTC()
pagina = 3
counter = 0
tc_result = 0
i2c = I2C(sda=Pin(4), scl=Pin(5))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
def handle_interrupt(pin):
global pagina
pagina += 1
if pagina > 3: pagina = 0
print(pagina)
time.sleep(1)
def sleep_interrupt(pin): deepsleep()
pin_up = Pin(14, Pin.IN)
pin_ok = Pin(32, Pin.IN)
pin_sleep = Pin(34, Pin.IN)
pin_up.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
pin_sleep.irq(trigger=Pin.IRQ_RISING, handler=sleep_interrupt)
def orologio():
data = rtc.datetime()
display.rect(10, 10, 107, 43, 1)
display.text('%02d/%02d/%04d'%(data[2],data[1],data[0]), 25, 22, 1)
display.text('%02d:%02d:%02d'%(data[4],data[5],data[6]), 33, 32, 1)
def ntp_sync():
display.rect(0, 0, 128, 15, 1)
display.text('NTP Sync', 35, 4, 1)
display.text('Premere tasto X', 0, 20, 1)
display.text('per sincronizzare', 0, 30, 1)
display.text('la data e ora', 10, 40, 1)
def testa_croce():
global tc_result
display.rect(0, 0, 128, 15, 1)
display.rect(30, 30, 68, 20, 1)
display.text('Testa o croce', 12, 4, 1)
if pin_ok.value(): tc_result = random.randint(1,2)
if tc_result == 0: display.text('Premi X', 36, 36, 1)
elif tc_result == 1: display.text('TESTA!', 42, 36, 1)
elif tc_result == 2: display.text('CROCE!', 42, 36, 1)
def contatore():
global counter
display.rect(0, 0, 128, 15, 1)
display.text('Contatore', 27, 4, 1)
display.text('Totale: %d'%counter, 27, 36, 1)
if pin_ok.value():
counter += 1
while True:
display.fill(0)
if pagina == 0: orologio()
elif pagina == 1: ntp_sync()
elif pagina == 2: testa_croce()
elif pagina == 3: contatore()
display.show()
time.sleep(0.5)