Skip to content

Commit

Permalink
Use polling
Browse files Browse the repository at this point in the history
  • Loading branch information
hifiberry committed May 22, 2024
1 parent 42a19c6 commit 0ca8361
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions ac2/plugins/control/powercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import time
import os
from typing import Dict
from datetime import timedelta

from smbus import SMBus
import gpiod
from gpiod.line import Edge
import select

from ac2.constants import STATE_PLAYING, STATE_UNDEF
from ac2.plugins.control.controller import Controller
Expand Down Expand Up @@ -182,8 +184,6 @@ def shutdown(self):
os.system("systemctl poweroff")

def interrupt_callback(self):
logging.info("Received interrupt")

try:
rotary_change = twos_comp(self.bus.read_byte_data(ADDRESS, REG_ROTARYCHANGE), 8) # this is a signed byte
button_state = self.bus.read_byte_data(ADDRESS, REG_BUTTONSTATE)
Expand Down Expand Up @@ -211,12 +211,20 @@ def run(self):
try:
with gpiod.request_lines(
self.chippath,
consumer="watch-line-rising",
config={self.intpinpi:
gpiod.LineSettings(edge_detection=Edge.BOTH)},
consumer="async-watch-line-value",
config={
self.intpinpi: gpiod.LineSettings(
edge_detection=Edge.BOTH,
debounce_period=timedelta(milliseconds=10),
)
},
) as request:
while not self.finished:
# Blocks until at least one event is available
poll = select.poll()
poll.register(request.fd, select.POLLIN)
while True:
# Other fds could be registered with the poll and be handled
# separately using the return value (fd, event) from poll()
poll.poll()
for event in request.read_edge_events():
self.interrupt_callback()
except Exception as e:
Expand Down

0 comments on commit 0ca8361

Please sign in to comment.