Instantly share code, notes, and snippets.

@tdicola tdicola/neopixelw.py Secret
Created Sep 24, 2016

Embed
What would you like to do?
# NeoPixel driver for MicroPython on ESP8266
# MIT license; Copyright (c) 2016 Damien P. George
from esp import neopixel_write
class NeoPixelRGBW:
def __init__(self, pin, n):
self.pin = pin
self.n = n
self.buf = bytearray(n * 4)
self.pin.init(pin.OUT)
def __setitem__(self, index, val):
r, g, b, w = val
self.buf[index * 4] = g
self.buf[index * 4 + 1] = r
self.buf[index * 4 + 2] = b
self.buf[index * 4 + 3] = w
def __getitem__(self, index):
i = index * 4
return self.buf[i + 1], self.buf[i], self.buf[i + 2], self.buf[i + 3]
def fill(self, color):
r, g, b, w = color
for i in range(len(self.buf) / 4):
self.buf[i * 4] = g
self.buf[i * 4 + 1] = r
self.buf[i * 4 + 2] = b
self.buf[i * 4 + 3] = w
def write(self):
neopixel_write(self.pin, self.buf, True)
import machine
import math
import neopixelw
import time
PIXEL_WIDTH = 8
PIXEL_HEIGHT = 5
MAX_BRIGHT = 100.0
np = neopixelw.NeoPixelRGBW(machine.Pin(15), PIXEL_WIDTH*PIXEL_HEIGHT)
# Clear all the pixels and turn them off.
np.fill((0,0,0,0))
np.write()
while True:
np.fill((0,0,0,0))
current = time.ticks_ms() / 1000.0
for x in range(PIXEL_WIDTH):
for y in range(PIXEL_HEIGHT):
v = 0.0
v += math.sin(x+current)
v += math.sin(1.0*(x*math.sin(current/0.5)+y*math.cos(current/0.25))+current)
cx = x + 0.5*math.sin(current/5.0)
cy = y + 0.5*math.cos(current/3.0)
v += math.sin(math.sqrt((math.pow(cx, 2.0)+math.pow(cy, 2.0))+1.0)+current)
v = (v+3.0)/6.0
r = math.sin(v*math.pi)
g = math.sin(v*math.pi+2.0*math.pi/3.0)
b = math.sin(v*math.pi+4.0*math.pi/3.0)
#w = math.sin(v*math.pi+6.0*math.pi/3.0)
np[y*PIXEL_WIDTH+x] = (int(MAX_BRIGHT*r),int(MAX_BRIGHT*g),int(MAX_BRIGHT*b),0)
np.write()
@slzatz

This comment has been minimized.

Copy link

slzatz commented Oct 8, 2016

Don't you want those rgb values to the absolute values or am I missing something?

@SThomas1972

This comment has been minimized.

Copy link

SThomas1972 commented Apr 15, 2017

is there a way for getting this working with the neopixel module in Circuit Python 0.9.1 on the ESP8266.

np = neopixel.NeoPixel(board.GPIO15, PIXEL_WIDTH*PIXEL_HEIGHT)
Traceback (most recent call last):
File "", line 1, in
File "neopixel.py", line 15, in init
AttributeError: 'Pin' object has no attribute 'init'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment