dhylands / python_lcd

Python based library for talking to character based LCDs.
MIT License
298 stars 116 forks source link

One linefeed too many, in case the line is filled up to the last character (4x20 display) / CircuitPython Port #28

Open CrossLAN opened 3 years ago

CrossLAN commented 3 years ago

@dhylands In a 4x20 display, if I fill the line to the last row, it skips the following line without replacement and the following line remains empty. This is independent of whether I put a \n at the end of the line or not.

If I fill the display only with 19 characters instead of the 20 possible characters, the line jump to the following line works.

Is this intentional or is it possible to turn it off? Or am I doing something wrong?

Here is my code for testing

import board
import busio
from time import sleep, monotonic
from lcd.circuitpython_i2c_lcd import I2cLcd

# The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x26

def test_main():
    """Test function for verifying basic functionality."""
    print("Running test_main")
    i2c = busio.I2C(board.SCL, board.SDA)

    # circuitpython seems to require locking the i2c bus
    while i2c.try_lock():
        pass

    # 2 lines, 16 characters per line
    #lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

    #4 lines, 20 characters per line
    lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 4, 20)

    while True:
        lcd.clear()
        lcd.move_to(0, 0)
        lcd.putstr("It works!\nSecond line\n")
        print("It works!\nSecond line\n")
        sleep(2)
        lcd.clear()
        lcd.move_to(0, 0)
        lcd.putstr("12345678901234567890\n123xxxx890123yyyy890\n")
        print("12345678901234567890\n123xxxx890123yyyy890\n")
        sleep(2)
        print("Ende\n")

#if __name__ == "__main__":
test_main()
CrossLAN commented 3 years ago

@dhylands

I walked through your code and the following change in lcd_api.py will fix this issue:

    def putchar(self, char):
        """Writes the indicated character to the LCD at the current cursor
        position, and advances the cursor by one position.
        """
        if char != '\n':
            self.hal_write_data(ord(char))
            self.cursor_x += 1
        if self.cursor_x >= self.num_columns or char == '\n':
            self.cursor_x = 0
            if char == '\n':
                self.cursor_y += 1
            if self.cursor_y >= self.num_lines:
                self.cursor_y = 0
            self.move_to(self.cursor_x, self.cursor_y)
CrossLAN commented 3 years ago

@dhylands I created a pull request #29 to get this fixed.