from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import i2cLcd
from time import sleep
# Define the I2C address and LCD parameters
I2C_ADDR = 0x27
totalRows = 2
totalCols = 16
# Initialize the I2C bus and LCD object
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, I2C_ADDR, totalCols, totalRows)
# Clear the LCD display
lcd.clear()
# Display a message on the LCD
lcd.putstr("Hello, LCD!")
# Wait for a moment
sleep(2)
# Clear the LCD display again
lcd.clear()
# #shows the address of liquid crystal display as 0x27. You will most likely get the same address for LCD with 16 columns and 2 rows.
# import machine
# from machine import Pin, SoftI2C
# from lcd_api import LcdApi
# from i2c_lcd import I2cLcd
# from time import sleep
# I2C_ADDR = 0x27
# totalRows = 2
# totalColumns = 16
# i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
# #i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)
# lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# while True:
# '''lcd.putstr("I2C LCD Tutorial")
# sleep(2)
# lcd.clear()
# lcd.putstr("Lets Count 0-10!")
# sleep(2)
# lcd.clear()
# for i in range(11):
# lcd.putstr(str(i))
# sleep(1)
# lcd.clear()'''
# Villager=bytearray([0x00,0x00,0x0A,0x00,0x04,0x04,0x11,0x0E])
# lcd.custom_char(0,Villager)
# lcd.putstr(chr(0) +"ESP32 with I2C LCD" )
# sleep(2)
# lcd.clear()
# # import utime
# # import gc
# # from lcd_api import LcdApi
# # from machine import I2C
# # # PCF8574 pin definitions
# # MASK_RS = 0x01 # P0
# # MASK_RW = 0x02 # P1
# # MASK_E = 0x04 # P2
# # SHIFT_BACKLIGHT = 3 # P3
# # SHIFT_DATA = 4 # P4-P7
# # class I2cLcd(LcdApi):
# # #Implements a HD44780 character LCD connected via PCF8574 on I2C
# # def __init__(self, i2c, i2c_addr, num_lines, num_columns):
# # self.i2c = i2c
# # self.i2c_addr = i2c_addr
# # self.i2c.writeto(self.i2c_addr, bytes([0]))
# # utime.sleep_ms(20) # Allow LCD time to powerup
# # # Send reset 3 times
# # self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
# # utime.sleep_ms(5) # Need to delay at least 4.1 msec
# # self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
# # utime.sleep_ms(1)
# # self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
# # utime.sleep_ms(1)
# # # Put LCD into 4-bit mode
# # self.hal_write_init_nibble(self.LCD_FUNCTION)
# # utime.sleep_ms(1)
# # LcdApi.__init__(self, num_lines, num_columns)
# # cmd = self.LCD_FUNCTION
# # if num_lines > 1:
# # cmd |= self.LCD_FUNCTION_2LINES
# # self.hal_write_command(cmd)
# # gc.collect()
# # def hal_write_init_nibble(self, nibble):
# # # Writes an initialization nibble to the LCD.
# # # This particular function is only used during initialization.
# # byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
# # self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
# # self.i2c.writeto(self.i2c_addr, bytes([byte]))
# # gc.collect()
# # def hal_backlight_on(self):
# # # Allows the hal layer to turn the backlight on
# # self.i2c.writeto(self.i2c_addr, bytes([1 << SHIFT_BACKLIGHT]))
# # gc.collect()
# # def hal_backlight_off(self):
# # #Allows the hal layer to turn the backlight off
# # self.i2c.writeto(self.i2c_addr, bytes([0]))
# # gc.collect()
# # def hal_write_command(self, cmd):
# # # Write a command to the LCD. Data is latched on the falling edge of E.
# # byte = ((self.backlight << SHIFT_BACKLIGHT) |
# # (((cmd >> 4) & 0x0f) << SHIFT_DATA))
# # self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
# # self.i2c.writeto(self.i2c_addr, bytes([byte]))
# # byte = ((self.backlight << SHIFT_BACKLIGHT) |
# # ((cmd & 0x0f) << SHIFT_DATA))
# # self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
# # self.i2c.writeto(self.i2c_addr, bytes([byte]))
# # if cmd <= 3:
# # # The home and clear commands require a worst case delay of 4.1 msec
# # utime.sleep_ms(5)
# # gc.collect()
# # def hal_write_data(self, data):
# # byte = (MASK_RS |
# # (self.backlight << SHIFT_BACKLIGHT) |
# # (((data >> 4) & 0x0f) << SHIFT_DATA))
# # self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
# # self.i2c.writeto(self.i2c_addr, bytes([byte]))
# # byte = (MASK_RS |
# # (self.backlight << SHIFT_BACKLIGHT) |
# # ((data & 0x0f) << SHIFT_DATA))
# # self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
# # self.i2c.writeto(self.i2c_addr, bytes([byte]))
# # gc.collect()