Created
June 12, 2019 15:02
-
-
Save phearnot/4af1fa68570498bfc1d2ff47e0e1d515 to your computer and use it in GitHub Desktop.
SainSmart 5V USB Relay Module control script
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
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