Simple test

Ensure your device works with this simple test.

examples/is31fl3731_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import busio
 6
 7# uncomment next line if you are using Feather CharlieWing LED 15 x 7
 8from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
 9
10# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
11# from adafruit_is31fl3731.matrix import Matrix as Display
12# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
13# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
14# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
15# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
16# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
17# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
18
19# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
20# i2c = busio.I2C(board.GP21, board.GP20)
21
22i2c = busio.I2C(board.SCL, board.SDA)
23
24display = Display(i2c)
25
26# draw a box on the display
27# first draw the top and bottom edges
28for x in range(display.width):
29    display.pixel(x, 0, 50)
30    display.pixel(x, display.height - 1, 50)
31# now draw the left and right edges
32for y in range(display.height):
33    display.pixel(0, y, 50)
34    display.pixel(display.width - 1, y, 50)

Matrix Examples

Other examples working on matrix display.

examples/is31fl3731_blink_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import busio
 6
 7# uncomment next line if you are using Feather CharlieWing LED 15 x 7
 8from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
 9
10# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
11# from adafruit_is31fl3731.matrix import Matrix as Display
12# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
13# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
14# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
15# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
16# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
17# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
18
19# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
20# i2c = busio.I2C(board.GP21, board.GP20)
21
22i2c = busio.I2C(board.SCL, board.SDA)
23
24# array pattern in bits; top row-> bottom row, 8 bits in each row
25an_arrow = bytearray((0x08, 0x0C, 0xFE, 0xFF, 0xFE, 0x0C, 0x08, 0x00, 0x00))
26
27display = Display(i2c)
28
29offset = (display.width - 8) // 2
30
31# first load the frame with the arrows; moves the an_arrow to the right in each
32# frame
33display.sleep(True)  # turn display off while updating blink bits
34display.fill(0)
35for y in range(display.height):
36    row = an_arrow[y]
37    for x in range(8):
38        bit = 1 << (7 - x) & row
39        if bit:
40            display.pixel(x + offset, y, 50, blink=True)
41
42display.blink(1000)  # ranges from 270 to 2159; smaller the number to faster blink
43display.sleep(False)  # turn display on
examples/is31fl3731_frame_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import busio
 7
 8# uncomment next line if you are using Feather CharlieWing LED 15 x 7
 9from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
10
11# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
12# from adafruit_is31fl3731.matrix import Matrix as Display
13# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
14# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
15# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
16# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
17# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
18# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
19
20# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
21# i2c = busio.I2C(board.GP21, board.GP20)
22
23i2c = busio.I2C(board.SCL, board.SDA)
24
25# arrow pattern in bits; top row-> bottom row, 8 bits in each row
26arrow = bytearray((0x08, 0x0C, 0xFE, 0xFF, 0xFE, 0x0C, 0x08, 0x00, 0x00))
27
28display = Display(i2c)
29
30# first load the frame with the arrows; moves the arrow to the right in each
31# frame
32display.sleep(True)  # turn display off while frames are updated
33for frame in range(display.width - 8):
34    display.frame(frame, show=False)
35    display.fill(0)
36    for y in range(display.height):
37        row = arrow[y]
38        for x in range(8):
39            bit = 1 << (7 - x) & row
40            # display the pixel into selected frame with varying intensity
41            if bit:
42                display.pixel(x + frame, y, frame**2 + 1)
43display.sleep(False)
44# now tell the display to show the frame one at time
45while True:
46    for frame in range(8):
47        display.frame(frame)
48        time.sleep(0.1)
examples/is31fl3731_text_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import busio
 6import adafruit_framebuf
 7
 8# uncomment next line if you are using Feather CharlieWing LED 15 x 7
 9# from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
10# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
11# from adafruit_is31fl3731.matrix import Matrix as Display
12# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
13from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
14
15# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
16# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
17# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
18# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
19
20# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
21# i2c = busio.I2C(board.GP21, board.GP20)
22
23i2c = busio.I2C(board.SCL, board.SDA)
24
25display = Display(i2c)
26
27text_to_show = "Adafruit!!"
28
29# Create a framebuffer for our display
30buf = bytearray(32)  # 2 bytes tall x 16 wide = 32 bytes (9 bits is 2 bytes)
31fb = adafruit_framebuf.FrameBuffer(
32    buf, display.width, display.height, adafruit_framebuf.MVLSB
33)
34
35
36frame = 0  # start with frame 0
37while True:
38    for i in range(len(text_to_show) * 9):
39        fb.fill(0)
40        fb.text(text_to_show, -i + display.width, 0, color=1)
41
42        # to improve the display flicker we can use two frame
43        # fill the next frame with scrolling text, then
44        # show it.
45        display.frame(frame, show=False)
46        # turn all LEDs off
47        display.fill(0)
48        for x in range(display.width):
49            # using the FrameBuffer text result
50            bite = buf[x]
51            for y in range(display.height):
52                bit = 1 << y & bite
53                # if bit > 0 then set the pixel brightness
54                if bit:
55                    display.pixel(x, y, 50)
56
57        # now that the frame is filled, show it.
58        display.frame(frame, show=True)
59        frame = 0 if frame else 1
examples/is31fl3731_wave_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import busio
 6
 7# uncomment next line if you are using Feather CharlieWing LED 15 x 7
 8from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
 9
10# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
11# from adafruit_is31fl3731.matrix import Matrix as Display
12# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
13# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
14# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
15# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
16# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
17# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
18
19# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
20# i2c = busio.I2C(board.GP21, board.GP20)
21
22i2c = busio.I2C(board.SCL, board.SDA)
23
24# fmt: off
25sweep = [ 1, 2, 3, 4, 6, 8, 10, 15, 20, 30, 40, 60,
26    60, 40, 30, 20, 15, 10, 8, 6, 4, 3, 2, 1, ]
27# fmt: on
28
29frame = 0
30
31display = Display(i2c)
32
33while True:
34    for incr in range(24):
35        # to reduce update flicker, use two frames
36        # make a frame active, don't show it yet
37        display.frame(frame, show=False)
38        # fill the display with the next frame
39        for x in range(display.width):
40            for y in range(display.height):
41                display.pixel(x, y, sweep[(x + y + incr) % 24])
42        # show the next frame
43        display.frame(frame, show=True)
44        if frame:
45            frame = 0
46        else:
47            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# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example to extract the frames and other parameters from an animated gif
 6and then run the animation on the display.
 7
 8Usage:
 9python3 is31fl3731_pillow_animated_gif.py animated.gif
10
11This example is for use on (Linux) computers that are using CPython with
12Adafruit Blinka to support CircuitPython libraries. CircuitPython does
13not support PIL/pillow (python imaging library)!
14
15Author(s): Melissa LeBlanc-Williams for Adafruit Industries
16"""
17
18import sys
19import board
20from PIL import Image
21
22# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
23# from adafruit_is31fl3731.matrix import Matrix as Display
24# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
25from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
26
27# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
28# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
29
30i2c = board.I2C()  # uses board.SCL and board.SDA
31# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
32
33display = Display(i2c)
34
35
36# Open the gif
37if len(sys.argv) < 2:
38    print("No image file specified")
39    print("Usage: python3 is31fl3731_pillow_animated_gif.py animated.gif")
40    sys.exit()
41
42image = Image.open(sys.argv[1])
43
44# Make sure it's animated
45if not image.is_animated:
46    print("Specified image is not animated")
47    sys.exit()
48
49# Get the autoplay information from the gif
50delay = image.info["duration"]
51
52# Figure out the correct loop count
53if "loop" in image.info:
54    loops = image.info["loop"]
55    if loops > 0:
56        loops += 1
57else:
58    loops = 1
59
60# IS31FL3731 only supports 0-7
61loops = min(loops, 7)
62
63# Get the frame count (maximum 8 frames)
64frame_count = min(image.n_frames, 8)
65
66# Load each frame of the gif onto the Matrix
67for frame in range(frame_count):
68    image.seek(frame)
69    frame_image = Image.new("L", (display.width, display.height))
70    frame_image.paste(
71        image.convert("L"),
72        (
73            display.width // 2 - image.width // 2,
74            display.height // 2 - image.height // 2,
75        ),
76    )
77    display.image(frame_image, frame=frame)
78
79display.autoplay(delay=delay, loops=loops)
examples/is31fl3731_pillow_marquee.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example to scroll some text as a marquee
 6
 7This example is for use on (Linux) computers that are using CPython with
 8Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 9not support PIL/pillow (python imaging library)!
10
11Author(s): Melissa LeBlanc-Williams for Adafruit Industries
12"""
13
14import board
15from PIL import Image, ImageDraw, ImageFont
16
17# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
18# from adafruit_is31fl3731.matrix import Matrix as Display
19# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
20from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
21
22# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
23# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
24
25SCROLLING_TEXT = "You can display a personal message here..."
26BRIGHTNESS = 64  # Brightness can be between 0-255
27
28i2c = board.I2C()  # uses board.SCL and board.SDA
29# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
30
31display = Display(i2c)
32
33# Load a font
34font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 8)
35
36# Create an image that contains the text
37text_width, text_height = font.getsize(SCROLLING_TEXT)
38text_image = Image.new("L", (text_width, text_height))
39text_draw = ImageDraw.Draw(text_image)
40text_draw.text((0, 0), SCROLLING_TEXT, font=font, fill=BRIGHTNESS)
41
42# Create an image for the display
43image = Image.new("L", (display.width, display.height))
44draw = ImageDraw.Draw(image)
45
46# Load the text in each frame
47while True:
48    for x in range(text_width + display.width):
49        draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0)
50        image.paste(
51            text_image, (display.width - x, display.height // 2 - text_height // 2 - 1)
52        )
53        display.image(image)
examples/is31fl3731_pillow_numbers.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example to utilize the Python Imaging Library (Pillow) and draw bitmapped text
 6to 8 frames and then run autoplay on those frames.
 7
 8This example is for use on (Linux) computers that are using CPython with
 9Adafruit Blinka to support CircuitPython libraries. CircuitPython does
10not support PIL/pillow (python imaging library)!
11
12Author(s): Melissa LeBlanc-Williams for Adafruit Industries
13"""
14
15import board
16from PIL import Image, ImageDraw, ImageFont
17
18# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
19# from adafruit_is31fl3731.matrix import Matrix as Display
20# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
21from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
22
23# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
24# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
25
26BRIGHTNESS = 32  # Brightness can be between 0-255
27
28i2c = board.I2C()  # uses board.SCL and board.SDA
29# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
30
31display = Display(i2c)
32
33display.fill(0)
34
35# 256 Color Grayscale Mode
36image = Image.new("L", (display.width, display.height))
37draw = ImageDraw.Draw(image)
38
39# Load a font in 2 different sizes.
40font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 10)
41
42# Load the text in each frame
43for x in range(8):
44    draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0)
45    draw.text((x + 1, -2), str(x + 1), font=font, fill=BRIGHTNESS)
46    display.image(image, frame=x)
47
48display.autoplay(delay=500)

Colorful Examples

Example that works on the RGB Led Shim.

examples/is31fl3731_ledshim_rainbow.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import busio
 7from adafruit_is31fl3731.led_shim import LedShim as Display
 8
 9i2c = busio.I2C(board.SCL, board.SDA)
10
11# initial display if you are using Pimoroni LED SHIM
12display = Display(i2c)
13
14# fmt: off
15# This list 28 colors from a rainbow...
16rainbow = [
17    (255, 0, 0), (255, 54, 0), (255, 109, 0), (255, 163, 0),
18    (255, 218, 0), (236, 255, 0), (182, 255, 0), (127, 255, 0),
19    (72, 255, 0), (18, 255, 0), (0, 255, 36), (0, 255, 91),
20    (0, 255, 145), (0, 255, 200), (0, 255, 255), (0, 200, 255),
21    (0, 145, 255), (0, 91, 255), (0, 36, 255), (18, 0, 255),
22    (72, 0, 255), (127, 0, 255), (182, 0, 255), (236, 0, 255),
23    (255, 0, 218), (255, 0, 163), (255, 0, 109), (255, 0, 54),
24]
25# fmt: on
26
27
28for y in range(3):
29    for x in range(28):
30        display.pixel(x, y, 255)
31        time.sleep(0.1)
32        display.pixel(x, y, 0)
33
34while True:
35    for offset in range(28):
36        for x in range(28):
37            r, g, b = rainbow[(x + offset) % 28]
38            display.pixelrgb(x, r, g, b)

Example that works on the RGB Matrix 5x5.

examples/is31fl3731_rgbmatrix5x5_rainbow.py
  1# SPDX-FileCopyrightText: 2021 Sandy Macdonald, David Glaude, James Carr
  2# SPDX-License-Identifier: MIT
  3
  4"""
  5Example to display a rainbow animation on the 5x5 RGB Matrix Breakout.
  6
  7Usage:
  8Rename this file code.py and pop it on your Raspberry Pico's
  9CIRCUITPY drive.
 10
 11This example is for use on the Pico Explorer Base or other board that use the same SDA/SCL pin.
 12
 13Author(s): Sandy Macdonald, David Glaude, James Carr
 14"""
 15
 16import time
 17import math
 18import busio
 19import board
 20
 21from adafruit_is31fl3731.rgbmatrix5x5 import RGBmatrix5x5 as Display
 22
 23
 24def hsv_to_rgb(hue, sat, val):
 25    # pylint: disable=too-many-return-statements
 26    """
 27    Convert HSV colour to RGB
 28
 29    :param hue: hue; 0.0-1.0
 30    :param sat: saturation; 0.0-1.0
 31    :param val: value; 0.0-1.0
 32    """
 33
 34    if sat == 0.0:
 35        return val, val, val
 36
 37    i = int(hue * 6.0)
 38
 39    p = val * (1.0 - sat)
 40    f = (hue * 6.0) - i
 41    q = val * (1.0 - sat * f)
 42    t = val * (1.0 - sat * (1.0 - f))
 43
 44    i %= 6
 45
 46    if i == 0:
 47        return val, t, p
 48    if i == 1:
 49        return q, val, p
 50    if i == 2:
 51        return p, val, t
 52    if i == 3:
 53        return p, q, val
 54    if i == 4:
 55        return t, p, val
 56    if i == 5:
 57        return val, p, q
 58
 59    # Will never reach here but it keeps pylint happier
 60    return val, val, val
 61
 62
 63# Create the I2C bus on a Pico Explorer Base
 64i2c = busio.I2C(board.GP5, board.GP4)
 65
 66# Set up 5x5 RGB matrix Breakout
 67display = Display(i2c)
 68
 69
 70def test_pixels(r, g, b):
 71    # Draw each row from left to right, top to bottom
 72    for y in range(0, 5):
 73        for x in range(0, 5):
 74            display.fill(0)  # Clear display
 75            display.pixelrgb(x, y, r, g, b)
 76            time.sleep(0.05)
 77
 78
 79def test_rows(r, g, b):
 80    # Draw full rows from top to bottom
 81    for y in range(0, 5):
 82        display.fill(0)  # Clear display
 83        for x in range(0, 5):
 84            display.pixelrgb(x, y, r, g, b)
 85        time.sleep(0.2)
 86
 87
 88def test_columns(r, g, b):
 89    # Draw full columns from left to right
 90    for x in range(0, 5):
 91        display.fill(0)  # Clear display
 92        for y in range(0, 5):
 93            display.pixelrgb(x, y, r, g, b)
 94        time.sleep(0.2)
 95
 96
 97def test_rainbow_sweep():
 98    step = 0
 99
100    for _ in range(100):
101        for y in range(0, 5):
102            for x in range(0, 5):
103                pixel_hue = (x + y + (step / 20)) / 8
104                pixel_hue = pixel_hue - int(pixel_hue)
105                pixel_hue += 0
106                pixel_hue = pixel_hue - math.floor(pixel_hue)
107
108                rgb = hsv_to_rgb(pixel_hue, 1, 1)
109
110                display.pixelrgb(
111                    x, y, int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
112                )
113
114        time.sleep(0.01)
115        step += 3
116
117
118while True:
119    test_pixels(64, 0, 0)  # RED
120    test_pixels(0, 64, 0)  # GREEN
121    test_pixels(0, 0, 64)  # BLUE
122    test_pixels(64, 64, 64)  # WHITE
123
124    test_rows(64, 0, 0)  # RED
125    test_rows(0, 64, 0)  # GREEN
126    test_rows(0, 0, 64)  # BLUE
127    test_rows(64, 64, 64)  # WHITE
128
129    test_columns(64, 0, 0)  # RED
130    test_columns(0, 64, 0)  # GREEN
131    test_columns(0, 0, 64)  # BLUE
132    test_columns(64, 64, 64)  # WHITE
133
134    test_rainbow_sweep()