Last active
November 3, 2021 03:22
-
-
Save scoates/2ed49a9984999b9fa33fbf406b4d5535 to your computer and use it in GitHub Desktop.
PNG to FastLED bitmap array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SERPENTINE=1 pipenv run python img2c.py ~/Desktop/star.png > src/bitmap.h | |
# star.png is a 16x16 PNG file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
from PIL import Image | |
im = Image.open(sys.argv[1]) | |
# pass SERPENTINE=1 in env to get the matrix to "zigzag" where rows are connected at the ends | |
serpentine = os.environ.get("SERPENTINE") == "1" | |
pixels = list(im.getdata()) | |
if serpentine: | |
w = im.width | |
h = im.height | |
for row in range(1, h, 2): | |
pixels[row * w : (row * w) + w] = pixels[ | |
((row * w) + w) - 1 : (row * w) - 1 : -1 | |
] | |
print("#include <FastLED.h>") | |
print(f"CRGB bitmap[{len(pixels)}] = {'{'}") | |
for i, c in enumerate(pixels): | |
print(f" CRGB({c[0]}, {c[1]}, {c[2]}){',' if i < len(pixels) - 1 else ''}") | |
print("};") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
#include "bitmap.h" | |
#define NUM_LEDS 256 | |
#define LED_PIN 25 | |
CRGB leds[NUM_LEDS]; | |
void setup() { | |
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); | |
FastLED.setBrightness(7); | |
Serial.begin(57600); | |
FastLED.clear(true); | |
} | |
void loop() { | |
EVERY_N_MILLISECONDS(100) { | |
memcpy(&leds[0], &bitmap[0], NUM_LEDS * sizeof(CRGB)); | |
FastLED.show(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment