import utime
import math
import machine
from machine import Pin
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
I2C_ADDR = 0x27 # Decimal 39
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
SDA_PIN = 0
SCL_PIN = 1
PLAY_PIN = 2
RESET_PIN = 3
print("Running StopWatch")
i2c = I2C(0, sda=machine.Pin(SDA_PIN), scl=machine.Pin(SCL_PIN), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
button_play = Pin(PLAY_PIN, Pin.IN, Pin.PULL_UP)
button_reset = Pin(RESET_PIN, Pin.IN, Pin.PULL_UP)
lcd.putstr("Stopwatch")
utime.sleep(2)
lcd.clear()
lcd.hide_cursor()
timer = 0
running = False
lcd.clear()
# lcd.show_cursor()
# lcd.blinking()
lcd.putstr("Stopwatch")
while True:
# If button pressed then toggle
if (button_play.value() == False):
running = not running
print ("Status: "+str(running))
# sleep prevents single press causing multiple play / pause signals
# this is a simple, but basic method
utime.sleep(0.1)
if (button_reset.value() == False):
print ("Reset")
timer = 0
lcd.clear()
lcd.putstr("Stopwatch")
utime.sleep(0.1)
pom = str(math.floor(timer))
# while (len(pom) < 16):
# pom = " " + pom
lcd.move_to(16 - len(pom) ,1)
lcd.putstr(pom)
if (running):
timer += 1
utime.sleep(1)