Skip to content

Instantly share code, notes, and snippets.

@sshh12
Created April 25, 2019 23:14
Show Gist options
  • Save sshh12/0b528178b2b6b9a6b004525fd09ec6fa to your computer and use it in GitHub Desktop.
Save sshh12/0b528178b2b6b9a6b004525fd09ec6fa to your computer and use it in GitHub Desktop.
A snippet for controlling a "Saitek Flight Multi Panel" with Python via HID.
# http://www.saitek.com/uk/prod-bak/multi.html
from pynput.keyboard import Key, Controller
import hid
import time
keyboard = Controller()
name = 'Pro Flight Multi Panel'
vend_id, product_id = None, None
for dev in hid.enumerate():
if dev['product_string'] == name:
print(dev)
vend_id = dev['vendor_id']
product_id = dev['product_id']
break
handlers = [
[[None] * 8, [None] * 8, [None] * 8],
[[None] * 8, [None] * 8, [None] * 8]
]
def multi_panel_handle(byte_idx, bit, non_zero):
def wrap(func):
if non_zero:
handlers[1][byte_idx][bit] = func
else:
handlers[0][byte_idx][bit] = func
return func
return wrap
@multi_panel_handle(1, 7, True)
def on_auto_throttle_arm():
print('Throttle ARM')
pass
@multi_panel_handle(1, 7, False)
def on_auto_throttle_off():
print('Throttle OFF')
pass
@multi_panel_handle(2, 0, True)
def on_flaps_up():
print('Flaps Up')
pass
@multi_panel_handle(2, 1, True)
def on_flaps_down():
print('Flaps Down')
pass
@multi_panel_handle(0, 5, True)
def on_tune_up():
print('Tune Up')
pass
@multi_panel_handle(0, 6, True)
def on_tune_down():
print('Tune Down')
pass
@multi_panel_handle(2, 2, True)
def on_pitch_trim_up():
print('Trim Up')
pass
@multi_panel_handle(2, 3, True)
def on_pitch_trim_down():
print('Trim Down')
pass
h = hid.device()
h.open(vend_id, product_id)
h.set_nonblocking(1)
h.send_feature_report([0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0])
for i in range(100):
bits = [0,
((i + 1) % 11),
((i + 2) % 11),
((i + 3) % 11),
((i + 4) % 11),
((i + 5) % 11),
((i + 1) % 11),
((i + 2) % 11),
((i + 3) % 11),
((i + 4) % 11),
((i + 5) % 11),
1 << (i % 8), 0]
print(bits)
print(len(bits))
print(h.send_feature_report(bits))
# time.sleep(0.01)
h.write([1, 5, 0, 3, 4, 5, 0, 0, 2, 3, 0, 255, 0])
while True:
data = h.read(64)
if data:
print(data)
a, b, c = data
print(bin(data[0]), bin(data[1]), bin(data[2]))
for i, val in enumerate([a, b, c]):
for bit in range(8):
idx = 1 if val & (1 << bit) else 0
func = handlers[idx][i][bit]
if func: func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment