From 317402d5ba28fd5c57e8426a5f2661c811654f30 Mon Sep 17 00:00:00 2001 From: le-Bark <43352109+le-Bark@users.noreply.github.com> Date: Wed, 7 Apr 2021 21:07:23 -0400 Subject: [PATCH] hd44780_spi: added lcd support to the mightyboard (#4121) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Denis --- config/generic-mightyboard.cfg | 13 +++ docs/Config_Reference.md | 19 ++-- klippy/extras/display/display.py | 3 +- klippy/extras/display/hd44780_spi.py | 125 +++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 klippy/extras/display/hd44780_spi.py diff --git a/config/generic-mightyboard.cfg b/config/generic-mightyboard.cfg index 4f336fee..9a784566 100644 --- a/config/generic-mightyboard.cfg +++ b/config/generic-mightyboard.cfg @@ -117,3 +117,16 @@ scl_pin: PJ5 sda_pin: PJ6 wiper: 0.50 scale: 0.773 + +[display] +lcd_type: hd44780_spi +spi_software_mosi_pin: PC3 +spi_software_sclk_pin: PC2 +#miso not used, dummy pin. +spi_software_miso_pin: PJ1 +latch_pin: PC4 + +click_pin: ^PJ0 +back_pin: ^PJ2 +up_pin: ^PJ4 +down_pin: ^PJ3 diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index cca85161..7833a492 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -3089,7 +3089,8 @@ Support for a display attached to the micro-controller. [display] lcd_type: # The type of LCD chip in use. This may be "hd44780" (which is used -# in "RepRapDiscount 2004 Smart Controller" type displays), "st7920" +# in "RepRapDiscount 2004 Smart Controller" type displays),"hd44780_spi" +# (which is used in mightyboard based printers), "st7920" # (which is used in "RepRapDiscount 12864 Full Graphic Smart # Controller" type displays), "emulated_st7920" (which emulate a ST7920 # display but won't work properly with the "st7920" display driver), @@ -3122,12 +3123,16 @@ lcd_type: #spi_software_sclk_pin: #spi_software_mosi_pin: #spi_software_miso_pin: -# The pins connected to an emulated_st7920 type lcd. The en_pin corresponds -# to the cs_pin of the st7920 type lcd, spi_software_sclk_pin corresponds -# to sclk_pin and spi_software_mosi_pin corresponds to sid_pin. The -# spi_software_miso_pin needs to be set to an unused pin of the printer -# mainboard as the st7920 as no MISO pin but the software spi implementation -# requires this pin to be configured. The default spi_speed is 1MHz. +# The pins connected to an emulated_st7920 or hd44780_spi type lcd. +# The en_pin corresponds to the cs_pin of the st7920 type lcd, +# spi_software_sclk_pin corresponds to sclk_pin and spi_software_mosi_pin +# corresponds to sid_pin. The spi_software_miso_pin needs to be set to an +# unused pin of the printer mainboard as the st7920 as no MISO pin +# but the software spi implementation requires this pin to be configured. +# The default spi_speed is 1MHz. +#latch_pin: +# Used with the hd44780_spi type lcd, connected to the latch pin of the +# shift register. #cs_pin: #a0_pin: #rst_pin: diff --git a/klippy/extras/display/display.py b/klippy/extras/display/display.py index e43d3307..b416d9d2 100644 --- a/klippy/extras/display/display.py +++ b/klippy/extras/display/display.py @@ -6,7 +6,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import logging, os, ast -from . import hd44780, st7920, uc1701, menu +from . import hd44780, hd44780_spi, st7920, uc1701, menu # Normal time between each screen redraw REDRAW_TIME = 0.500 @@ -17,6 +17,7 @@ LCD_chips = { 'st7920': st7920.ST7920, 'emulated_st7920': st7920.EmulatedST7920, 'hd44780': hd44780.HD44780, 'uc1701': uc1701.UC1701, 'ssd1306': uc1701.SSD1306, 'sh1106': uc1701.SH1106, + 'hd44780_spi': hd44780_spi.hd44780_spi } # Storage of [display_template my_template] config sections diff --git a/klippy/extras/display/hd44780_spi.py b/klippy/extras/display/hd44780_spi.py new file mode 100644 index 00000000..a935a832 --- /dev/null +++ b/klippy/extras/display/hd44780_spi.py @@ -0,0 +1,125 @@ +# Support for HD44780 (20x4 text) LCD displays +# +# Copyright (C) 2018 Kevin O'Connor +# Copyright (C) 2018 Eric Callahan +# Copyright (C) 2021 Marc-Andre Denis +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import logging +from .. import bus + +LINE_LENGTH_DEFAULT="20" +LINE_LENGTH_OPTIONS={"16":16, "20":20} + +TextGlyphs = { 'right_arrow': '\x7e' } + + + +class hd44780_spi: + def __init__(self, config): + self.printer = config.get_printer() + self.hd44780_protocol_init = config.getboolean('hd44780_protocol_init', + True) + # spi config + self.spi = bus.MCU_SPI_from_config( + config, 0x00, pin_option="latch_pin") + self.mcu = self.spi.get_mcu() + #self.spi.spi_send([0x01,0xa0]) + self.data_mask = (1<<1) + self.command_mask = 0 + self.enable_mask = (1<<3) + + self.icons = {} + self.line_length = config.getchoice('line_length', LINE_LENGTH_OPTIONS, + LINE_LENGTH_DEFAULT) + + # framebuffers + 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('~'*2*self.line_length), + 0x80), + (self.text_framebuffers[1], bytearray('~'*2*self.line_length), + 0xc0), + # Glyph framebuffer + (self.glyph_framebuffer, bytearray('~'*64), 0x40) ] + def send_4_bits(self, cmd, is_data, minclock): + if is_data: + mask = self.data_mask + else: + mask = self.command_mask + self.spi.spi_send([(cmd & 0xF0) | mask], minclock) + self.spi.spi_send([(cmd & 0xF0) | mask | self.enable_mask], minclock) + self.spi.spi_send([(cmd & 0xF0) | mask], minclock) + def send(self, cmds, is_data=False, minclock=0): + for data in cmds: + self.send_4_bits(data,is_data,minclock) + self.send_4_bits(data<<4,is_data,minclock) + def flush(self): + # Find all differences in the framebuffers and send them to the chip + for new_data, old_data, fb_id in self.all_framebuffers: + if new_data == old_data: + continue + # Find the position of all changed bytes in this framebuffer + diffs = [[i, 1] for i, (n, o) in enumerate(zip(new_data, old_data)) + if n != o] + # 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 + if self.hd44780_protocol_init: + init = [[0x33], [0x33], [0x32], [0x28, 0x28, 0x02]] + else: + init = [[0x02]] + # 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, minclock=minclock) + self.flush() + def write_text(self, x, y, data): + 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(): + data = glyph_data.get('icon5x8') + if data is not None: + self.icons[glyph_name] = data + def write_glyph(self, x, y, glyph_name): + data = self.icons.get(glyph_name) + if data is not None: + slot, bits = data + self.write_text(x, y, [slot]) + self.glyph_framebuffer[slot * 8:(slot + 1) * 8] = bits + return 1 + char = TextGlyphs.get(glyph_name) + if char is not None: + # Draw character + self.write_text(x, y, char) + return 1 + return 0 + def write_graphics(self, x, y, data): + pass + def clear(self): + spaces = ' ' * 2*self.line_length + self.text_framebuffers[0][:] = spaces + self.text_framebuffers[1][:] = spaces + def get_dimensions(self): + return (self.line_length, 4)