musicmouse/espmusicmouse/host_driver/main.py

102 lines
3.7 KiB
Python

import asyncio
import serial_asyncio
from led_cmds import (ColorRGBW, ColorHSV, EffectStaticConfig,
EffectRandomTwoColorInterpolationConfig, EffectAlexaSwipeConfig,
EffectSwipeAndChange)
from host_driver import MusicMouseProtocol, RfidTokenRead, RotaryEncoderEvent, ButtonEvent
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",
bytes.fromhex("88043c6ede"): "omnom",
bytes.fromhex("88040b78ff"): "eichhoernchen",
bytes.fromhex("8804bc7444"): "hund",
}
playlists = {
fig: audio_player.create_playlist(sorted(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.led_ring_effect(EffectStaticConfig(color, start, end))
def on_music_end_callback(protocol):
eff = EffectAlexaSwipeConfig()
eff.forward = False
protocol.led_ring_effect(eff)
protocol.mouse_led_effect(EffectStaticConfig(ColorRGBW(0, 0, 0, 0)))
def on_firmware_msg(protocol: MusicMouseProtocol, 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.led_ring_effect(eff)
protocol.mouse_led_effect(EffectStaticConfig(ColorRGBW(0, 0, 0, 0)))
audio_player.pause()
elif 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.led_ring_effect(eff)
protocol.mouse_led_effect(
EffectStaticConfig(ColorRGBW(18 / 255 / 2, 156 / 255 / 2, 140 / 255 / 2, 0)))
print(figure)
if figure in playlists:
audio_player.set_playlist(playlists[figure])
audio_player.play_from_start()
elif isinstance(message, RotaryEncoderEvent):
if audio_player.is_playing():
if message.direction == 2:
audio_player.change_volume(2)
elif message.direction == 1:
audio_player.change_volume(-2)
elif isinstance(message, ButtonEvent):
if message.button == "left" and message.event == "pressed":
audio_player.previous()
elif message.button == "right" and message.event == "pressed":
audio_player.next()
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()