Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ ChangeLog
+------------+------------------------------------------------------------------------+------------+
| Version | Description | Date |
+============+========================================================================+============+
| **1.10.0** | * Drop support for Python 3.9 | TBA |
| **1.10.0** | * Add native ``scrollphathd`` device, driving Pimoroni's Scroll pHAT | TBC |
| | HD via a from-scratch IS31FL3731 I2C driver | |
| | * Drop support for Python 3.9 | |
+------------+------------------------------------------------------------------------+------------+
| **1.9.0** | * Drop support for Python 3.8 | 2026/02/01 |
+------------+------------------------------------------------------------------------+------------+
Expand Down
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ Luma.LED_Matrix
:target: https://pypi.org/project/luma.led-matrix

Python 3 library interfacing LED matrix displays with the MAX7219 driver (using
SPI), WS2812 (NeoPixels, inc Pimoroni Unicorn pHat/Hat and Unicorn Hat HD) and
APA102 (DotStar) on the Raspberry Pi and other Linux-based single board computers
SPI), WS2812 (NeoPixels, inc Pimoroni Unicorn pHat/Hat and Unicorn Hat HD),
APA102 (DotStar) and IS31FL3731 (Pimoroni Scroll pHAT HD) on the Raspberry Pi
and other Linux-based single board computers
- it provides a `Pillow <https://pillow.readthedocs.io/>`_-compatible drawing
canvas, and other functionality to support:

Expand Down
5 changes: 3 additions & 2 deletions doc/intro.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Introduction
------------
Python library interfacing LED matrix displays with the MAX7219 driver (using
SPI) and WS2812 & APA102 NeoPixels (inc Pimoroni Unicorn pHat/Hat and Unicorn
Hat HD) on the Raspberry Pi and other Linux-based single board computers - it
SPI), WS2812 & APA102 NeoPixels (inc Pimoroni Unicorn pHat/Hat and Unicorn
Hat HD), and the IS31FL3731 driver (inc Pimoroni Scroll pHAT HD, over I2C)
on the Raspberry Pi and other Linux-based single board computers - it
provides a Pillow-compatible drawing canvas, and other functionality to
support:

Expand Down
26 changes: 25 additions & 1 deletion doc/python-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ This should animate a green dot moving left-to-right down each line.

Pimoroni Unicorn HAT HD
"""""""""""""""""""""""
Pimoroni sells the `Unicorn HAT HD <https://shop.pimoroni.com/products/unicorn-hat-hd>`_,
Pimoroni also sells the `Unicorn HAT HD <https://shop.pimoroni.com/products/unicorn-hat-hd>`_,
comprising 256 high-intensity RGB LEDs in a 16x16 arrangement. The pixels are driven by an
ARM STM32F making the display appear as an SPI device:

Expand Down Expand Up @@ -440,6 +440,30 @@ APA102 RGB pixels can have their brightness individually controlled: by setting
the alpha chanel to a translucent value (as per the above example) will set the
brightness accordingly.

Scroll pHAT HD (IS31FL3731)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`Pimoroni's Scroll pHAT HD <https://shop.pimoroni.com/products/scroll-phat-hd>`_
is a 17x7 matrix of individually PWM-dimmable white LEDs, driven by an onboard
ISSI IS31FL3731 chip and addressed over I2C - no ``serial_interface`` is
needed, so the device can typically be constructed with no arguments at all:

.. code:: python

from luma.core.render import canvas
from luma.led_matrix.device import scrollphathd

device = scrollphathd()

with canvas(device) as draw:
draw.point((0, 0), fill="white")
draw.point((1, 0), fill=(255, 255, 255, 0x40)) # dimmer, via alpha

As with :py:class:`~luma.led_matrix.device.apa102` and
:py:class:`~luma.led_matrix.device.unicornhathd`, per-pixel brightness can be
set by giving a pixel a translucent alpha value; since the underlying LEDs
are a single colour (white), the brightest of the red, green and blue
channel values is used as that pixel's intensity.

Emulators
^^^^^^^^^
There are various `display emulators <http://github.com/rm-hull/luma.emulator>`_
Expand Down
38 changes: 38 additions & 0 deletions examples/scrollphathd_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2026 Richard Hull and contributors
# See LICENSE.rst for details.

import time

from luma.led_matrix.device import scrollphathd
from luma.core.render import canvas

device = scrollphathd()


def main():
# Sweep a single lit pixel across the whole 17x7 grid
for y in range(device.height):
for x in range(device.width):
with canvas(device) as draw:
draw.point((x, y), fill="white")
time.sleep(0.02)

# Fade the whole display up and down using the alpha channel to
# control per-pixel brightness
for _ in range(3):
for alpha in list(range(0, 256, 8)) + list(range(255, -1, -8)):
with canvas(device) as draw:
draw.rectangle(device.bounding_box, fill=(255, 255, 255, alpha))
time.sleep(0.01)

device.clear()
device.show()


if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
25 changes: 25 additions & 0 deletions luma/led_matrix/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ class max7219(object):
SCANLIMIT = 0x0B
SHUTDOWN = 0x0C
DISPLAYTEST = 0x0F


class is31fl3731(object):
NUM_FRAMES = 8
NUM_PIXELS = 144
Comment thread
thijstriemstra marked this conversation as resolved.
Resolved

# The "function" registers (mode, frame select, shutdown, etc) all live
# in a reserved configuration bank, selected the same way as any other
# frame.
CONFIG_BANK = 0x0B
BANK_ADDRESS = 0xFD

MODE = 0x00
FRAME = 0x01
AUDIOSYNC = 0x06
SHUTDOWN = 0x0A

PICTURE_MODE = 0x00

# Within a frame, the first 18 bytes (144 bits) are a bitmap of which
# LED positions are physically wired up and should be lit; the PWM
# brightness values for those same 144 positions follow on immediately
# afterwards.
LED_ENABLE = 0x00
LED_PWM = 0x24
167 changes: 166 additions & 1 deletion luma/led_matrix/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from luma.led_matrix.segment_mapper import dot_muncher, regular


__all__ = ["max7219", "ws2812", "neopixel", "neosegment", "apa102", "unicornhathd"]
__all__ = ["max7219", "ws2812", "neopixel", "neosegment", "apa102", "unicornhathd", "scrollphathd"]


class max7219(device):
Expand Down Expand Up @@ -623,3 +623,168 @@ def contrast(self, value):
self._brightness = value
if self._last_image is not None:
self.display(self._last_image)


class scrollphathd(device):
"""
Display adapter for Pimoroni's `Scroll pHAT HD
<https://shop.pimoroni.com/products/scroll-phat-hd>`_ - a 17x7 array of
individually PWM-dimmable white LEDs. It is addressed *natively* over
I2C via the onboard ISSI IS31FL3731 LED matrix driver chip's register
map - this is a fresh, from-scratch implementation and not a wrapper
around Pimoroni's own ``scrollphathd`` library.

As every LED offers 256 levels of brightness, a 32-bit RGBA
:py:mod:`PIL.Image` is expected. Since the underlying hardware is a
single-colour (white) LED matrix, the brightest of the red, green and
blue channel values is used as each pixel's intensity; as with
:py:class:`apa102` and :py:class:`unicornhathd`, if a pixel is not
fully opaque, its alpha channel value is used to set the brightness of
the respective LED instead.

.. note::
Scroll pHAT HD is only ever addressed over I2C, so (unlike most of
the other devices in this module) there is no ``serial_interface``
parameter to supply. Instead, an (optional) ``i2c_bus`` may be
injected - this is typically only needed for testing, with a
stubbed/mocked bus.

:param i2c_bus: An `smbus2 <https://pypi.org/project/smbus2>`_
compatible I2C bus instance, exposing (at least) ``write_byte_data``
and ``write_i2c_block_data`` methods. If not supplied,
``smbus2.SMBus(1)`` is used.
:param address: 7-bit I2C address of the attached IS31FL3731
(default: ``0x74``).
:type address: int
:param rotate: Whether the device dimensions should be rotated in-situ:
A value of: 0=0°, 1=90°, 2=180°, 3=270°. If not supplied, zero is
assumed.
:type rotate: int

.. versionadded:: 1.10.0
"""
def __init__(self, i2c_bus=None, address=0x74, rotate=0, **kwargs):
super(scrollphathd, self).__init__(luma.led_matrix.const.is31fl3731, noop())
self.capabilities(17, 7, rotate, mode="RGBA")

self._i2c = i2c_bus or self.__smbus2__()
self._address = address
self._current_frame = 0
self._last_image = None
self._brightness = 0x70

# Reset into a known state: briefly shutdown, then wake, select
# simple "picture" (as opposed to auto-play or audio-play) mode,
# and ensure audio-modulated brightness is disabled.
self.hide()
self.show()
self._write_config_register(self._const.MODE, self._const.PICTURE_MODE)
self._write_config_register(self._const.AUDIOSYNC, 0)

# The 17x7 visible grid only occupies 119 of the chip's 144
# addressable LED positions; precompute the mapping once, and use
# it to tell the chip which positions are actually wired up.
self._pixel_map = [self._pixel_address(x, y)
for y in range(self._h) for x in range(self._w)]

wired = [False] * self._const.NUM_PIXELS
for index in self._pixel_map:
wired[index] = True
for frame in range(self._const.NUM_FRAMES):
self._enable_leds(frame, wired)

self.clear()

def __smbus2__(self):
import smbus2
return smbus2.SMBus(1)

def display(self, image):
"""
Takes a 32-bit RGBA :py:mod:`PIL.Image` and dumps it to the Scroll
pHAT HD's LED matrix via the IS31FL3731's register interface.
"""
assert image.mode == self.mode
assert image.size == self.size
self._last_image = image.copy()

image = self.preprocess(image)
normalized_brightness = self._brightness / 255.0
pixel_map = self._pixel_map

pwm_values = [0] * self._const.NUM_PIXELS
for index, (r, g, b, a) in enumerate(image.getdata()):
brightness = (a / 255.0) if a != 0xFF else normalized_brightness
pwm_values[pixel_map[index]] = int(max(r, g, b) * brightness)

frame = self._current_frame
self._select_bank(frame)
self._write_block(self._const.LED_PWM, pwm_values)
self._write_config_register(self._const.FRAME, frame)
self._current_frame ^= 1

def show(self):
"""
Wakes the device out of a prior low-power sleep mode.
"""
self._write_config_register(self._const.SHUTDOWN, 1)

def hide(self):
"""
Puts the device into low-power sleep mode. LED memory contents are
retained, so the display resumes from where it left off once
:py:meth:`show` is called again.
"""
self._write_config_register(self._const.SHUTDOWN, 0)

def contrast(self, value):
"""
Sets the LED intensity to the desired level, in the range 0-255.
As with :py:class:`apa102` and :py:class:`unicornhathd`, this is
only applied to fully opaque pixels; pixels with a translucent
alpha channel use that value to set brightness instead.

:param level: Desired contrast level in the range of 0-255.
:type level: int
"""
assert 0x00 <= value <= 0xFF
self._brightness = value
if self._last_image is not None:
self.display(self._last_image)

def _enable_leds(self, frame, enabled_pixels):
enable_bitmap = bytearray(self._const.NUM_PIXELS // 8)
for index, enabled in enumerate(enabled_pixels):
if enabled:
enable_bitmap[index // 8] |= 1 << (index % 8)

self._select_bank(frame)
self._write_block(self._const.LED_ENABLE, list(enable_bitmap))

def _write_config_register(self, register, value):
self._select_bank(self._const.CONFIG_BANK)
self._i2c.write_byte_data(self._address, register, value)

def _select_bank(self, bank):
self._i2c.write_byte_data(self._address, self._const.BANK_ADDRESS, bank)

def _write_block(self, start_register, data):
for offset in range(0, len(data), 32):
chunk = data[offset:offset + 32]
self._i2c.write_i2c_block_data(self._address, start_register + offset, chunk)

@staticmethod
def _pixel_address(x, y):
"""
Translates a logical (x, y) coordinate on the visible 17x7 display
into the corresponding LED index within the IS31FL3731's 144-pixel
internal frame buffer.

The board is built from two 8-column-wide LED matrix dies (plus a
shared middle column at x=8), wired in mirrored/opposite
orientations to one another, which is why the mapping splits and
differs either side of that middle column.
"""
if x <= 8:
return (8 - x) * 16 + (6 - y)
return (x - 9) * 16 + 8 + y
Loading