Thomascountz / micropython_i2c_lcd

This MicroPython library provides a framework for interacting with HD44780-based LCD displays through a PCF8574 I/O expander. It offers a high-level API for LCD control, including text display, cursor manipulation, and backlight settings, while also providing lower-level access to the GPIO operations on the PCF8574.
MIT License
3 stars 3 forks source link

[question] How to position the cursor and write text from there? #4

Closed BertLindeman closed 6 months ago

BertLindeman commented 6 months ago

Hi,

Just started to use your lcd library.

  1. Looking for a way to update e.g. the top row from col 8 for 8 characters.
  2. update only the first 8 positions on a row.

I see functions are in HD44780 but I do not know how to implement it.

A snippet:


cur_line = 0
cur_col = 8
lcd_device.set_cursor(cur_line, cur_col)

or

HD44780.set_cursor(lcd_device, cur_line, cur_col)

Both obviously do not work. Maybe an idea to add such to the test program?

Thanks,

Bert

Thomascountz commented 6 months ago

Hi Bert,

I'm on mobile, but let me see if I can help before I get back to my desk.

From what I understand, you'd like to write N characters beginning at cursor position X, Y (column, row).

For this, I believe you can use this method to set the cursor: https://github.com/Thomascountz/micropython_i2c_lcd/blob/main/hd44780.py#L234-L241, and then write the characters while advancing the cursor or write a string of length 8 using the other methods in the class.

Anywhere not visited by the cursor will not be updated. So long as you use the lower-level hd44780 class methods, you should be able to achieve the manipulation you're going for.

It's been a while since I've visited this code myself, so as you suggest, I will try writing a test for this when I'm back home.

Thanks for trying out the library! I would love to see what you're making!

BertLindeman commented 6 months ago

Fast response Thomas.

Will try it. I am just fiddling with the sunfounders kit combined with NodeRed. Current "project" Get Open Weather map current weather and show on a 1602 lcd. No rocket science but fun to do.

Thanks for the rapid response.

Bert

Thomascountz commented 6 months ago

That sounds like an awesome project and after Googling it, it looks like there are great things in the kit to build with. I hope your weather station project goes well!

Here's the code I'm hoping will address your original question along with a video preview of a simulation. Please let me know if this isn't quite what you were looking for.

https://github.com/Thomascountz/micropython_i2c_lcd/assets/19786848/b6449224-f7d2-4424-a587-2c0aa90f0d7e

from machine import Pin, I2C
import utime
from pcf8574 import PCF8574
from hd44780 import HD44780
from lcd import LCD

def main_test():
    print("Create an I2C object")
    i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)

    print("Create a PCF8574 object")
    pcf8574 = PCF8574(i2c)

    print("Create an HD44780 object")
    hd44780 = HD44780(pcf8574, num_lines=2, num_columns=16)

    print("Create an LCD object")
    lcd = LCD(hd44780, pcf8574)

    while True:
        print ("Clear the display")
        lcd.clear()

        print("Turn on backlight")
        lcd.backlight_on()

        print("Test writing a string to the first line of the LCD")
        lcd.write_line("Hello, world!", 0)
        utime.sleep(3)

        # Ref: https://github.com/Thomascountz/micropython_i2c_lcd/issues/4
        print("Test writing an 8-character string at a specific cursor position")
        greetings = ["friends!", "amigos!", "venner!", "Freunde!", "vrienden!"]

        for greeting in greetings:
            hd44780.set_cursor(line=0, column=7)
            hd44780.write_string(greeting)
            utime.sleep(1)

        utime.sleep(3)

        print ("Clear the display")
        lcd.clear()

        print("Test writing 8 characters to a specific cursor position one at a time")
        hd44780.set_cursor(line=0, column=7) # midway through the top line
        for character in "I <3 uPy!":
            hd44780.write_char(character) # Cursor "auto-advances"
            if character != " ":
                utime.sleep(1)
        utime.sleep(2)

        print(
            "Write a string to the first line of the LCD to indicate that the test is completed"
        )
        lcd.write_line("Test completed!", 0)

        print("Wait for a while")
        utime.sleep(2)

        print("Clear the LCD at the end of each loop iteration")
        lcd.clear()

if __name__ == "__main__":
    main_test()

Cheers! 🚀

Thomascountz commented 6 months ago

Here is a link to where you can access the simulation on Wowki: https://wokwi.com/projects/396956588422568961

BertLindeman commented 6 months ago

FUN! I just started this morning for the first test on wokwi. Coincidence does not exist. A great way to show the breadboard and the results.

BertLindeman commented 6 months ago

Updated my test script and used wokwi to share it: wokwi project

Thanks.

Bert

Thomascountz commented 6 months ago

Awesome stuff! I hope this little library makes using the display easier. And, I forgot to say, thank you very much for the PR/contribution!