Created
March 13, 2021 04:17
-
-
Save Neradoc/fbcaa7fe1504e20b0932bedbb8f957ba to your computer and use it in GitHub Desktop.
My exploration of naive (bad) async, and a tasko version, of a potentiometer controlling neopixels
This file contains hidden or 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 board | |
import time | |
from analogio import AnalogIn | |
from digitalio import DigitalInOut,Pull | |
from simpleio import map_range | |
from neopixel import NeoPixel | |
# LED to blink | |
led = DigitalInOut(board.LED) | |
led.switch_to_output() | |
# potentiometer to read | |
potar = AnalogIn(board.A0) | |
# neopixel strand to strandify | |
NUM_PIXELS = 30 | |
pixels = NeoPixel(board.GP20, NUM_PIXELS, auto_write=False) | |
# color rules (3 exemples) | |
def color_dot(pixel_pos): | |
pixels.fill((30,0,0)) | |
pixels[pixel_pos] = (0,255,0) | |
def color_gauge(pixel_pos): | |
for x in range(0,pixel_pos+1): | |
pixels[x] = (0,255,0) | |
for x in range(pixel_pos+1,NUM_PIXELS): | |
pixels[x] = (30,0,0) | |
def color_gradient(pixel_pos): | |
for x in range(0,NUM_PIXELS): | |
color = ( | |
min(255, abs(pixel_pos - x) * 64), | |
max(0, 255 - abs(pixel_pos - x) * 64), | |
0, | |
) | |
pixels[x] = color | |
# stabilize the value of the potar | |
MAXDIFF = 0.5 | |
prev_potar = -1000 | |
def change_pix(): | |
global prev_potar | |
potar_val = map_range(potar.value,0,65535,NUM_PIXELS,0) | |
delta = abs(prev_potar - potar_val) | |
if delta > MAXDIFF: | |
print(f"{prev_potar} - {potar_val} = {delta}",end="") | |
prev_potar = potar_val | |
pixel_pos = min(NUM_PIXELS-1,max(0,int(potar_val))) | |
print(f" -> {pixel_pos}") | |
color_gauge(pixel_pos) | |
pixels.show() | |
while True: | |
duration = map_range(potar.value,0,65535,0.05,0.5) | |
change_pix() | |
for x in range(4): | |
led.value = not led.value | |
for i in range(int(100*duration)): | |
change_pix() | |
time.sleep(0.01) | |
for i in range(int(100*duration)): | |
change_pix() | |
time.sleep(0.05) |
This file contains hidden or 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 board | |
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 | |
# LED to blink | |
led = DigitalInOut(board.LED) | |
led.switch_to_output() | |
# potentiometer to read (between potar_out and GND) | |
potar_out = DigitalInOut(board.GP21) | |
potar_out.switch_to_output() | |
potar_out.value = True | |
potar = AnalogIn(board.A0) | |
# button to switch modes | |
pin = DigitalInOut(board.GP0) | |
pin.switch_to_input(Pull.UP) | |
switcher = Debouncer(pin) | |
# neopixel strand to strandify | |
NUM_PIXELS = 30 | |
pixels = NeoPixel(board.GP20, NUM_PIXELS, auto_write=False) | |
# stabilize the value of the potar | |
MAXDIFF = 0.5 | |
# color rules (3 exemples) | |
def color_dot(pixel_pos): | |
pixels.fill((30,0,0)) | |
pixels[pixel_pos] = (0,200,0) | |
def color_gauge(pixel_pos): | |
for x in range(0,pixel_pos+1): | |
pixels[x] = (0,200,0) | |
for x in range(pixel_pos+1,NUM_PIXELS): | |
pixels[x] = (30,0,0) | |
def color_gradient(pixel_pos): | |
for x in range(0,NUM_PIXELS): | |
color = ( | |
min(100, abs(pixel_pos - x) * 20), | |
max(0, 200 - abs(pixel_pos - x) * 50), | |
0, | |
) | |
pixels[x] = color | |
# list of color mode functions | |
color_modes = [color_gradient, color_dot, color_gauge] | |
# current color mode (that function will be called) | |
current_mode = color_modes[0] | |
# signal that the color mode has changed (or any other reason to update) | |
refresh = False | |
# cycle through color modes according to the switcher button | |
mode_index = 0 | |
async def switch_mode(): | |
global current_mode, mode_index, refresh | |
switcher.update() | |
if switcher.rose: | |
mode_index = (mode_index + 1) % len(color_modes) | |
current_mode = color_modes[mode_index] | |
refresh = True | |
print("mode",current_mode) | |
# read the potentiometer and change the colors of the neopixels accordingly | |
prev_potar = -1000 | |
async def change_pix(): | |
global prev_potar, refresh | |
potar_val = map_range(potar.value,0,65535,NUM_PIXELS,0) | |
delta = abs(prev_potar - potar_val) | |
if delta > MAXDIFF or refresh: | |
refresh = False | |
print(f"{prev_potar} - {potar_val} = {delta}",end="") | |
prev_potar = potar_val | |
pixel_pos = min(NUM_PIXELS-1,max(0,int(potar_val))) | |
print(f" -> {pixel_pos}") | |
current_mode(pixel_pos) | |
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) | |
# do all the scheduling | |
tasko.schedule(hz=20, coroutine_function=change_pix) | |
tasko.schedule(hz=20, coroutine_function=switch_mode) | |
tasko.add_task(blinkit()) | |
tasko.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment