musicmouse/espmusicmouse/host_driver/main.py

50 lines
1.8 KiB
Python

import asyncio
import serial_asyncio
from led_cmds import (ColorRGBW, ColorHSV, EffectStaticConfig,
EffectRandomTwoColorInterpolationConfig, EffectAlexaSwipeConfig,
EffectSwipeAndChange)
from host_driver import MusicMouseProtocol, RfidTokenRead
from player import AudioPlayer
from glob import glob
import os
MUSIC_FOLDER = "/home/martin/code/musicmouse/espmusicmouse/host_driver/music"
audio_player = AudioPlayer()
rfid_token_map = {
bytes.fromhex("0000000000"): "None",
bytes.fromhex("88041174e9"): "elefant",
bytes.fromhex("8804ce7230"): "fuchs",
bytes.fromhex("88040d71f0"): "eule",
}
def on_firmware_msg(protocol, message):
print("Got message", message)
if isinstance(message, RfidTokenRead) and message.id in rfid_token_map:
if rfid_token_map[message.id] == "None":
eff = EffectAlexaSwipeConfig()
eff.forward = False
print("Nothing")
protocol.send_message(eff)
audio_player.pause()
else:
eff = EffectSwipeAndChange()
eff.swipe.primary_color = ColorRGBW(1, 1, 0, 0)
protocol.send_message(eff)
figure = rfid_token_map[message.id]
glob_result = glob(os.path.join(MUSIC_FOLDER, figure, "*.mp3"))
audio_player.set_file(glob_result[0])
audio_player.play()
loop = asyncio.get_event_loop()
coro = serial_asyncio.create_serial_connection(loop,
MusicMouseProtocol,
'/dev/ttyUSB0',
baudrate=115200)
transport, protocol = loop.run_until_complete(coro)
protocol.register_message_callback(on_firmware_msg)
loop.run_forever()
loop.close()