summaryrefslogtreecommitdiff
path: root/ledctl.py
diff options
context:
space:
mode:
Diffstat (limited to 'ledctl.py')
-rw-r--r--ledctl.py34
1 files changed, 32 insertions, 2 deletions
diff --git a/ledctl.py b/ledctl.py
index 8f04110..38d28fa 100644
--- a/ledctl.py
+++ b/ledctl.py
@@ -5,6 +5,8 @@ import board
import neopixel
import time
+from os.path import exists
+
# parse command line arguments
# TODO parse multiple LEDs e.g. ./ledctl --led 1 red --led 2 white
@@ -21,9 +23,22 @@ leds_max = 6
leds_num = 0
leds_array = neopixel.NeoPixel(board.D18, leds_max)
-# TODO restore LED array from state file
+# restore LED array from state file
statefile = '/tmp/ledctl.state'
+if exists(statefile):
+ print('reading ' + statefile)
+
+ with open(statefile, 'r') as f:
+ lines = f.readlines()
+ i = 0;
+ for line in lines:
+ values = line.split(' ')
+ if (i < leds_num):
+ leds_array[i] = (values[0], values[1], values[2]);
+ i = i + 1
+ f.close()
+
# figure out if we want to control a single LED or all of them
if args.led:
if (args.led < 1) or (args.led > leds_max):
@@ -48,12 +63,27 @@ def rgbval(colorname):
return switch.get(colorname, 'Valid colors are black, white, ired, green, blue, yellow, on, off')
if args.color:
+ print('color ' + args.color)
leds_color = rgbval(args.color);
if (leds_num > 0):
+ print('setting LED ' + leds_num)
+
leds_array[leds_num] = leds_color
else:
+ print('setting all LEDs')
+
leds_array.fill(leds_color)
-# TODO write LED array to state file
+# write LED array to state file
+
+print('writing ' + statefile)
+
+with open(statefile, 'w') as f:
+ for i in range(leds_max):
+ for c in range(3):
+ f.write ('%d ' % leds_array[i][c])
+ f.write('\n');
+
+ f.close()