from machine import Pin, SPI, I2C
import time
from mfrc522 import MFRC522
from products import products
from lcd1602 import I2cLcd
# ตั้งค่าอุปกรณ์
buzzer = Pin(15, Pin.OUT)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
spi = SPI(0, baudrate=2500000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
rfid = MFRC522(spi=spi, gpio_cs=17, gpio_rst=20)
def beep(duration=0.1):
buzzer.value(1)
time.sleep(duration)
buzzer.value(0)
lcd.clear()
lcd.putstr("Scan RFID Card..")
total_price = 0
total_items = 0
while True:
status, tag_type = rfid.request(rfid.REQIDL)
if status == rfid.OK:
status, uid = rfid.anticoll()
if status == rfid.OK:
uid_text = ":".join("{:02X}".format(x) for x in uid)
product = products.get(uid_text)
lcd.clear()
if product:
name, price = product
total_price += price
total_items += 1
beep(0.1)
lcd.move_to(0, 0)
lcd.putstr(f"{name[:10]:<10} {price:>4}B")
lcd.move_to(0, 1)
lcd.putstr(f"{total_items}pcs Total:{total_price}B")
else:
beep(0.3)
lcd.move_to(0, 0)
lcd.putstr("Unknown Product")
lcd.move_to(0, 1)
lcd.putstr(f"ID:{uid_text[:12]}")
time.sleep(2)
lcd.clear()
lcd.putstr("Scan RFID Card..")
time.sleep(0.05)