From 4cefd2a594e8f3ba8a686705c58cdb3c9140b6b8 Mon Sep 17 00:00:00 2001 From: Felipe Martin Date: Tue, 17 Jan 2017 22:14:56 +0100 Subject: [PATCH] Base luxafor interface to operate with the device --- README.md | 48 +++++++++++++- luxafor/__init__.py | 0 luxafor/luxafor.py | 152 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 luxafor/__init__.py create mode 100644 luxafor/luxafor.py diff --git a/README.md b/README.md index 22fde95..c3ef4c1 100644 --- a/README.md +++ b/README.md @@ -1 +1,47 @@ -# pyluxafor \ No newline at end of file +pyluxafor +========= + +Helper interface and CLI to interact with [luxafor](https://luxafor.com) products. + + +## Usage + +``` python +from luxafor import luxafor + +lux = luxafor.Luxafor() + +# Led types +luxafor.Leds.ALL +luxafor.Leds.FRONT +luxafor.Leds.BACK +luxafor.Leds.LEDn # Where n is a number from 1 to 6, refer to the class + +# Set a basic color +# From: off, yellow, green, blue, magenta, cyan, red, white +lux.set_basic_color('green') + +# Set a static color +lux.set_color(, , , ) + +# Fade to a color +lux.fade(, , , , ) + +# Strobe +lux.strobe(, , , , , ) + +# Wave +# Wave types: +# 1: Short wave +# 2: Long wave +# 3: Overlapping short wave +# 4: Overlapping long wave +lux.wave(, , , , , , ) + +# Enable predefined pattern +# Patterns from 1 to 8 +lux.pattern(, ) + +# Turn off the luxafor +lux.turn_off() +``` diff --git a/luxafor/__init__.py b/luxafor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/luxafor/luxafor.py b/luxafor/luxafor.py new file mode 100644 index 0000000..d894fa6 --- /dev/null +++ b/luxafor/luxafor.py @@ -0,0 +1,152 @@ +# -*- coding: utf-8 -*- + +import usb.core +import usb.util + + +class Leds: + """ + Helper class with static attributes to select Luxafor leds. + + - All: All the leds. + - Front: Front side of the flag. + - Back: Back side of the flag. + - Led#: Specific led number. + + +--------------------------+ + | | | + |6 3| | + | | | + Front side|5 2|Back side | + | | | + |4 1| | + | | | + | +---------------------+ + | | + | | + | | + | | + +----+ + """ + ALL = 0xFF + BACK = 0x41 + FRONT = 0x42 + + LED1 = 0x01 + LED2 = 0x02 + LED3 = 0x03 + LED4 = 0x04 + LED5 = 0x05 + LED6 = 0x06 + + +class Packet: + byte0 = 0 + byte1 = 0 + byte2 = None + byte3 = None + byte4 = None + byte5 = None + byte6 = None + byte7 = None + + def get_byte(self, byte_num): + return getattr(self, 'byte%d' % byte_num, None) + + def render(self): + pkt = filter( + lambda x: x is not None, + (self.get_byte(n) for n in range(8))) + return list(pkt) + + +class Luxafor: + VENDOR_ID = 0x04D8 + PRODUCT_ID = 0xF372 + + DEVICE_ID = 1 + + COLORS = { + 'red': 0x52, + 'green': 0x47, + 'blue': 0x42, + 'cyan': 0x43, + 'magenta': 0x4D, + 'yellow': 0x59, + 'white': 0x57, + 'off': 0x4F, + } + + def __init__(self): + device = usb.core.find(idVendor=self.VENDOR_ID, + idProduct=self.PRODUCT_ID) + + if device is None: + raise Exception('Device was not found.') + + device.set_configuration() + + self.device = device + + def set_basic_color(self, color): + if color in self.COLORS: + pkt = Packet() + pkt.byte0 = 0 + pkt.byte1 = self.COLORS.get(color, 79) + + self.device.write(self.DEVICE_ID, pkt.render()) + + def set_color(self, red=255, green=255, blue=255, led=Leds.ALL): + pkt = Packet() + pkt.byte0 = 1 + pkt.byte1 = led + pkt.byte2 = red + pkt.byte3 = green + pkt.byte4 = blue + self.device.write(self.DEVICE_ID, pkt.render()) + + def fade(self, red=255, green=255, blue=255, led=Leds.ALL, speed=255): + pkt = Packet() + pkt.byte0 = 2 + pkt.byte1 = led + pkt.byte2 = red + pkt.byte3 = green + pkt.byte4 = blue + pkt.byte5 = speed + self.device.write(self.DEVICE_ID, pkt.render()) + + def strobe(self, red=255, green=255, blue=255, led=Leds.ALL, + speed=6, repeat=1): + pkt = Packet() + pkt.byte0 = 3 + pkt.byte1 = led + pkt.byte2 = red + pkt.byte3 = green + pkt.byte4 = blue + pkt.byte5 = speed + pkt.byte6 = 0 + pkt.byte7 = repeat + self.device.write(self.DEVICE_ID, pkt.render()) + + def wave(self, red=255, green=255, blue=255, wave=1, led=Leds.ALL, + duration=2, repeat=1): + pkt = Packet() + pkt.byte0 = 4 + pkt.byte1 = led + pkt.byte2 = red + pkt.byte3 = green + pkt.byte4 = blue + pkt.byte5 = 0 + pkt.byte6 = repeat + pkt.byte7 = duration + self.device.write(self.DEVICE_ID, pkt.render()) + + def pattern(self, pattern=0, repeat=255): + pkt = Packet() + pkt.byte0 = 6 + pkt.byte1 = pattern + pkt.byte2 = repeat + self.device.write(self.DEVICE_ID, pkt.render()) + + def turn_off(self): + self.set_basic_color('off')