blob: b1dfb03f8fb9773e31b64466f25680044712d017 [file] [log] [blame]
#!/usr/bin/env fuchsia-vendored-python
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Other
### Actuates the state of an external relay by writing a single byte to |tty|.
## usage: fx set-relay [--off|--on] /dev/ttyUSB0
##
## Turn on or off a USB-connected relay on the specified device.
import argparse
import sys
def main():
parser = argparse.ArgumentParser(
'Actuates the state of an external relay')
parser.add_argument('tty',
type=argparse.FileType('wb'),
help='TTY to which the external relay is connected')
state_parser = parser.add_mutually_exclusive_group(required=True)
state_parser.add_argument('--on', dest='state', action='store_true')
state_parser.add_argument('--off', dest='state', action='store_false')
args = parser.parse_args()
# Different relays require different sequences to turn on/off. The
# sequences don't interfere with each other so just do them all.
if args.state:
args.tty.write(b'\xa0\x01\x00\xa1')
args.tty.write(b'\x02')
else:
args.tty.write(b'\xa0\x01\x01\xa2')
args.tty.write(b'\x01')
if __name__ == '__main__':
sys.exit(main())