|
| 1 | +import time |
| 2 | +import serial |
| 3 | +import argparse |
| 4 | +import serials_find |
| 5 | + |
| 6 | +def crc8_dvb_s2(crc, a): |
| 7 | + crc = crc ^ a |
| 8 | + for ii in range(8): |
| 9 | + if crc & 0x80: |
| 10 | + crc = (crc << 1) ^ 0xD5 |
| 11 | + else: |
| 12 | + crc = crc << 1 |
| 13 | + return crc & 0xFF |
| 14 | + |
| 15 | +def send_msp(port, baud, body): |
| 16 | + s = serial.Serial(port=port, baudrate=baud, |
| 17 | + bytesize=8, parity='N', stopbits=1, |
| 18 | + timeout=1, xonxoff=0, rtscts=0) |
| 19 | + crc = 0 |
| 20 | + for x in body: |
| 21 | + crc = crc8_dvb_s2(crc, x) |
| 22 | + msp = [ord('$'),ord('X'),ord('<')] |
| 23 | + msp = msp + body |
| 24 | + msp.append(crc) |
| 25 | + s.write(msp) |
| 26 | + print(msp) |
| 27 | + time.sleep(2.0) |
| 28 | + print(s.read_all()) |
| 29 | + |
| 30 | +def send_clear(port, baud): |
| 31 | + msp = [0,0xb6,0x00,1,0,0x02] |
| 32 | + send_msp(port, baud, msp) |
| 33 | + |
| 34 | +def send_display(port, baud): |
| 35 | + msp = [0,0xb6,0x00,1,0,0x04] |
| 36 | + send_msp(port, baud, msp) |
| 37 | + |
| 38 | +def send_msg(port, baud, row, col, str): |
| 39 | + l = 4+len(str) |
| 40 | + msp = [0,0xb6,0x00,l%256,int(l/256),0x03,row,col,0] |
| 41 | + for x in [*str]: |
| 42 | + msp.append(ord(x)) |
| 43 | + send_msp(port, baud, msp) |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + parser = argparse.ArgumentParser( |
| 47 | + description="Initialize EdgeTX passthrough to internal module") |
| 48 | + parser.add_argument("-b", "--baud", type=int, default=460800, |
| 49 | + help="Baud rate for passthrough communication") |
| 50 | + parser.add_argument("-p", "--port", type=str, |
| 51 | + help="Override serial port autodetection and use PORT") |
| 52 | + parser.add_argument("-c", "--clear", action='store_true', |
| 53 | + help="Clear the OSD") |
| 54 | + parser.add_argument("-d", "--display", action='store_true', |
| 55 | + help="Draw the currently set OSD") |
| 56 | + parser.add_argument("row", type=int, nargs='?', default=0, |
| 57 | + help="Row where message is to be displayed") |
| 58 | + parser.add_argument("col", type=int, nargs='?', default=0, |
| 59 | + help="Column where message is to be displayed") |
| 60 | + parser.add_argument("message", type=str, nargs='?', default='', |
| 61 | + help="Message to be displayed") |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + if (args.port == None): |
| 65 | + args.port = serials_find.get_serial_port() |
| 66 | + |
| 67 | + if args.clear: |
| 68 | + send_clear(args.port, args.baud) |
| 69 | + elif args.display: |
| 70 | + send_display(args.port, args.baud) |
| 71 | + else: |
| 72 | + send_msg(args.port, args.baud, args.row, args.col, args.message) |
0 commit comments