Skip to content

Commit

Permalink
improve button responsiveness, beep again when long press is detected…
Browse files Browse the repository at this point in the history
… while button is still pressed
  • Loading branch information
vergoh committed Jan 4, 2024
1 parent 49368e0 commit 4f6a168
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/buttonpress_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
DEBOUNCE = 30

class button_async():
def __init__(self, buttonpin = None, long_press_duration_ms = 1000):
def __init__(self, buttonpin = None, long_press_duration_ms = 1000, buzzer = None):
self.pin = buttonpin
self.long_press_duration_ms = long_press_duration_ms
self._buzzer = buzzer
self._pressed = False
self._was_pressed = False
self._press_duration_ms = 0
Expand All @@ -25,13 +26,17 @@ async def run(self):

press_start_time_ms = time.ticks_ms()

await asyncio.sleep_ms(DEBOUNCE)
self._pressed = True
if self._buzzer is not None:
self._buzzer.buzz()

if self.pin.value() == 1:
continue
await asyncio.sleep_ms(DEBOUNCE)

self._pressed = True
long_press_buzzed = False
while self.pin.value() == 0:
if self._buzzer is not None and long_press_buzzed is False and time.ticks_diff(time.ticks_ms(), press_start_time_ms) >= self.long_press_duration_ms:
self._buzzer.buzz()
long_press_buzzed = True
await asyncio.sleep_ms(10)
self._pressed = False
self._press_duration_ms = time.ticks_diff(time.ticks_ms(), press_start_time_ms)
Expand Down
10 changes: 4 additions & 6 deletions src/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ def __init__(self):
if self.config['use_buzzer']:
self.buzzer = buzzer(Pin(self.config['pins']['buzzer'], Pin.OUT), frequency = self.config['buzzer_frequency'], duty = self.config['buzzer_duty'])
self.buzzer.buzz()
else:
self.buzzer = None

if not self.config['spotify'].get('client_id') or not self.config['spotify'].get('client_secret'):
self.oled.show(_app_name, "client not configured", separator = False)
raise RuntimeError("client_id and/or client_secret not configured")

self.button_playpause = button_async(Pin(self.config['pins']['button_playpause'], Pin.IN, Pin.PULL_UP), long_press_duration_ms = self.config['long_press_duration_milliseconds'])
self.button_next = button_async(Pin(self.config['pins']['button_next'], Pin.IN, Pin.PULL_UP), long_press_duration_ms = self.config['long_press_duration_milliseconds'])
self.button_playpause = button_async(Pin(self.config['pins']['button_playpause'], Pin.IN, Pin.PULL_UP), long_press_duration_ms = self.config['long_press_duration_milliseconds'], buzzer = self.buzzer)
self.button_next = button_async(Pin(self.config['pins']['button_next'], Pin.IN, Pin.PULL_UP), long_press_duration_ms = self.config['long_press_duration_milliseconds'], buzzer = self.buzzer)
print("buttons enabled")

if self.config['setup_network']:
Expand Down Expand Up @@ -165,8 +167,6 @@ def _handle_buttons(self, api_tokens, playing):

if self.button_playpause.was_pressed():
print("play/pause button pressed")
if self.config['use_buzzer']:
self.buzzer.buzz()
if playing:
if self.button_playpause.was_longpressed():
self.oled.show(_app_name, "saving track", separator = False)
Expand All @@ -184,8 +184,6 @@ def _handle_buttons(self, api_tokens, playing):

elif self.button_next.was_pressed():
print("next button pressed")
if self.config['use_buzzer']:
self.buzzer.buzz()
if playing:
if self.button_next.was_longpressed():
if self.pause_after_current:
Expand Down

0 comments on commit 4f6a168

Please sign in to comment.