Updated adalight sketch to use the number of leds sent in the header (#374)

- NUM_LEDS becomes MAX_LEDS and any led data beyond that is ignored

Added a python udp listener and serial sender.
- Intended for windows users with a COM port based led strip.
- Works with hyperion
This commit is contained in:
penfold42 2017-01-22 20:27:01 +11:00 committed by redPanther
parent 05ee316c1d
commit abc798a479
2 changed files with 225 additions and 24 deletions

View File

@ -9,10 +9,11 @@
set following values to your needs set following values to your needs
**************************************/ **************************************/
#define INITAL_LED_TEST_ENABLED true #define INITIAL_LED_TEST_ENABLED true
#define INITIAL_LED_TEST_BRIGHTNESS 32 // 0..255
// Number of leds in your strip. set to "1" and ANALOG_OUTPUT_ENABLED to "true" to activate analog only // Number of leds in your strip. set to "1" and ANALOG_OUTPUT_ENABLED to "true" to activate analog only
#define NUM_LEDS 100 #define MAX_LEDS 100
// type of your led controller, possible values, see below // type of your led controller, possible values, see below
#define LED_TYPE WS2812B #define LED_TYPE WS2812B
@ -54,8 +55,9 @@
#define COLOR_CORRECTION TypicalLEDStrip // predefined fastled color correction #define COLOR_CORRECTION TypicalLEDStrip // predefined fastled color correction
//#define COLOR_CORRECTION CRGB(255,255,255) // or RGB value describing the color correction //#define COLOR_CORRECTION CRGB(255,255,255) // or RGB value describing the color correction
// Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf) // Baudrate, higher rate allows faster refresh rate and more LEDs
#define serialRate 460800 // use 115200 for ftdi based boards #define serialRate 460800 // use 115200 for ftdi based boards
//#define serialRate 115200 // use 115200 for ftdi based boards
/************************************** /**************************************
@ -70,7 +72,7 @@ uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
unsigned long endTime; unsigned long endTime;
// Define the array of leds // Define the array of leds
CRGB leds[NUM_LEDS]; CRGB leds[MAX_LEDS];
// set rgb to analog led stripe // set rgb to analog led stripe
void showAnalogRGB(const CRGB& led) { void showAnalogRGB(const CRGB& led) {
@ -86,7 +88,7 @@ void showAnalogRGB(const CRGB& led) {
// set color to all leds // set color to all leds
void showColor(const CRGB& led) { void showColor(const CRGB& led) {
#if NUM_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
LEDS.showColor(led); LEDS.showColor(led);
#endif #endif
showAnalogRGB(led); showAnalogRGB(led);
@ -94,8 +96,8 @@ void showColor(const CRGB& led) {
// switch of digital and analog leds // switch of digital and analog leds
void switchOff() { void switchOff() {
#if NUM_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB)); memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
FastLED.show(); FastLED.show();
#endif #endif
showAnalogRGB(leds[0]); showAnalogRGB(leds[0]);
@ -118,25 +120,25 @@ bool checkIncommingData() {
// main function that setups and runs the code // main function that setups and runs the code
void setup() { void setup() {
Serial.begin(serialRate);
// additional ground pin to make wiring a bit easier
pinMode(ANALOG_GROUND_PIN, OUTPUT);
digitalWrite(ANALOG_GROUND_PIN, LOW);
// analog output // analog output
if (ANALOG_OUTPUT_ENABLED) { if (ANALOG_OUTPUT_ENABLED) {
// additional ground pin to make wiring a bit easier
pinMode(ANALOG_GROUND_PIN, OUTPUT);
digitalWrite(ANALOG_GROUND_PIN, LOW);
pinMode(ANALOG_BLUE_PIN , OUTPUT); pinMode(ANALOG_BLUE_PIN , OUTPUT);
pinMode(ANALOG_RED_PIN , OUTPUT); pinMode(ANALOG_RED_PIN , OUTPUT);
pinMode(ANALOG_GREEN_PIN, OUTPUT); pinMode(ANALOG_GREEN_PIN, OUTPUT);
} }
// Uncomment/edit one of the following lines for your leds arrangement. // Uncomment/edit one of the following lines for your leds arrangement.
int ledCount = NUM_LEDS; int ledCount = MAX_LEDS;
if (ANALOG_MODE == ANALOG_MODE_LAST_LED) { if (ANALOG_MODE == ANALOG_MODE_LAST_LED) {
ledCount--; ledCount--;
} }
#if NUM_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, ledCount); FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, ledCount);
#endif #endif
@ -147,14 +149,14 @@ void setup() {
FastLED.setDither ( DITHER_MODE ); FastLED.setDither ( DITHER_MODE );
// initial RGB flash // initial RGB flash
#if INITAL_LED_TEST_ENABLED == true #if INITIAL_LED_TEST_ENABLED == true
showColor(CRGB(255, 0, 0)); delay(400); Serial.println("initial test");
showColor(CRGB(0, 255, 0)); delay(400); showColor(CRGB(INITIAL_LED_TEST_BRIGHTNESS, 0, 0)); delay(400);
showColor(CRGB(0, 0, 255)); delay(400); showColor(CRGB(0, INITIAL_LED_TEST_BRIGHTNESS, 0)); delay(400);
showColor(CRGB(0, 0, INITIAL_LED_TEST_BRIGHTNESS )); delay(400);
#endif #endif
showColor(CRGB(0, 0, 0)); showColor(CRGB(0, 0, 0));
Serial.begin(serialRate);
Serial.print("Ada\n"); // Send "Magic Word" string to host Serial.print("Ada\n"); // Send "Magic Word" string to host
@ -183,14 +185,16 @@ void setup() {
// if checksum does not match go back to wait // if checksum does not match go back to wait
if (chk != (hi ^ lo ^ 0x55)) continue; if (chk != (hi ^ lo ^ 0x55)) continue;
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB)); memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
transmissionSuccess = true; transmissionSuccess = true;
sum_r = 0; sum_r = 0;
sum_g = 0; sum_g = 0;
sum_b = 0; sum_b = 0;
int num_leds = (hi<<8) + lo + 1;
// read the transmission data and set LED values // read the transmission data and set LED values
for (uint8_t idx = 0; idx < NUM_LEDS; idx++) { for (uint8_t idx = 0; idx < min(num_leds,MAX_LEDS); idx++) {
byte r, g, b; byte r, g, b;
if (!checkIncommingData()) { if (!checkIncommingData()) {
transmissionSuccess = false; transmissionSuccess = false;
@ -220,22 +224,22 @@ void setup() {
// shows new values // shows new values
if (transmissionSuccess) { if (transmissionSuccess) {
endTime = millis() + OFF_TIMEOUT; endTime = millis() + OFF_TIMEOUT;
#if NUM_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
FastLED.show(); FastLED.show();
#endif #endif
#if ANALOG_OUTPUT_ENABLED == true #if ANALOG_OUTPUT_ENABLED == true
#if ANALOG_MODE == ANALOG_MODE_LAST_LED #if ANALOG_MODE == ANALOG_MODE_LAST_LED
showAnalogRGB(leds[NUM_LEDS-1]); showAnalogRGB(leds[MAX_LEDS-1]);
#else #else
showAnalogRGB(CRGB(sum_r/NUM_LEDS, sum_g/NUM_LEDS, sum_b/NUM_LEDS)); showAnalogRGB(CRGB(sum_r/MAX_LEDS, sum_g/MAX_LEDS, sum_b/MAX_LEDS));
#endif #endif
#endif #endif
} }
} }
} // end of setup } // end of setup
void loop() { void loop() {
// Not used. See note in setup() function. // Not used. See note in setup() function.
} }

View File

@ -0,0 +1,197 @@
#!/usr/bin/env python
#
# Simple UDP to Serial redirector.
# Author: https://github.com/penfold42
# raw udp packets to raw serial:
# python.exe udp_adalight.py -P 2801 COM4 115200
# raw udp packets to adalight serial protocol:
# python.exe udp_adalight.py -a -P 2801 COM4 115200
# Derived from: https://github.com/pyserial/pyserial/blob/master/examples/tcp_serial_redirect.py
#
# (C) 2002-2016 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier: BSD-3-Clause
import sys
import socket
import serial
import serial.threaded
import time
class SerialToNet(serial.threaded.Protocol):
"""serial->socket"""
def __init__(self):
self.socket = None
def __call__(self):
return self
def data_received(self, data):
if self.socket is not None:
self.socket.sendall(data)
if __name__ == '__main__': # noqa
import argparse
parser = argparse.ArgumentParser(
description='Simple UDP to Serial redirector.',
epilog="""\
NOTE: no security measures are implemented. Anyone can remotely connect
to this service over the network.
""")
parser.add_argument(
'SERIALPORT',
help="serial port name")
parser.add_argument(
'BAUDRATE',
type=int,
nargs='?',
help='set baud rate, default: %(default)s',
default=115200)
parser.add_argument(
'-q', '--quiet',
action='store_true',
help='suppress non error messages',
default=False)
parser.add_argument(
'-a', '--ada',
action='store_true',
help='prepend adalight header to serial packets',
default=False)
parser.add_argument(
'--develop',
action='store_true',
help='Development mode, prints Python internals on errors',
default=False)
group = parser.add_argument_group('serial port')
group.add_argument(
"--parity",
choices=['N', 'E', 'O', 'S', 'M'],
type=lambda c: c.upper(),
help="set parity, one of {N E O S M}, default: N",
default='N')
group.add_argument(
'--rtscts',
action='store_true',
help='enable RTS/CTS flow control (default off)',
default=False)
group.add_argument(
'--xonxoff',
action='store_true',
help='enable software flow control (default off)',
default=False)
group.add_argument(
'--rts',
type=int,
help='set initial RTS line state (possible values: 0, 1)',
default=None)
group.add_argument(
'--dtr',
type=int,
help='set initial DTR line state (possible values: 0, 1)',
default=None)
group = parser.add_argument_group('network settings')
exclusive_group = group.add_mutually_exclusive_group()
exclusive_group.add_argument(
'-P', '--localport',
type=int,
help='local UDP port',
default=2801)
args = parser.parse_args()
# connect to serial port
ser = serial.serial_for_url(args.SERIALPORT, do_not_open=True)
ser.baudrate = args.BAUDRATE
ser.parity = args.parity
ser.rtscts = args.rtscts
ser.xonxoff = args.xonxoff
if args.rts is not None:
ser.rts = args.rts
if args.dtr is not None:
ser.dtr = args.dtr
if not args.quiet:
sys.stderr.write(
'--- UDP to Serial redirector\n'
'--- listening on udp port {a.localport}\n'
'--- sending to {p.name} {p.baudrate},{p.bytesize}{p.parity}{p.stopbits}\n'
'--- type Ctrl-C / BREAK to quit\n'.format(p=ser, a=args))
try:
ser.open()
except serial.SerialException as e:
sys.stderr.write('Could not open serial port {}: {}\n'.format(ser.name, e))
sys.exit(1)
ser_to_net = SerialToNet()
serial_worker = serial.threaded.ReaderThread(ser, ser_to_net)
serial_worker.start()
srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('', args.localport))
try:
intentional_exit = False
while True:
try:
while True:
try:
data,addr = srv.recvfrom(1024)
if not data:
break
if args.ada:
numleds = len(data)/3
hi = (numleds-1)/256
lo = (numleds-1)&255
sum = hi^lo^0x55
ser.write ("Ada"+ chr(hi) + chr(lo) + chr(sum))
ser.write(data) # get a bunch of bytes and send them
except socket.error as msg:
if args.develop:
raise
sys.stderr.write('ERROR: {}\n'.format(msg))
# probably got disconnected
break
except KeyboardInterrupt:
intentional_exit = True
raise
except socket.error as msg:
if args.develop:
raise
sys.stderr.write('ERROR: {}\n'.format(msg))
finally:
ser_to_net.socket = None
sys.stderr.write('Disconnected\n')
except KeyboardInterrupt:
pass
sys.stderr.write('\n--- exit ---\n')
serial_worker.stop()