from machine import Pin, SoftI2C 
from ssd1306 import SSD1306_I2C
from time import sleep 
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04 

# ESP32 Pin assignment 
i2c_OLED = SoftI2C(scl=Pin(18), sda=Pin(19), freq=100000) 
i2c_LCD= SoftI2C(scl=Pin(18), sda=Pin(19), freq=9999) #initializing the I2C method for ESP32

# HCSR04 ultrasonic

sensor = HCSR04(trigger_pin=12, echo_pin=14,echo_timeout_us=100000)



I2C_OLED_ADDR = 0x3c
I2C_LCD_ADDR = 0x27

oled_width = 128 
oled_height = 64

totalRows = 2
totalColumns = 16

distance = sensor.distance_cm()
oled = SSD1306_I2C(oled_width, oled_height, i2c_OLED, I2C_OLED_ADDR)



while True:
    trigPin.value(0)
    time.sleep_us(2)
    trigPin.value(1)
    time.sleep_us(10)
    trigPin.value(0)

    totalTime = machine.time_pulse_us(echoPin,1)

    distance=totalTime*0.034/2
    oled.text("Distance: ", 0, 0) 
    oled.text(str(distance), 7, 8) 
    oled.show()