-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up button to toggle speaker state (#2)
* attempt to set up button * add type ignores to suppress warnings * ignore another type * add missing pin in controller and type ignore * ensure env file can be read and fix speaker turn on bug * improve logs for debugging, and use asyncio create_task instead of another run * style * logging with plug name * revert to asyncio.run * add debugging note * use singletons for controllers * add to requirements-rpi * better errors and asyncio loop management * fix loop management for repeated button presses * fix singleton bug, there should be one instance per ip-name plug * update readme * style readme * fix tests after refactoring * style * add citation
- Loading branch information
Showing
14 changed files
with
286 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
wheel | ||
RPi.GPIO | ||
rpi-lgpio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import asyncio | ||
import logging | ||
import RPi.GPIO as GPIO # type: ignore | ||
|
||
from src.controllers.singleton_base import SingletonMeta | ||
from src.controllers.smart_plug_controller import SmartPlugController | ||
|
||
|
||
class ButtonController(metaclass=SingletonMeta): | ||
def __init__( | ||
self, | ||
speakers_controller: SmartPlugController, | ||
mixer_controller: SmartPlugController, | ||
pin: int = 2, | ||
): | ||
self.speakers_controller = speakers_controller | ||
self.mixer_controller = mixer_controller | ||
self.pin = pin | ||
self.setup_gpio() | ||
|
||
def setup_gpio(self): | ||
GPIO.setmode(GPIO.BCM) | ||
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | ||
GPIO.add_event_detect( | ||
self.pin, GPIO.RISING, callback=self.button_callback, bouncetime=5000 | ||
) | ||
|
||
def button_callback(self, channel): | ||
"""Callback function that runs when the button is pressed""" | ||
logging.info("Button pressed.") | ||
|
||
# Create a new event loop for this thread | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
|
||
# Run the coroutine in the event loop | ||
loop.run_until_complete(self.toggle_speakers()) | ||
|
||
def read_button_state(self): | ||
logging.info(f"The button value is {GPIO.input(self.pin)}") | ||
print(f"For Shree, the button value is {GPIO.input(self.pin)}") | ||
|
||
async def toggle_speakers(self): | ||
"""Toggles the state of the speakers. | ||
Speakers must turn off before mixer, but mixer must turn on before speakers.""" | ||
try: | ||
is_on = await self.speakers_controller.is_on() | ||
|
||
if is_on: | ||
await self.speakers_controller.turn_off() | ||
logging.info("Speakers turned off manually.") | ||
else: | ||
await self.mixer_controller.turn_on() | ||
logging.info("Speakers turned on manually.") | ||
except Exception as e: | ||
logging.error("Unable to check state of speakers, so ignoring button press") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class SingletonMeta(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super().__call__(*args, **kwargs) | ||
return cls._instances[cls] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.