Skip to content

Instantly share code, notes, and snippets.

@mattytrentini
Created July 29, 2018 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattytrentini/89fda73088770601f10cd42d210b2c01 to your computer and use it in GitHub Desktop.
Save mattytrentini/89fda73088770601f10cd42d210b2c01 to your computer and use it in GitHub Desktop.
neopixel_simulator.py
from itertools import chain
TOP = range(4)
TOP_LEFT = range(4,8)
TOP_RIGHT = range(8,12)
MIDDLE = range(12, 16)
BOTTOM_LEFT = range(16,20)
BOTTOM_RIGHT = range(20,24)
BOTTOM = range(24,28)
DOT = [29]
char_to_segment = {'1': chain(TOP_RIGHT, BOTTOM_RIGHT),
'2': chain(TOP, TOP_RIGHT, MIDDLE, BOTTOM_LEFT, BOTTOM),
'3': chain(TOP, TOP_RIGHT, MIDDLE, BOTTOM_LEFT, BOTTOM),
'4': chain(TOP_LEFT, TOP_RIGHT, MIDDLE, BOTTOM_RIGHT),
'5': chain(TOP, TOP_LEFT, MIDDLE, BOTTOM_RIGHT, BOTTOM),
'6': chain(TOP_LEFT, BOTTOM_LEFT, MIDDLE, BOTTOM_RIGHT, BOTTOM),
'7': chain(TOP, TOP_RIGHT, BOTTOM_RIGHT),
'8': chain(TOP, TOP_LEFT, TOP_RIGHT, MIDDLE, BOTTOM_LEFT, BOTTOM_RIGHT, BOTTOM),
'9': chain(TOP, TOP_LEFT, TOP_RIGHT, MIDDLE, BOTTOM_RIGHT),
'.': DOT}
class PrintNeoPixel():
def __init__(self, num_neopixels):
self.neo_values = [(0, 0, 0)] * num_neopixels
self.neo_length = num_neopixels
def __setitem__(self, idx, colour):
self.neo_values[idx] = colour
def write(self):
print(self.neo_values)
class Neo7Segment():
def __init__(self, neopixel, num_neos):
self.neopixel = neopixel
self.num_neos = num_neos
def set(self, display_string, colour):
''' Simple interface, single colour only '''
# assert if display_string is >len(self.nem_neos)
# for each character in display_string
# turn on neopixels corresponding to the character (lookup and set)
for index, c in enumerate(display_string):
for neo_index in char_to_segment[c]:
self.neopixel[neo_index + (29 * index)] = colour
self.neopixel.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment