pre-commit and examples
This commit is contained in:
parent
18d480d6b2
commit
711012ec84
6 changed files with 184 additions and 270 deletions
|
|
@ -1,4 +1,4 @@
|
|||
# SPDX-FileCopyrightText: 2024
|
||||
# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
|
|
@ -6,19 +6,7 @@
|
|||
`adafruit_epd.jd79661` - Adafruit JD79661 - quad-color ePaper display driver
|
||||
====================================================================================
|
||||
CircuitPython driver for Adafruit JD79661 quad-color display breakouts
|
||||
* Author(s): [Your name here]
|
||||
|
||||
**Hardware:**
|
||||
* JD79661 Quad-Color ePaper Display
|
||||
|
||||
**Notes on Architecture:**
|
||||
This is the first quad-color display in the CircuitPython EPD library. Unlike tri-color
|
||||
displays that use separate buffers for black and red/yellow, the JD79661 uses a single
|
||||
buffer with 2 bits per pixel to represent 4 colors (black, white, yellow, red).
|
||||
|
||||
This driver overrides the parent class's dual-buffer architecture to accommodate the
|
||||
quad-color packed pixel format. All drawing operations are reimplemented to work with
|
||||
the 2-bit color depth.
|
||||
* Author(s): Liz Clark
|
||||
"""
|
||||
|
||||
import time
|
||||
|
|
@ -33,6 +21,7 @@ try:
|
|||
import typing
|
||||
|
||||
from busio import SPI
|
||||
from circuitpython_typing.pil import Image
|
||||
from digitalio import DigitalInOut
|
||||
from typing_extensions import Literal
|
||||
|
||||
|
|
@ -73,25 +62,12 @@ _JD79661_CMD_4D = const(0x4D)
|
|||
|
||||
|
||||
class Adafruit_JD79661(Adafruit_EPD):
|
||||
"""driver class for Adafruit JD79661 quad-color ePaper display breakouts
|
||||
|
||||
This driver implements a quad-color display with a single buffer using 2 bits
|
||||
per pixel. This differs from the parent class architecture which assumes
|
||||
separate buffers for black and color pixels in tri-color displays.
|
||||
|
||||
**Color Architecture:**
|
||||
- Uses a single buffer with 2 bits per pixel
|
||||
- Supports 4 colors: BLACK (0b00), WHITE (0b01), YELLOW (0b10), RED (0b11)
|
||||
- All drawing methods are overridden to handle the 2-bit packed pixel format
|
||||
- The parent class's dual-buffer methods (_blackframebuf/_colorframebuf) are
|
||||
set to the same buffer for compatibility but are not used directly
|
||||
"""
|
||||
"""Driver for the JD79661 quad-color ePaper display breakouts"""
|
||||
|
||||
# Add color constants for convenience - these match parent class where applicable
|
||||
BLACK = const(0) # 0b00 in the display buffer
|
||||
WHITE = const(1) # 0b01 in the display buffer
|
||||
YELLOW = const(2) # 0b10 in the display buffer
|
||||
RED = const(3) # 0b11 in the display buffer
|
||||
BLACK = const(0) # 0b00 in the display buffer
|
||||
WHITE = const(1) # 0b01 in the display buffer
|
||||
YELLOW = const(2) # 0b10 in the display buffer
|
||||
RED = const(3) # 0b11 in the display buffer
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -106,38 +82,27 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
busy_pin: DigitalInOut,
|
||||
) -> None:
|
||||
"""Initialize the quad-color display driver.
|
||||
|
||||
|
||||
Note: This driver uses a different buffer architecture than the parent class.
|
||||
Instead of separate black and color buffers, it uses a single buffer with
|
||||
2 bits per pixel to represent 4 colors.
|
||||
"""
|
||||
super().__init__(width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy_pin)
|
||||
|
||||
# Adjust width to be divisible by 8 for proper byte alignment
|
||||
stride = width
|
||||
if stride % 8 != 0:
|
||||
stride += 8 - stride % 8
|
||||
|
||||
# For quad-color display, we need 2 bits per pixel
|
||||
# So buffer size is width * height / 4 bytes
|
||||
self._buffer1_size = int(stride * height / 4)
|
||||
self._buffer2_size = 0 # No second buffer for this display
|
||||
|
||||
if sramcs_pin:
|
||||
self._buffer1 = self.sram.get_view(0)
|
||||
# IMPORTANT: Both buffers point to the same memory for compatibility
|
||||
# with parent class, but only _buffer1 is actually used
|
||||
self._buffer2 = self._buffer1
|
||||
else:
|
||||
self._buffer1 = bytearray(self._buffer1_size)
|
||||
# IMPORTANT: Both buffers point to the same memory for compatibility
|
||||
# with parent class, but only _buffer1 is actually used
|
||||
self._buffer2 = self._buffer1
|
||||
|
||||
# Create framebuffers for API compatibility with parent class
|
||||
# NOTE: These framebuffers are not used for actual drawing operations
|
||||
# since they don't support 2-bit color depth. All drawing is done
|
||||
# through overridden methods that directly manipulate the buffer.
|
||||
self._framebuf1 = adafruit_framebuf.FrameBuffer(
|
||||
self._buffer1,
|
||||
width,
|
||||
|
|
@ -146,15 +111,15 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
buf_format=adafruit_framebuf.MHMSB,
|
||||
)
|
||||
self._framebuf2 = self._framebuf1 # Same framebuffer for compatibility
|
||||
|
||||
|
||||
# Set single byte transactions
|
||||
self._single_byte_tx = True
|
||||
|
||||
|
||||
# Set up buffer references for parent class compatibility
|
||||
# Both point to the same buffer since we don't have separate color planes
|
||||
self.set_black_buffer(0, False)
|
||||
self.set_color_buffer(0, False)
|
||||
|
||||
|
||||
# Initialize with default fill
|
||||
self.fill(Adafruit_JD79661.WHITE)
|
||||
|
||||
|
|
@ -178,15 +143,19 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
"""Power up the display in preparation for writing RAM and updating"""
|
||||
self.hardware_reset()
|
||||
self.busy_wait()
|
||||
|
||||
|
||||
# Send initialization sequence
|
||||
time.sleep(0.01) # Wait 10ms
|
||||
|
||||
|
||||
self.command(_JD79661_CMD_4D, bytearray([0x78]))
|
||||
self.command(_JD79661_PANEL_SETTING, bytearray([0x8F, 0x29])) # PSR, Display resolution is 128x250
|
||||
self.command(
|
||||
_JD79661_PANEL_SETTING, bytearray([0x8F, 0x29])
|
||||
) # PSR, Display resolution is 128x250
|
||||
self.command(_JD79661_POWER_SETTING, bytearray([0x07, 0x00])) # PWR
|
||||
self.command(_JD79661_POFS, bytearray([0x10, 0x54, 0x44])) # POFS
|
||||
self.command(_JD79661_BOOSTER_SOFTSTART, bytearray([0x05, 0x00, 0x3F, 0x0A, 0x25, 0x12, 0x1A]))
|
||||
self.command(
|
||||
_JD79661_BOOSTER_SOFTSTART, bytearray([0x05, 0x00, 0x3F, 0x0A, 0x25, 0x12, 0x1A])
|
||||
)
|
||||
self.command(_JD79661_CDI, bytearray([0x37])) # CDI
|
||||
self.command(_JD79661_TCON, bytearray([0x02, 0x02])) # TCON
|
||||
self.command(_JD79661_RESOLUTION, bytearray([0, 128, 0, 250])) # TRES
|
||||
|
|
@ -197,12 +166,11 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
self.command(_JD79661_CMD_E9, bytearray([0x01]))
|
||||
self.command(_JD79661_PLL_CONTROL, bytearray([0x08]))
|
||||
self.command(_JD79661_POWER_ON)
|
||||
|
||||
|
||||
self.busy_wait()
|
||||
|
||||
def power_down(self) -> None:
|
||||
"""Power down the display - required when not actively displaying!"""
|
||||
# Only deep sleep if we have a reset pin
|
||||
if self._rst:
|
||||
self.command(_JD79661_POWER_OFF, bytearray([0x00]))
|
||||
self.busy_wait()
|
||||
|
|
@ -217,47 +185,39 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
time.sleep(1) # Wait 1 second if no busy pin
|
||||
|
||||
def write_ram(self, index: Literal[0, 1]) -> int:
|
||||
"""Send the one byte command for starting the RAM write process.
|
||||
|
||||
Note: The index parameter is ignored since JD79661 uses a single buffer.
|
||||
This parameter exists for API compatibility with the parent class.
|
||||
"""
|
||||
"""Send the one byte command for starting the RAM write process."""
|
||||
# JD79661 uses same command for all data
|
||||
return self.command(_JD79661_DATA_START_XMIT, end=False)
|
||||
|
||||
def set_ram_address(self, x: int, y: int) -> None:
|
||||
"""Set the RAM address location.
|
||||
|
||||
Note: Not used on JD79661 chipset. Exists for API compatibility.
|
||||
"""
|
||||
"""Set the RAM address location."""
|
||||
# Not used for JD79661
|
||||
pass
|
||||
|
||||
def fill(self, color: int) -> None:
|
||||
"""Fill the entire display with the specified color.
|
||||
|
||||
This method is overridden to handle the 2-bit packed pixel format
|
||||
used by the quad-color display.
|
||||
|
||||
|
||||
Args:
|
||||
color: Color value (BLACK, WHITE, YELLOW, or RED)
|
||||
|
||||
|
||||
Raises:
|
||||
ValueError: If an invalid color is specified
|
||||
"""
|
||||
# Map colors to fill patterns (4 pixels per byte)
|
||||
color_map = {
|
||||
Adafruit_JD79661.BLACK: 0x00, # 0b00000000 - all pixels black
|
||||
Adafruit_JD79661.WHITE: 0x55, # 0b01010101 - all pixels white
|
||||
Adafruit_JD79661.BLACK: 0x00, # 0b00000000 - all pixels black
|
||||
Adafruit_JD79661.WHITE: 0x55, # 0b01010101 - all pixels white
|
||||
Adafruit_JD79661.YELLOW: 0xAA, # 0b10101010 - all pixels yellow
|
||||
Adafruit_JD79661.RED: 0xFF, # 0b11111111 - all pixels red
|
||||
Adafruit_JD79661.RED: 0xFF, # 0b11111111 - all pixels red
|
||||
}
|
||||
|
||||
|
||||
if color not in color_map:
|
||||
raise ValueError(f"Invalid color: {color}. Use BLACK (0), WHITE (1), YELLOW (2), or RED (3).")
|
||||
|
||||
raise ValueError(
|
||||
f"Invalid color: {color}. Use BLACK (0), WHITE (1), YELLOW (2), or RED (3)."
|
||||
)
|
||||
|
||||
fill_byte = color_map[color]
|
||||
|
||||
|
||||
if self.sram:
|
||||
self.sram.erase(0x00, self._buffer1_size, fill_byte)
|
||||
else:
|
||||
|
|
@ -266,13 +226,10 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
|
||||
def pixel(self, x: int, y: int, color: int) -> None:
|
||||
"""Draw a single pixel in the display buffer.
|
||||
|
||||
This method is overridden to handle the 2-bit packed pixel format.
|
||||
Each byte contains 4 pixels, with 2 bits per pixel.
|
||||
|
||||
|
||||
Args:
|
||||
x: X coordinate
|
||||
y: Y coordinate
|
||||
y: Y coordinate
|
||||
color: Color value (BLACK, WHITE, YELLOW, or RED)
|
||||
"""
|
||||
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
|
||||
|
|
@ -305,7 +262,7 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
Adafruit_JD79661.YELLOW: _JD79661_YELLOW,
|
||||
Adafruit_JD79661.RED: _JD79661_RED,
|
||||
}
|
||||
|
||||
|
||||
if color not in color_map:
|
||||
# Default to white for invalid colors
|
||||
pixel_color = _JD79661_WHITE
|
||||
|
|
@ -314,15 +271,15 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
|
||||
# Calculate byte address (4 pixels per byte)
|
||||
addr = (x + y * stride) // 4
|
||||
|
||||
|
||||
# Calculate bit offset within byte (2 bits per pixel)
|
||||
# Pixels are packed left-to-right, MSB first
|
||||
bit_offset = (3 - (x % 4)) * 2
|
||||
|
||||
|
||||
# Create masks
|
||||
byte_mask = 0x3 << bit_offset
|
||||
byte_value = (pixel_color & 0x3) << bit_offset
|
||||
|
||||
|
||||
# Read, modify, write
|
||||
if self.sram:
|
||||
current = self.sram.read8(addr)
|
||||
|
|
@ -333,10 +290,9 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
self._buffer1[addr] &= ~byte_mask
|
||||
self._buffer1[addr] |= byte_value
|
||||
|
||||
# Override these methods to handle quad-color properly
|
||||
def rect(self, x: int, y: int, width: int, height: int, color: int) -> None:
|
||||
"""Draw a rectangle.
|
||||
|
||||
|
||||
Overridden to use the quad-color pixel method.
|
||||
"""
|
||||
for i in range(x, x + width):
|
||||
|
|
@ -348,7 +304,7 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
|
||||
def fill_rect(self, x: int, y: int, width: int, height: int, color: int) -> None:
|
||||
"""Fill a rectangle with the passed color.
|
||||
|
||||
|
||||
Overridden to use the quad-color pixel method.
|
||||
"""
|
||||
for i in range(x, x + width):
|
||||
|
|
@ -357,16 +313,15 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
|
||||
def line(self, x_0: int, y_0: int, x_1: int, y_1: int, color: int) -> None:
|
||||
"""Draw a line from (x_0, y_0) to (x_1, y_1) in passed color.
|
||||
|
||||
|
||||
Overridden to use the quad-color pixel method.
|
||||
"""
|
||||
# Bresenham's line algorithm
|
||||
dx = abs(x_1 - x_0)
|
||||
dy = abs(y_1 - y_0)
|
||||
sx = 1 if x_0 < x_1 else -1
|
||||
sy = 1 if y_0 < y_1 else -1
|
||||
err = dx - dy
|
||||
|
||||
|
||||
while True:
|
||||
self.pixel(x_0, y_0, color)
|
||||
if x_0 == x_1 and y_0 == y_1:
|
||||
|
|
@ -389,54 +344,42 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
font_name: str = "font5x8.bin",
|
||||
size: int = 1,
|
||||
) -> None:
|
||||
"""Write text string at location (x, y) in given color, using font file.
|
||||
|
||||
This method is for CircuitPython's built-in bitmap fonts only.
|
||||
For TrueType fonts, use PIL/Pillow to draw text on an image and then
|
||||
display the image using the image() method.
|
||||
"""
|
||||
# Validate color
|
||||
if color not in [Adafruit_JD79661.BLACK, Adafruit_JD79661.WHITE,
|
||||
Adafruit_JD79661.YELLOW, Adafruit_JD79661.RED]:
|
||||
raise ValueError(f"Invalid color: {color}. Use BLACK (0), WHITE (1), YELLOW (2), or RED (3).")
|
||||
|
||||
# Since we can't use the parent's framebuffer text method directly
|
||||
# (it only supports 1-bit depth), we need to render to a temporary buffer
|
||||
# and then copy the pixels in the requested color
|
||||
|
||||
# Estimate text dimensions
|
||||
text_width = len(string) * 6 * size # ~6 pixels per char
|
||||
text_height = 8 * size # 8 pixel high font
|
||||
|
||||
# Bounds check
|
||||
"""Write text string at location (x, y) in given color, using font file."""
|
||||
color_map = {
|
||||
Adafruit_JD79661.BLACK: _JD79661_BLACK,
|
||||
Adafruit_JD79661.WHITE: _JD79661_WHITE,
|
||||
Adafruit_JD79661.YELLOW: _JD79661_YELLOW,
|
||||
Adafruit_JD79661.RED: _JD79661_RED,
|
||||
}
|
||||
if color not in color_map:
|
||||
raise ValueError(
|
||||
f"Invalid color: {color}. Use BLACK (0), WHITE (1), YELLOW (2), or RED (3)."
|
||||
)
|
||||
|
||||
text_width = len(string) * 6 * size
|
||||
text_height = 8 * size
|
||||
|
||||
text_width = min(text_width, self.width - x)
|
||||
text_height = min(text_height, self.height - y)
|
||||
|
||||
|
||||
if text_width <= 0 or text_height <= 0:
|
||||
return
|
||||
|
||||
# Create temporary monochrome buffer
|
||||
|
||||
temp_buf_width = ((text_width + 7) // 8) * 8
|
||||
temp_buf = bytearray((temp_buf_width * text_height) // 8)
|
||||
|
||||
# Create temporary framebuffer
|
||||
|
||||
temp_fb = adafruit_framebuf.FrameBuffer(
|
||||
temp_buf,
|
||||
temp_buf_width,
|
||||
text_height,
|
||||
buf_format=adafruit_framebuf.MHMSB
|
||||
temp_buf, temp_buf_width, text_height, buf_format=adafruit_framebuf.MHMSB
|
||||
)
|
||||
|
||||
# Render text
|
||||
|
||||
temp_fb.fill(0)
|
||||
temp_fb.text(string, 0, 0, 1, font_name=font_name, size=size)
|
||||
|
||||
# Copy pixels in the requested color
|
||||
|
||||
for j in range(text_height):
|
||||
for i in range(text_width):
|
||||
byte_index = (j * temp_buf_width + i) // 8
|
||||
bit_index = 7 - ((j * temp_buf_width + i) % 8)
|
||||
|
||||
|
||||
if byte_index < len(temp_buf):
|
||||
if (temp_buf[byte_index] >> bit_index) & 1:
|
||||
self.pixel(x + i, y + j, color)
|
||||
|
|
@ -452,159 +395,73 @@ class Adafruit_JD79661(Adafruit_EPD):
|
|||
)
|
||||
if self.sram:
|
||||
raise RuntimeError("PIL image is not for use with SRAM assist")
|
||||
|
||||
# Grab all the pixels from the image, faster than getpixel.
|
||||
pix = image.load()
|
||||
# clear out any display buffers
|
||||
self.fill(Adafruit_EPD.WHITE)
|
||||
|
||||
# Clear out any display buffers (assuming white background)
|
||||
self.fill(Adafruit_JD79661.WHITE)
|
||||
|
||||
if image.mode == "RGB": # RGB Mode
|
||||
for y in range(image.size[1]):
|
||||
for x in range(image.size[0]):
|
||||
pixel = pix[x, y]
|
||||
# Check for yellow first (high red + green, low blue)
|
||||
if (pixel[0] >= 0x80) and (pixel[1] >= 0x80) and (pixel[2] < 0x80):
|
||||
# yellowish
|
||||
r, g, b = pixel[0], pixel[1], pixel[2]
|
||||
|
||||
# Calculate brightness/luminance for better color detection
|
||||
brightness = (r + g + b) / 3
|
||||
|
||||
# Color detection logic with thresholds
|
||||
if brightness >= 200: # Light colors -> White
|
||||
# White is typically the default, so we might not need to set it
|
||||
# self.pixel(x, y, Adafruit_EPD.WHITE)
|
||||
pass
|
||||
elif r >= 128 and g >= 128 and b < 80: # Yellow detection
|
||||
# High red and green, low blue
|
||||
self.pixel(x, y, Adafruit_JD79661.YELLOW)
|
||||
# Then check for red (high red, low green and blue)
|
||||
elif (pixel[0] >= 0x80) and (pixel[1] < 0x80) and (pixel[2] < 0x80):
|
||||
# reddish
|
||||
elif r >= 128 and g < 80 and b < 80: # Red detection
|
||||
# High red, low green and blue
|
||||
self.pixel(x, y, Adafruit_JD79661.RED)
|
||||
# Then black (all low)
|
||||
elif (pixel[0] < 0x80) and (pixel[1] < 0x80) and (pixel[2] < 0x80):
|
||||
# dark
|
||||
elif brightness < 80: # Dark colors -> Black
|
||||
# All RGB values are low
|
||||
self.pixel(x, y, Adafruit_JD79661.BLACK)
|
||||
# else: remains white (from fill)
|
||||
|
||||
elif image.mode == "L": # Grayscale
|
||||
elif r > g and r > b and r >= 100:
|
||||
# Red-dominant
|
||||
self.pixel(x, y, Adafruit_JD79661.RED)
|
||||
elif r >= 100 and g >= 100:
|
||||
# Both red and green high -> Yellow
|
||||
self.pixel(x, y, Adafruit_JD79661.YELLOW)
|
||||
elif brightness < 128:
|
||||
# Medium-dark -> Black
|
||||
self.pixel(x, y, Adafruit_JD79661.BLACK)
|
||||
# else: remains white (default)
|
||||
|
||||
elif image.mode == "L": # Grayscale Mode
|
||||
for y in range(image.size[1]):
|
||||
for x in range(image.size[0]):
|
||||
pixel = pix[x, y]
|
||||
if pixel < 0x40: # 0-63
|
||||
|
||||
# Map grayscale to 4 levels
|
||||
if pixel < 64:
|
||||
self.pixel(x, y, Adafruit_JD79661.BLACK)
|
||||
elif pixel < 0x80: # 64-127
|
||||
elif pixel < 128:
|
||||
self.pixel(x, y, Adafruit_JD79661.RED) # Or could use YELLOW
|
||||
elif pixel < 192:
|
||||
self.pixel(x, y, Adafruit_JD79661.YELLOW)
|
||||
elif pixel < 0xC0: # 128-191
|
||||
self.pixel(x, y, Adafruit_JD79661.RED)
|
||||
# else: 192-255 remains white
|
||||
# else: pixel >= 192 -> WHITE (default)
|
||||
|
||||
elif image.mode == "P": # Palette Mode (optional, for indexed color)
|
||||
# Convert to RGB first for easier processing
|
||||
rgb_image = image.convert("RGB")
|
||||
self.image(rgb_image) # Recursive call with RGB image
|
||||
|
||||
else:
|
||||
raise ValueError("Image must be in mode RGB or mode L.")
|
||||
raise ValueError("Image must be in mode RGB, L, or P.")
|
||||
|
||||
def image_dithered(self, image, dither_type="floyd-steinberg") -> None:
|
||||
"""Display an image with dithering to better represent colors/shades.
|
||||
|
||||
This method converts the image to use only the 4 available colors
|
||||
using error diffusion dithering for better visual quality.
|
||||
|
||||
Args:
|
||||
image: PIL Image object
|
||||
dither_type: Type of dithering - "floyd-steinberg" or "simple"
|
||||
|
||||
Raises:
|
||||
ValueError: If image dimensions don't match display
|
||||
RuntimeError: If SRAM is being used (not supported)
|
||||
"""
|
||||
imwidth, imheight = image.size
|
||||
if imwidth != self.width or imheight != self.height:
|
||||
raise ValueError(
|
||||
f"Image must be same dimensions as display ({self.width}x{self.height})."
|
||||
)
|
||||
if self.sram:
|
||||
raise RuntimeError("PIL image dithering is not supported with SRAM assist")
|
||||
|
||||
# Convert to RGB if not already
|
||||
if image.mode != "RGB":
|
||||
image = image.convert("RGB")
|
||||
|
||||
# Define our 4 color palette in RGB
|
||||
palette = [
|
||||
(0, 0, 0), # BLACK
|
||||
(255, 255, 255), # WHITE
|
||||
(255, 255, 0), # YELLOW
|
||||
(255, 0, 0), # RED
|
||||
]
|
||||
|
||||
# Create a working copy of the image as a list of lists
|
||||
pixels = []
|
||||
for y in range(imheight):
|
||||
row = []
|
||||
for x in range(imwidth):
|
||||
r, g, b = image.getpixel((x, y))
|
||||
row.append([r, g, b])
|
||||
pixels.append(row)
|
||||
|
||||
if dither_type == "floyd-steinberg":
|
||||
# Floyd-Steinberg dithering
|
||||
for y in range(imheight):
|
||||
for x in range(imwidth):
|
||||
old_pixel = pixels[y][x]
|
||||
|
||||
# Find closest color in palette
|
||||
min_dist = float('inf')
|
||||
closest_color = 0
|
||||
closest_rgb = palette[0]
|
||||
|
||||
for i, pal_color in enumerate(palette):
|
||||
dist = sum((old_pixel[j] - pal_color[j])**2 for j in range(3))
|
||||
if dist < min_dist:
|
||||
min_dist = dist
|
||||
closest_color = i
|
||||
closest_rgb = pal_color
|
||||
|
||||
# Set pixel to closest color
|
||||
self.pixel(x, y, closest_color)
|
||||
|
||||
# Calculate error
|
||||
error = [old_pixel[i] - closest_rgb[i] for i in range(3)]
|
||||
|
||||
# Distribute error to neighboring pixels
|
||||
if x + 1 < imwidth:
|
||||
for i in range(3):
|
||||
pixels[y][x + 1][i] += error[i] * 7 / 16
|
||||
|
||||
if y + 1 < imheight:
|
||||
if x > 0:
|
||||
for i in range(3):
|
||||
pixels[y + 1][x - 1][i] += error[i] * 3 / 16
|
||||
|
||||
for i in range(3):
|
||||
pixels[y + 1][x][i] += error[i] * 5 / 16
|
||||
|
||||
if x + 1 < imwidth:
|
||||
for i in range(3):
|
||||
pixels[y + 1][x + 1][i] += error[i] * 1 / 16
|
||||
|
||||
else: # Simple nearest-color mapping
|
||||
for y in range(imheight):
|
||||
for x in range(imwidth):
|
||||
pixel = pixels[y][x]
|
||||
|
||||
# Find closest color in palette
|
||||
min_dist = float('inf')
|
||||
closest_color = 0
|
||||
|
||||
for i, pal_color in enumerate(palette):
|
||||
dist = sum((pixel[j] - pal_color[j])**2 for j in range(3))
|
||||
if dist < min_dist:
|
||||
min_dist = dist
|
||||
closest_color = i
|
||||
|
||||
self.pixel(x, y, closest_color)
|
||||
|
||||
# Parent class drawing method overrides for documentation
|
||||
def set_black_buffer(self, index: Literal[0, 1], inverted: bool) -> None:
|
||||
"""Set the index for the black buffer data.
|
||||
|
||||
Note: This method exists for API compatibility but has no effect on the
|
||||
JD79661 since it uses a single buffer with 2-bit color depth rather than
|
||||
separate black/color buffers.
|
||||
"""
|
||||
"""Set the index for the black buffer data."""
|
||||
super().set_black_buffer(index, inverted)
|
||||
|
||||
def set_color_buffer(self, index: Literal[0, 1], inverted: bool) -> None:
|
||||
"""Set the index for the color buffer data.
|
||||
|
||||
Note: This method exists for API compatibility but has no effect on the
|
||||
JD79661 since it uses a single buffer with 2-bit color depth rather than
|
||||
separate black/color buffers.
|
||||
"""
|
||||
super().set_color_buffer(index, inverted)
|
||||
"""Set the index for the color buffer data."""
|
||||
super().set_color_buffer(index, inverted)
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 21 KiB |
|
|
@ -11,6 +11,7 @@ from adafruit_epd.epd import Adafruit_EPD
|
|||
from adafruit_epd.il0373 import Adafruit_IL0373
|
||||
from adafruit_epd.il0398 import Adafruit_IL0398
|
||||
from adafruit_epd.il91874 import Adafruit_IL91874
|
||||
from adafruit_epd.jd79661 import Adafruit_JD79661
|
||||
from adafruit_epd.ssd1608 import Adafruit_SSD1608
|
||||
from adafruit_epd.ssd1675 import Adafruit_SSD1675
|
||||
from adafruit_epd.ssd1675b import Adafruit_SSD1675B
|
||||
|
|
@ -18,9 +19,6 @@ from adafruit_epd.ssd1680 import Adafruit_SSD1680
|
|||
from adafruit_epd.ssd1681 import Adafruit_SSD1681
|
||||
from adafruit_epd.uc8151d import Adafruit_UC8151D
|
||||
|
||||
# create the spi device and pins we will need
|
||||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
|
||||
# create the spi device and pins we will need
|
||||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
ecs = digitalio.DigitalInOut(board.D4)
|
||||
|
|
@ -32,6 +30,7 @@ busy = digitalio.DigitalInOut(board.D7) # can be None to not use this pin
|
|||
|
||||
# give them all to our driver
|
||||
print("Creating display")
|
||||
# display = Adafruit_JD79661(122, 150, # 2.13" Quad-color display
|
||||
# display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display
|
||||
# display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display
|
||||
# display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display
|
||||
|
|
@ -70,6 +69,7 @@ height = display.height
|
|||
image = Image.new("RGB", (width, height))
|
||||
|
||||
WHITE = (0xFF, 0xFF, 0xFF)
|
||||
YELLOW = (0xFF, 0xFF, 0x00)
|
||||
RED = (0xFF, 0x00, 0x00)
|
||||
BLACK = (0x00, 0x00, 0x00)
|
||||
|
||||
|
|
@ -118,9 +118,15 @@ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
|
|||
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
|
||||
# font = ImageFont.truetype('Minecraftia.ttf', 8)
|
||||
|
||||
if type(display) == Adafruit_JD79661:
|
||||
# for quad color, test yellow
|
||||
fill = YELLOW
|
||||
else:
|
||||
# otherwise, text is red
|
||||
fill = RED
|
||||
# Write two lines of text.
|
||||
draw.text((x, top), "Hello", font=font, fill=RED)
|
||||
draw.text((x, top + 20), "World!", font=font, fill=RED)
|
||||
draw.text((x, top), "Hello", font=font, fill=fill)
|
||||
draw.text((x, top + 20), "World!", font=font, fill=fill)
|
||||
|
||||
# Display image.
|
||||
display.image(image)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ from adafruit_epd.ek79686 import Adafruit_EK79686
|
|||
from adafruit_epd.il0373 import Adafruit_IL0373
|
||||
from adafruit_epd.il0398 import Adafruit_IL0398
|
||||
from adafruit_epd.il91874 import Adafruit_IL91874
|
||||
from adafruit_epd.jd79661 import Adafruit_JD79661
|
||||
from adafruit_epd.ssd1608 import Adafruit_SSD1608
|
||||
from adafruit_epd.ssd1675 import Adafruit_SSD1675
|
||||
from adafruit_epd.ssd1680 import Adafruit_SSD1680, Adafruit_SSD1680Z
|
||||
|
|
@ -42,6 +43,7 @@ rst = digitalio.DigitalInOut(board.D27)
|
|||
busy = digitalio.DigitalInOut(board.D17)
|
||||
|
||||
# give them all to our driver
|
||||
# display = Adafruit_JD79661(122, 150, # 2.13" Quad-color display
|
||||
# display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display
|
||||
# display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display
|
||||
# display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color or mono display
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from adafruit_epd.ek79686 import Adafruit_EK79686
|
|||
from adafruit_epd.il0373 import Adafruit_IL0373
|
||||
from adafruit_epd.il0398 import Adafruit_IL0398
|
||||
from adafruit_epd.il91874 import Adafruit_IL91874
|
||||
from adafruit_epd.jd79661 import Adafruit_JD79661
|
||||
from adafruit_epd.ssd1608 import Adafruit_SSD1608
|
||||
from adafruit_epd.ssd1675 import Adafruit_SSD1675
|
||||
from adafruit_epd.ssd1680 import Adafruit_SSD1680, Adafruit_SSD1680Z
|
||||
|
|
@ -32,6 +33,7 @@ rst = digitalio.DigitalInOut(board.D27)
|
|||
busy = digitalio.DigitalInOut(board.D17)
|
||||
|
||||
# give them all to our driver
|
||||
# display = Adafruit_JD79661(122, 150, # 2.13" Quad-color display
|
||||
# display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display
|
||||
# display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display
|
||||
# display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color or mono display
|
||||
|
|
@ -86,6 +88,38 @@ image = image.crop((x, y, x + display.width, y + display.height)).convert("RGB")
|
|||
# Convert to Monochrome and Add dithering
|
||||
# image = image.convert("1").convert("L")
|
||||
|
||||
if type(display) == Adafruit_JD79661:
|
||||
# Create a palette with the 4 colors: Black, White, Red, Yellow
|
||||
# The palette needs 768 values (256 colors × 3 channels)
|
||||
palette = []
|
||||
|
||||
# We'll map the 256 palette indices to our 4 colors
|
||||
# 0-63: Black, 64-127: Red, 128-191: Yellow, 192-255: White
|
||||
for i in range(256):
|
||||
if i < 64:
|
||||
palette.extend([0, 0, 0]) # Black
|
||||
elif i < 128:
|
||||
palette.extend([255, 0, 0]) # Red
|
||||
elif i < 192:
|
||||
palette.extend([255, 255, 0]) # Yellow
|
||||
else:
|
||||
palette.extend([255, 255, 255]) # White
|
||||
|
||||
# Create a palette image
|
||||
palette_img = Image.new("P", (1, 1))
|
||||
palette_img.putpalette(palette)
|
||||
|
||||
# Optional: Enhance colors before dithering for better results
|
||||
# from PIL import ImageEnhance
|
||||
# enhancer = ImageEnhance.Color(image)
|
||||
# image = enhancer.enhance(1.5) # Increase color saturation
|
||||
|
||||
# Quantize the image using Floyd-Steinberg dithering
|
||||
image = image.quantize(palette=palette_img, dither=Image.FLOYDSTEINBERG)
|
||||
|
||||
# Convert back to RGB for the display driver
|
||||
image = image.convert("RGB")
|
||||
|
||||
# Display image.
|
||||
display.image(image)
|
||||
display.display()
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from adafruit_epd.epd import Adafruit_EPD
|
|||
from adafruit_epd.il0373 import Adafruit_IL0373
|
||||
from adafruit_epd.il0398 import Adafruit_IL0398
|
||||
from adafruit_epd.il91874 import Adafruit_IL91874
|
||||
from adafruit_epd.jd79661 import Adafruit_JD79661
|
||||
from adafruit_epd.ssd1608 import Adafruit_SSD1608
|
||||
from adafruit_epd.ssd1675 import Adafruit_SSD1675
|
||||
from adafruit_epd.ssd1680 import Adafruit_SSD1680, Adafruit_SSD1680Z
|
||||
|
|
@ -26,6 +27,7 @@ busy = digitalio.DigitalInOut(board.D5) # can be None to not use this pin
|
|||
|
||||
# give them all to our drivers
|
||||
print("Creating display")
|
||||
# display = Adafruit_JD79661(122, 150, # 2.13" Quad-color display
|
||||
# display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display
|
||||
# display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display
|
||||
# display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display
|
||||
|
|
@ -58,20 +60,33 @@ display = Adafruit_IL0373(
|
|||
# display.set_color_buffer(1, True)
|
||||
|
||||
display.rotation = 1
|
||||
if type(display) == Adafruit_JD79661:
|
||||
WHITE = Adafruit_JD79661.WHITE
|
||||
BLACK = Adafruit_JD79661.BLACK
|
||||
RED = Adafruit_JD79661.RED
|
||||
YELLOW = Adafruit_JD79661.YELLOW
|
||||
else:
|
||||
WHITE = Adafruit_EPD.WHITE
|
||||
BLACK = Adafruit_EPD.BLACK
|
||||
RED = Adafruit_EPD.RED
|
||||
|
||||
# clear the buffer
|
||||
print("Clear buffer")
|
||||
display.fill(Adafruit_EPD.WHITE)
|
||||
display.pixel(10, 100, Adafruit_EPD.BLACK)
|
||||
display.fill(WHITE)
|
||||
display.pixel(10, 100, BLACK)
|
||||
|
||||
print("Draw Rectangles")
|
||||
display.fill_rect(5, 5, 10, 10, Adafruit_EPD.RED)
|
||||
display.rect(0, 0, 20, 30, Adafruit_EPD.BLACK)
|
||||
display.fill_rect(5, 5, 10, 10, RED)
|
||||
display.rect(0, 0, 20, 30, BLACK)
|
||||
|
||||
print("Draw lines")
|
||||
display.line(0, 0, display.width - 1, display.height - 1, Adafruit_EPD.BLACK)
|
||||
display.line(0, display.height - 1, display.width - 1, 0, Adafruit_EPD.RED)
|
||||
if type(display) == Adafruit_JD79661:
|
||||
display.line(0, 0, display.width - 1, display.height - 1, YELLOW)
|
||||
display.line(0, display.height - 1, display.width - 1, 0, YELLOW)
|
||||
else:
|
||||
display.line(0, 0, display.width - 1, display.height - 1, BLACK)
|
||||
display.line(0, display.height - 1, display.width - 1, 0, RED)
|
||||
|
||||
print("Draw text")
|
||||
display.text("hello world", 25, 10, Adafruit_EPD.BLACK)
|
||||
display.text("hello world", 25, 10, BLACK)
|
||||
display.display()
|
||||
|
|
|
|||
Loading…
Reference in a new issue