Last active
November 10, 2024 10:13
-
-
Save alessaba/e9b5c7304d641dbd20ed807d8c528675 to your computer and use it in GitHub Desktop.
Interacts with the DCSD Alex Cable's Status LEDs. This can be adapted to any use case you want.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
try: | |
import pylibftdi | |
except ImportError: | |
import os | |
os.system("brew install libftdi libusb; brew link libusb; pip3 install pylibftdi") | |
print("Dependencies installed.\n") | |
''' | |
Value Map | |
0xF0: All Lights Off (0,0,0) | |
0xF1: Red Light On (0,0,1) | |
0xF2: Green Light On (1,0,0) | |
0xF3: Red + Green Lights On (1,0,1) | |
0xF8: Yellow Light On (0,1,0) | |
0xF9: Red + Yellow Lights On (0,1,1) | |
0xFA: Yellow + Green Lights On (1,1,0) | |
0xFB: All Lights On (1,1,1) | |
''' | |
pylibftdi.USB_PID_LIST.append(0x8a88) # PID | |
pylibftdi.USB_VID_LIST.append(0x0403) # VID | |
# Device Config | |
def connettiDCSD(): | |
luciDevice = pylibftdi.BitBangDevice("AH022NWG") # Device Serial for the Status LED FTDI. | |
if luciDevice.device_id: | |
print(f"Found FTDI chip with requested Serial Number: {luciDevice.device_id}") | |
luciDevice.open() | |
return luciDevice | |
def dcsd_lights_set(green, yellow, red): | |
lights_map = { | |
(0,0,0): 0xF0, | |
(0,0,1): 0xF1, | |
(1,0,0): 0xF2, | |
(1,0,1): 0xF3, | |
(0,1,0): 0xF8, | |
(0,1,1): 0xF9, | |
(1,1,0): 0xFA, | |
(1,1,1): 0xFB | |
} | |
tuple_key = tuple([green,yellow,red]) | |
luciDevice.ftdi_fn.ftdi_set_bitmode(lights_map[tuple_key], 0x20) # CBUS Bitmode | |
def blinkenlights(): | |
from time import sleep | |
i = 0 | |
command = [(0,0,0),(1,0,0),(1,1,0),(1,1,1),(0,1,1),(0,0,1)] | |
try: | |
while 1: | |
dcsd_lights_set(*(command[i])) # With *() possiamo direttamente passare una tupla alla funzione | |
i+=1 | |
if i == 6: | |
i = 0 | |
sleep(.3) | |
except KeyboardInterrupt: | |
dcsd_lights_set(1,1,1) # Sets all lights on | |
print("\r\rDone!") | |
def randlights(): | |
from random import randint | |
from time import sleep | |
while 1: | |
dcsd_lights_set(randint(0, 1), randint(0, 1), randint(0, 1)) | |
sleep(.3) | |
def semaforo(trosso, tverde): | |
import time, random | |
while True: | |
dcsd_lights_set(0, 0, 1) | |
time.sleep(trosso) | |
dcsd_lights_set(1, 0, 0) | |
time.sleep(tverde) | |
dcsd_lights_set(0, 1, 0) | |
time.sleep(random.randint(3, 10)) | |
luciDevice = connettiDCSD() | |
semaforo(10, 20) | |
dcsd_lights_set(0,0,0) # Spegne tutte le luci | |
luciDevice.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment