REFACTOR: Create separate sound module

This commit is contained in:
Dominique Barton 2019-03-24 21:44:39 +01:00
parent ff2993dd70
commit 24ae8d42eb
3 changed files with 27 additions and 13 deletions

22
mopidy_pummeluff/sound.py Normal file
View file

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
'''
Python module to play sounds.
'''
from __future__ import absolute_import, unicode_literals, print_function
__all__ = (
'play_sound',
)
from os import path, system
def play_sound(sound):
'''
Play sound via aplay.
:param str sound: The name of the sound file
'''
file_path = path.join(path.dirname(__file__), 'sounds', sound)
system('aplay -q {}'.format(file_path))

View file

@ -9,7 +9,6 @@ __all__ = (
'CardReader',
)
from os import path, system
from threading import Thread
from time import time
from logging import getLogger
@ -18,6 +17,7 @@ import RPi.GPIO as GPIO
from pirc522 import RFID
from mopidy_pummeluff.cards import Card
from mopidy_pummeluff.sound import play_sound
LOGGER = getLogger(__name__)
@ -40,16 +40,6 @@ class CardReader(Thread):
daemon = True
latest = None
@staticmethod
def play_sound(sound):
'''
Play sound via aplay.
:param str sound: The name of the sound file
'''
file_path = path.join(path.dirname(__file__), 'sounds', sound)
system('aplay -q {}'.format(file_path))
def __init__(self, core, stop_event):
'''
Class constructor.
@ -119,12 +109,12 @@ class CardReader(Thread):
if card.registered:
LOGGER.info('Triggering action of registered card')
self.play_sound('success.wav')
play_sound('success.wav')
card(self.core)
else:
LOGGER.info('Card is not registered, thus doing nothing')
self.play_sound('fail.wav')
play_sound('fail.wav')
card.scanned = time()
CardReader.latest = card

View file

@ -15,6 +15,7 @@ from logging import getLogger
import RPi.GPIO as GPIO
from mopidy_pummeluff.actions import shutdown, play_pause
from mopidy_pummeluff.sound import play_sound
LOGGER = getLogger(__name__)
@ -62,6 +63,7 @@ class GPIOHandler(Thread):
GPIO.setmode(GPIO.BOARD)
def callback(pin): # pylint: disable=missing-docstring
play_sound('success.wav')
self.button_pins[pin](self.core)
for pin in self.button_pins.values():