82 lines
2.7 KiB
Python
82 lines
2.7 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("88041174e9"): "elefant",
|
|
bytes.fromhex("8804ce7230"): "fuchs",
|
|
bytes.fromhex("88040d71f0"): "eule",
|
|
}
|
|
|
|
playlists = {
|
|
fig: audio_player.create_playlist(glob(os.path.join(MUSIC_FOLDER, fig, "*.mp3")))
|
|
for fig in rfid_token_map.values()
|
|
}
|
|
|
|
mouse_leds = {
|
|
'right_foot': (0, 6),
|
|
'left_foot': (6, 6 + 6),
|
|
'left_ear': (6 + 6, 6 + 6 + 16),
|
|
'right_ear': (6 + 6 + 16, 6 + 6 + 16 + 17),
|
|
}
|
|
|
|
|
|
def set_mouse_leds(protocol, position: str, color: ColorRGBW):
|
|
start, end = mouse_leds[position]
|
|
protocol.send_message(EffectStaticConfig(color, start, end))
|
|
|
|
|
|
def on_music_end_callback(protocol):
|
|
eff = EffectAlexaSwipeConfig()
|
|
eff.forward = False
|
|
protocol.send_message(eff)
|
|
|
|
|
|
def on_firmware_msg(protocol, message):
|
|
print("Got message", message)
|
|
if isinstance(message, RfidTokenRead) and message.id == bytes.fromhex("0000000000"):
|
|
if audio_player.is_playing():
|
|
eff = EffectAlexaSwipeConfig()
|
|
eff.forward = False
|
|
else:
|
|
eff = EffectStaticConfig(ColorRGBW(0, 0, 0, 0))
|
|
protocol.send_message(eff)
|
|
audio_player.pause()
|
|
if isinstance(message, RfidTokenRead) and message.id in rfid_token_map:
|
|
eff = EffectSwipeAndChange()
|
|
eff.swipe.primary_color = ColorRGBW(1, 1, 0, 0)
|
|
figure = rfid_token_map[message.id]
|
|
|
|
if figure == "eule":
|
|
eff.swipe.primary_color = ColorRGBW(0.96, 0.7, 0, 0)
|
|
eff.swipe.secondary_color = ColorRGBW(0.96, 0.7, 0, 0)
|
|
eff.swipe.swipe_speed = 180
|
|
eff.swipe.bell_curve_width_in_leds = 6
|
|
|
|
protocol.send_message(eff)
|
|
audio_player.set_playlist(playlists[figure])
|
|
audio_player.play_from_start()
|
|
|
|
|
|
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)
|
|
|
|
audio_player.on_playlist_end_callback = lambda: on_music_end_callback(protocol)
|
|
|
|
loop.run_forever()
|
|
loop.close() |