class Graphic: WIDTH = 12 HEIGHT = 9 RENDER_DELAY = .001 FPS = 60 TPF = float(1) / FPS def __init__(self, panel): self._panel = panel if self._panel.width not in range( Graphic.WIDTH + 1) and self._panel.height not in range(Graphic.HEIGHT + 1): raise SystemError('Panel size is illegal!') self._running = True self._coms = LEDBoard(2, 3, 4, 17, 27, 22, 10, 9, 11) self._segs = LEDBoard(14, 15, 18, 23, 24, 25, 8, 7, 12, 16, 20, 21) self._canvas = Canvas(self._panel.width, self._panel.height) self._render_thread = Thread(target=self.render) self._update_thread = Thread(target=self.update) def render(self): while self._running: for i in range(self._panel.height): self._segs.value = self._canvas.matrix[i] com = self._coms.leds[i] com.on() sleep(Graphic.RENDER_DELAY) com.off() def update(self): last = time() while self._running: start = time() current = time() self._panel.render(self._canvas, current - last) last = current try: sleep(start + Graphic.TPF - time()) except IOError: pass def start(self): self._coms.off() self._segs.on() self._render_thread.start() self._update_thread.start() def stop(self): self._running = False self._render_thread.join() self._update_thread.join() self._segs.close() self._coms.close()
#!/usr/bin/python3 from gpiozero import LEDBoard from signal import pause leds = LEDBoard(21, 20, 16, 12, 7, 8, 25, 23, pwm=True) print("Press Crtl-C to stop the program.") while True: try: leds.value = (0.2, 0.3, 0.4, 0.5, 0.6, 1, 0.7, 0.8) pause() except KeyboardInterrupt: print("Stopping program.\n") leds.close() exit()
# And God said, "This is the sign of the covenant I am making between me and you and # # every living creature with you, a covenant for all generations to come: I have set # # my rainbow in the clouds, and it will be the sign of the covenant between me and the # # earth. Whenever I bring clouds over the earth and the rainbow appears in the clouds, # # I will remember my covenant between me and you and all living creatures of every kind. # # Never again will the waters become a flood to destroy all life. # # # ############################################################################################## from gpiozero import LEDBoard from signal import pause rainbow = LEDBoard(red=21, orange=20, yellow=16, green=12, blue=7, purple=8, pink=25, white=23, pwm=True) print("Press Crtl-C to stop the program.") while True: try: rainbow.pulse(fade_in_time=1, fade_out_time=1, n=None, background=True) pause() except KeyboardInterrupt: print("Stopping program.\n") rainbow.close() exit()
from gpiozero import LEDBarGraph, LEDBoard from time import sleep bargraph = LEDBoard(3, 4, 17, 27, 22, 10, 9, 11, 7, 12) try: bargraph.blink() while True: # for led in bargraph.leds: # bargraph.off() # led.on() sleep(.1) except KeyboardInterrupt: pass finally: bargraph.close()
class PyTenki: def __init__(self, forecast=None, led_pins=None, button_pin=None): self._forecast = forecast self._leds = None self._button = None self._normalize_weather_str() self.assign_leds(led_pins) self.assign_button(button_pin) @property def forecast(self): return self._forecast @forecast.setter def forecast(self, forecast): self._forecast = forecast self._normalize_weather_str() def _normalize_weather_str(self): try: strings = (('大', ''), ('暴風', ''), ('雷', ''), ('一時', '時々'), ('雨か雪', '雨'), ('雪か雨', '雪')) for before, after in strings: tmp = self._forecast['weather'] self._forecast['weather'] = tmp.replace(before, after) except (AttributeError, TypeError): pass def _compose_forecast_summary(self): try: day = self._forecast.get('day') city = self._forecast.get('city') weather = self._forecast.get('weather') if all([day, city, weather]): fcast_weather = FCAST_WEATHER.format(day=day, city=city, weather=weather) fcast_max_temp = '' fcast_min_temp = '' temps = self._forecast.get('temp') max_temp = temps.get('max') min_temp = temps.get('min') if max_temp: fcast_max_temp = FCAST_MAX_TEMP.format(max_temp=max_temp) if min_temp: fcast_min_temp = FCAST_MIN_TEMP.format(min_temp=min_temp) return ''.join([fcast_weather, fcast_max_temp, fcast_min_temp]) except AttributeError: pass return FCAST_SUM_ERR @_exc_attr_err def assign_leds(self, led_pins): self._close_leds() try: self._leds = LEDBoard( fine=led_pins.get('fine'), cloud=led_pins.get('cloud'), rain=led_pins.get('rain'), snow=led_pins.get('snow'), pwm=True, ) except PinInvalidPin: pass @_exc_attr_err def _close_leds(self): self._leds.close() def assign_button(self, button_pin): try: self._close_button() self._button = Button(button_pin) except (GPIOPinMissing, PinInvalidPin): pass @_exc_attr_err def _close_button(self): self._button.close() @_exc_attr_err def tts_forecast_summary_after_button_press(self): self._button.when_pressed = self._tts_forecast_summary def _tts_forecast_summary(self): summary = self._compose_forecast_summary() try: p1 = subprocess.Popen(['echo', summary], stdout=subprocess.PIPE) p2 = subprocess.Popen([ 'open_jtalk', '-x', DIC_FPATH, '-m', VOICE_FPATH, '-ow', SPEECH_FPATH ], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() p2.communicate() p2.wait() subprocess.run(['aplay', '--quiet', SPEECH_FPATH]) subprocess.run(['rm', '-f', SPEECH_FPATH]) except OSError: pass def operate_all_weather_leds(self, on_time=1, off_time=1, fade_in_time=1, fade_out_time=1): self._operate_fine_led(on_time, off_time, fade_in_time, fade_out_time) self._operate_cloud_led(on_time, off_time, fade_in_time, fade_out_time) self._operate_rain_led(on_time, off_time, fade_in_time, fade_out_time) self._operate_snow_led(on_time, off_time, fade_in_time, fade_out_time) @_exc_attr_err def _operate_fine_led(self, on_time=1, off_time=1, fade_in_time=1, fade_out_time=1): weather = self._forecast.get('weather') if weather.startswith('晴'): self._leds.fine.on() elif 'のち晴' in weather: self._leds.fine.pulse(fade_in_time, fade_out_time) elif '時々晴' in weather: self._leds.fine.blink(on_time, off_time) else: self._leds.fine.off() @_exc_attr_err def _operate_cloud_led(self, on_time=1, off_time=1, fade_in_time=1, fade_out_time=1): weather = self._forecast.get('weather') if weather.startswith('曇'): self._leds.cloud.on() elif 'のち曇' in weather: self._leds.cloud.pulse(fade_in_time, fade_out_time) elif '時々曇' in weather: self._leds.cloud.blink(on_time, off_time) else: self._leds.cloud.off() @_exc_attr_err def _operate_rain_led(self, on_time=1, off_time=1, fade_in_time=1, fade_out_time=1): weather = self._forecast.get('weather') if weather.startswith('雨'): self._leds.rain.on() elif 'のち雨' in weather: self._leds.rain.pulse(fade_in_time, fade_out_time) elif '時々雨' in weather: self._leds.rain.blink(on_time, off_time) else: self._leds.rain.off() @_exc_attr_err def _operate_snow_led(self, on_time=1, off_time=1, fade_in_time=1, fade_out_time=1): weather = self._forecast.get('weather') if weather.startswith('雪'): self._leds.snow.on() elif 'のち雪' in weather: self._leds.snow.pulse(fade_in_time, fade_out_time) elif '時々雪' in weather: self._leds.snow.blink(on_time, off_time) else: self._leds.snow.off()
day = now.strftime('%A') show_text(day) else: chars = [ str(hour / 10), str(hour % 10), str(minute / 10), str(minute % 10) ] dots[1] = (second % 2 == 0) sleep(.1) render_thread = Thread(target=render) display_seconds_thread = Thread(target=display_seconds) try: render_thread.start() display_seconds_thread.start() while running: update() except (KeyboardInterrupt, SystemExit): pass finally: running = False render_thread.join() display_seconds_thread.join() coms.close() segs.close() bargraph.close()