diff options
| author | Ingar <ingar@telenet.be> | 2022-02-06 16:12:18 +0100 |
|---|---|---|
| committer | Ingar <ingar@telenet.be> | 2022-02-06 16:12:18 +0100 |
| commit | 0133a185af1600d6bf8b432bb722b315a90487b1 (patch) | |
| tree | c386a08ec0c88d774ba5ff0eff6598938bd7e6f8 /ledctl.py | |
| parent | 68d1032f1b1dfffe36a78e2fb23e30444244001a (diff) | |
write LED state to state file
Diffstat (limited to 'ledctl.py')
| -rw-r--r-- | ledctl.py | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -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() |
