# Driver for official MicroPython LCD160CR display # MIT license; Copyright (c) 2017 Damien P. George from micropython import const from utime import sleep_ms from ustruct import calcsize, pack_into import uerrno, machine # for set_orient PORTRAIT = const(0) LANDSCAPE = const(1) PORTRAIT_UPSIDEDOWN = const(2) LANDSCAPE_UPSIDEDOWN = const(3) # for set_startup_deco; can be or'd STARTUP_DECO_NONE = const(0) STARTUP_DECO_MLOGO = const(1) STARTUP_DECO_INFO = const(2) _uart_baud_table = { 2400: 0, 4800: 1, 9600: 2, 19200: 3, 38400: 4, 57600: 5, 115200: 6, 230400: 7, 460800: 8, } class LCD160CR: def __init__(self, connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98): if connect in ("X", "Y", "XY", "YX"): i = connect[-1] j = connect[0] y = j + "4" elif connect == "C": i = 2 j = 2 y = "A7" else: if pwr is None or i2c is None or spi is None: raise ValueError('must specify valid "connect" or all of "pwr", "i2c" and "spi"') if pwr is None: pwr = machine.Pin(y, machine.Pin.OUT) if i2c is None: i2c = machine.I2C(i, freq=1000000) if spi is None: spi = machine.SPI(j, baudrate=13500000, polarity=0, phase=0) if not pwr.value(): pwr(1) sleep_ms(10) # else: # alread have power # lets be optimistic... # set connections self.pwr = pwr self.i2c = i2c self.spi = spi self.i2c_addr = i2c_addr # create temp buffers and memoryviews self.buf16 = bytearray(16) self.buf19 = bytearray(19) self.buf = [None] * 10 for i in range(1, 10): self.buf[i] = memoryview(self.buf16)[0:i] self.buf1 = self.buf[1] self.array4 = [0, 0, 0, 0] # set default orientation and window self.set_orient(PORTRAIT) self._fcmd2b("= n: self.i2c.readfrom_into(self.i2c_addr, buf) return t -= 1 sleep_ms(1) raise OSError(uerrno.ETIMEDOUT) def oflush(self, n=255): t = 5000 while t: self.i2c.readfrom_into(self.i2c_addr + 1, self.buf1) r = self.buf1[0] if r >= n: return t -= 1 machine.idle() raise OSError(uerrno.ETIMEDOUT) def iflush(self): t = 5000 while t: self.i2c.readfrom_into(self.i2c_addr, self.buf16) if self.buf16[0] == 0: return t -= 1 sleep_ms(1) raise OSError(uerrno.ETIMEDOUT) #### MISC METHODS #### @staticmethod def rgb(r, g, b): return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3) @staticmethod def clip_line(c, w, h): while True: ca = ce = 0 if c[1] < 0: ca |= 8 elif c[1] > h: ca |= 4 if c[0] < 0: ca |= 1 elif c[0] > w: ca |= 2 if c[3] < 0: ce |= 8 elif c[3] > h: ce |= 4 if c[2] < 0: ce |= 1 elif c[2] > w: ce |= 2 if ca & ce: return False elif ca | ce: ca |= ce if ca & 1: if c[2] < c[0]: c[0], c[2] = c[2], c[0] c[1], c[3] = c[3], c[1] c[1] += ((-c[0]) * (c[3] - c[1])) // (c[2] - c[0]) c[0] = 0 elif ca & 2: if c[2] < c[0]: c[0], c[2] = c[2], c[0] c[1], c[3] = c[3], c[1] c[3] += ((w - 1 - c[2]) * (c[3] - c[1])) // (c[2] - c[0]) c[2] = w - 1 elif ca & 4: if c[0] == c[2]: if c[1] >= h: c[1] = h - 1 if c[3] >= h: c[3] = h - 1 else: if c[3] < c[1]: c[0], c[2] = c[2], c[0] c[1], c[3] = c[3], c[1] c[2] += ((h - 1 - c[3]) * (c[2] - c[0])) // (c[3] - c[1]) c[3] = h - 1 else: if c[0] == c[2]: if c[1] < 0: c[1] = 0 if c[3] < 0: c[3] = 0 else: if c[3] < c[1]: c[0], c[2] = c[2], c[0] c[1], c[3] = c[3], c[1] c[0] += ((-c[1]) * (c[2] - c[0])) // (c[3] - c[1]) c[1] = 0 else: return True #### SETUP COMMANDS #### def set_power(self, on): self.pwr(on) sleep_ms(15) def set_orient(self, orient): self._fcmd2("= 2: self.i2c.readfrom_into(self.i2c_addr, self.buf[3]) return self.buf[3][1] | self.buf[3][2] << 8 t -= 1 sleep_ms(1) raise OSError(uerrno.ETIMEDOUT) def get_line(self, x, y, buf): l = len(buf) // 2 self._fcmd2b("= l: self.i2c.readfrom_into(self.i2c_addr, buf) return t -= 1 sleep_ms(1) raise OSError(uerrno.ETIMEDOUT) def screen_dump(self, buf, x=0, y=0, w=None, h=None): if w is None: w = self.w - x if h is None: h = self.h - y if w <= 127: line = bytearray(2 * w + 1) line2 = None else: # split line if more than 254 bytes needed buflen = (w + 1) // 2 line = bytearray(2 * buflen + 1) line2 = memoryview(line)[: 2 * (w - buflen) + 1] for i in range(min(len(buf) // (2 * w), h)): ix = i * w * 2 self.get_line(x, y + i, line) buf[ix : ix + len(line) - 1] = memoryview(line)[1:] ix += len(line) - 1 if line2: self.get_line(x + buflen, y + i, line2) buf[ix : ix + len(line2) - 1] = memoryview(line2)[1:] ix += len(line2) - 1 def screen_load(self, buf): l = self.w * self.h * 2 + 2 self._fcmd2b("= 0x200: self._send(ar[n : n + 0x200]) n += 0x200 else: self._send(ar[n:]) while n < self.w * self.h * 2: self._send(b"\x00") n += 1 #### TEXT COMMANDS #### def set_pos(self, x, y): self._fcmd2("= self.w or y >= self.h: return elif x < 0 or y < 0: left = top = True if x < 0: left = False w += x x = 0 if y < 0: top = False h += y y = 0 if cmd == 0x51 or cmd == 0x72: # draw interior self._fcmd2b("> 7 != 0 def get_touch(self): self._send(b"\x02T") # implicit LCD output flush b = self.buf[4] self._waitfor(3, b) return b[1] >> 7, b[2], b[3] #### ADVANCED COMMANDS #### def set_spi_win(self, x, y, w, h): pack_into( " 32: raise ValueError("length must be 32 or less") self._fcmd2(" 0xFFFF: raise ValueError("length must be 65535 or less") self.oflush() self._fcmd2("