import json
import machine
import time
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
from machine import Pin
ok = Pin(32, Pin.IN, Pin.PULL_UP)
up = Pin(34, Pin.IN, Pin.PULL_UP)
down = Pin(12, Pin.IN, Pin.PULL_UP)
def lcdDisplay(lcd, label, depth):
lcd.clear()
if depth == 0:
lcd.putstr('Choisir une classe ')
else:
lcd.putstr('Choisir un eleve ')
lcd.putstr(label)
def selectPasswd(bdd, lcd):
current = bdd
current_i = 0
current_max = len(current)
current_keys = list(current.keys())
old_name = ""
depth = 0
changed = True
while depth < 2:
old_i = current_i
if up.value() == 0:
current_i += 1
changed = True
if down.value() == 0:
current_i -= 1
changed = True
current_i = current_i % current_max
if ok.value() == 0:
depth += 1
old_name = current_keys[current_i]
current = current[current_keys[current_i]]
current_i = 0
current_max = len(current)
current_keys = list(current.keys())
changed = True
if changed and depth < 2:
lcdDisplay(lcd, current_keys[current_i], depth)
changed = False
time.sleep(0.2)
return (old_name, current['passwd'])
# Chargement de la BDD JSON
bdd = {}
with open('bdd.json', 'r') as json_file:
bdd = json.load(json_file)
print("JSON DB loaded !")
# Initialisation du LCD
i2c = machine.I2C(0, sda=machine.Pin(21), scl=machine.Pin(22))
lcd = I2cLcd(i2c, 0x27, 4, 20)
while True:
name, passwd = selectPasswd(bdd, lcd)
lcd.clear()
lcd.putstr(f"Sending '{passwd}' for {name} ...")
time.sleep(3)