diff --git a/Partition.py b/Partition.py deleted file mode 100644 index c54ec62..0000000 --- a/Partition.py +++ /dev/null @@ -1,34 +0,0 @@ -import asyncio, datetime -from typing import Callable - -from .Note import Note - -async def wait_until(dt): - # sleep until the specified datetime - now = datetime.datetime.now() - await asyncio.sleep((dt - now).total_seconds()) - -async def run_at(dt, coro): - await wait_until(dt) - return await coro - - -class Partition: - - def __init__(self, name:str, notes:list[Note]) -> None: - - self.__name = name - self.__notes = notes - - - async def play(self, output_lambda:Callable([str, tuple[int, int, int], int], None)): - now = datetime.datetime.now() - for note in self.__notes: - asyncio.create_task(run_at( - now + datetime.time(milliseconds = note.get_start_time()), - output_lambda( - note.get_key(), - note.get_color(), - note.get_duration() - ) - )) diff --git a/Note.py b/chroma_case/Note.py similarity index 87% rename from Note.py rename to chroma_case/Note.py index acca0d5..1d9e8dd 100644 --- a/Note.py +++ b/chroma_case/Note.py @@ -11,6 +11,9 @@ class Note: def get_key(self): return self.__key + def get_color(self): + return self.__color + def get_start_time(self): return self.__start_time diff --git a/chroma_case/Partition.py b/chroma_case/Partition.py new file mode 100644 index 0000000..02a2359 --- /dev/null +++ b/chroma_case/Partition.py @@ -0,0 +1,41 @@ +import asyncio, datetime +from typing import Callable + +from .Note import Note + +async def wait_until(dt): + # sleep until the specified datetime + now = datetime.datetime.now() + await asyncio.sleep((dt - now).total_seconds()) + +async def run_at(dt, coro): + await wait_until(dt) + return await coro + +class Partition: + + def __init__(self, name:str, notes:list[Note]) -> None: + + self.__name = name + self.__notes = notes + + + def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]): + now = datetime.datetime.now() + tasks_to_wait = [] + for note in self.__notes: + tasks_to_wait.append( + asyncio.create_task( + lambda: asyncio.wait( + run_at( + now + datetime.timedelta(milliseconds= note.get_start_time()), + output_lambda( + note.get_key(), + note.get_color(), + note.get_duration() + ) + ) + ) + ) + ) + asyncio.wait(tasks_to_wait) diff --git a/chroma_case/__init__.py b/chroma_case/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chroma_case/__pycache__/Note.cpython-39.pyc b/chroma_case/__pycache__/Note.cpython-39.pyc new file mode 100644 index 0000000..cec098b Binary files /dev/null and b/chroma_case/__pycache__/Note.cpython-39.pyc differ diff --git a/chroma_case/__pycache__/Partition.cpython-39.pyc b/chroma_case/__pycache__/Partition.cpython-39.pyc new file mode 100644 index 0000000..d87e69c Binary files /dev/null and b/chroma_case/__pycache__/Partition.cpython-39.pyc differ diff --git a/chroma_case/__pycache__/__init__.cpython-39.pyc b/chroma_case/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..22be7e9 Binary files /dev/null and b/chroma_case/__pycache__/__init__.cpython-39.pyc differ diff --git a/leds.py b/leds.py index 121f10e..858bd39 100755 --- a/leds.py +++ b/leds.py @@ -27,7 +27,6 @@ def playNote(color, secondsToStay, pixelsToFill): pixels[pixelIndex] = color time.sleep(secondsToStay) -def incomingNote(color, time, pixelsToFill): diff --git a/main.py b/main.py new file mode 100644 index 0000000..e4901e1 --- /dev/null +++ b/main.py @@ -0,0 +1,21 @@ +from chroma_case.Partition import Partition +from chroma_case.Note import Note +import asyncio +import sys + +def printing(key, color, duration): + print(f"key: {key}, c:{color} for {duration / 1000}s") + + +def main(): + n = Note("fa", (0, 255, 0), 1000, 1000) + + p = Partition("test", [n]) + + p.play(printing) + + return 0 + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..9dbc03c --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +import asyncio + +async def nested(): + return 42 + +async def main(): + # Schedule nested() to run soon concurrently + # with "main()". + task = asyncio.create_task(nested()) + + # "task" can now be used to cancel "nested()", or + # can simply be awaited to wait until it is complete: + await task + +asyncio.run(main()) \ No newline at end of file