Skip to content

Instantly share code, notes, and snippets.

@Neradoc
Last active May 1, 2021 18:43
Show Gist options
  • Save Neradoc/b10b28a356a50be9f920ae0f42611c43 to your computer and use it in GitHub Desktop.
Save Neradoc/b10b28a356a50be9f920ae0f42611c43 to your computer and use it in GitHub Desktop.
My first example to discover how to use tasko (with a while True task and a scheduled task)
"""
A 30 led Neopixel strip connected to GP20
2 potentiometers to change its look
See pins used and requirements in the code
"""
import board
import math
import time
from analogio import AnalogIn
from digitalio import DigitalInOut,Pull
from simpleio import map_range
from neopixel import NeoPixel
import tasko
from adafruit_debouncer import Debouncer
import adafruit_fancyled.adafruit_fancyled as fancy
# LED to blink
led = DigitalInOut(board.LED)
led.switch_to_output()
# potentiometer to read (between 3V and GND)
potar = AnalogIn(board.A0)
# brightness potentiometer (between 3V and GND)
brightness_pot = AnalogIn(board.A1)
# button to switch modes
pin = DigitalInOut(board.GP0)
pin.switch_to_input(Pull.UP)
switcher = Debouncer(pin)
# buttons to switch brightness
pin = DigitalInOut(board.GP3)
pin.switch_to_input(Pull.UP)
color_incr = Debouncer(pin)
pin = DigitalInOut(board.GP5)
pin.switch_to_input(Pull.UP)
color_decr = Debouncer(pin)
# neopixel strand to strandify
NUM_PIXELS = 30
pixels = NeoPixel(board.GP20, NUM_PIXELS, auto_write=False)
pixels.brightness = 0.05
# stabilize the value of the potar
MAXDIFF = 0.5
# defined colors
predef_colors = [
(fancy.CRGB(128,0,0), fancy.CRGB(0,255,0)),
(fancy.CRGB(0,64,64), fancy.CRGB(128,128,0)),
(fancy.CRGB(0,0,128), fancy.CRGB(128,0,128)),
]
current_def_color = 0
OFF_COLOR, ON_COLOR = predef_colors[2]
# color rules (3 exemples)
def color_dot(pixel_pos):
pixels.fill(OFF_COLOR.pack())
pixels[pixel_pos] = ON_COLOR.pack()
def color_gauge(pixel_pos):
for x in range(0,pixel_pos+1):
pixels[x] = ON_COLOR.pack()
for x in range(pixel_pos+1,NUM_PIXELS):
pixels[x] = OFF_COLOR.pack()
def color_gradient(pixel_pos):
for x in range(0,NUM_PIXELS):
mix = 1 / (1 + 0.5 * abs(pixel_pos - x)) ** 2
if mix < 0.1: mix = 0
print(mix)
color = fancy.mix(OFF_COLOR, ON_COLOR, mix)
pixels[x] = color.pack()
# list of color mode functions
color_modes = [color_gradient, color_gauge, color_dot, ]
# current color mode (that function will be called)
current_mode = color_modes[0]
# cycle through color modes according to the switcher button
mode_index = 0
async def switch_mode():
global current_mode, mode_index
switcher.update()
if switcher.rose:
mode_index = (mode_index + 1) % len(color_modes)
current_mode = color_modes[mode_index]
print("mode",current_mode)
refresh_potar()
# read the potentiometer and change the colors of the neopixels accordingly
prev_potar = -1000
cur_potar = 0
async def change_pix():
global prev_potar, cur_potar
potar_val = map_range(potar.value,0,65535,NUM_PIXELS,0)
delta = abs(prev_potar - potar_val)
if delta > MAXDIFF:
print(f"potar: {prev_potar} - {potar_val} = {delta}",end="")
prev_potar = potar_val
cur_potar = min(NUM_PIXELS-1,max(0,int(potar_val)))
print(f" -> {cur_potar}")
refresh_potar()
# display the pixels for the current value, with the current mode
def refresh_potar():
current_mode(cur_potar)
pixels.show()
# read the brightness potentiometer
async def brightness_pot_change():
# 0.22 = sqrt(0.05) -> 0.05 is the min value
potar_val = map_range(brightness_pot.value,0,65535,0.22,1)
pixels.brightness = potar_val ** 2
pixels.show()
# blink a LED at a speed based on the potentiometer
async def blinkit():
while True:
duration = map_range(potar.value,0,65535,0.05,0.5)
for x in range(4):
led.value = not led.value
await tasko.sleep(duration)
# await tasko.sleep(5 * duration)
# manage brightness
async def switch_color_schemes():
global current_def_color, OFF_COLOR, ON_COLOR
color_incr.update()
color_decr.update()
if color_incr.rose:
current_def_color = (current_def_color + 1) % len(predef_colors)
OFF_COLOR, ON_COLOR = predef_colors[current_def_color]
refresh_potar()
elif color_decr.rose:
current_def_color = (current_def_color - 1) % len(predef_colors)
OFF_COLOR, ON_COLOR = predef_colors[current_def_color]
refresh_potar()
# do all the scheduling
tasko.schedule(hz=10, coroutine_function=change_pix)
tasko.schedule(hz=20, coroutine_function=switch_mode)
tasko.schedule(hz=20, coroutine_function=switch_color_schemes)
tasko.schedule(hz=5, coroutine_function=brightness_pot_change)
tasko.add_task(blinkit())
tasko.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment