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
5
6import board
7import busio
8
9# uncomment next line if you are using Feather CharlieWing LED 15 x 7
10from adafruit_is31fl3731.charlie_wing import CharlieWing as Display
11
12# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
13# from adafruit_is31fl3731.matrix import Matrix as Display
14# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
15# from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
16# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
17# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
18# uncomment next line if you are using Pimoroni 11x7 LED Matrix Breakout
19# from adafruit_is31fl3731.matrix_11x7 import Matrix11x7 as Display
20
21# uncomment this line if you use a Pico, here with SCL=GP21 and SDA=GP20.
22# i2c = busio.I2C(board.GP21, board.GP20)
23
24i2c = busio.I2C(board.SCL, board.SDA)
25
26# arrow pattern in bits; top row-> bottom row, 8 bits in each row
27arrow = bytearray((0x08, 0x0C, 0xFE, 0xFF, 0xFE, 0x0C, 0x08, 0x00, 0x00))
28
29display = Display(i2c)
30
31# first load the frame with the arrows; moves the arrow to the right in each
32# frame
33display.sleep(True) # turn display off while frames are updated
34for frame in range(display.width - 8):
35 display.frame(frame, show=False)
36 display.fill(0)
37 for y in range(display.height):
38 row = arrow[y]
39 for x in range(8):
40 bit = 1 << (7 - x) & row
41 # display the pixel into selected frame with varying intensity
42 if bit:
43 display.pixel(x + frame, y, frame**2 + 1)
44display.sleep(False)
45# now tell the display to show the frame one at time
46while True:
47 for frame in range(8):
48 display.frame(frame)
49 time.sleep(0.1)
examples/is31fl3731_text_example.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import adafruit_framebuf
5import board
6import busio
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(buf, display.width, display.height, adafruit_framebuf.MVLSB)
32
33
34frame = 0 # start with frame 0
35while True:
36 for i in range(len(text_to_show) * 9):
37 fb.fill(0)
38 fb.text(text_to_show, -i + display.width, 0, color=1)
39
40 # to improve the display flicker we can use two frame
41 # fill the next frame with scrolling text, then
42 # show it.
43 display.frame(frame, show=False)
44 # turn all LEDs off
45 display.fill(0)
46 for x in range(display.width):
47 # using the FrameBuffer text result
48 bite = buf[x]
49 for y in range(display.height):
50 bit = 1 << y & bite
51 # if bit > 0 then set the pixel brightness
52 if bit:
53 display.pixel(x, y, 50)
54
55 # now that the frame is filled, show it.
56 display.frame(frame, show=True)
57 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
19
20import board
21from PIL import Image
22
23# uncomment next line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
24# from adafruit_is31fl3731.matrix import Matrix as Display
25# uncomment next line if you are using Adafruit 16x8 Charlieplexed Bonnet
26from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display
27
28# uncomment next line if you are using Pimoroni Scroll Phat HD LED 17 x 7
29# from adafruit_is31fl3731.scroll_phat_hd import ScrollPhatHD as Display
30
31i2c = board.I2C() # uses board.SCL and board.SDA
32# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
33
34display = Display(i2c)
35
36
37# Open the gif
38if len(sys.argv) < 2:
39 print("No image file specified")
40 print("Usage: python3 is31fl3731_pillow_animated_gif.py animated.gif")
41 sys.exit()
42
43image = Image.open(sys.argv[1])
44
45# Make sure it's animated
46if not image.is_animated:
47 print("Specified image is not animated")
48 sys.exit()
49
50# Get the autoplay information from the gif
51delay = image.info["duration"]
52
53# Figure out the correct loop count
54if "loop" in image.info:
55 loops = image.info["loop"]
56 if loops > 0:
57 loops += 1
58else:
59 loops = 1
60
61# IS31FL3731 only supports 0-7
62loops = min(loops, 7)
63
64# Get the frame count (maximum 8 frames)
65frame_count = min(image.n_frames, 8)
66
67# Load each frame of the gif onto the Matrix
68for frame in range(frame_count):
69 image.seek(frame)
70 frame_image = Image.new("L", (display.width, display.height))
71 frame_image.paste(
72 image.convert("L"),
73 (
74 display.width // 2 - image.width // 2,
75 display.height // 2 - image.height // 2,
76 ),
77 )
78 display.image(frame_image, frame=frame)
79
80display.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(text_image, (display.width - x, display.height // 2 - text_height // 2 - 1))
51 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
5
6import board
7import busio
8
9from adafruit_is31fl3731.led_shim import LedShim as Display
10
11i2c = busio.I2C(board.SCL, board.SDA)
12
13# initial display if you are using Pimoroni LED SHIM
14display = Display(i2c)
15
16# fmt: off
17# This list 28 colors from a rainbow...
18rainbow = [
19 (255, 0, 0), (255, 54, 0), (255, 109, 0), (255, 163, 0),
20 (255, 218, 0), (236, 255, 0), (182, 255, 0), (127, 255, 0),
21 (72, 255, 0), (18, 255, 0), (0, 255, 36), (0, 255, 91),
22 (0, 255, 145), (0, 255, 200), (0, 255, 255), (0, 200, 255),
23 (0, 145, 255), (0, 91, 255), (0, 36, 255), (18, 0, 255),
24 (72, 0, 255), (127, 0, 255), (182, 0, 255), (236, 0, 255),
25 (255, 0, 218), (255, 0, 163), (255, 0, 109), (255, 0, 54),
26]
27# fmt: on
28
29
30for y in range(3):
31 for x in range(28):
32 display.pixel(x, y, 255)
33 time.sleep(0.1)
34 display.pixel(x, y, 0)
35
36while True:
37 for offset in range(28):
38 for x in range(28):
39 r, g, b = rainbow[(x + offset) % 28]
40 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 math
17import time
18
19import board
20import busio
21
22from adafruit_is31fl3731.rgbmatrix5x5 import RGBmatrix5x5 as Display
23
24
25def hsv_to_rgb( # noqa: PLR0911 Too many return statements
26 hue, sat, val
27):
28 """
29 Convert HSV colour to RGB
30
31 :param hue: hue; 0.0-1.0
32 :param sat: saturation; 0.0-1.0
33 :param val: value; 0.0-1.0
34 """
35
36 if sat == 0.0:
37 return val, val, val
38
39 i = int(hue * 6.0)
40
41 p = val * (1.0 - sat)
42 f = (hue * 6.0) - i
43 q = val * (1.0 - sat * f)
44 t = val * (1.0 - sat * (1.0 - f))
45
46 i %= 6
47
48 if i == 0:
49 return val, t, p
50 if i == 1:
51 return q, val, p
52 if i == 2:
53 return p, val, t
54 if i == 3:
55 return p, q, val
56 if i == 4:
57 return t, p, val
58 if i == 5:
59 return val, p, q
60
61
62# Create the I2C bus on a Pico Explorer Base
63i2c = busio.I2C(board.GP5, board.GP4)
64
65# Set up 5x5 RGB matrix Breakout
66display = Display(i2c)
67
68
69def test_pixels(r, g, b):
70 # Draw each row from left to right, top to bottom
71 for y in range(0, 5):
72 for x in range(0, 5):
73 display.fill(0) # Clear display
74 display.pixelrgb(x, y, r, g, b)
75 time.sleep(0.05)
76
77
78def test_rows(r, g, b):
79 # Draw full rows from top to bottom
80 for y in range(0, 5):
81 display.fill(0) # Clear display
82 for x in range(0, 5):
83 display.pixelrgb(x, y, r, g, b)
84 time.sleep(0.2)
85
86
87def test_columns(r, g, b):
88 # Draw full columns from left to right
89 for x in range(0, 5):
90 display.fill(0) # Clear display
91 for y in range(0, 5):
92 display.pixelrgb(x, y, r, g, b)
93 time.sleep(0.2)
94
95
96def test_rainbow_sweep():
97 step = 0
98
99 for _ in range(100):
100 for y in range(0, 5):
101 for x in range(0, 5):
102 pixel_hue = (x + y + (step / 20)) / 8
103 pixel_hue = pixel_hue - int(pixel_hue)
104 pixel_hue += 0
105 pixel_hue = pixel_hue - math.floor(pixel_hue)
106
107 rgb = hsv_to_rgb(pixel_hue, 1, 1)
108
109 display.pixelrgb(x, y, int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255))
110
111 time.sleep(0.01)
112 step += 3
113
114
115while True:
116 test_pixels(64, 0, 0) # RED
117 test_pixels(0, 64, 0) # GREEN
118 test_pixels(0, 0, 64) # BLUE
119 test_pixels(64, 64, 64) # WHITE
120
121 test_rows(64, 0, 0) # RED
122 test_rows(0, 64, 0) # GREEN
123 test_rows(0, 0, 64) # BLUE
124 test_rows(64, 64, 64) # WHITE
125
126 test_columns(64, 0, 0) # RED
127 test_columns(0, 64, 0) # GREEN
128 test_columns(0, 0, 64) # BLUE
129 test_columns(64, 64, 64) # WHITE
130
131 test_rainbow_sweep()