from machine import Pin, RTC
from ssd1306 import SSD1306_I2C
from display import init_i2c, display_logo, display_text, pix_res_x, pix_res_y
from keypad import Keypad4x4Read
from database import barcode_database, fetch_product_name
from utils import get_date_input, calculate_days_difference, col_list, row_list
from leds import update_leds, leds
import utime
# Define a callback function for the button press
def button_callback(pin):
print("button was pressed")
def main():
rtc = RTC()
rtc.datetime((2024, 6, 13, 0, 0, 0, 0, 0))
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
display_logo(oled)
utime.sleep(2)
display_text(oled, "Enter barcode:")
input_sequence = ""
product_name = ""
# Initialize button on Pin 16
button = Pin(16, Pin.IN, Pin.PULL_UP)
button.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)
while True:
today_struct = rtc.datetime()
today_date = "{:04}-{:02}-{:02}".format(today_struct[0], today_struct[1], today_struct[2])
# Check if any product is expired
any_expired = False
before_expired = False
for barcode, (name, expire_date) in barcode_database.items():
days_difference = calculate_days_difference(expire_date, today_date)
if days_difference < 0:
any_expired = True
break
elif days_difference <= 3 and days_difference >= 0:
before_expired = True
break
update_leds(any_expired, before_expired)
key = Keypad4x4Read(col_list, row_list)
if key:
print(f"You pressed: {key}")
input_sequence += key
display_text(oled, input_sequence)
if len(input_sequence) >= 10:
input_sequence = ""
# Get input date
input_date = get_date_input(oled)
print(f"Input date: {input_date}")
if input_date:
barcode_database[input_sequence] = ("Test Produkt", input_date)
today_struct = rtc.datetime()
today_date = "{:04}-{:02}-{:02}".format(today_struct[0], today_struct[1], today_struct[2])
print(f"Today's date: {today_date}")
days_difference = calculate_days_difference(input_date, today_date)
print(f"Days difference: {days_difference}")
if days_difference < 0:
leds[0].value(1) # Rot
leds[1].value(0)
leds[2].value(0)
elif days_difference <= 3 and days_difference >= 0:
leds[1].value(1) # Gelb
leds[0].value(0)
leds[2].value(0)
else:
leds[2].value(1) # Grün
leds[0].value(0)
leds[1].value(0)
utime.sleep(2)
leds[2].value(0)
leds[0].value(0)
leds[1].value(0) # Alle LEDs ausschalten
display_text(oled, "Enter barcode:")
input_sequence = ""
product_name = ""
else:
display_text(oled, "Invalid date", clear=False)
utime.sleep(2)
display_text(oled, "Enter barcode:")
input_sequence = ""
product_name = ""
utime.sleep(0.5)
if __name__ == "__main__":
main()