first led test with asyncio

This commit is contained in:
Clément Le Bihan
2021-12-10 14:19:01 +01:00
parent 8e7301c280
commit 5ca48b6655
5 changed files with 50 additions and 26 deletions

View File

@@ -1,21 +1,13 @@
class Note:
def __init__(self, key, color, start_time, duration) -> None:
def __init__(self, start_time, data) -> None:
self.__key = key
self.__color = color
self.__start_time = start_time
self.__duration = duration
def get_key(self):
return self.__key
def get_color(self):
return self.__color
self.__data = data
def get_start_time(self):
return self.__start_time
def get_duration(self):
return self.__duration
def get_data(self):
return self.__data

View File

@@ -20,7 +20,7 @@ class Partition:
self.__notes = notes
async def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]):
async def play(self, output_lambda:Callable[[object], None]):
now = datetime.datetime.now()
tasks_to_wait = []
for note in self.__notes:
@@ -28,11 +28,7 @@ class Partition:
asyncio.create_task(
run_at(
now + datetime.timedelta(milliseconds= note.get_start_time()),
output_lambda(
note.get_key(),
note.get_color(),
note.get_duration()
)
output_lambda(note.get_data())
)
)
)

52
main.py
View File

@@ -3,22 +3,58 @@ from chroma_case.Note import Note
import asyncio
import sys
async def printing(key, color, duration):
print(f"key: {key}, c:{color} for {duration / 1000}s")
await asyncio.sleep(duration / 1000)
print(f"end of {key}")
import board, neopixel
pixels = neopixel.NeoPIxel(board.D18, 20, brightness=0.1)
notePixels = { 'si': [0, 1],
'la#': [2, 3],
'la': [4, 5],
'sol#':[6],
'sol':[7, 8, 9],
'fa#':[10],
'fa':[11, 12, 13],
'mi':[14, 15, 16],
're#':[17],
're':[18, 19],
'do#':[],
'do':[]}
async def to_chroma_case(data):
global pixels
colored_pixels = notePixels[data["key"].lower()]
for pixelId in colored_pixels:
pixels[pixelId] = data["color"]
await asyncio.sleep(data['duration'] / 1000)
for pixelId in colored_pixels:
pixels[pixelId] = 0
async def printing(data):
print(f"key: {data['key']}, c:{data['color']} for {data['duration'] / 1000}s")
await asyncio.sleep(data['duration'] / 1000)
print(f"end of {data['key']}")
async def main():
n = Note("fa", (0, 255, 0), 1000, 7000)
n = Note(1000, {
"duration": 1500,
"color": (0, 255, 0),
"key": "fa",
})
p = Partition("test",
[n,
Note("fa#", (234, 255, 0), 3000, 2000)]
[
n,
Note(1500, {
"duration": 2000,
"color": (255, 0, 0),
"key": "fa lo",
})
]
)
await p.play(printing)
await p.play(to_chroma_case)
return 0