Python Documentation
Breadcrumbs

How to read button presses from a RESPONSEPixx

This demo shows how to read and report button presses using the RESPONSEPixx and the digital input log.

Python
from pypixxlib import _libdpx as dp
from psychopy import core


dp.DPxOpen()
dp.DPxEnableDinDebounce() #this tells the DIN logger to ignore the 30ms following a transition, so that the bounce of the button after a press/release isn’t logged 


#First, let's make a dictionary of codes that correspond to our buttons. This is in decimal.
#Note 1: these codes ARE NOT UNIVERSAL. You can check what your own button codes are using the PyPixx Digital I/O demo
#Note 2: these codes are for single button presses only. If two buttons are pressed at the same time this will generate a unique code, which we will ignore
buttonCodes = {65527:'blue', 65533:'yellow', 65534:'red', 65531:'green', 65519:'white', 65535:'button release'}
exitButton = 'white'
myLog = dp.DPxSetDinLog(12e6, 1000)
dp.DPxStartDinLog()
dp.DPxUpdateRegCache()
startTime = dp.DPxGetTime()
finished = False


#let's create a loop which checks the schedule at 0.25 s intervals for button presses.
#Any time a button press is found, we print the timestamp and button pressed.
#If a designated exit button is pressed, we disconnect.
while finished == False:
    #read device status
    dp.DPxUpdateRegCache()
    dp.DPxGetDinStatus(myLog)
    newEvents = myLog["newLogFrames"]
    
    if newEvents > 0:
        eventList = dp.DPxReadDinLog(myLog, newEvents)
        for x in eventList:
            if x[1] in buttonCodes:
                #look up the name of the button
                buttonID = buttonCodes[x[1]]
                #get the time of the press, since we started logging
                time = round(x[0] - startTime, 2)
                printStr = 'Button pressed! Button code: ' + str(x[1]) + ', Button ID: ' + buttonID + ', Time:' + str(time)
                print(printStr)
                if buttonID == exitButton:
                    finished = True
    core.wait(0.25)
    
#Stop logging
dp.DPxStopDinLog()
dp.DPxUpdateRegCache()
dp.DPxDisableDinDebounce()