Skip to content

Instantly share code, notes, and snippets.

@phearnot
Created June 12, 2019 15:02
Show Gist options
  • Save phearnot/4af1fa68570498bfc1d2ff47e0e1d515 to your computer and use it in GitHub Desktop.
Save phearnot/4af1fa68570498bfc1d2ff47e0e1d515 to your computer and use it in GitHub Desktop.
SainSmart 5V USB Relay Module control script
from pyftdi.ftdi import Ftdi
from sys import argv
from array import array
import re
if re.fullmatch(r'[\.x]{4}', argv[1]):
relay_mask = sum([flag == 'x' and value or 0 for flag, value in zip(argv[1], [1, 2, 4, 8])])
ftdi = Ftdi.create_from_url('ftdi:///1')
ftdi.set_baudrate(9600)
ftdi.set_bitmode(255, 1)
ftdi.set_line_property(8, 1, 'N')
ftdi.write_data(array('B', [relay_mask]))
else:
print(f'Invalid pattern: {argv[1]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment