import pyiforce as ifr import serial import pyvjoy import pyvjoy_ffb as ffb 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) ffb.add_ffb(j) device_state = 0 def handle_ffb(ffb_type, value, packet): global device_state if ffb_type == ffb.FFBPType.PT_EFFREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_ENVREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_CONDREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_PRIDREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_CONSTREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_RAMPREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_CSTMREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_SMPLREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_EFOPREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_BLKFRREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_CTRLREP: if value == ffb.FFB_CTRL.CTRL_ENACT: device_state |= ifr.DeviceState.ENABLE_FFB elif value == ffb.FFB_CTRL.CTRL_DISACT: device_state &= ~ifr.DeviceState.ENABLE_FFB elif value == ffb.FFB_CTRL.CTRL_STOPALL: device_state |= ifr.DeviceState.STOP_ALL elif value == ffb.FFB_CTRL.CTRL_DEVRST: device_state = 0 elif value == ffb.FFB_CTRL.CTRL_DEVPAUSE: device_state |= ifr.DeviceState.PAUSE_FFB elif value == ffb.FFB_CTRL.CTRL_DEVCONT: device_state &= ~ifr.DeviceState.PAUSE_FFB send_pkt(port, ifr.Packet.SET_DEVICE_STATE, [device_state]) elif ffb_type == ffb.FFBPType.PT_GAINREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_SETCREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_NEWEFREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_BLKLDREP: print('not implemented: %02x' % ffb_type) elif ffb_type == ffb.FFBPType.PT_POOLREP: print('not implemented: %02x' % ffb_type) # FFB_CTRL # FFB_DATA # FFB_EFF_COND # FFB_EFF_CONSTANT # FFB_EFF_ENVLP # FFB_EFF_OP # FFB_EFF_PERIOD # FFB_EFF_RAMP # FFB_EFF_REPORT_Dir # FFB_EFF_REPORT # FFB_EFFECTS # FFBEType # FFBOP # FfbPacket # FfbPacketHelper # FFBPType # 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) # ifr.set_handler(ifr.Packet.INPUT_WHEEL, print) while True: ifr.rx_pump()