Adafruit_CircuitPython_EPD/adafruit_epd/il0373.py
2018-07-12 15:38:32 -04:00

172 lines
5.2 KiB
Python

from Adafruit_EPD.epd import Adafruit_EPD
from Adafruit_EPD.mcp_sram import Adafruit_MCP_SRAM
from micropython import const
import time
IL0373_PANEL_SETTING = const(0x00)
IL0373_POWER_SETTING = const(0x01)
IL0373_POWER_OFF = const(0x02)
IL0373_POWER_OFF_SEQUENCE = const(0x03)
IL0373_POWER_ON = const(0x04)
IL0373_POWER_ON_MEASURE = const(0x05)
IL0373_BOOSTER_SOFT_START = const(0x06)
IL0373_DEEP_SLEEP = const(0x07)
IL0373_DTM1 = const(0x10)
IL0373_DATA_STOP = const(0x11)
IL0373_DISPLAY_REFRESH = const(0x12)
IL0373_DTM2 = const(0x13)
IL0373_PDTM1 = const(0x14)
IL0373_PDTM2 = const(0x15)
IL0373_PDRF = const(0x16)
IL0373_LUT1 = const(0x20)
IL0373_LUTWW = const(0x21)
IL0373_LUTBW = const(0x22)
IL0373_LUTWB = const(0x23)
IL0373_LUTBB = const(0x24)
IL0373_PLL = const(0x30)
IL0373_CDI = const(0x50)
IL0373_RESOLUTION = const(0x61)
IL0373_VCM_DC_SETTING = const(0x82)
class Adafruit_IL0373(Adafruit_EPD):
def __init__(self, width, height, rst, dc, busy, srcs, cs, spi):
super().__init__(width, height, rst, dc, busy, srcs, cs, spi)
self.bw_bufsize = int(width * height / 8)
self.red_bufsize = int(width * height / 8)
self.begin()
def begin(self, reset=True):
super(Adafruit_IL0373, self).begin(reset)
while self._busy.value == False:
pass
self.command(IL0373_POWER_SETTING, bytearray([0x03, 0x00, 0x2b, 0x2b, 0x09]))
self.command(IL0373_BOOSTER_SOFT_START, bytearray([0x17, 0x17, 0x17]))
def update(self):
self.command(IL0373_DISPLAY_REFRESH)
while self._busy.value == False:
pass
self.command(IL0373_CDI, bytearray([0x17]))
self.command(IL0373_VCM_DC_SETTING, bytearray([0x00]))
self.command(IL0373_POWER_OFF)
time.sleep(2)
def power_up(self):
self.command(IL0373_POWER_ON)
while self._busy.value == False:
pass
time.sleep(.2)
self.command(IL0373_PANEL_SETTING, bytearray([0xCF]))
self.command(IL0373_CDI, bytearray([0x37]))
self.command(IL0373_PLL, bytearray([0x29]))
b1 = self.height & 0xFF
b2 = (self.height >> 8) & 0xFF
b3 = self.width & 0xFF
b4 = (self.width >> 8) & 0xFF
self.command(IL0373_RESOLUTION, bytearray([b1, b2, b3, b4]))
self.command(IL0373_VCM_DC_SETTING, bytearray([0x0A]))
def display(self):
self.power_up()
while not self.spi_device.try_lock():
pass
self.sram.cs.value = False
#send read command
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
#send start address
self.spi_device.write(bytearray([0x00, 0x00]))
self.spi_device.unlock()
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out
c = self.command(IL0373_DTM1, end=False)
while not self.spi_device.try_lock():
pass
self._dc.value = True
xfer = bytearray([c])
outbuf = bytearray(1)
for i in range(self.bw_bufsize):
outbuf[0] = xfer[0]
self.spi_device.write_readinto(outbuf, xfer)
self._cs.value = True
self.sram.cs.value = True
time.sleep(.002)
self.sram.cs.value = False
#send read command
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
#send start address
self.spi_device.write(bytearray([(self.bw_bufsize >> 8), (self.bw_bufsize & 0xFF)]))
self.spi_device.unlock()
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out
c = self.command(IL0373_DTM2, end=False)
while not self.spi_device.try_lock():
pass
self._dc.value = True
xfer = bytearray([c])
outbuf = bytearray(1)
for i in range(self.bw_bufsize):
outbuf[0] = xfer[0]
self.spi_device.write_readinto(outbuf, xfer)
self._cs.value = True
self.sram.cs.value = True
self.spi_device.unlock()
self.update()
def draw_pixel(self, x, y, color):
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
return
if x == 0:
x = 1
addr = int(((self.width - x) * self.height + y)/8)
if color == Adafruit_EPD.RED:
addr = addr + self.bw_bufsize
c = self.sram.read8(addr)
if color == Adafruit_EPD.WHITE:
c = c | (1 << (7 - y%8))
elif color == Adafruit_EPD.RED or color == Adafruit_EPD.BLACK:
c = c & ~(1 << (7 - y%8))
elif color == Adafruit_EPD.INVERSE:
c = c ^ (1 << (7 - y%8))
self.sram.write8(addr, c)
def get_pixel(self, x, y, color):
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
return
if x == 0:
x = 1
addr = int(((self.width - x) * self.height + y)/8)
if color == Adafruit_EPD.RED:
addr = addr + self.bw_bufsize
c = self.sram.read8(addr)
return c
def clear_buffer(self):
self.sram.erase(0x00, self.bw_bufsize, 0xFF)
self.sram.erase(self.bw_bufsize, self.red_bufsize, 0xFF)
def clear_display(self):
self.clear_buffer()
self.display()