Skip to content

Commit c225150

Browse files
committed
make optimizations in case we dont have to toggle CS pin on each write.
also add examples for u2if versions of feather epd
1 parent a410e4d commit c225150

File tree

3 files changed

+155
-15
lines changed

3 files changed

+155
-15
lines changed

adafruit_epd/epd.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ def display(self): # pylint: disable=too-many-branches
107107
databyte = self._spi_transfer(databyte)
108108
self.sram.cs_pin.value = True
109109
else:
110-
for databyte in self._buffer1:
111-
self._spi_transfer(databyte)
110+
self._spi_transfer(self._buffer1)
112111

113112
self._cs.value = True
114113
self.spi_device.unlock()
@@ -140,8 +139,7 @@ def display(self): # pylint: disable=too-many-branches
140139
databyte = self._spi_transfer(databyte)
141140
self.sram.cs_pin.value = True
142141
else:
143-
for databyte in self._buffer2:
144-
self._spi_transfer(databyte)
142+
self._spi_transfer(self._buffer2)
145143

146144
self._cs.value = True
147145
self.spi_device.unlock()
@@ -171,23 +169,39 @@ def command(self, cmd, data=None, end=True):
171169

172170
if data is not None:
173171
self._dc.value = True
174-
for b in data:
175-
self._spi_transfer(b)
172+
self._spi_transfer(data)
176173
if end:
177174
self._cs.value = True
178175
self.spi_device.unlock()
179176

180177
return ret
181178

182-
def _spi_transfer(self, databyte):
183-
"""Transfer one byte, toggling the cs pin if required by the EPD chipset"""
184-
self._spibuf[0] = databyte
185-
if self._single_byte_tx:
186-
self._cs.value = False
187-
self.spi_device.write_readinto(self._spibuf, self._spibuf)
188-
if self._single_byte_tx:
189-
self._cs.value = True
190-
return self._spibuf[0]
179+
def _spi_transfer(self, data):
180+
"""Transfer one byte or bytearray, toggling the cs pin if required by the EPD chipset"""
181+
if isinstance(data, int): # single byte!
182+
self._spibuf[0] = data
183+
184+
# easy & fast case: array and no twiddling
185+
if not self._single_byte_tx and isinstance(data, bytearray):
186+
self.spi_device.write(data)
187+
return None
188+
189+
# if its a single byte
190+
if isinstance(data, int): # single byte!
191+
if self._single_byte_tx:
192+
self._cs.value = False
193+
try:
194+
self.spi_device.write_readinto(self._spibuf, self._spibuf)
195+
except NotImplementedError:
196+
self.spi_device.write(self._spibuf)
197+
if self._single_byte_tx:
198+
self._cs.value = True
199+
return self._spibuf[0]
200+
201+
if isinstance(data, bytearray):
202+
for x in data:
203+
self._spi_transfer(x)
204+
return None
191205

192206
def power_up(self):
193207
"""Power up the display in preparation for writing RAM and updating.

examples/feather_epd_blinka.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import digitalio
5+
import busio
6+
import board
7+
from adafruit_epd.epd import Adafruit_EPD
8+
import time
9+
10+
# create the spi device and pins we will need
11+
spi = busio.SPI(board.EPD_SCK, MOSI=board.EPD_MOSI, MISO=None)
12+
epd_cs = digitalio.DigitalInOut(board.EPD_CS)
13+
epd_dc = digitalio.DigitalInOut(board.EPD_DC)
14+
epd_reset = digitalio.DigitalInOut(board.EPD_RESET)
15+
epd_busy = digitalio.DigitalInOut(board.EPD_BUSY)
16+
srcs = None
17+
18+
from adafruit_epd.ssd1680 import Adafruit_SSD1680
19+
display = Adafruit_SSD1680(122, 250, spi, cs_pin=epd_cs, dc_pin=epd_dc,
20+
sramcs_pin=srcs, rst_pin=epd_reset,
21+
busy_pin=epd_busy)
22+
23+
display.rotation = 3
24+
display.fill(Adafruit_EPD.WHITE)
25+
26+
display.fill_rect(20, 20, 50, 60, Adafruit_EPD.BLACK)
27+
display.hline(80, 30, 60, Adafruit_EPD.BLACK)
28+
display.vline(80, 30, 60, Adafruit_EPD.BLACK)
29+
30+
# draw repeatedly with pauses
31+
while True:
32+
display.display()
33+
time.sleep(15)

examples/feather_epd_blinka_pillow.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import digitalio
5+
import busio
6+
import board
7+
from adafruit_epd.epd import Adafruit_EPD
8+
import time
9+
10+
from PIL import Image
11+
from PIL import ImageDraw
12+
from PIL import ImageFont
13+
14+
# create the spi device and pins we will need
15+
spi = busio.SPI(board.EPD_SCK, MOSI=board.EPD_MOSI, MISO=None)
16+
epd_cs = digitalio.DigitalInOut(board.EPD_CS)
17+
epd_dc = digitalio.DigitalInOut(board.EPD_DC)
18+
epd_reset = digitalio.DigitalInOut(board.EPD_RESET)
19+
epd_busy = digitalio.DigitalInOut(board.EPD_BUSY)
20+
srcs = None
21+
22+
from adafruit_epd.ssd1680 import Adafruit_SSD1680
23+
display = Adafruit_SSD1680(122, 250, spi, cs_pin=epd_cs, dc_pin=epd_dc,
24+
sramcs_pin=srcs, rst_pin=epd_reset,
25+
busy_pin=epd_busy)
26+
27+
display.rotation = 3
28+
# Create blank image for drawing.
29+
# Make sure to create image with mode '1' for 1-bit color.
30+
width = display.width
31+
height = display.height
32+
image = Image.new("RGB", (width, height))
33+
34+
WHITE = (0xFF, 0xFF, 0xFF)
35+
BLACK = (0x00, 0x00, 0x00)
36+
37+
# clear the display
38+
display.fill(Adafruit_EPD.WHITE)
39+
40+
# Get drawing object to draw on image.
41+
draw = ImageDraw.Draw(image)
42+
# empty it
43+
draw.rectangle((0, 0, width, height), fill=WHITE)
44+
45+
# Draw an outline box
46+
draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE)
47+
# Draw some shapes.
48+
# First define some constants to allow easy resizing of shapes.
49+
padding = 5
50+
shape_width = 30
51+
top = padding
52+
bottom = height - padding
53+
# Move left to right keeping track of the current x position for drawing shapes.
54+
x = padding
55+
# Draw an ellipse.
56+
draw.ellipse((x, top, x + shape_width, bottom), outline=BLACK, fill=WHITE)
57+
x += shape_width + padding
58+
# Draw a rectangle.
59+
draw.rectangle((x, top, x + shape_width, bottom), outline=WHITE, fill=BLACK)
60+
x += shape_width + padding
61+
# Draw a triangle.
62+
draw.polygon(
63+
[(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)],
64+
outline=BLACK,
65+
fill=WHITE,
66+
)
67+
x += shape_width + padding
68+
# Draw an X.
69+
draw.line((x, bottom, x + shape_width, top), fill=BLACK)
70+
draw.line((x, top, x + shape_width, bottom), fill=BLACK)
71+
x += shape_width + padding
72+
73+
# Load default font.
74+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
75+
76+
# Alternatively load a TTF font. Make sure the .ttf font
77+
# file is in the same directory as the python script!
78+
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
79+
# font = ImageFont.truetype('Minecraftia.ttf', 8)
80+
81+
# Write two lines of text.
82+
draw.text((x, top), "Hello", font=font, fill=BLACK)
83+
draw.text((x, top + 20), "World!", font=font, fill=BLACK)
84+
85+
display.image(image)
86+
display.display()
87+
88+
time.sleep(10)
89+
90+
blinkaimage = Image.open('examples/epd_bonnet_blinka_250x122.bmp')
91+
display.image(blinkaimage)
92+
display.display()
93+

0 commit comments

Comments
 (0)