DM: make pylint happy

This commit is contained in:
dean 2018-07-12 16:28:01 -04:00
parent c673a7eacd
commit 01d7cfb046
3 changed files with 203 additions and 198 deletions

View file

@ -1,124 +1,131 @@
import time import time
from Adafruit_EPD import mcp_sram from Adafruit_EPD import mcp_sram
import digitalio import digitalio
import busio
from board import * from board import *
class Adafruit_EPD(object): class Adafruit_EPD(object):
"""Base class for EPD displays """Base class for EPD displays
""" """
BLACK = 0 BLACK = 0
WHITE = 1 WHITE = 1
INVERSE = 2 INVERSE = 2
RED = 3 RED = 3
DARK = 4 DARK = 4
LIGHT = 5 LIGHT = 5
def __init__(self, width, height, rst, dc, busy, srcs, cs, def __init__(self, width, height, rst_pin, dc_pin, busy_pin, srcs_pin, cs_pin, spi):
spi): self.width = width
self.width = width self.height = height
self.height = height
# Setup reset pin. # Setup reset pin.
self._rst = rst self._rst = rst_pin
self._rst.direction = digitalio.Direction.OUTPUT self._rst.direction = digitalio.Direction.OUTPUT
# Setup busy pin. # Setup busy pin.
self._busy = busy self._busy = busy_pin
self._busy.direction = digitalio.Direction.INPUT self._busy.direction = digitalio.Direction.INPUT
# Setup dc pin. # Setup dc pin.
self._dc = dc self._dc = dc_pin
self._dc.direction = digitalio.Direction.OUTPUT self._dc.direction = digitalio.Direction.OUTPUT
# Setup cs pin. # Setup cs pin.
self._cs = cs self._cs = cs_pin
self._cs.direction = digitalio.Direction.OUTPUT self._cs.direction = digitalio.Direction.OUTPUT
self.spi_device = spi self.spi_device = spi
self.sram = mcp_sram.Adafruit_MCP_SRAM(srcs, spi) self.sram = mcp_sram.Adafruit_MCP_SRAM(srcs_pin, spi)
def begin(self, reset=True): def begin(self, reset=True):
self._cs.value = True self._cs.value = True
self._dc.value = False self._dc.value = False
if reset: if reset:
self._rst.value = False self._rst.value = False
time.sleep(.1) time.sleep(.1)
self._rst.value = True self._rst.value = True
time.sleep(.1) time.sleep(.1)
def command(self, c, data=None, end=True): def command(self, cmd, data=None, end=True):
"""Send command byte to display.""" """Send command byte to display."""
self._cs.value = True self._cs.value = True
self._dc.value = False self._dc.value = False
self._cs.value = False self._cs.value = False
outbuf = bytearray(1) outbuf = bytearray(1)
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self.spi_device.write_readinto(bytearray([c]), outbuf) self.spi_device.write_readinto(bytearray([cmd]), outbuf)
if data is not None: if data is not None:
self.data(data) self.data(data)
elif end: elif end:
self._cs.value = True self._cs.value = True
self.spi_device.unlock() self.spi_device.unlock()
return outbuf[0] return outbuf[0]
def data(self, d): def data(self, dat):
"""Send data to display.""" """Send data to display."""
self._dc.value = True self._dc.value = True
self.spi_device.write(d) self.spi_device.write(dat)
self._cs.value = True self._cs.value = True
self.spi_device.unlock() self.spi_device.unlock()
#framebuf methods def draw_pixel(self, x, y, color):
def fill(self, color): pass
self.format.fill_rect(self, 0, 0, self.width, self.height, color)
def fill_rect(self, x, y, width, height, color): def get_pixel(self, x, y):
if width < 1 or height < 1 or (x+width) <= 0 or (y+height) <= 0 or y >= self.height or x >= self.width: pass
return
xend = min(self.width, x+width)
yend = min(self.height, y+height)
x = max(x, 0)
y = max(y, 0)
for _x in range(xend - x):
for _y in range(yend - y):
self.draw_pixel(x + _x, y + _y, color)
def pixel(self, x, y, color=None): #framebuf methods
if x < 0 or x >= self.width or y < 0 or y >= self.height: def fill(self, color):
return self.fill_rect(self, 0, 0, self.width, self.height, color)
if color is None:
return self.get_pixel(self, x, y)
else:
self.draw_pixel(self, x, y, color)
def hline(self, x, y, width, color): def fill_rect(self, x, y, width, height, color):
self.fill_rect(x, y, width, 1, color) if width < 1 or height < 1 or (x+width) <= 0 or (y+height) <= 0 \
or y >= self.height or x >= self.width:
return
xend = min(self.width, x+width)
yend = min(self.height, y+height)
x = max(x, 0)
y = max(y, 0)
for _x in range(xend - x):
for _y in range(yend - y):
self.draw_pixel(x + _x, y + _y, color)
return
def vline(self, x, y, height, color): def pixel(self, x, y, color=None):
self.fill_rect(x, y, 1, height, color) if x < 0 or x >= self.width or y < 0 or y >= self.height:
return None
if color is None:
return self.get_pixel(self, x, y)
def rect(self, x, y, width, height, color): self.draw_pixel(self, x, y, color)
self.fill_rect(x, y, width, 1, color) return None
self.fill_rect(x, y+height, width, 1, color)
self.fill_rect(self, x, y, 1, height, color)
self.fill_rect(self, x+width, y, 1, height, color)
def line(self): def hline(self, x, y, width, color):
raise NotImplementedError() self.fill_rect(x, y, width, 1, color)
def blit(self): def vline(self, x, y, height, color):
raise NotImplementedError() self.fill_rect(x, y, 1, height, color)
def scroll(self): def rect(self, x, y, width, height, color):
raise NotImplementedError() self.fill_rect(x, y, width, 1, color)
self.fill_rect(x, y+height, width, 1, color)
self.fill_rect(self, x, y, 1, height, color)
self.fill_rect(self, x+width, y, 1, height, color)
def text(self): def line(self):
raise NotImplementedError() raise NotImplementedError()
def blit(self):
raise NotImplementedError()
def scroll(self):
raise NotImplementedError()
def text(self):
raise NotImplementedError()

View file

@ -1,7 +1,7 @@
import time
from micropython import const
from Adafruit_EPD.epd import Adafruit_EPD from Adafruit_EPD.epd import Adafruit_EPD
from Adafruit_EPD.mcp_sram import Adafruit_MCP_SRAM from Adafruit_EPD.mcp_sram import Adafruit_MCP_SRAM
from micropython import const
import time
IL0373_PANEL_SETTING = const(0x00) IL0373_PANEL_SETTING = const(0x00)
IL0373_POWER_SETTING = const(0x01) IL0373_POWER_SETTING = const(0x01)
@ -29,8 +29,9 @@ IL0373_RESOLUTION = const(0x61)
IL0373_VCM_DC_SETTING = const(0x82) IL0373_VCM_DC_SETTING = const(0x82)
class Adafruit_IL0373(Adafruit_EPD): class Adafruit_IL0373(Adafruit_EPD):
def __init__(self, width, height, rst, dc, busy, srcs, cs, spi): def __init__(self, width, height, rst_pin, dc_pin, busy_pin, srcs_pin, cs_pin, spi):
super().__init__(width, height, rst, dc, busy, srcs, cs, spi) super(Adafruit_IL0373, self).__init__(width, height, rst_pin, dc_pin, busy_pin, \
srcs_pin, cs_pin, spi)
self.bw_bufsize = int(width * height / 8) self.bw_bufsize = int(width * height / 8)
self.red_bufsize = int(width * height / 8) self.red_bufsize = int(width * height / 8)
@ -38,19 +39,19 @@ class Adafruit_IL0373(Adafruit_EPD):
self.begin() self.begin()
def begin(self, reset=True): def begin(self, reset=True):
super(Adafruit_IL0373, self).begin(reset) super(Adafruit_IL0373, self).begin(reset)
while self._busy.value == False: while self._busy.value is False:
pass pass
self.command(IL0373_POWER_SETTING, bytearray([0x03, 0x00, 0x2b, 0x2b, 0x09])) self.command(IL0373_POWER_SETTING, bytearray([0x03, 0x00, 0x2b, 0x2b, 0x09]))
self.command(IL0373_BOOSTER_SOFT_START, bytearray([0x17, 0x17, 0x17])) self.command(IL0373_BOOSTER_SOFT_START, bytearray([0x17, 0x17, 0x17]))
def update(self): def update(self):
self.command(IL0373_DISPLAY_REFRESH) self.command(IL0373_DISPLAY_REFRESH)
while self._busy.value == False: while self._busy.value is False:
pass pass
self.command(IL0373_CDI, bytearray([0x17])) self.command(IL0373_CDI, bytearray([0x17]))
self.command(IL0373_VCM_DC_SETTING, bytearray([0x00])) self.command(IL0373_VCM_DC_SETTING, bytearray([0x00]))
@ -60,70 +61,72 @@ class Adafruit_IL0373(Adafruit_EPD):
def power_up(self): def power_up(self):
self.command(IL0373_POWER_ON) self.command(IL0373_POWER_ON)
while self._busy.value == False: while self._busy.value is False:
pass pass
time.sleep(.2) time.sleep(.2)
self.command(IL0373_PANEL_SETTING, bytearray([0xCF])) self.command(IL0373_PANEL_SETTING, bytearray([0xCF]))
self.command(IL0373_CDI, bytearray([0x37])) self.command(IL0373_CDI, bytearray([0x37]))
self.command(IL0373_PLL, bytearray([0x29])) self.command(IL0373_PLL, bytearray([0x29]))
b1 = self.height & 0xFF _b1 = self.height & 0xFF
b2 = (self.height >> 8) & 0xFF _b2 = (self.height >> 8) & 0xFF
b3 = self.width & 0xFF _b3 = self.width & 0xFF
b4 = (self.width >> 8) & 0xFF _b4 = (self.width >> 8) & 0xFF
self.command(IL0373_RESOLUTION, bytearray([b1, b2, b3, b4])) self.command(IL0373_RESOLUTION, bytearray([_b1, _b2, _b3, _b4]))
self.command(IL0373_VCM_DC_SETTING, bytearray([0x0A])) self.command(IL0373_VCM_DC_SETTING, bytearray([0x0A]))
def display(self): def display(self):
self.power_up() self.power_up()
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self.sram.cs.value = False self.sram._cs.value = False
#send read command #send read command
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ])) self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
#send start address #send start address
self.spi_device.write(bytearray([0x00, 0x00])) self.spi_device.write(bytearray([0x00, 0x00]))
self.spi_device.unlock() self.spi_device.unlock()
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out #first data byte from SRAM will be transfered in at the
c = self.command(IL0373_DTM1, end=False) #same time as the EPD command is transferred out
cmd = self.command(IL0373_DTM1, end=False)
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self._dc.value = True self._dc.value = True
xfer = bytearray([c]) xfer = bytearray([cmd])
outbuf = bytearray(1) outbuf = bytearray(1)
for i in range(self.bw_bufsize): for i in range(self.bw_bufsize):
outbuf[0] = xfer[0] outbuf[0] = xfer[0]
self.spi_device.write_readinto(outbuf, xfer) self.spi_device.write_readinto(outbuf, xfer)
self._cs.value = True self._cs.value = True
self.sram.cs.value = True self.sram._cs.value = True
time.sleep(.002) time.sleep(.002)
self.sram.cs.value = False self.sram._cs.value = False
#send read command #send read command
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ])) self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
#send start address #send start address
self.spi_device.write(bytearray([(self.bw_bufsize >> 8), (self.bw_bufsize & 0xFF)])) self.spi_device.write(bytearray([(self.bw_bufsize >> 8), (self.bw_bufsize & 0xFF)]))
self.spi_device.unlock() self.spi_device.unlock()
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out #first data byte from SRAM will be transfered in at the
c = self.command(IL0373_DTM2, end=False) #same time as the EPD command is transferred out
cmd = self.command(IL0373_DTM2, end=False)
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self._dc.value = True self._dc.value = True
xfer = bytearray([c]) xfer = bytearray([cmd])
outbuf = bytearray(1) outbuf = bytearray(1)
for i in range(self.bw_bufsize): for i in range(self.bw_bufsize):
outbuf[0] = xfer[0] outbuf[0] = xfer[0]
self.spi_device.write_readinto(outbuf, xfer) self.spi_device.write_readinto(outbuf, xfer)
self._cs.value = True self._cs.value = True
self.sram.cs.value = True self.sram._cs.value = True
self.spi_device.unlock() self.spi_device.unlock()
self.update() self.update()
@ -131,36 +134,37 @@ class Adafruit_IL0373(Adafruit_EPD):
def draw_pixel(self, x, y, color): def draw_pixel(self, x, y, color):
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height): if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
return return
if x == 0: if x == 0:
x = 1 x = 1
addr = int(((self.width - x) * self.height + y)/8) addr = int(((self.width - x) * self.height + y)/8)
if color == Adafruit_EPD.RED: if color == Adafruit_EPD.RED:
addr = addr + self.bw_bufsize addr = addr + self.bw_bufsize
c = self.sram.read8(addr) current = self.sram.read8(addr)
if color == Adafruit_EPD.WHITE: if color == Adafruit_EPD.WHITE:
c = c | (1 << (7 - y%8)) current = current | (1 << (7 - y%8))
elif color == Adafruit_EPD.RED or color == Adafruit_EPD.BLACK: elif color == Adafruit_EPD.RED or color == Adafruit_EPD.BLACK:
c = c & ~(1 << (7 - y%8)) current = current & ~(1 << (7 - y%8))
elif color == Adafruit_EPD.INVERSE: elif color == Adafruit_EPD.INVERSE:
c = c ^ (1 << (7 - y%8)) current = current ^ (1 << (7 - y%8))
self.sram.write8(addr, c) self.sram.write8(addr, current)
return
def get_pixel(self, x, y, color): def get_pixel(self, x, y, color):
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height): if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
return return None
if x == 0: if x == 0:
x = 1 x = 1
addr = int(((self.width - x) * self.height + y)/8) addr = int(((self.width - x) * self.height + y)/8)
if color == Adafruit_EPD.RED: if color == Adafruit_EPD.RED:
addr = addr + self.bw_bufsize addr = addr + self.bw_bufsize
c = self.sram.read8(addr) current = self.sram.read8(addr)
return c return current
def clear_buffer(self): def clear_buffer(self):
self.sram.erase(0x00, self.bw_bufsize, 0xFF) self.sram.erase(0x00, self.bw_bufsize, 0xFF)
@ -169,4 +173,3 @@ class Adafruit_IL0373(Adafruit_EPD):
def clear_display(self): def clear_display(self):
self.clear_buffer() self.clear_buffer()
self.display() self.display()

View file

@ -1,77 +1,72 @@
import time
from micropython import const from micropython import const
import digitalio import digitalio
from adafruit_bus_device.spi_device import SPIDevice
import busio
from board import *
SRAM_SEQUENTIAL_MODE = const(1 << 6) SRAM_SEQUENTIAL_MODE = const(1 << 6)
class Adafruit_MCP_SRAM: class Adafruit_MCP_SRAM(object):
SRAM_READ = 0x03 SRAM_READ = 0x03
SRAM_WRITE = 0x02 SRAM_WRITE = 0x02
SRAM_RDSR = 0x05 SRAM_RDSR = 0x05
SRAM_WRSR = 0x01 SRAM_WRSR = 0x01
def __init__(self, cs, spi): def __init__(self, cs_pin, spi):
# Handle hardware SPI # Handle hardware SPI
self.spi_device = spi self.spi_device = spi
self.cs = cs self._cs = cs_pin
self.cs.direction = digitalio.Direction.OUTPUT self._cs.direction = digitalio.Direction.OUTPUT
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self.cs.value = False self._cs.value = False
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_WRSR, 0x43])) self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_WRSR, 0x43]))
self.cs.value = True self._cs.value = True
self.spi_device.unlock() self.spi_device.unlock()
def write(self, addr, buf, reg=SRAM_WRITE): def write(self, addr, buf, reg=SRAM_WRITE):
c = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF] + buf) cmd = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF] + buf)
while not self.spi_device.try_lock(): while not self.spi_device.try_lock():
pass pass
self.cs.value = False self._cs.value = False
self.spi_device.write(c) self.spi_device.write(cmd)
self.cs.value = True self._cs.value = True
self.spi_device.unlock() self.spi_device.unlock()
def read(self, addr, length, reg=SRAM_READ): def read(self, addr, length, reg=SRAM_READ):
c = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF]) cmd = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF])
buf = bytearray(length)
while not self.spi_device.try_lock():
pass
self.cs.value = False
self.spi_device.write(c)
self.spi_device.readinto(buf)
self.cs.value = True
self.spi_device.unlock()
return buf
def read8(self, addr, reg=SRAM_READ): buf = bytearray(length)
return self.read(addr, 1, reg)[0] while not self.spi_device.try_lock():
pass
self._cs.value = False
self.spi_device.write(cmd)
self.spi_device.readinto(buf)
self._cs.value = True
self.spi_device.unlock()
return buf
def read16(self, addr, reg=SRAM_READ): def read8(self, addr, reg=SRAM_READ):
buf = self.read(addr, 2, reg) return self.read(addr, 1, reg)[0]
return (buf[0] << 8 | buf[1])
def write8(self, addr, value, reg=SRAM_WRITE): def read16(self, addr, reg=SRAM_READ):
self.write(addr, [value], reg) buf = self.read(addr, 2, reg)
return buf[0] << 8 | buf[1]
def write16(self, addr, value, reg=SRAM_WRITE): def write8(self, addr, value, reg=SRAM_WRITE):
self.write(addr, [value >> 8, value], reg) self.write(addr, [value], reg)
def erase(self, addr, length, value): def write16(self, addr, value, reg=SRAM_WRITE):
c = bytearray([Adafruit_MCP_SRAM.SRAM_WRITE, (addr >> 8) & 0xFF, addr & 0xFF]) self.write(addr, [value >> 8, value], reg)
while not self.spi_device.try_lock(): def erase(self, addr, length, value):
pass cmd = bytearray([Adafruit_MCP_SRAM.SRAM_WRITE, (addr >> 8) & 0xFF, addr & 0xFF])
self.cs.value = False
self.spi_device.write(c) while not self.spi_device.try_lock():
for x in range(length): pass
self.spi_device.write(bytearray([value])) self._cs.value = False
self.cs.value = True self.spi_device.write(cmd)
self.spi_device.unlock() for x in range(length):
self.spi_device.write(bytearray([value]))
self._cs.value = True
self.spi_device.unlock()