diff --git a/scripts/statusRGB_listener.py b/scripts/statusRGB_listener.py index ff068d85ebe46892bea038d7d5dff7628f11d22f..fb7564a95678cd6fd38c981fb456d5379bcb6f51 100755 --- a/scripts/statusRGB_listener.py +++ b/scripts/statusRGB_listener.py @@ -4,8 +4,7 @@ import os import sys import time -import argparse -import select +import subprocess from neopixel import Adafruit_NeoPixel, ws @@ -29,6 +28,7 @@ LED_INVERT = False # True to invert the signal (when using NPN transistor LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering +# Pipe where the code listen for color commands PIPE_PATH = '/dev/rgb_pipe' @@ -44,37 +44,44 @@ def sanitize_color(s): return r, g, b +def command_string_to_display(cs, strip): + """Display the color based on the 'color time' string""" + data = data0.split(' ') + colors = sanitize_color(data[0]) + if colors is None: + return 1 + r, g, b = colors + try: + t = float(data[1]) + except: + t = -1 + + strip.setPixelColorRGB(0, r, g, b) + strip.show() + if t > 0: + time.sleep(t) + strip.setPixelColorRGB(0, 0, 0, 0) + strip.show() + return 0 + + if __name__ == '__main__': - if not os.path.exists(PIPE_PATH): - os.mkfifo(PIPE_PATH) - os.system('chmod 666 ' + PIPE_PATH) # Acces r+w for non-root users + if os.path.exists(PIPE_PATH): + os.remove(PIPE_PATH) + + os.mkfifo(PIPE_PATH) + os.system('chown root:hermod {0} && chmod g+rw {0}'.format(PIPE_PATH)) # rw access for hermod group # Create NeoPixel object with appropriate configuration. - strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) + strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, + LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) # Intialize the library (must be called once before other functions). strip.begin() - with open(PIPE_PATH, 'r') as fifo: - while True: - select.select([fifo],[],[fifo]) - data0 = fifo.read().strip() - data = data0.split(' ') - colors = sanitize_color(data[0]) - if colors is None: - continue - r, g, b = colors - try: - t = float(data[1]) - except: - t = -1 - - strip.setPixelColorRGB(0, r, g, b) - strip.show() - if t > 0: - time.sleep(t) - strip.setPixelColorRGB(0, 0, 0, 0) - strip.show() - - strip.setPixelColorRGB(0, 0, 0, 0) - strip.show() + # Listen to the pipe and display colors + while True: + data0 = subprocess.check_output(['cat', PIPE_PATH]).decode('utf-8') + command_string_to_display(data0, strip) + + command_string_to_display('000000', strip)