2018-11-24 05:41:41 +03:00
|
|
|
# Support for UC1701 (and similar) 128x64 graphics LCD displays
|
2018-06-28 01:15:50 +03:00
|
|
|
#
|
2019-08-18 04:46:45 +03:00
|
|
|
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
2018-06-28 01:15:50 +03:00
|
|
|
# Copyright (C) 2018 Eric Callahan <arksine.code@gmail.com>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging
|
2020-06-12 20:38:12 +03:00
|
|
|
from .. import bus
|
2020-06-12 16:55:57 +03:00
|
|
|
from . import font8x14
|
2018-06-28 01:15:50 +03:00
|
|
|
|
|
|
|
BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
|
|
|
|
|
2018-09-20 20:11:25 +03:00
|
|
|
TextGlyphs = { 'right_arrow': '\x1a', 'degrees': '\xf8' }
|
2018-09-20 19:40:30 +03:00
|
|
|
|
2018-11-24 05:41:41 +03:00
|
|
|
class DisplayBase:
|
2020-09-10 06:07:40 +03:00
|
|
|
def __init__(self, io, columns=128, x_offset=0):
|
2018-11-24 05:41:41 +03:00
|
|
|
self.send = io.send
|
2018-11-19 01:56:36 +03:00
|
|
|
# framebuffers
|
2019-11-25 05:21:37 +03:00
|
|
|
self.columns = columns
|
2020-09-10 06:07:40 +03:00
|
|
|
self.x_offset = x_offset
|
2019-11-25 05:21:37 +03:00
|
|
|
self.vram = [bytearray(self.columns) for i in range(8)]
|
|
|
|
self.all_framebuffers = [(self.vram[i], bytearray('~'*self.columns), i)
|
2018-11-21 20:43:16 +03:00
|
|
|
for i in range(8)]
|
2018-11-22 02:03:13 +03:00
|
|
|
# Cache fonts and icons in display byte order
|
2019-02-27 21:24:50 +03:00
|
|
|
self.font = [self._swizzle_bits(bytearray(c))
|
|
|
|
for c in font8x14.VGA_FONT]
|
2018-11-22 02:03:13 +03:00
|
|
|
self.icons = {}
|
2018-06-28 01:15:50 +03:00
|
|
|
def flush(self):
|
2018-11-21 20:43:16 +03:00
|
|
|
# Find all differences in the framebuffers and send them to the chip
|
|
|
|
for new_data, old_data, page in self.all_framebuffers:
|
|
|
|
if new_data == old_data:
|
2018-06-28 01:15:50 +03:00
|
|
|
continue
|
|
|
|
# Find the position of all changed bytes in this framebuffer
|
2018-11-21 20:43:16 +03:00
|
|
|
diffs = [[i, 1] for i, (n, o) in enumerate(zip(new_data, old_data))
|
|
|
|
if n != o]
|
2018-06-28 01:15:50 +03: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 + 5 >= nextpos and nextcount < 16:
|
|
|
|
diffs[i][1] = nextcount + (nextpos - pos)
|
|
|
|
del diffs[i+1]
|
2018-11-21 20:43:16 +03:00
|
|
|
# Transmit changes
|
2018-06-28 01:15:50 +03:00
|
|
|
for col_pos, count in diffs:
|
|
|
|
# Set Position registers
|
|
|
|
ra = 0xb0 | (page & 0x0F)
|
|
|
|
ca_msb = 0x10 | ((col_pos >> 4) & 0x0F)
|
|
|
|
ca_lsb = col_pos & 0x0F
|
|
|
|
self.send([ra, ca_msb, ca_lsb])
|
|
|
|
# Send Data
|
2018-11-21 20:43:16 +03:00
|
|
|
self.send(new_data[col_pos:col_pos+count], is_data=True)
|
|
|
|
old_data[:] = new_data
|
2018-11-22 02:03:13 +03:00
|
|
|
def _swizzle_bits(self, data):
|
2020-06-11 20:28:37 +03:00
|
|
|
# Convert from "rows of pixels" format to "columns of pixels"
|
|
|
|
top = bot = 0
|
2018-11-22 02:03:13 +03:00
|
|
|
for row in range(8):
|
2020-06-11 20:28:37 +03:00
|
|
|
spaced = (data[row] * 0x8040201008040201) & 0x8080808080808080
|
|
|
|
top |= spaced >> (7 - row)
|
|
|
|
spaced = (data[row + 8] * 0x8040201008040201) & 0x8080808080808080
|
|
|
|
bot |= spaced >> (7 - row)
|
|
|
|
bits_top = [(top >> s) & 0xff for s in range(0, 64, 8)]
|
|
|
|
bits_bot = [(bot >> s) & 0xff for s in range(0, 64, 8)]
|
|
|
|
return (bytearray(bits_top), bytearray(bits_bot))
|
2020-06-11 03:58:45 +03:00
|
|
|
def set_glyphs(self, glyphs):
|
|
|
|
for glyph_name, glyph_data in glyphs.items():
|
|
|
|
icon = glyph_data.get('icon16x16')
|
|
|
|
if icon is not None:
|
|
|
|
top1, bot1 = self._swizzle_bits(icon[0])
|
|
|
|
top2, bot2 = self._swizzle_bits(icon[1])
|
|
|
|
self.icons[glyph_name] = (top1 + top2, bot1 + bot2)
|
2018-06-28 01:15:50 +03:00
|
|
|
def write_text(self, x, y, data):
|
|
|
|
if x + len(data) > 16:
|
|
|
|
data = data[:16 - min(x, 16)]
|
2018-11-22 02:03:13 +03:00
|
|
|
pix_x = x * 8
|
2020-09-10 06:07:40 +03:00
|
|
|
pix_x += self.x_offset
|
2018-11-22 02:03:13 +03:00
|
|
|
page_top = self.vram[y * 2]
|
|
|
|
page_bot = self.vram[y * 2 + 1]
|
2020-09-05 05:29:55 +03:00
|
|
|
for c in bytearray(data):
|
|
|
|
bits_top, bits_bot = self.font[c]
|
2018-11-22 02:03:13 +03:00
|
|
|
page_top[pix_x:pix_x+8] = bits_top
|
|
|
|
page_bot[pix_x:pix_x+8] = bits_bot
|
2018-06-28 01:15:50 +03:00
|
|
|
pix_x += 8
|
2020-06-11 03:58:45 +03:00
|
|
|
def write_graphics(self, x, y, data):
|
|
|
|
if x >= 16 or y >= 4 or len(data) != 16:
|
|
|
|
return
|
|
|
|
bits_top, bits_bot = self._swizzle_bits(data)
|
2018-11-22 02:03:13 +03:00
|
|
|
pix_x = x * 8
|
2020-09-10 06:07:40 +03:00
|
|
|
pix_x += self.x_offset
|
2020-06-11 03:58:45 +03:00
|
|
|
page_top = self.vram[y * 2]
|
|
|
|
page_bot = self.vram[y * 2 + 1]
|
|
|
|
for i in range(8):
|
|
|
|
page_top[pix_x + i] ^= bits_top[i]
|
|
|
|
page_bot[pix_x + i] ^= bits_bot[i]
|
2018-09-20 18:58:43 +03:00
|
|
|
def write_glyph(self, x, y, glyph_name):
|
2018-11-22 02:03:13 +03:00
|
|
|
icon = self.icons.get(glyph_name)
|
|
|
|
if icon is not None and x < 15:
|
2018-09-20 18:58:43 +03:00
|
|
|
# Draw icon in graphics mode
|
2018-11-22 02:03:13 +03:00
|
|
|
pix_x = x * 8
|
2020-09-10 06:07:40 +03:00
|
|
|
pix_x += self.x_offset
|
2018-11-22 02:03:13 +03:00
|
|
|
page_idx = y * 2
|
|
|
|
self.vram[page_idx][pix_x:pix_x+16] = icon[0]
|
|
|
|
self.vram[page_idx + 1][pix_x:pix_x+16] = icon[1]
|
2018-09-20 19:40:30 +03:00
|
|
|
return 2
|
|
|
|
char = TextGlyphs.get(glyph_name)
|
|
|
|
if char is not None:
|
|
|
|
# Draw character
|
|
|
|
self.write_text(x, y, char)
|
|
|
|
return 1
|
|
|
|
return 0
|
2018-06-28 01:15:50 +03:00
|
|
|
def clear(self):
|
2019-11-25 05:21:37 +03:00
|
|
|
zeros = bytearray(self.columns)
|
2018-11-21 20:43:16 +03:00
|
|
|
for page in self.vram:
|
2018-06-28 19:59:47 +03:00
|
|
|
page[:] = zeros
|
2018-11-22 02:42:06 +03:00
|
|
|
def get_dimensions(self):
|
|
|
|
return (16, 4)
|
2018-11-21 21:43:48 +03:00
|
|
|
|
2018-11-24 05:41:41 +03:00
|
|
|
# IO wrapper for "4 wire" spi bus (spi bus with an extra data/control line)
|
|
|
|
class SPI4wire:
|
|
|
|
def __init__(self, config, data_pin_name):
|
2020-06-12 20:38:12 +03:00
|
|
|
self.spi = bus.MCU_SPI_from_config(config, 0, default_speed=10000000)
|
2019-08-18 04:46:45 +03:00
|
|
|
dc_pin = config.get(data_pin_name)
|
2020-06-12 20:38:12 +03:00
|
|
|
self.mcu_dc = bus.MCU_bus_digital_out(self.spi.get_mcu(), dc_pin,
|
|
|
|
self.spi.get_command_queue())
|
2018-11-24 05:41:41 +03:00
|
|
|
def send(self, cmds, is_data=False):
|
2019-08-18 04:46:45 +03:00
|
|
|
self.mcu_dc.update_digital_out(is_data,
|
|
|
|
reqclock=BACKGROUND_PRIORITY_CLOCK)
|
2018-11-24 05:41:41 +03:00
|
|
|
self.spi.spi_send(cmds, reqclock=BACKGROUND_PRIORITY_CLOCK)
|
|
|
|
|
|
|
|
# IO wrapper for i2c bus
|
|
|
|
class I2C:
|
|
|
|
def __init__(self, config, default_addr):
|
2020-06-12 20:38:12 +03:00
|
|
|
self.i2c = bus.MCU_I2C_from_config(config, default_addr=default_addr,
|
|
|
|
default_speed=400000)
|
2018-11-24 05:41:41 +03:00
|
|
|
def send(self, cmds, is_data=False):
|
|
|
|
if is_data:
|
|
|
|
hdr = 0x40
|
|
|
|
else:
|
|
|
|
hdr = 0x00
|
|
|
|
cmds = bytearray(cmds)
|
|
|
|
cmds.insert(0, hdr)
|
|
|
|
self.i2c.i2c_write(cmds, reqclock=BACKGROUND_PRIORITY_CLOCK)
|
|
|
|
|
2019-12-02 18:12:34 +03:00
|
|
|
# Helper code for toggling a reset pin on startup
|
|
|
|
class ResetHelper:
|
|
|
|
def __init__(self, pin_desc, io_bus):
|
|
|
|
self.mcu_reset = None
|
|
|
|
if pin_desc is None:
|
|
|
|
return
|
2020-06-12 20:38:12 +03:00
|
|
|
self.mcu_reset = bus.MCU_bus_digital_out(io_bus.get_mcu(), pin_desc,
|
|
|
|
io_bus.get_command_queue())
|
2019-12-02 18:12:34 +03:00
|
|
|
def init(self):
|
|
|
|
if self.mcu_reset is None:
|
|
|
|
return
|
|
|
|
mcu = self.mcu_reset.get_mcu()
|
|
|
|
curtime = mcu.get_printer().get_reactor().monotonic()
|
|
|
|
print_time = mcu.estimated_print_time(curtime)
|
2019-12-03 19:13:54 +03:00
|
|
|
# Toggle reset
|
2019-12-02 18:12:34 +03:00
|
|
|
minclock = mcu.print_time_to_clock(print_time + .100)
|
|
|
|
self.mcu_reset.update_digital_out(0, minclock=minclock)
|
|
|
|
minclock = mcu.print_time_to_clock(print_time + .200)
|
|
|
|
self.mcu_reset.update_digital_out(1, minclock=minclock)
|
2019-12-03 19:13:54 +03:00
|
|
|
# Force a delay to any subsequent commands on the command queue
|
|
|
|
minclock = mcu.print_time_to_clock(print_time + .300)
|
|
|
|
self.mcu_reset.update_digital_out(1, minclock=minclock)
|
2019-12-02 18:12:34 +03:00
|
|
|
|
2018-11-24 05:41:41 +03:00
|
|
|
# The UC1701 is a "4-wire" SPI display device
|
|
|
|
class UC1701(DisplayBase):
|
|
|
|
def __init__(self, config):
|
2019-12-02 18:12:34 +03:00
|
|
|
io = SPI4wire(config, "a0_pin")
|
|
|
|
DisplayBase.__init__(self, io)
|
2019-04-02 19:49:08 +03:00
|
|
|
self.contrast = config.getint('contrast', 40, minval=0, maxval=63)
|
2019-12-02 18:12:34 +03:00
|
|
|
self.reset = ResetHelper(config.get("rst_pin", None), io.spi)
|
2018-11-24 05:41:41 +03:00
|
|
|
def init(self):
|
2019-12-02 18:12:34 +03:00
|
|
|
self.reset.init()
|
2018-11-24 05:41:41 +03:00
|
|
|
init_cmds = [0xE2, # System reset
|
|
|
|
0x40, # Set display to start at line 0
|
|
|
|
0xA0, # Set SEG direction
|
|
|
|
0xC8, # Set COM Direction
|
|
|
|
0xA2, # Set Bias = 1/9
|
|
|
|
0x2C, # Boost ON
|
|
|
|
0x2E, # Voltage regulator on
|
|
|
|
0x2F, # Voltage follower on
|
|
|
|
0xF8, # Set booster ratio
|
|
|
|
0x00, # Booster ratio value (4x)
|
|
|
|
0x23, # Set resistor ratio (3)
|
|
|
|
0x81, # Set Electronic Volume
|
2019-04-02 19:49:08 +03:00
|
|
|
self.contrast, # Electronic Volume value
|
2018-11-24 05:41:41 +03:00
|
|
|
0xAC, # Set static indicator off
|
|
|
|
0x00, # NOP
|
|
|
|
0xA6, # Disable Inverse
|
|
|
|
0xAF] # Set display enable
|
|
|
|
self.send(init_cmds)
|
|
|
|
self.send([0xA5]) # display all
|
|
|
|
self.send([0xA4]) # normal display
|
|
|
|
self.flush()
|
|
|
|
|
|
|
|
# The SSD1306 supports both i2c and "4-wire" spi
|
2018-12-28 03:11:46 +03:00
|
|
|
class SSD1306(DisplayBase):
|
2020-09-10 06:07:40 +03:00
|
|
|
def __init__(self, config, columns=128, x_offset=0):
|
2018-11-24 05:41:41 +03:00
|
|
|
cs_pin = config.get("cs_pin", None)
|
|
|
|
if cs_pin is None:
|
2018-12-28 03:11:46 +03:00
|
|
|
io = I2C(config, 60)
|
2019-08-18 05:03:51 +03:00
|
|
|
io_bus = io.i2c
|
2018-11-24 05:41:41 +03:00
|
|
|
else:
|
|
|
|
io = SPI4wire(config, "dc_pin")
|
2019-08-18 05:03:51 +03:00
|
|
|
io_bus = io.spi
|
2019-12-02 18:12:34 +03:00
|
|
|
self.reset = ResetHelper(config.get("reset_pin", None), io_bus)
|
2020-09-10 06:07:40 +03:00
|
|
|
DisplayBase.__init__(self, io, columns, x_offset)
|
2020-07-20 03:18:08 +03:00
|
|
|
self.contrast = config.getint('contrast', 239, minval=0, maxval=255)
|
|
|
|
self.vcomh = config.getint('vcomh', 0, minval=0, maxval=63)
|
|
|
|
self.invert = config.getboolean('invert', False)
|
2018-11-21 21:43:48 +03:00
|
|
|
def init(self):
|
2019-12-02 18:12:34 +03:00
|
|
|
self.reset.init()
|
2018-11-21 21:43:48 +03:00
|
|
|
init_cmds = [
|
|
|
|
0xAE, # Display off
|
|
|
|
0xD5, 0x80, # Set oscillator frequency
|
|
|
|
0xA8, 0x3f, # Set multiplex ratio
|
|
|
|
0xD3, 0x00, # Set display offset
|
|
|
|
0x40, # Set display start line
|
|
|
|
0x8D, 0x14, # Charge pump setting
|
|
|
|
0x20, 0x02, # Set Memory addressing mode
|
|
|
|
0xA1, # Set Segment re-map
|
|
|
|
0xC8, # Set COM output scan direction
|
|
|
|
0xDA, 0x12, # Set COM pins hardware configuration
|
2020-07-20 03:18:08 +03:00
|
|
|
0x81, self.contrast, # Set contrast control
|
2018-11-21 21:43:48 +03:00
|
|
|
0xD9, 0xA1, # Set pre-charge period
|
2020-07-20 03:18:08 +03:00
|
|
|
0xDB, self.vcomh, # Set VCOMH deselect level
|
2018-11-21 21:43:48 +03:00
|
|
|
0x2E, # Deactivate scroll
|
|
|
|
0xA4, # Output ram to display
|
2020-07-20 03:18:08 +03:00
|
|
|
0xA7 if self.invert else 0xA6, # Set normal/invert
|
2018-11-21 21:43:48 +03:00
|
|
|
0xAF, # Display on
|
|
|
|
]
|
|
|
|
self.send(init_cmds)
|
|
|
|
self.flush()
|
2019-11-25 05:21:37 +03:00
|
|
|
|
|
|
|
# the SH1106 is SSD1306 compatible with up to 132 columns
|
|
|
|
class SH1106(SSD1306):
|
|
|
|
def __init__(self, config):
|
2020-09-10 06:07:40 +03:00
|
|
|
x_offset = config.getint('x_offset', 0, minval=0, maxval=3)
|
|
|
|
SSD1306.__init__(self, config, 132, x_offset=x_offset)
|