Simple test

Ensure your device works with this simple test.

examples/is31fl3731_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio

# uncomment next line if you are using Feather CharlieWing LED 15 x 7
from adafruit_is31fl3731.charlie_wing import CharlieWing as Display

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display

# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
# i2c = busio.I2C(board.GP21, board.GP20)

i2c = busio.I2C(board.SCL, board.SDA)

display = Display(i2c)

# draw a box on the display
# first draw the top and bottom edges
for x in range(display.width):
    display.pixel(x, 0, 50)
    display.pixel(x, display.height - 1, 50)
# now draw the left and right edges
for y in range(display.height):
    display.pixel(0, y, 50)
    display.pixel(display.width - 1, y, 50)

Matrix Examples

Other examples working on matrix display.

examples/is31fl3731_blink_example.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio

# uncomment next line if you are using Feather CharlieWing LED 15 x 7
from adafruit_is31fl3731.charlie_wing import CharlieWing as Display

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display

# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
# i2c = busio.I2C(board.GP21, board.GP20)

i2c = busio.I2C(board.SCL, board.SDA)

# array pattern in bits; top row-> bottom row, 8 bits in each row
an_arrow = bytearray((0x08, 0x0C, 0xFE, 0xFF, 0xFE, 0x0C, 0x08, 0x00, 0x00))

display = Display(i2c)

offset = (display.width - 8) // 2

# first load the frame with the arrows; moves the an_arrow to the right in each
# frame
display.sleep(True)  # turn display off while updating blink bits
display.fill(0)
for y in range(display.height):
    row = an_arrow[y]
    for x in range(8):
        bit = 1 << (7 - x) & row
        if bit:
            display.pixel(x + offset, y, 50, blink=True)

display.blink(1000)  # ranges from 270 to 2159; smaller the number to faster blink
display.sleep(False)  # turn display on
examples/is31fl3731_frame_example.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio

# uncomment next line if you are using Feather CharlieWing LED 15 x 7
from adafruit_is31fl3731.charlie_wing import CharlieWing as Display

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display

# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
# i2c = busio.I2C(board.GP21, board.GP20)

i2c = busio.I2C(board.SCL, board.SDA)

# arrow pattern in bits; top row-> bottom row, 8 bits in each row
arrow = bytearray((0x08, 0x0C, 0xFE, 0xFF, 0xFE, 0x0C, 0x08, 0x00, 0x00))

display = Display(i2c)

# first load the frame with the arrows; moves the arrow to the right in each
# frame
display.sleep(True)  # turn display off while frames are updated
for frame in range(display.width - 8):
    display.frame(frame, show=False)
    display.fill(0)
    for y in range(display.height):
        row = arrow[y]
        for x in range(8):
            bit = 1 << (7 - x) & row
            # display the pixel into selected frame with varying intensity
            if bit:
                display.pixel(x + frame, y, frame ** 2 + 1)
display.sleep(False)
# now tell the display to show the frame one at time
while True:
    for frame in range(8):
        display.frame(frame)
        time.sleep(0.1)
examples/is31fl3731_text_example.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio
import adafruit_framebuf

# uncomment next line if you are using Feather CharlieWing LED 15 x 7
# from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display

# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display

# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
# i2c = busio.I2C(board.GP21, board.GP20)

i2c = busio.I2C(board.SCL, board.SDA)

display = Display(i2c)

text_to_show = "Adafruit!!"

# Create a framebuffer for our display
buf = bytearray(32)  # 2 bytes tall x 16 wide = 32 bytes (9 bits is 2 bytes)
fb = adafruit_framebuf.FrameBuffer(
    buf, display.width, display.height, adafruit_framebuf.MVLSB
)


frame = 0  # start with frame 0
while True:
    for i in range(len(text_to_show) * 9):
        fb.fill(0)
        fb.text(text_to_show, -i + display.width, 0, color=1)

        # to improve the display flicker we can use two frame
        # fill the next frame with scrolling text, then
        # show it.
        display.frame(frame, show=False)
        # turn all LEDs off
        display.fill(0)
        for x in range(display.width):
            # using the FrameBuffer text result
            bite = buf[x]
            for y in range(display.height):
                bit = 1 << y & bite
                # if bit > 0 then set the pixel brightness
                if bit:
                    display.pixel(x, y, 50)

        # now that the frame is filled, show it.
        display.frame(frame, show=True)
        frame = 0 if frame else 1
examples/is31fl3731_wave_example.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio

# uncomment next line if you are using Feather CharlieWing LED 15 x 7
from adafruit_is31fl3731.charlie_wing import CharlieWing as Display

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display

# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
# i2c = busio.I2C(board.GP21, board.GP20)

i2c = busio.I2C(board.SCL, board.SDA)

# fmt: off
sweep = [ 1, 2, 3, 4, 6, 8, 10, 15, 20, 30, 40, 60,
    60, 40, 30, 20, 15, 10, 8, 6, 4, 3, 2, 1, ]
# fmt: on

frame = 0

display = Display(i2c)

while True:
    for incr in range(24):
        # to reduce update flicker, use two frames
        # make a frame active, don't show it yet
        display.frame(frame, show=False)
        # fill the display with the next frame
        for x in range(display.width):
            for y in range(display.height):
                display.pixel(x, y, sweep[(x + y + incr) % 24])
        # show the next frame
        display.frame(frame, show=True)
        if frame:
            frame = 0
        else:
            frame = 1

Pillow Examples

Examples that utilize the Python Imaging Library (Pillow) for use on (Linux) computers that are using CPython with Adafruit Blinka to support CircuitPython libraries. CircuitPython does not support PIL/pillow (python imaging library)!

examples/is31fl3731_pillow_animated_gif.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Example to extract the frames and other parameters from an animated gif
and then run the animation on the display.

Usage:
python3 is31fl3731_pillow_animated_gif.py animated.gif

This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!

Author(s): Melissa LeBlanc-Williams for Adafruit Industries
"""

import sys
import board
from PIL import Image

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display

# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display

i2c = board.I2C()

display = Display(i2c)


# Open the gif
if len(sys.argv) < 2:
    print("No image file specified")
    print("Usage: python3 is31fl3731_pillow_animated_gif.py animated.gif")
    sys.exit()

image = Image.open(sys.argv[1])

# Make sure it's animated
if not image.is_animated:
    print("Specified image is not animated")
    sys.exit()

# Get the autoplay information from the gif
delay = image.info["duration"]

# Figure out the correct loop count
if "loop" in image.info:
    loops = image.info["loop"]
    if loops > 0:
        loops += 1
else:
    loops = 1

# IS31FL3731 only supports 0-7
loops = min(loops, 7)

# Get the frame count (maximum 8 frames)
frame_count = min(image.n_frames, 8)

# Load each frame of the gif onto the Matrix
for frame in range(frame_count):
    image.seek(frame)
    frame_image = Image.new("L", (display.width, display.height))
    frame_image.paste(
        image.convert("L"),
        (
            display.width // 2 - image.width // 2,
            display.height // 2 - image.height // 2,
        ),
    )
    display.image(frame_image, frame=frame)

display.autoplay(delay=delay, loops=loops)
examples/is31fl3731_pillow_marquee.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Example to scroll some text as a marquee

This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!

Author(s): Melissa LeBlanc-Williams for Adafruit Industries
"""

import board
from PIL import Image, ImageDraw, ImageFont

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display

# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display

SCROLLING_TEXT = "You can display a personal message here..."
BRIGHTNESS = 64  # Brightness can be between 0-255

i2c = board.I2C()

display = Display(i2c)

# Load a font
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 8)

# Create an image that contains the text
text_width, text_height = font.getsize(SCROLLING_TEXT)
text_image = Image.new("L", (text_width, text_height))
text_draw = ImageDraw.Draw(text_image)
text_draw.text((0, 0), SCROLLING_TEXT, font=font, fill=BRIGHTNESS)

# Create an image for the display
image = Image.new("L", (display.width, display.height))
draw = ImageDraw.Draw(image)

# Load the text in each frame
while True:
    for x in range(text_width + display.width):
        draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0)
        image.paste(
            text_image, (display.width - x, display.height // 2 - text_height // 2 - 1)
        )
        display.image(image)
examples/is31fl3731_pillow_numbers.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Example to utilize the Python Imaging Library (Pillow) and draw bitmapped text
to 8 frames and then run autoplay on those frames.

This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!

Author(s): Melissa LeBlanc-Williams for Adafruit Industries
"""

import board
from PIL import Image, ImageDraw, ImageFont

# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
# from adafruit_is31fl3731.matrix import Matrix as Display
# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display

# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display

BRIGHTNESS = 32  # Brightness can be between 0-255

i2c = board.I2C()

display = Display(i2c)

display.fill(0)

# 256 Color Grayscale Mode
image = Image.new("L", (display.width, display.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 10)

# Load the text in each frame
for x in range(8):
    draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0)
    draw.text((x + 1, -2), str(x + 1), font=font, fill=BRIGHTNESS)
    display.image(image, frame=x)

display.autoplay(delay=500)

Colorful Examples

Example that works on the RGB Led Shim.

examples/is31fl3731_ledshim_rainbow.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from adafruit_is31fl3731.led_shim import LedShim as Display

i2c = busio.I2C(board.SCL, board.SDA)

# initial display if you are using Pimoroni LED SHIM
display = Display(i2c)

# fmt: off
# This list 28 colors from a rainbow...
rainbow = [
    (255, 0, 0), (255, 54, 0), (255, 109, 0), (255, 163, 0),
    (255, 218, 0), (236, 255, 0), (182, 255, 0), (127, 255, 0),
    (72, 255, 0), (18, 255, 0), (0, 255, 36), (0, 255, 91),
    (0, 255, 145), (0, 255, 200), (0, 255, 255), (0, 200, 255),
    (0, 145, 255), (0, 91, 255), (0, 36, 255), (18, 0, 255),
    (72, 0, 255), (127, 0, 255), (182, 0, 255), (236, 0, 255),
    (255, 0, 218), (255, 0, 163), (255, 0, 109), (255, 0, 54),
]
# fmt: on


for y in range(3):
    for x in range(28):
        display.pixel(x, y, 255)
        time.sleep(0.1)
        display.pixel(x, y, 0)

while True:
    for offset in range(28):
        for x in range(28):
            r, g, b = rainbow[(x + offset) % 28]
            display.pixelrgb(x, r, g, b)

Example that works on the RGB Matrix 5x5.

examples/is31fl3731_rgbmatrix5x5_rainbow.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# SPDX-FileCopyrightText: 2021 Sandy Macdonald, David Glaude, James Carr
# SPDX-License-Identifier: MIT

"""
Example to display a rainbow animation on the 5x5 RGB Matrix Breakout.

Usage:
Rename this file code.py and pop it on your Raspberry Pico's
CIRCUITPY drive.

This example is for use on the Pico Explorer Base or other board that use the same SDA/SCL pin.

Author(s): Sandy Macdonald, David Glaude, James Carr
"""

import time
import math
import busio
import board

from adafruit_is31fl3731.rgbmatrix5x5 import RGBmatrix5x5 as Display


def hsv_to_rgb(hue, sat, val):
    # pylint: disable=too-many-return-statements
    """
    Convert HSV colour to RGB

    :param hue: hue; 0.0-1.0
    :param sat: saturation; 0.0-1.0
    :param val: value; 0.0-1.0
    """

    if sat == 0.0:
        return val, val, val

    i = int(hue * 6.0)

    p = val * (1.0 - sat)
    f = (hue * 6.0) - i
    q = val * (1.0 - sat * f)
    t = val * (1.0 - sat * (1.0 - f))

    i %= 6

    if i == 0:
        return val, t, p
    if i == 1:
        return q, val, p
    if i == 2:
        return p, val, t
    if i == 3:
        return p, q, val
    if i == 4:
        return t, p, val
    if i == 5:
        return val, p, q

    # Will never reach here but it keeps pylint happier
    return val, val, val


# Create the I2C bus on a Pico Explorer Base
i2c = busio.I2C(board.GP5, board.GP4)

# Set up 5x5 RGB matrix Breakout
display = Display(i2c)


def test_pixels(r, g, b):
    # Draw each row from left to right, top to bottom
    for y in range(0, 5):
        for x in range(0, 5):
            display.fill(0)  # Clear display
            display.pixelrgb(x, y, r, g, b)
            time.sleep(0.05)


def test_rows(r, g, b):
    # Draw full rows from top to bottom
    for y in range(0, 5):
        display.fill(0)  # Clear display
        for x in range(0, 5):
            display.pixelrgb(x, y, r, g, b)
        time.sleep(0.2)


def test_columns(r, g, b):
    # Draw full columns from left to right
    for x in range(0, 5):
        display.fill(0)  # Clear display
        for y in range(0, 5):
            display.pixelrgb(x, y, r, g, b)
        time.sleep(0.2)


def test_rainbow_sweep():
    step = 0

    for _ in range(100):
        for y in range(0, 5):
            for x in range(0, 5):
                pixel_hue = (x + y + (step / 20)) / 8
                pixel_hue = pixel_hue - int(pixel_hue)
                pixel_hue += 0
                pixel_hue = pixel_hue - math.floor(pixel_hue)

                rgb = hsv_to_rgb(pixel_hue, 1, 1)

                display.pixelrgb(
                    x, y, int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
                )

        time.sleep(0.01)
        step += 3


while True:
    test_pixels(64, 0, 0)  # RED
    test_pixels(0, 64, 0)  # GREEN
    test_pixels(0, 0, 64)  # BLUE
    test_pixels(64, 64, 64)  # WHITE

    test_rows(64, 0, 0)  # RED
    test_rows(0, 64, 0)  # GREEN
    test_rows(0, 0, 64)  # BLUE
    test_rows(64, 64, 64)  # WHITE

    test_columns(64, 0, 0)  # RED
    test_columns(0, 64, 0)  # GREEN
    test_columns(0, 0, 64)  # BLUE
    test_columns(64, 64, 64)  # WHITE

    test_rainbow_sweep()