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

Bookworm/Pi5 Compatibility: Upgrade to latest boilerplate, port to gpiod #36

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Switch PWM to use an Event.
  • Loading branch information
Gadgetoid committed Feb 26, 2025
commit 3ff4f65a619f9f11c9408b8c535a8d755593471c
11 changes: 5 additions & 6 deletions grow/pwm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from threading import Thread
from threading import Event, Thread

import gpiod
import gpiodevice
Expand All @@ -11,19 +11,18 @@
class PWM:
_pwms: list = []
_t_pwm: Thread = None
_pwm_running: bool = False
_t_pwm_event: Event = Event()

@staticmethod
def start_thread():
if PWM._t_pwm is None:
PWM._pwm_running = True
PWM._t_pwm = Thread(target=PWM._run)
PWM._t_pwm.start()

@staticmethod
def stop_thread():
if PWM._t_pwm is not None:
PWM._pwm_running = False
PWM._t_pwm_event.set()
PWM._t_pwm.join()
PWM._t_pwm = None

Expand All @@ -34,13 +33,13 @@ def _add(pwm):
@staticmethod
def _remove(pwm):
index = PWM._pwms.index(pwm)
del PWM._pwms[index]
PWM._pwms.pop(index)
if len(PWM._pwms) == 0:
PWM.stop_thread()

@staticmethod
def _run():
while PWM._pwm_running:
while not PWM._t_pwm_event.is_set():
PWM.run()

@staticmethod
Expand Down