Skip to content
Snippets Groups Projects
Commit fff03801 authored by Hermod's avatar Hermod
Browse files

[Badge] Fix: statusRGB_listener was taking 100% CPU after first time

parent fca1be81
Branches
No related tags found
No related merge requests found
......@@ -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,25 +44,12 @@ def sanitize_color(s):
return r, g, b
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
# 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)
# 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()
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:
continue
return 1
r, g, b = colors
try:
t = float(data[1])
......@@ -75,6 +62,26 @@ if __name__ == '__main__':
time.sleep(t)
strip.setPixelColorRGB(0, 0, 0, 0)
strip.show()
return 0
strip.setPixelColorRGB(0, 0, 0, 0)
strip.show()
if __name__ == '__main__':
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)
# Intialize the library (must be called once before other functions).
strip.begin()
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment