from machine import I2C, Pin, UART
from gpio_lcd import GpioLcd
import time
import ds1307

count1 = 0 

lcd = GpioLcd(rs_pin=Pin(16),
          enable_pin=Pin(17),
          d4_pin=Pin(18),
          d5_pin=Pin(19),
          d6_pin=Pin(20),
          d7_pin=Pin(21),
          num_lines=2,
          num_columns=16
          )
button1 = Pin(11,Pin.IN,Pin.PULL_DOWN)
i2c = I2C(0, scl=Pin(9), sda=Pin(8))
rtc = ds1307.DS1307(i2c, 0x68)

csv_filename = "data.csv"

with open(csv_filename, "a") as f:
    f.write("TOKEN,PRESS_TIME,CALL_TIME\n")
    f.close()

while 1:
    date_time = rtc.datetime
    date_str = "{:02d}/{:02d}/{:04d}".format(date_time[2], date_time[1], date_time[0])
    time_str = "{:02d}:{:02d}:{:02d}".format(date_time[3], date_time[4], date_time[5])

    lcd.move_to(0,0)
    lcd.putstr("PRESS BUTTON ")
    lcd.move_to(1,1)
    x = button1.value()

    if(x==1):
        count1 = count1 + 1
        lcd.clear()
        lcd.putstr("TOKEN :" + str(count1))
        with open(csv_filename, "a") as f:
            line = "{},{},\n".format(count1,time_str)
            f.write(line)
            print(line.strip())
            f.close()
        time.sleep(1)
        lcd.clear()


BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
GND5VSDASCLSQWRTCDS1307+