diff --git a/adafruit_epd/epd.py b/adafruit_epd/epd.py index 902d365..fcccc26 100644 --- a/adafruit_epd/epd.py +++ b/adafruit_epd/epd.py @@ -107,8 +107,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self._spi_transfer(databyte) self.sram.cs_pin.value = True else: - for databyte in self._buffer1: - self._spi_transfer(databyte) + self._spi_transfer(self._buffer1) self._cs.value = True self.spi_device.unlock() @@ -140,8 +139,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self._spi_transfer(databyte) self.sram.cs_pin.value = True else: - for databyte in self._buffer2: - self._spi_transfer(databyte) + self._spi_transfer(self._buffer2) self._cs.value = True self.spi_device.unlock() @@ -171,23 +169,39 @@ def command(self, cmd, data=None, end=True): if data is not None: self._dc.value = True - for b in data: - self._spi_transfer(b) + self._spi_transfer(data) if end: self._cs.value = True self.spi_device.unlock() return ret - def _spi_transfer(self, databyte): - """Transfer one byte, toggling the cs pin if required by the EPD chipset""" - self._spibuf[0] = databyte - if self._single_byte_tx: - self._cs.value = False - self.spi_device.write_readinto(self._spibuf, self._spibuf) - if self._single_byte_tx: - self._cs.value = True - return self._spibuf[0] + def _spi_transfer(self, data): + """Transfer one byte or bytearray, toggling the cs pin if required by the EPD chipset""" + if isinstance(data, int): # single byte! + self._spibuf[0] = data + + # easy & fast case: array and no twiddling + if not self._single_byte_tx and isinstance(data, bytearray): + self.spi_device.write(data) + return None + + # if its a single byte + if isinstance(data, int): # single byte! + if self._single_byte_tx: + self._cs.value = False + try: + self.spi_device.write_readinto(self._spibuf, self._spibuf) + except NotImplementedError: + self.spi_device.write(self._spibuf) + if self._single_byte_tx: + self._cs.value = True + return self._spibuf[0] + + if isinstance(data, bytearray): + for x in data: + self._spi_transfer(x) + return None def power_up(self): """Power up the display in preparation for writing RAM and updating. diff --git a/examples/epd_bonnet.py b/examples/epd_bonnet.py index e04a431..821b2a7 100644 --- a/examples/epd_bonnet.py +++ b/examples/epd_bonnet.py @@ -61,29 +61,29 @@ draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE) # Draw some shapes. # First define some constants to allow easy resizing of shapes. -padding = 5 -shape_width = 30 -top = padding -bottom = height - padding +PADDING = 5 +SHAPE_WIDTH = 30 +TOP = PADDING +bottom = height - PADDING # Move left to right keeping track of the current x position for drawing shapes. -x = padding +x = PADDING # Draw an ellipse. -draw.ellipse((x, top, x + shape_width, bottom), outline=BLACK, fill=WHITE) -x += shape_width + padding +draw.ellipse((x, TOP, x + SHAPE_WIDTH, bottom), outline=BLACK, fill=WHITE) +x += SHAPE_WIDTH + PADDING # Draw a rectangle. -draw.rectangle((x, top, x + shape_width, bottom), outline=WHITE, fill=BLACK) -x += shape_width + padding +draw.rectangle((x, TOP, x + SHAPE_WIDTH, bottom), outline=WHITE, fill=BLACK) +x += SHAPE_WIDTH + PADDING # Draw a triangle. draw.polygon( - [(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)], + [(x, bottom), (x + SHAPE_WIDTH / 2, TOP), (x + SHAPE_WIDTH, bottom)], outline=BLACK, fill=WHITE, ) -x += shape_width + padding +x += SHAPE_WIDTH + PADDING # Draw an X. -draw.line((x, bottom, x + shape_width, top), fill=BLACK) -draw.line((x, top, x + shape_width, bottom), fill=BLACK) -x += shape_width + padding +draw.line((x, bottom, x + SHAPE_WIDTH, TOP), fill=BLACK) +draw.line((x, TOP, x + SHAPE_WIDTH, bottom), fill=BLACK) +x += SHAPE_WIDTH + PADDING # Load default font. font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20) @@ -94,8 +94,8 @@ # font = ImageFont.truetype('Minecraftia.ttf', 8) # Write two lines of text. -draw.text((x, top), "Hello", font=font, fill=BLACK) -draw.text((x, top + 20), "World!", font=font, fill=BLACK) +draw.text((x, TOP), "Hello", font=font, fill=BLACK) +draw.text((x, TOP + 20), "World!", font=font, fill=BLACK) while True: if not switch1.value: @@ -106,7 +106,8 @@ time.sleep(0.01) if not switch2.value: print("Switch 2") - display.fill(Adafruit_EPD.WHITE) + blinkaimage = Image.open("epd_bonnet_blinka_250x122.bmp") + display.image(blinkaimage) display.display() while not switch2.value: time.sleep(0.01) diff --git a/examples/epd_bonnet_blinka_250x122.bmp b/examples/epd_bonnet_blinka_250x122.bmp new file mode 100644 index 0000000..389e32f Binary files /dev/null and b/examples/epd_bonnet_blinka_250x122.bmp differ diff --git a/examples/epd_bonnet_blinka_250x122.bmp.license b/examples/epd_bonnet_blinka_250x122.bmp.license new file mode 100644 index 0000000..a784acf --- /dev/null +++ b/examples/epd_bonnet_blinka_250x122.bmp.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT diff --git a/examples/feather_epd_blinka.py b/examples/feather_epd_blinka.py new file mode 100644 index 0000000..1aae116 --- /dev/null +++ b/examples/feather_epd_blinka.py @@ -0,0 +1,40 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import digitalio +import busio +import board +from adafruit_epd.epd import Adafruit_EPD +from adafruit_epd.ssd1680 import Adafruit_SSD1680 + +# create the spi device and pins we will need +spi = busio.SPI(board.EPD_SCK, MOSI=board.EPD_MOSI, MISO=None) +epd_cs = digitalio.DigitalInOut(board.EPD_CS) +epd_dc = digitalio.DigitalInOut(board.EPD_DC) +epd_reset = digitalio.DigitalInOut(board.EPD_RESET) +epd_busy = digitalio.DigitalInOut(board.EPD_BUSY) +srcs = None + +display = Adafruit_SSD1680( + 122, + 250, + spi, + cs_pin=epd_cs, + dc_pin=epd_dc, + sramcs_pin=srcs, + rst_pin=epd_reset, + busy_pin=epd_busy, +) + +display.rotation = 3 +display.fill(Adafruit_EPD.WHITE) + +display.fill_rect(20, 20, 50, 60, Adafruit_EPD.BLACK) +display.hline(80, 30, 60, Adafruit_EPD.BLACK) +display.vline(80, 30, 60, Adafruit_EPD.BLACK) + +# draw repeatedly with pauses +while True: + display.display() + time.sleep(15) diff --git a/examples/feather_epd_blinka_pillow.py b/examples/feather_epd_blinka_pillow.py new file mode 100644 index 0000000..0ec82b6 --- /dev/null +++ b/examples/feather_epd_blinka_pillow.py @@ -0,0 +1,98 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import digitalio +import busio +import board +from PIL import Image +from PIL import ImageDraw +from PIL import ImageFont +from adafruit_epd.epd import Adafruit_EPD +from adafruit_epd.ssd1680 import Adafruit_SSD1680 + +# create the spi device and pins we will need +spi = busio.SPI(board.EPD_SCK, MOSI=board.EPD_MOSI, MISO=None) +epd_cs = digitalio.DigitalInOut(board.EPD_CS) +epd_dc = digitalio.DigitalInOut(board.EPD_DC) +epd_reset = digitalio.DigitalInOut(board.EPD_RESET) +epd_busy = digitalio.DigitalInOut(board.EPD_BUSY) +srcs = None + +display = Adafruit_SSD1680( + 122, + 250, + spi, + cs_pin=epd_cs, + dc_pin=epd_dc, + sramcs_pin=srcs, + rst_pin=epd_reset, + busy_pin=epd_busy, +) + +display.rotation = 3 +# Create blank image for drawing. +# Make sure to create image with mode '1' for 1-bit color. +width = display.width +height = display.height +image = Image.new("RGB", (width, height)) + +WHITE = (0xFF, 0xFF, 0xFF) +BLACK = (0x00, 0x00, 0x00) + +# clear the display +display.fill(Adafruit_EPD.WHITE) + +# Get drawing object to draw on image. +draw = ImageDraw.Draw(image) +# empty it +draw.rectangle((0, 0, width, height), fill=WHITE) + +# Draw an outline box +draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE) +# Draw some shapes. +# First define some constants to allow easy resizing of shapes. +padding = 5 +shape_width = 30 +top = padding +bottom = height - padding +# Move left to right keeping track of the current x position for drawing shapes. +x = padding +# Draw an ellipse. +draw.ellipse((x, top, x + shape_width, bottom), outline=BLACK, fill=WHITE) +x += shape_width + padding +# Draw a rectangle. +draw.rectangle((x, top, x + shape_width, bottom), outline=WHITE, fill=BLACK) +x += shape_width + padding +# Draw a triangle. +draw.polygon( + [(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)], + outline=BLACK, + fill=WHITE, +) +x += shape_width + padding +# Draw an X. +draw.line((x, bottom, x + shape_width, top), fill=BLACK) +draw.line((x, top, x + shape_width, bottom), fill=BLACK) +x += shape_width + padding + +# Load default font. +font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20) + +# Alternatively load a TTF font. Make sure the .ttf font +# file is in the same directory as the python script! +# Some other nice fonts to try: http://www.dafont.com/bitmap.php +# font = ImageFont.truetype('Minecraftia.ttf', 8) + +# Write two lines of text. +draw.text((x, top), "Hello", font=font, fill=BLACK) +draw.text((x, top + 20), "World!", font=font, fill=BLACK) + +display.image(image) +display.display() + +time.sleep(10) + +blinkaimage = Image.open("examples/epd_bonnet_blinka_250x122.bmp") +display.image(blinkaimage) +display.display()