dbrgn / RPLCD

A Raspberry Pi LCD library for the widely used Hitachi HD44780 controller, written in Python. GPIO (parallel) and I²C modes supported.
https://rplcd.readthedocs.io/en/latest/
MIT License
261 stars 72 forks source link

Cursor position (0, 20) invalid on a 4x20 LCD #93

Closed dbrgn closed 1 year ago

dbrgn commented 5 years ago

When initializing an LCD with "auto_linebreaks=False", then writing a 20 character string to position (0, 0) seems to fail.

Traceback (most recent call last):
  File "/usr/local/bin/lcd.py", line 60, in <module>
    main(controller, addr, cols, rows)
  File "/usr/local/bin/lcd.py", line 28, in main
    loop(lcd, unit)
  File "/usr/local/bin/lcd.py", line 48, in loop
    lcd.write_string(datestring.ljust(lcd.lcd.cols))
  File "/usr/local/lib/python3.5/dist-packages/RPLCD/lcd.py", line 279, in write_string
    self.write(char)
  File "/usr/local/lib/python3.5/dist-packages/RPLCD/lcd.py", line 406, in write
    self.cursor_pos = newpos
  File "/usr/local/lib/python3.5/dist-packages/RPLCD/lcd.py", line 151, in _set_cursor_pos
    raise ValueError(msg.format(pos=value, lcd=self.lcd))
ValueError: Cursor position (0, 20) invalid on a 4x20 LCD.
dhrone commented 5 years ago

You need to decide what you are going to do when auto_linebreaks is true and you have completely filled a line. I'd suggest returning col to 0. Perhaps change line 404 in lcd.py to... newpos = (row, col + 1 if col + 1 < self.lcd.cols else 0)

dbrgn commented 5 years ago

I'm not sure. My preferred solution would be to simply continue writing to the memory. If you turn off auto linebreaks, then you're on your own and any logic should be disabled.

This means that we'd have to disable bounds checks in that case.

joscha commented 5 years ago

I agree with @dbrgn, once auto linebreaks are off I think if you write more cols than there are it shouldn't be the problem of the library any more.

dbrgn commented 5 years ago

Pull requests are welcome! I don't currently have much time for working on this bugfix.

JsBergbau commented 4 years ago

Anything new when this issue will be fixed in the official release you install with pip? Even resetting the Cursorposition doesn't help. Second write leads to an exception. Since I return the Cursorposition to the beginning, this shouldn't happen. If you wriite "ab" in the second write, display shows "ab3456789abcdefghijk", so moving the cursor seems to work.

lcd.write_string("123456789abcdefghijk")
lcd.home()
lcd.cursor_pos=(0,0)
lcd.write_string("123456789abcdefghijk") #Exception
ghost commented 3 years ago

This is also applies to my 16x2 display is there any workaround, other than just not using the right-most char?

dbrgn commented 3 years ago

is there any workaround, other than just not using the right-most char?

Maybe using auto_linebreaks=False.

Anything new when this issue will be fixed in the official release you install with pip?

Pull requests fixing this issue would be welcome! See this comment on how it should be fixed.

dbrgn commented 2 years ago

Oops, it seems that in my previous comment I overlooked that there's already a proposed fix in #109. Let's continue the discussion there.

poggenpower commented 1 year ago

Hi,

have the same issue and took a look into the code. Shouldn't this code https://github.com/dbrgn/RPLCD/blob/master/RPLCD/lcd.py#L404-L420 ensure that the cursor is not running out of its scope with auto_linebreaks=True? If I understand the code correct will the cursor move to the next line if the current is filled and if the last line is filled it flips to POS (0, 0). Not sure why it is failing.

dbrgn commented 1 year ago

Should be fixed with #109.