Controlling the RESPONSEPixx via the I/O Hub
This demo shows how to set up the I/O hub included with your TRACKPixx /Mini for RESPONSEPixx button box management. It uses the joystick element from the pygame package to record inputs, and the serial package to get the hub time and write LED intensities to the button box.
"""
USB_IO_HUB_Script.py
Demo file to show how to use the gamepad device and serial port interface.
The demo will print up to 10 detection events on the connected RESPONSEPixx.
The IO Hub enumerates as two devices: 1. Gamepad Controller, 2. Serial interface
The gamepad interface is polled for keypress events.
Whenever a keypress is detected, the serial interface is used to get the event time.
The serial interface can also be used to get the current time.
This is useful to set the referece time of an experiment.
Another use of the serial interface is to set the button LED intensity ranging from 0 to 100.
"""
import pygame
import serial
from time import sleep
import time
import ctypes, os
pygame.joystick.init()
pygame.init()
_joystick = pygame.joystick.Joystick(0)
_joystick.init()
# The following variable is to be changed for a particular OS.
serialPortNumber = 'COM4'
serialPort = serial.Serial(serialPortNumber, timeout = 1)
buttonDict = {0: 'RED', 1 : 'YELLOW', 2 : 'GREEN', 3 : 'BLUE', 4 : 'WHITE'}
commandDict = {'Current' : 0, 'Event' : 1, 'Release' : 2}
# There are three types of response time that we can query. All times are on the ms scale.
# An event is when a button is pressed but not released.
# 0 - Get current system time from the moment the IO-Hub was plugged in a Host.
# 1 - Get the event time if any. Will return 0 if no event has been generated yet.
# 2 - Get the release time if any. Will return 0 is no release has been generated yet.
def getTime(ser, timeType):
ser.flushInput()
if timeType == 0:
ser.write(b'GetCurrentTime\r')
elif timeType == 1:
ser.write(b'GetEventTime\r')
elif timeType == 2:
ser.write(b'GetReleaseTime\r')
return int(ser.readline().strip())
# A LED can be set to a particular % intensity between 0 and 100.
def setLedIntensity(ser, led, intensity):
ser.write(b'SetLed%d%d\r'%(led, intensity))
def getCurrentVersion(ser):
ser.write(b'GetVersion\r')
return int(ser.readline().strip()[1:])
def setAllLed(ser, state):
if state==True:
ser.write(b'SetLedAllOn\r')
else:
ser.write(b'SetLedAllOff\r')
print("USB IO Hub test")
print("Current time %d"%(getTime(serialPort, commandDict['Current'])))
print("Current version %d"%(getCurrentVersion(serialPort)))
t0 = getTime(serialPort, commandDict['Current'])
setAllLed(serialPort, True)
sleep(2)
setAllLed(serialPort, False)
doTest = True
buttonState = [False] * 10
while doTest:
event = pygame.event.wait()
if event.type == pygame.JOYBUTTONDOWN:
for i in range(0, 10):
if _joystick.get_button(i):
if buttonState[i]:
continue
buttonState[i] = True
currentEventTime = getTime(serialPort, commandDict['Event'])
print("Button Pressed %s at time %d ms"%(buttonDict[i], currentEventTime-t0))
setLedIntensity(serialPort, i, 50)
elif event.type == pygame.JOYBUTTONUP:
for i in range(0, 5):
if _joystick.get_button(i) == False and buttonState[i] == True:
currentEventTime = getTime(serialPort, commandDict['Release'])
print("Button %s released at time %d ms"%(buttonDict[i], currentEventTime-t0))
setLedIntensity(serialPort, i, 0)
buttonState[i] = False
if buttonDict[i] == 'WHITE':
doTest = False
_joystick.quit( )
serialPort.close()