mirror of
https://github.com/google/mozc-devices.git
synced 2025-11-09 01:03:26 +03:00
Add a LED for chassis
This commit is contained in:
@@ -29,13 +29,13 @@ class LEDBase():
|
||||
try:
|
||||
import RPi.GPIO as GPIO
|
||||
import threading
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
|
||||
class LED(LEDBase):
|
||||
ON = 'ON'
|
||||
OFF = 'OFF'
|
||||
|
||||
def __init__(self, pin):
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
self._pin = pin
|
||||
self._lock = threading.Lock()
|
||||
self._timer = None
|
||||
@@ -72,6 +72,63 @@ try:
|
||||
self._timer.cancel()
|
||||
self._timer = None
|
||||
|
||||
import wiringpi
|
||||
wiringpi.wiringPiSetupPhys()
|
||||
class SlowLED(LEDBase):
|
||||
DUTY_MAX = 1024
|
||||
INTERVAL_MS = 10
|
||||
|
||||
def __init__(self, pin, delay_ms):
|
||||
self._pin = pin
|
||||
self._delay_ms = delay_ms
|
||||
wiringpi.pinMode(pin, wiringpi.GPIO.PWM_OUTPUT)
|
||||
self._target_duty = 0
|
||||
self._current_duty = 0
|
||||
self._remaining_cycles = 0
|
||||
self._lock = threading.Lock()
|
||||
self._timer = None
|
||||
self.off()
|
||||
|
||||
# Called every 10ms
|
||||
def _update(self):
|
||||
with self._lock:
|
||||
diff = (self._target_duty - self._current_duty) // \
|
||||
self._remaining_cycles
|
||||
self._current_duty += diff
|
||||
self._remaining_cycles -= 1
|
||||
self._set_duty_immediate(self._current_duty)
|
||||
if self._remaining_cycles == 0:
|
||||
self._timer.cancel()
|
||||
self._timer = None
|
||||
return
|
||||
self._timer = threading.Timer(self.INTERVAL_MS * 0.001, self._update)
|
||||
self._timer.daemon = True
|
||||
self._timer.start()
|
||||
|
||||
|
||||
# |duty|: 0 - 100
|
||||
def _set_duty_immediate(self, duty):
|
||||
wiringpi.pwmWrite(self._pin, int(self.DUTY_MAX * duty // 100))
|
||||
|
||||
def _set_duty_slow(self, duty, time_ms):
|
||||
with self._lock:
|
||||
self._target_duty = duty
|
||||
self._remaining_cycles = time_ms // self.INTERVAL_MS
|
||||
if self._timer is None:
|
||||
self._timer = threading.Timer(self.INTERVAL_MS * 0.001, self._update)
|
||||
self._timer.daemon = True
|
||||
self._timer.start()
|
||||
|
||||
def on(self):
|
||||
self._set_duty_slow(100, self._delay_ms)
|
||||
|
||||
def off(self):
|
||||
self._set_duty_slow(0, self._delay_ms)
|
||||
|
||||
# |brightness|: 0 - 100
|
||||
def set_brightness(self, brightness):
|
||||
self._set_duty_slow(brightness, self._delay_ms)
|
||||
|
||||
except ImportError as e:
|
||||
import sys
|
||||
|
||||
@@ -80,3 +137,4 @@ except ImportError as e:
|
||||
|
||||
LED_BLUE = LED(38)
|
||||
LED_RED = LED(40)
|
||||
LED_CHASSIS = SlowLED(32, 500)
|
||||
|
||||
Reference in New Issue
Block a user