Skip to content

Instantly share code, notes, and snippets.

@scoates
Created January 17, 2022 01:47
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 scoates/24963963763f52578a7987b15388725a to your computer and use it in GitHub Desktop.
Save scoates/24963963763f52578a7987b15388725a to your computer and use it in GitHub Desktop.
WIP of neopixel mocking
class MockNeoPixel:
SCALE = 16 # scale the imgcat (iTerm2) display by this much
def __init__(self, pin, num_pixels):
self.pin = pin
self.num_pixels = num_pixels
self.pixels = [(0, 0, 0) for i in range(self.num_pixels)]
self.config = {}
def __getitem__(self, k):
return self.pixels[k]
def __setitem__(self, k, v):
self.pixels[k] = v
def __len__(self):
return len(self.pixels)
def set_config(self, config):
self.config = config
def write(self):
print(f"writing… {len(self.pixels)} pixels")
if self.config:
from imgcat import imgcat
from PIL import Image
w = self.config["PIXELS_W"]
h = self.config["PIXELS_H"]
im = Image.new("RGB", (w, h))
# de-serpentine if serpentined
pixels = (
serpentine(self.pixels, w, h)
if self.config["SERPENTINE"]
else self.pixels
)
im.putdata([tuple(p) for p in pixels])
im = im.resize((w * self.SCALE, h * self.SCALE), Image.NEAREST)
imgcat(im)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment