From 5be6a7d4f3764b5d04e88af28a0dc7b150893bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Fri, 10 Dec 2021 11:50:06 +0100 Subject: [PATCH] first push --- Note.py | 18 +++++++++++++++++ Partition.py | 34 +++++++++++++++++++++++++++++++ leds.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 Note.py create mode 100644 Partition.py create mode 100755 leds.py diff --git a/Note.py b/Note.py new file mode 100644 index 0000000..acca0d5 --- /dev/null +++ b/Note.py @@ -0,0 +1,18 @@ + + +class Note: + def __init__(self, key, color, start_time, duration) -> None: + + self.__key = key + self.__color = color + self.__start_time = start_time + self.__duration = duration + + def get_key(self): + return self.__key + + def get_start_time(self): + return self.__start_time + + def get_duration(self): + return self.__duration \ No newline at end of file diff --git a/Partition.py b/Partition.py new file mode 100644 index 0000000..c54ec62 --- /dev/null +++ b/Partition.py @@ -0,0 +1,34 @@ +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/leds.py b/leds.py new file mode 100755 index 0000000..121f10e --- /dev/null +++ b/leds.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 + +import board +import neopixel +import time +import sys +import asyncio + +colorToFill = (0, 0, 0) +pixels = neopixel.NeoPIxel(board.D18, 20, brightness=0.01) + +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':[]} + +def playNote(color, secondsToStay, pixelsToFill): + for pixelIndex in pixelsToFill: + pixels[pixelIndex] = color + time.sleep(secondsToStay) + +def incomingNote(color, time, pixelsToFill): + + + + +def launchMusic(noteList): + + pixels.fill(0,0,0) + pixels.write() + + for notes, tempo in noteList: + for note in notes: + playNote((255, 0, 0), tempo, notePixels[note.lower()]) + pixels.fill(colorToFill) + pixels.write() + +music = [ + (['sol'], 1), + (['sol'], 1), + (['sol'], 1), + (['re#'], 1), + (['la#'], 0.5), + (['sol'], 0.5), + (['re#'], 1), + (['la#'], 0.5), + ] + +launchMusic(music)