Add Jack-o'-Lantern code

This commit is contained in:
Phillip Burgess 2018-09-28 14:08:25 -07:00
parent d186d405c5
commit 93269f5a59
4 changed files with 137 additions and 0 deletions

View file

@ -0,0 +1,30 @@
// Jack-o-Lantern sketch for Adafruit Circuit Playground (Classic or Express)
#include "Adafruit_CircuitPlayground.h"
void setup() {
CircuitPlayground.begin();
CircuitPlayground.setBrightness(255); // LEDs full blast!
}
uint8_t prev = 128; // Start brightness in middle
void loop() {
uint8_t lvl = random(64, 192); // End brightness at 128±64
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
prev = lvl; // Assign end brightness to next start
}
void split(uint8_t y1, uint8_t y2, uint8_t offset) {
if(offset) { // Split further into sub-segments w/midpoint at ±offset
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
split(y1 , mid, offset / 2); // First segment (offset is halved)
split(mid, y2 , offset / 2); // Second segment (ditto)
} else { // No further subdivision - y1 determines LED brightness
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);
CircuitPlayground.strip.show();
delay(4);
}
}

View file

@ -0,0 +1,33 @@
// Jack-o-Lantern sketch for Adafruit Hallowing
#include "Adafruit_NeoPixel.h"
#define NUM_PIXELS 30
Adafruit_NeoPixel strip(NUM_PIXELS, 4, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
}
uint8_t prev = 128; // Start brightness in middle
void loop() {
uint8_t lvl = random(64, 192); // End brightness at 128±64
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
prev = lvl; // Assign end brightness to next start
}
void split(uint8_t y1, uint8_t y2, uint8_t offset) {
if(offset) { // Split further into sub-segments w/midpoint at ±offset
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
split(y1 , mid, offset / 2); // First segment (offset is halved)
split(mid, y2 , offset / 2); // Second segment (ditto)
} else { // No further subdivision - y1 determines LED brightness
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
for(uint8_t i=0; i<NUM_PIXELS; i++) strip.setPixelColor(i, c);
strip.show();
delay(4);
}
}

View file

@ -0,0 +1,37 @@
"""Jack-o'-Lantern flame example Adafruit Circuit Playground Express"""
import math
import board
import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:
import random
NUMPIX = 10 # Number of NeoPixels
PIXPIN = board.D8 # Pin where NeoPixels are connected
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
PREV = 128
def split(first, second, offset):
"""
Subdivide a brightness range, introducing a random offset in middle,
then call recursively with smaller offsets along the way.
@param1 first: Initial brightness value.
@param1 second: Ending brightness value.
@param1 offset: Midpoint offset range is +/- this amount max.
"""
if offset != 0:
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
offset = int(offset / 2)
split(first, mid, offset)
split(mid, second, offset)
else:
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
STRIP.fill((int(level), int(level / 8), int(level / 48)))
STRIP.write()
while True: # Loop forever...
LVL = random.randint(64, 191)
split(PREV, LVL, 32)
PREV = LVL

View file

@ -0,0 +1,37 @@
"""Jack-o'-Lantern flame example Adafruit Hallowing"""
import math
import board
import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:
import random
NUMPIX = 30 # Number of NeoPixels
PIXPIN = board.EXTERNAL_NEOPIXEL # Pin where NeoPixels are connected
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
PREV = 128
def split(first, second, offset):
"""
Subdivide a brightness range, introducing a random offset in middle,
then call recursively with smaller offsets along the way.
@param1 first: Initial brightness value.
@param1 second: Ending brightness value.
@param1 offset: Midpoint offset range is +/- this amount max.
"""
if offset != 0:
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
offset = int(offset / 2)
split(first, mid, offset)
split(mid, second, offset)
else:
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
STRIP.fill((int(level), int(level / 8), int(level / 48)))
STRIP.write()
while True: # Loop forever...
LVL = random.randint(64, 191)
split(PREV, LVL, 32)
PREV = LVL