from machine import Pin, I2C,Timer
import time
from hcsr04 import HCSR04
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import oled
button = Pin(23,Pin.IN,Pin.PULL_UP)
debounce_delay = 50
button_presses = 0
MAX_BUTTON_PRESS = 5
# I2C Pin setup for ESP32
i2c = I2C(0,scl=Pin(22), sda=Pin(21), freq=4000000)
# LCD setup for ESP32
I2C_ADDR = i2c.scan()[0]
totalRows = 2
totalColumns = 16
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
#oled
oled1 = oled.I2C(128, 64, i2c)
# Ultrasonic Sensor setup
ultra = HCSR04(25,26)
#Button Setup
def handle_button_press_tmr(tmr):
global button_presses
tmr.deinit()
if button.value() == 0:
button_presses = (button_presses + 1)
if button_presses == MAX_BUTTON_PRESS:
button_presses = 1
def handle_button_press_intr(pin):
tmr = Timer(-1)
tmr.init(mode=Timer.ONE_SHOT, period=debounce_delay, callback=handle_button_press_tmr)
def measure_distance():
# Return the distance in cm
return ultra.distance_cm()
def display_distance(distance):
# Display distance on LCD
lcd.clear()
lcd.putstr("Distance:{_distance:.3f}".format(_distance=distance))
displayed_string = ''
def display_string(to_display):
global displayed_string
if to_display == displayed_string:
return
lcd.clear()
lcd.putstr(to_display)
displayed_string = to_display
def main():
button.irq(handler = handle_button_press_intr,trigger = Pin.IRQ_FALLING | Pin.IRQ_RISING)
global button_presses
distance = measure_distance()
while True:
# Keep the main functionality running
if button_presses == 0:
display_string('Press Button to Start')
elif button_presses == 1:
I2C.init(scl=Pin(22), sda=Pin(21), freq=40000)
display_distance(measure_distance())
elif button_presses == 2:
I2C.init(freq=4000)
display_string('DECI Students')
time.sleep(1)
display_string('Embedded Systems')
elif button_presses == 3:
I2C.init(freq=1000)
distance = measure_distance()
if distance < 50:
display_string('Too Close')
elif 50 < distance < 100 :
display_string('Keep Distance')
elif distance > 100:
display_string('Safe')
elif button_presses == 4:
display_string(f'LCD Address {I2C_ADDR}')
if distance> 70:
oled1.clear()
oled1.text("safe", 10, 3)
oled1.show()
else:
oled1.clear()
oled1.text("keep distance", 10, 3)
oled1.show()
time.sleep(1)
if __name__ == "__main__":
main()