Beginning of cooking cycle
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
from machine import Pin, PWM
|
||||
from machine import Pin, PWM, Timer
|
||||
import time
|
||||
|
||||
class RGBLED:
|
||||
"""
|
||||
MicroPython driver for 4-pin RGB LEDs on ESP32 / Heltec boards.
|
||||
Supports state tracking, color setting, brightness scaling, and state toggling.
|
||||
Supports state tracking, color setting, brightness scaling,
|
||||
state toggling, and non-blocking blinking via machine.Timer.
|
||||
"""
|
||||
|
||||
# Preset RGB tuples for quick use
|
||||
RED = (255, 0, 0)
|
||||
GREEN = (0, 255, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
YELLOW = (255, 150, 0)
|
||||
YELLOW = (255, 120, 0)
|
||||
WHITE_YELLOW = (150, 30, 0)
|
||||
ORANGE = (255, 50, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
OFF = (0, 0, 0)
|
||||
|
||||
def __init__(self, red_pin, green_pin, blue_pin, common_anode=False, freq=1000):
|
||||
def __init__(self, red_pin, green_pin, blue_pin, common_anode=False, freq=1000, timer_id=1):
|
||||
"""
|
||||
:param red_pin: GPIO pin number for Red channel
|
||||
:param green_pin: GPIO pin number for Green channel
|
||||
:param blue_pin: GPIO pin number for Blue channel
|
||||
:param common_anode: Set True if cathode is connected to 3.3V instead of GND
|
||||
:param freq: PWM frequency in Hz (default 1000Hz)
|
||||
:param timer_id: Hardware/software timer ID for non-blocking blinks (-1 uses soft timers on ESP32).
|
||||
"""
|
||||
self._r_pwm = PWM(Pin(red_pin, Pin.OUT), freq=freq)
|
||||
self._g_pwm = PWM(Pin(green_pin, Pin.OUT), freq=freq)
|
||||
@@ -34,6 +37,10 @@ class RGBLED:
|
||||
self._brightness = 1.0 # Brightness factor [0.0 to 1.0]
|
||||
self._is_on = True # Master power state
|
||||
|
||||
# Blink state variables
|
||||
self._timer = Timer(timer_id)
|
||||
self._is_blinking = False
|
||||
|
||||
self._apply()
|
||||
|
||||
def _apply(self):
|
||||
@@ -88,7 +95,11 @@ class RGBLED:
|
||||
"""Returns True if the LED is currently powered on."""
|
||||
return self._is_on
|
||||
|
||||
# --- Helper Methods ---
|
||||
@property
|
||||
def is_blinking(self):
|
||||
return self._is_blinking
|
||||
|
||||
# --- Basic Control Methods ---
|
||||
|
||||
def set_rgb(self, r, g, b):
|
||||
"""Alternative setter for individual R, G, B integer values."""
|
||||
@@ -109,8 +120,40 @@ class RGBLED:
|
||||
self._is_on = not self._is_on
|
||||
self._apply()
|
||||
|
||||
# --- Non-Blocking Blinking Methods ---
|
||||
|
||||
def _timer_callback(self, t):
|
||||
"""Internal callback executed by machine.Timer."""
|
||||
self.toggle()
|
||||
|
||||
def blink_on(self, interval_ms=500):
|
||||
"""Starts background blinking at the specified interval in milliseconds."""
|
||||
if self._is_blinking:
|
||||
self._timer.deinit()
|
||||
|
||||
self._is_blinking = True
|
||||
self.on() # Ensure initial state is on
|
||||
self._timer.init(
|
||||
period=interval_ms,
|
||||
mode=Timer.PERIODIC,
|
||||
callback=self._timer_callback
|
||||
)
|
||||
|
||||
def blink_off(self):
|
||||
"""Stops blinking and returns control to steady state."""
|
||||
if self._is_blinking:
|
||||
self._timer.deinit()
|
||||
self._is_blinking = False
|
||||
|
||||
def blink_toggle(self, interval_ms=500):
|
||||
"""Toggles blinking state (starts if stopped, stops if active)."""
|
||||
if self._is_blinking:
|
||||
self.blink_off()
|
||||
else:
|
||||
self.blink_on(interval_ms)
|
||||
|
||||
def deinit(self):
|
||||
"""Releases the hardware PWM pins when finished."""
|
||||
"""Releases the hardware PWM pins and timer when finished."""
|
||||
self._r_pwm.deinit()
|
||||
self._g_pwm.deinit()
|
||||
self._b_pwm.deinit()
|
||||
Reference in New Issue
Block a user