Skip to content

Instantly share code, notes, and snippets.

@omkensey
Created March 15, 2022 03:07
Show Gist options
  • Save omkensey/8400d9e9f86075f62b4007198951d015 to your computer and use it in GitHub Desktop.
Save omkensey/8400d9e9f86075f62b4007198951d015 to your computer and use it in GitHub Desktop.
Luxafor interface script just for playing with color/brightness effects
# Based on https://github.com/jamesmdavies88/LuxaforMute/blob/main/mute.py
# Make sure to do the udev configuration there first
# This script also catches a SIGINT and turns off the LEDs, then exits cleanly
from array import array
import usb.core
import usb.util
import os
import signal
import sys
import time
import math
def main():
dev = usb.core.find(idVendor=0x04D8, idProduct=0xF372)
if dev is None:
print("Luxafor Mute is not connected")
return
try:
dev.detach_kernel_driver(0)
except usb.core.USBError:
pass
try:
dev.set_configuration()
except usb.core.USBError:
print(
"If you receive this error, it is likely you haven't configured the rule for the Luxafor device"
)
return
def exit_handler(signal, frame):
print('Caught an exit signal, turning off')
dev.write(1, [1, 1] + color_off + [0, 0, 0])
dev.write(1, [1, 2] + color_off + [0, 0, 0])
dev.write(1, [1, 3] + color_off + [0, 0, 0])
dev.write(1, [1, 4] + color_off + [0, 0, 0])
dev.write(1, [1, 5] + color_off + [0, 0, 0])
dev.write(1, [1, 6] + color_off + [0, 0, 0])
sys.exit(0)
signal.signal(signal.SIGINT, exit_handler)
dev.set_configuration()
color_white = [255, 255, 255]
color_red = [255, 0, 0]
color_orange = [255, 128, 0]
color_yellow = [255, 255, 0]
color_green = [0, 255, 0]
color_cyan = [0, 255, 255]
color_blue = [0, 0, 255]
color_purple = [255, 0, 255]
color_terraform = [0x84, 0x4f, 0xba]
color_vault = [0xff, 0xec, 0x6e]
color_consul = [0xdc, 0x47, 0x7d]
color_nomad = [0x60, 0xde, 0xa9]
color_vagrant = [0x2e, 0x71, 0xe5]
color_packer = [0x63, 0xd0, 0xff]
color_waypoint = [0x62, 0xd4, 0xdc]
color_boundary = [0xec, 0x58, 0x5d]
color_green_83 = [math.ceil(x * 5 / 6) for x in color_green]
color_green_67 = [math.ceil(x * 2 / 3) for x in color_green]
color_green_50 = [math.ceil(x / 2) for x in color_green]
color_green_33 = [math.ceil(x / 3) for x in color_green]
color_green_17 = [math.ceil(x / 6) for x in color_green]
color_off = [0, 0, 0]
spin_on = "true"
fade_on = "false"
step_delay = 0.2
led_fade_steps = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
color_order = [color_green, color_green_83, color_green_67, color_green_50, color_green_33, color_green_17]
while True:
# This part of the write command contains 8 bytes of data:
# [1, 1, 255, 0, 0, 0, 0, 0]
# Byte 1: Always set to 1 (Think this means solid colour)
# Byte 2: Value is set as 1-6 for each LED in the device
# Byte 3-5: RGB value for the colour you want to select
# Byte 6-8: Not sure what these are for, likely to be flash patterns etc
dev.write(1, [1, 1] + [math.ceil(x * led_fade_steps[0]) for x in color_order[0]] + [0, 0, 0])
dev.write(1, [1, 2] + [math.ceil(x * led_fade_steps[0]) for x in color_order[1]] + [0, 0, 0])
dev.write(1, [1, 3] + [math.ceil(x * led_fade_steps[0]) for x in color_order[2]] + [0, 0, 0])
dev.write(1, [1, 4] + [math.ceil(x * led_fade_steps[0]) for x in color_order[3]] + [0, 0, 0])
dev.write(1, [1, 5] + [math.ceil(x * led_fade_steps[0]) for x in color_order[4]] + [0, 0, 0])
dev.write(1, [1, 6] + [math.ceil(x * led_fade_steps[0]) for x in color_order[5]] + [0, 0, 0])
if spin_on == "true":
color_order = color_order[1:] + color_order[:1]
if fade_on == "true" and len(led_fade_steps) > 1:
led_fade_steps = led_fade_steps[1:] + led_fade_steps[:1]
if spin_on == "true" or fade_on == "true":
time.sleep(step_delay)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment