Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with starting the workout second time #23

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def on_system_control(event: dict) -> None:
if sandglass_thread is not None and sandglass_thread.is_alive():
led_controller.stop_sandglass_thread()
sandglass_thread.join()
sandglass_thread = None

if event["led_toggle"] is True:
sandglass_thread = threading.Thread(
Expand Down
3 changes: 3 additions & 0 deletions app/const.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Constants for the Stair Challenge app."""
# ruff: noqa: E501
import threading
from typing import Final

MQTT_SENSOR: Final = "sensor"
MQTT_WORKOUT: Final = "workout"

THREAD_STOP_EVENT = threading.Event()

# MQTT topics - Subscribed
MQTT_TEST_TOPIC: Final = "test"
MQTT_TRIGGER_TOPIC: Final = f"{MQTT_SENSOR}/+/trigger"
Expand Down
16 changes: 11 additions & 5 deletions app/led_controller.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""LED strip module."""
import secrets
import threading
import time
from enum import Enum

from rpi_ws281x import Color, PixelStrip

thread_stop_event = threading.Event()
from app.const import THREAD_STOP_EVENT


class Colors:
Expand Down Expand Up @@ -282,6 +281,9 @@ def sandglass(self, duration: int, color: Color) -> None:
duration (int): Duration of the sandglass animation in seconds.
color (Color): Color object with RGB values
"""
# Reset the stop event
THREAD_STOP_EVENT.clear()

num_leds = self.strip.numPixels()
self.set_color(color)

Expand All @@ -294,9 +296,9 @@ def sandglass(self, duration: int, color: Color) -> None:
for i in range(num_leds):
remaining_time = duration - (i * interval_seconds)

if thread_stop_event.is_set():
if THREAD_STOP_EVENT.is_set():
print("Force stopping sandglass animation")
thread_stop_event.clear()
THREAD_STOP_EVENT.clear()
break

# Turn off the current LED
Expand All @@ -318,4 +320,8 @@ def turn_off(self) -> None:

def stop_sandglass_thread(self) -> None:
"""Stop the sandglass thread."""
thread_stop_event.set()
THREAD_STOP_EVENT.set()

def reset_stop_event(self) -> None:
"""Reset the stop event."""
THREAD_STOP_EVENT.clear()