Add a LED in chassis (#18)

LED was equipped in the chassis. This patch turns it on.
SlowLED changes the brightness gradually. wiringpi is used for it to leverage the hardware PWA equipped by Raspberry Pi.
This commit is contained in:
Makoto Shimazu
2018-08-04 07:54:37 +09:00
committed by GitHub
parent 38690a1b0a
commit 0a438843ee
3 changed files with 75 additions and 4 deletions

View File

@@ -16,8 +16,12 @@
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
from nazoru.led import LED_BLUE, LED_RED from nazoru.led import LED_BLUE, LED_RED, LED_CHASSIS
import time
LED_RED.blink(1) LED_RED.blink(1)
LED_CHASSIS.on()
time.sleep(1)
LED_CHASSIS.off()
import argparse import argparse
import os import os
@@ -48,6 +52,7 @@ def main():
LED_RED.off() LED_RED.off()
LED_BLUE.blink(1) LED_BLUE.blink(1)
LED_CHASSIS.set_brightness(5)
print('Ready. Please input your scrrible.') print('Ready. Please input your scrrible.')
while True: while True:
@@ -60,9 +65,11 @@ def main():
print('done.') print('done.')
break break
LED_CHASSIS.set_brightness(100)
LED_RED.on() LED_RED.on()
result = predictor.predict_top_n(data, 5) result = predictor.predict_top_n(data, 5)
LED_RED.off() LED_RED.off()
LED_CHASSIS.set_brightness(5)
print('\n=== RESULTS ===') print('\n=== RESULTS ===')
for item in result: for item in result:

View File

@@ -67,6 +67,7 @@ setup(
'tensorflow', 'tensorflow',
'enum34;python_version<"3.4"', 'enum34;python_version<"3.4"',
'pyserial', 'pyserial',
'evdev;platform_system=="Linux"' 'evdev;platform_system=="Linux"',
'wiringpi'
] ]
) )

View File

@@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class LEDBase(): class LEDBase(object):
def __init__(self, pin): def __init__(self, pin):
self._pin = pin self._pin = pin
def on(self): def on(self):
@@ -25,17 +25,19 @@ class LEDBase():
pass pass
def blink(self, interval): def blink(self, interval):
pass pass
def set_brightness(self, brightness):
pass
try: try:
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import threading import threading
GPIO.setmode(GPIO.BOARD)
class LED(LEDBase): class LED(LEDBase):
ON = 'ON' ON = 'ON'
OFF = 'OFF' OFF = 'OFF'
def __init__(self, pin): def __init__(self, pin):
GPIO.setmode(GPIO.BOARD)
self._pin = pin self._pin = pin
self._lock = threading.Lock() self._lock = threading.Lock()
self._timer = None self._timer = None
@@ -72,11 +74,72 @@ try:
self._timer.cancel() self._timer.cancel()
self._timer = None 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: except ImportError as e:
import sys import sys
class LED(LEDBase): class LED(LEDBase):
pass pass
class SlowLED(LEDBase):
def __init__(self, pin, delay_ms):
super(SlowLED, self).__init__(pin)
LED_BLUE = LED(38) LED_BLUE = LED(38)
LED_RED = LED(40) LED_RED = LED(40)
LED_CHASSIS = SlowLED(32, 500)