Skip to content

Commit

Permalink
hd44780: allow to configure line length (Klipper3d#3543)
Browse files Browse the repository at this point in the history
This allows to use 16x4 displays rather than only 20x4.

Signed-off-by: Martin Hierholzer <hier@beta-centauri.de>
  • Loading branch information
mhier authored and tntclaus committed Apr 18, 2021
1 parent d1ba89d commit eeccf32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/Config_Reference.md
Expand Up @@ -2983,6 +2983,10 @@ lcd_type:
#d7_pin:
# The pins connected to an hd44780 type lcd. These parameters must
# be provided when using an hd44780 display.
#line_length:
# Set the number of characters per line for an hd44780 type lcd.
# Possible values are 20 (default) and 16. The number of lines is
# fixed to 4.
#cs_pin:
#sclk_pin:
#sid_pin:
Expand Down
23 changes: 15 additions & 8 deletions klippy/extras/display/hd44780.py
Expand Up @@ -7,6 +7,8 @@
import logging

BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
LINE_LENGTH_DEFAULT="20"
LINE_LENGTH_OPTIONS={"16":16, "20":20}

TextGlyphs = { 'right_arrow': '\x7e' }

Expand All @@ -19,6 +21,8 @@ def __init__(self, config):
ppins = self.printer.lookup_object('pins')
pins = [ppins.lookup_pin(config.get(name + '_pin'))
for name in ['rs', 'e', 'd4', 'd5', 'd6', 'd7']]
self.line_length = config.getchoice('line_length', LINE_LENGTH_OPTIONS,
LINE_LENGTH_DEFAULT)
mcu = None
for pin_params in pins:
if mcu is not None and pin_params['chip'] != mcu:
Expand All @@ -31,12 +35,15 @@ def __init__(self, config):
self.send_data_cmd = self.send_cmds_cmd = None
self.icons = {}
# framebuffers
self.text_framebuffers = [bytearray(' '*40), bytearray(' '*40)]
self.text_framebuffers = [bytearray(' '*2*self.line_length),
bytearray(' '*2*self.line_length)]
self.glyph_framebuffer = bytearray(64)
self.all_framebuffers = [
# Text framebuffers
(self.text_framebuffers[0], bytearray('~'*40), 0x80),
(self.text_framebuffers[1], bytearray('~'*40), 0xc0),
(self.text_framebuffers[0], bytearray('~'*2*self.line_length),
0x80),
(self.text_framebuffers[1], bytearray('~'*2*self.line_length),
0xc0),
# Glyph framebuffer
(self.glyph_framebuffer, bytearray('~'*64), 0x40) ]
def build_config(self):
Expand Down Expand Up @@ -90,9 +97,9 @@ def init(self):
self.send_cmds_cmd.send([self.oid, cmds], minclock=minclock)
self.flush()
def write_text(self, x, y, data):
if x + len(data) > 20:
data = data[:20 - min(x, 20)]
pos = x + ((y & 0x02) >> 1) * 20
if x + len(data) > self.line_length:
data = data[:self.line_length - min(x, self.line_length)]
pos = x + ((y & 0x02) >> 1) * self.line_length
self.text_framebuffers[y & 1][pos:pos+len(data)] = data
def set_glyphs(self, glyphs):
for glyph_name, glyph_data in glyphs.items():
Expand All @@ -115,8 +122,8 @@ def write_glyph(self, x, y, glyph_name):
def write_graphics(self, x, y, data):
pass
def clear(self):
spaces = ' ' * 40
spaces = ' ' * 2*self.line_length
self.text_framebuffers[0][:] = spaces
self.text_framebuffers[1][:] = spaces
def get_dimensions(self):
return (20, 4)
return (self.line_length, 4)

0 comments on commit eeccf32

Please sign in to comment.