pyiforce/test.py

79 lines
2.3 KiB
Python
Raw Normal View History

2020-10-30 02:00:14 +01:00
import pyiforce as ifr
import serial
import pyvjoy
2020-10-30 03:39:05 +01:00
import pyvjoy_ffb as ffb
2020-10-30 02:00:14 +01:00
PORT = 'COM7'
BAUD = 38400
def send_pkt(port, command, data):
pkt = bytes(ifr.tx_packet(command, data))
port.write(pkt)
def hex_print(d):
print(' '.join('%02x'%x for x in d))
def query_print(d):
print('QUERY[%02x]:%s'%(d['query'],' '.join('%02x'%x for x in d['data'])))
def setup_wheel(port):
ifr.set_handler(ifr.Packet.QUERY, query_print)
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.Manufacturer])
ifr.rx_pump()
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.Product])
ifr.rx_pump()
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.Version])
ifr.rx_pump()
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.NumberOfEffects])
ifr.rx_pump()
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.BufferSize])
ifr.rx_pump()
# send_pkt(port, ifr.Packet.SET_OVERALL, [ifr.OverallControlType.GAIN, 0x80])
send_pkt(port, ifr.Packet.QUERY, [ifr.Query.Open])
def serial_reader(sp):
while sp.is_open:
in_byte = sp.read(1)[0]
# print(f'rx:%02X'%in_byte)
yield in_byte
with serial.Serial(port=PORT, baudrate=BAUD) as port:
port.rts = True
ifr.create_rx_stream(serial_reader(port))
setup_wheel(port)
j = pyvjoy.VJoyDevice(1)
2020-10-30 03:39:05 +01:00
ffb.add_ffb(j)
2020-10-30 02:00:14 +01:00
# def handle_wheel(w):
# j.data.wAxisX = int(0x4000 * (w['axis']['wheel'] + 1))
# j.data.wAxisY = int(0x8000 * w['axis']['gas'])
# j.data.wAxisZ = int(0x8000 * w['axis']['brake'])
# j.data.lButtons = (
# 0x0000000000000001 if w['buttons']['A'] else 0) + (
# 0x0000000000000002 if w['buttons']['B'] else 0) + (
# 0x0000000000000004 if w['buttons']['1_PaddleUp_Start'] else 0) + (
# 0x0000000000000008 if w['buttons']['2_PaddleDown'] else 0) + (
# 0x0000000000000010 if w['buttons']['3'] else 0) + (
# 0x0000000000000020 if w['buttons']['4'] else 0)
# j.data.bHats = (
# -1 if w['hat']['center'] else
# 0 if w['hat']['up'] else
# 1 if w['hat']['right'] else
# 2 if w['hat']['down'] else
# 3 if w['hat']['left'] else -1)
# j.update()
# ifr.set_handler(ifr.Packet.INPUT_WHEEL, handle_wheel)
2020-10-30 03:39:05 +01:00
# ifr.set_handler(ifr.Packet.INPUT_WHEEL, print)
2020-10-30 02:00:14 +01:00
while True:
ifr.rx_pump()