2018-06-27 19:06:53 +02:00
|
|
|
# Support for HD44780 (20x4 text) LCD displays
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
# Copyright (C) 2018 Eric Callahan <arksine.code@gmail.com>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging
|
|
|
|
|
|
|
|
BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
|
2020-11-20 20:49:38 +01:00
|
|
|
LINE_LENGTH_DEFAULT="20"
|
|
|
|
LINE_LENGTH_OPTIONS={"16":16, "20":20}
|
2018-06-27 19:06:53 +02:00
|
|
|
|
2020-06-09 04:04:24 +02:00
|
|
|
TextGlyphs = { 'right_arrow': '\x7e' }
|
|
|
|
|
2020-01-24 06:01:48 +01:00
|
|
|
HD44780_DELAY = .000040
|
2018-06-27 19:06:53 +02:00
|
|
|
|
|
|
|
class HD44780:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
# pin config
|
|
|
|
ppins = self.printer.lookup_object('pins')
|
2018-07-26 15:44:45 +02:00
|
|
|
pins = [ppins.lookup_pin(config.get(name + '_pin'))
|
2018-06-27 19:06:53 +02:00
|
|
|
for name in ['rs', 'e', 'd4', 'd5', 'd6', 'd7']]
|
2020-11-20 20:49:38 +01:00
|
|
|
self.line_length = config.getchoice('line_length', LINE_LENGTH_OPTIONS,
|
|
|
|
LINE_LENGTH_DEFAULT)
|
2018-06-27 19:06:53 +02:00
|
|
|
mcu = None
|
|
|
|
for pin_params in pins:
|
|
|
|
if mcu is not None and pin_params['chip'] != mcu:
|
|
|
|
raise ppins.error("hd44780 all pins must be on same mcu")
|
|
|
|
mcu = pin_params['chip']
|
|
|
|
self.pins = [pin_params['pin'] for pin_params in pins]
|
|
|
|
self.mcu = mcu
|
|
|
|
self.oid = self.mcu.create_oid()
|
2018-09-03 17:48:22 +02:00
|
|
|
self.mcu.register_config_callback(self.build_config)
|
2018-06-27 19:06:53 +02:00
|
|
|
self.send_data_cmd = self.send_cmds_cmd = None
|
2020-06-09 04:04:24 +02:00
|
|
|
self.icons = {}
|
2018-06-27 19:06:53 +02:00
|
|
|
# framebuffers
|
2020-11-20 20:49:38 +01:00
|
|
|
self.text_framebuffers = [bytearray(' '*2*self.line_length),
|
|
|
|
bytearray(' '*2*self.line_length)]
|
2018-09-20 20:16:24 +02:00
|
|
|
self.glyph_framebuffer = bytearray(64)
|
|
|
|
self.all_framebuffers = [
|
2019-12-02 01:11:03 +01:00
|
|
|
# Text framebuffers
|
2020-11-20 20:49:38 +01:00
|
|
|
(self.text_framebuffers[0], bytearray('~'*2*self.line_length),
|
|
|
|
0x80),
|
|
|
|
(self.text_framebuffers[1], bytearray('~'*2*self.line_length),
|
|
|
|
0xc0),
|
2018-09-20 20:16:24 +02:00
|
|
|
# Glyph framebuffer
|
|
|
|
(self.glyph_framebuffer, bytearray('~'*64), 0x40) ]
|
2018-06-27 19:06:53 +02:00
|
|
|
def build_config(self):
|
|
|
|
self.mcu.add_config_cmd(
|
|
|
|
"config_hd44780 oid=%d rs_pin=%s e_pin=%s"
|
|
|
|
" d4_pin=%s d5_pin=%s d6_pin=%s d7_pin=%s delay_ticks=%d" % (
|
|
|
|
self.oid, self.pins[0], self.pins[1],
|
|
|
|
self.pins[2], self.pins[3], self.pins[4], self.pins[5],
|
|
|
|
self.mcu.seconds_to_clock(HD44780_DELAY)))
|
|
|
|
cmd_queue = self.mcu.alloc_command_queue()
|
|
|
|
self.send_cmds_cmd = self.mcu.lookup_command(
|
|
|
|
"hd44780_send_cmds oid=%c cmds=%*s", cq=cmd_queue)
|
|
|
|
self.send_data_cmd = self.mcu.lookup_command(
|
|
|
|
"hd44780_send_data oid=%c data=%*s", cq=cmd_queue)
|
|
|
|
def send(self, cmds, is_data=False):
|
|
|
|
cmd_type = self.send_cmds_cmd
|
|
|
|
if is_data:
|
|
|
|
cmd_type = self.send_data_cmd
|
|
|
|
cmd_type.send([self.oid, cmds], reqclock=BACKGROUND_PRIORITY_CLOCK)
|
|
|
|
#logging.debug("hd44780 %d %s", is_data, repr(cmds))
|
|
|
|
def flush(self):
|
|
|
|
# Find all differences in the framebuffers and send them to the chip
|
2018-09-20 20:16:24 +02:00
|
|
|
for new_data, old_data, fb_id in self.all_framebuffers:
|
2018-06-27 19:06:53 +02:00
|
|
|
if new_data == old_data:
|
|
|
|
continue
|
|
|
|
# Find the position of all changed bytes in this framebuffer
|
2019-02-27 19:31:47 +01:00
|
|
|
diffs = [[i, 1] for i, (n, o) in enumerate(zip(new_data, old_data))
|
|
|
|
if n != o]
|
2018-06-27 19:06:53 +02:00
|
|
|
# Batch together changes that are close to each other
|
|
|
|
for i in range(len(diffs)-2, -1, -1):
|
|
|
|
pos, count = diffs[i]
|
|
|
|
nextpos, nextcount = diffs[i+1]
|
|
|
|
if pos + 4 >= nextpos and nextcount < 16:
|
|
|
|
diffs[i][1] = nextcount + (nextpos - pos)
|
|
|
|
del diffs[i+1]
|
|
|
|
# Transmit changes
|
|
|
|
for pos, count in diffs:
|
|
|
|
chip_pos = pos
|
|
|
|
self.send([fb_id + chip_pos])
|
|
|
|
self.send(new_data[pos:pos+count], is_data=True)
|
|
|
|
old_data[:] = new_data
|
|
|
|
def init(self):
|
|
|
|
curtime = self.printer.get_reactor().monotonic()
|
|
|
|
print_time = self.mcu.estimated_print_time(curtime)
|
|
|
|
# Program 4bit / 2-line mode and then issue 0x02 "Home" command
|
2020-10-12 16:38:30 +02:00
|
|
|
init = [[0x33], [0x33], [0x32], [0x28, 0x28, 0x02]]
|
2018-06-27 19:06:53 +02:00
|
|
|
# Reset (set positive direction ; enable display and hide cursor)
|
|
|
|
init.append([0x06, 0x0c])
|
|
|
|
for i, cmds in enumerate(init):
|
|
|
|
minclock = self.mcu.print_time_to_clock(print_time + i * .100)
|
|
|
|
self.send_cmds_cmd.send([self.oid, cmds], minclock=minclock)
|
|
|
|
self.flush()
|
|
|
|
def write_text(self, x, y, data):
|
2020-11-20 20:49:38 +01:00
|
|
|
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
|
2019-12-02 01:11:03 +01:00
|
|
|
self.text_framebuffers[y & 1][pos:pos+len(data)] = data
|
2020-06-07 18:25:19 +02:00
|
|
|
def set_glyphs(self, glyphs):
|
2020-06-09 04:04:24 +02:00
|
|
|
for glyph_name, glyph_data in glyphs.items():
|
|
|
|
data = glyph_data.get('icon5x8')
|
|
|
|
if data is not None:
|
|
|
|
self.icons[glyph_name] = data
|
2018-09-20 18:40:30 +02:00
|
|
|
def write_glyph(self, x, y, glyph_name):
|
2020-06-09 04:04:24 +02:00
|
|
|
data = self.icons.get(glyph_name)
|
|
|
|
if data is not None:
|
|
|
|
slot, bits = data
|
2020-09-05 04:09:00 +02:00
|
|
|
self.write_text(x, y, [slot])
|
2020-06-09 04:04:24 +02:00
|
|
|
self.glyph_framebuffer[slot * 8:(slot + 1) * 8] = bits
|
|
|
|
return 1
|
2018-09-20 18:40:30 +02:00
|
|
|
char = TextGlyphs.get(glyph_name)
|
|
|
|
if char is not None:
|
|
|
|
# Draw character
|
|
|
|
self.write_text(x, y, char)
|
|
|
|
return 1
|
|
|
|
return 0
|
2020-06-11 02:58:45 +02:00
|
|
|
def write_graphics(self, x, y, data):
|
2020-02-16 21:22:16 +01:00
|
|
|
pass
|
2018-06-27 19:06:53 +02:00
|
|
|
def clear(self):
|
2020-11-20 20:49:38 +01:00
|
|
|
spaces = ' ' * 2*self.line_length
|
2019-12-02 01:11:03 +01:00
|
|
|
self.text_framebuffers[0][:] = spaces
|
|
|
|
self.text_framebuffers[1][:] = spaces
|
2018-11-22 00:42:06 +01:00
|
|
|
def get_dimensions(self):
|
2020-11-20 20:49:38 +01:00
|
|
|
return (self.line_length, 4)
|