Skip to content

Instantly share code, notes, and snippets.

@dglaude
Last active September 13, 2016 23:24
Show Gist options
  • Save dglaude/5683b7a21717e84518fe591b9a854bbc to your computer and use it in GitHub Desktop.
Save dglaude/5683b7a21717e84518fe591b9a854bbc to your computer and use it in GitHub Desktop.
Blinkt! on ESP8266 microPython

This document the portage of @pimoroni Blinkt! library to run on top of APA102 implementation in ESP8266 microPython

  • lib/colorsys.py only contain the function hsv_to_rgb(h, s, v) that is frequently used in Blinkt! examples.
  • lib/random.py only countain minimal set of integer random number into a range that is frequently used in Blinkt! examples.
  • lib/blinkt.py almost unmodified Blinkt! library ported to microPython
  • x
  • blinkt/my_rainbow.py
### Start of file: blinkt.py ###
from apa102 import APA102
from machine import Pin
DAT = 13
CLK = 14
NUM_PIXELS = 8
BRIGHTNESS = 7
# set GPIO14 to output to drive the clock
clock = Pin(CLK, Pin.OUT)
# set GPIO13 to output to drive the data
data = Pin(DAT, Pin.OUT)
# create APA102 driver on the clock and the data pin for 8 pixels
pixels = APA102(clock, data, NUM_PIXELS)
# set all pixel brightness value without touching the r,g,b
def set_brightness(brightness):
for x in range(NUM_PIXELS):
pixels[x] = (pixels[x][0], pixels[x][1], pixels[x][2], int(31.0 * brightness) & 0b11111)
set_brightness(BRIGHTNESS)
# set all pixel to 0,0,0 without touching the brightness
def clear():
for x in range(NUM_PIXELS):
pixels[x] = (0, 0, 0, pixels[x][3])
# write data to all pixels
def show():
pixels.write()
# set a pixel to a r,g,b value or r,g,b,brightness value
def set_pixel(x, r, g, b, brightness=None):
if brightness is None:
brightness = pixels[x][3]
else:
brightness = int(31.0 * brightness) & 0b11111
pixels[x] = [int(r) & 0xff, int(g) & 0xff, int(b) & 0xff, brightness]
### End of file blinkt.py ###
### Start of file: colorsys.py ###
# From colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h*6.0)
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f))
i = i%6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
### End of file colorsys.py ###
### Start of file: my_rainbow.py ###
import time
from blinkt import set_brightness, set_pixel, show
import colorsys
def clear():
for i in range(8):
set_pixel(i , 0, 0, 0)
show()
def main():
spacing = 360.0 / 16.0
hue = 0
set_brightness(0.1)
while True:
hue = int(time.ticks_ms() / 10) % 360
for x in range(8):
offset = x * spacing
h = ((hue + offset) % 360) / 360.0
r, g, b = [int(c*255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
set_pixel(x,r,g,b)
show()
time.sleep_ms(1)
### End of file my_rainbow.py ###
### Start of file: random.py ###
import os
def getrandbits(k):
numbytes = (k + 7) // 8
x = int.from_bytes(os.urandom(numbytes), 'big')
return x >> (numbytes * 8 - k)
def bit_length(n):
res = n
count = 1
while res>1:
res = res>>1
count = count+1
return count
def randbelow(n):
k = bit_length(n)
r = getrandbits(k)
while r >= n:
r = getrandbits(k)
return r
def randrange(start, stop=None, step=1):
istart = int(start)
if istart != start:
raise ValueError("non-integer arg 1 for randrange()")
if stop is None:
if istart > 0:
return randbelow(istart)
raise ValueError("empty range for randrange()")
# stop argument supplied.
istop = int(stop)
if istop != stop:
raise ValueError("non-integer stop for randrange()")
width = istop - istart
if step == 1 and width > 0:
return istart + randbelow(width)
if step == 1:
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
# Non-unit step argument supplied.
istep = int(step)
if istep != step:
raise ValueError("non-integer step for randrange()")
if istep > 0:
n = (width + istep - 1) // istep
elif istep < 0:
n = (width + istep + 1) // istep
else:
raise ValueError("zero step for randrange()")
if n <= 0:
raise ValueError("empty range for randrange()")
return istart + istep*randbelow(n)
def randint(a, b):
return randrange(a, b+1)
### End of file random.py ###
f=open('colorsys.py','w')
f.write('''
''')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment