adafruit_pixelmap

CircuitPython library for mapping multiple neopixels to behave as one for animations and other purposes.

  • Author(s): Kattni Rembor, Tim Cocks

Implementation Notes

Hardware:

Software and Dependencies:

class adafruit_pixelmap.PixelMap(strip, pixel_ranges, individual_pixels=False)

PixelMap lets you treat ranges of pixels as single pixels for animation purposes.

Parameters:
  • strip – An object that implements the Neopixel or Dotstar protocol.

  • pixel_ranges (iterable) – Pixel ranges (or individual pixels).

  • individual_pixels (bool) – Whether pixel_ranges are individual pixels.

To use with ranges of pixels:

import board
import neopixel
from adafruit_led_animation.helper import PixelMap
pixels = neopixel.NeoPixel(board.D6, 32, auto_write=False)

pixel_wing_horizontal = PixelMap(pixels, [(0, 8), (8, 16), (16, 24), (24, 32)])

pixel_wing_horizontal[0] = (255, 255, 0)
pixel_wing_horizontal.show()

To use with groups of individual pixels:

import board
import neopixel
from adafruit_led_animation.helper import PixelMap
pixels = neopixel.NeoPixel(board.D6, 32, auto_write=False)

pixel_wing_vertical = PixelMap(pixels, [
    (0, 8, 16, 24),
    (1, 9, 17, 25),
    (2, 10, 18, 26),
    (3, 11, 19, 27),
    (4, 12, 20, 28),
    (5, 13, 21, 29),
    (6, 14, 22, 30),
    (7, 15, 23, 31),
], individual_pixels=True)

pixel_wing_vertical[0] = (255, 255, 0)
pixel_wing_vertical.show()

To use with individual pixels:

import board
import neopixel
import time
from adafruit_led_animation.helper import PixelMap

pixels = neopixel.NeoPixel(board.D6, 8, auto_write=False)

pixel_map = PixelMap(pixels, [
    0, 7, 1, 6, 2, 5, 3, 4
], individual_pixels=True)

n = 0
while True:
    pixel_map[n] = AMBER
    pixel_map.show()
    n = n + 1
    if n > 7:
        n = 0
        pixel_map.fill(0)
    time.sleep(0.25)
property auto_write

auto_write from the underlying strip.

property brightness

brightness from the underlying strip.

fill(color)

Fill the used pixel ranges with color.

Parameters:

color – Color to fill all pixels referenced by this PixelMap definition with.

classmethod horizontal_lines(pixel_object, width, height, gridmap)

Generate a PixelMap of horizontal lines on a strip arranged in a grid.

Parameters:
  • pixel_object – pixel object

  • width – width of grid

  • height – height of grid

  • gridmap – a function to map x and y coordinates to the grid see vertical_strip_gridmap and horizontal_strip_gridmap

Returns:

PixelMap

Example: Horizontal lines on a 16x16 grid with the pixel rows oriented vertically,

alternating direction every row.

PixelMap.horizontal_lines(pixels, 16, 16, vertical_strip_gridmap(16))
show()

Shows the pixels on the underlying strip.

classmethod vertical_lines(pixel_object, width, height, gridmap)

Generate a PixelMap of horizontal lines on a strip arranged in a grid.

Parameters:
  • pixel_object – pixel object

  • width – width of grid

  • height – height of grid

  • gridmap – a function to map x and y coordinates to the grid see vertical_strip_gridmap and horizontal_strip_gridmap

Returns:

PixelMap

Example: Vertical lines on a 32x8 grid with the pixel rows oriented vertically,

alternating direction every row.

PixelMap.vertical_lines(pixels, 32, 8, vertical_strip_gridmap(8))
class adafruit_pixelmap.PixelSubset(pixel_object, start, end)

PixelSubset lets you work with a subset of a pixel object.

Parameters:
  • pixel_object – An object that implements the Neopixel or Dotstar protocol.

  • start (int) – Starting pixel number.

  • end (int) – Ending pixel number.

import board
import neopixel
from adafruit_led_animation.helper import PixelSubset
pixels = neopixel.NeoPixel(board.D12, 307, auto_write=False)

star_start = 260
star_arm = PixelSubset(pixels, star_start + 7, star_start + 15)
star_arm.fill((255, 0, 255))
pixels.show()
adafruit_pixelmap.horizontal_strip_gridmap(width, alternating=True)

Determines the pixel number for a grid with strips arranged horizontally.

Parameters:
  • width – grid width in pixels

  • alternating – Whether or not the lines in the grid run alternate directions in a zigzag

Returns:

mapper(x, y)

adafruit_pixelmap.vertical_strip_gridmap(height, alternating=True)

Returns a function that determines the pixel number for a grid with strips arranged vertically.

Parameters:
  • height – grid height in pixels

  • alternating – Whether or not the lines in the grid run alternate directions in a zigzag

Returns:

mapper(x, y)