diff options
| author | Ingar <ingar@telenet.be> | 2022-02-06 15:32:10 +0100 |
|---|---|---|
| committer | Ingar <ingar@telenet.be> | 2022-02-06 15:32:10 +0100 |
| commit | 68d1032f1b1dfffe36a78e2fb23e30444244001a (patch) | |
| tree | 75ad8374922c9d6e5c602fc87186f02870a2baeb /ledctl.py | |
Python script to control a WS2812 LED strip
Diffstat (limited to 'ledctl.py')
| -rw-r--r-- | ledctl.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/ledctl.py b/ledctl.py new file mode 100644 index 0000000..8f04110 --- /dev/null +++ b/ledctl.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import argparse +import board +import neopixel +import time + +# parse command line arguments + +# TODO parse multiple LEDs e.g. ./ledctl --led 1 red --led 2 white + +parser = argparse.ArgumentParser(description='Set LED colors') + +parser.add_argument('--led', type=int, help='LED number to control, starting at 1') +parser.add_argument('color', type=str, help='color or command') + +args = parser.parse_args(); + +# initialize LED array +leds_max = 6 +leds_num = 0 +leds_array = neopixel.NeoPixel(board.D18, leds_max) + +# TODO restore LED array from state file +statefile = '/tmp/ledctl.state' + +# 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): + parser.error("led must be within range") + else: + leds_num = args.led; + +# default color is white +leds_color = (255, 255, 255) + +def rgbval(colorname): + switch = { + 'on': (255, 255, 255), + 'off': (0, 0, 0), + 'white': (255, 255, 255), + 'black': (0, 0, 0), + 'red': (255, 0, 0), + 'green': (0, 255, 0), + 'blue': (0, 0, 255), + 'yellow': (255, 255, 0), + } + return switch.get(colorname, 'Valid colors are black, white, ired, green, blue, yellow, on, off') + +if args.color: + leds_color = rgbval(args.color); + +if (leds_num > 0): + leds_array[leds_num] = leds_color +else: + leds_array.fill(leds_color) + +# TODO write LED array to state file + |
