-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NF: Add plugin backend to ButtonBoxComponent
- Loading branch information
TEParsons
committed
Nov 20, 2023
1 parent
139f2af
commit e1996cd
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from psychopy.experiment import Param, getInitVals | ||
from psychopy.localization import _translate | ||
from psychopy.experiment.components.buttonBox import ButtonBoxBackend | ||
|
||
|
||
class TPadButtonBoxBackend(ButtonBoxBackend, key="tpad", label="BBTK TPad"): | ||
""" | ||
Adds backend parameters for the BBTK TPad to the ButtonBoxComponent | ||
""" | ||
def getParams(self): | ||
""" | ||
Get parameters from this backend to add to each new instance of ButtonBoxComponent | ||
Returns | ||
------- | ||
dict[str:Param] | ||
Dict of Param objects, which will be added to any Button Box Component's params, along with a dependency | ||
to only show them when this backend is selected | ||
list[str] | ||
List of param names, defining the order in which params should appear | ||
""" | ||
# define order | ||
order = [ | ||
"serialPort", | ||
] | ||
# define params | ||
params = {} | ||
|
||
def getPorts(): | ||
""" | ||
Get a list of ports which have TPad devices connected. | ||
""" | ||
from psychopy_bbtk.tpad import TPad | ||
ports = [] | ||
# iterate through available button boxes | ||
for profile in TPad.getAvailableDevices(): | ||
# add this box's port | ||
ports.append( | ||
profile['port'] | ||
) | ||
|
||
params['serialPort'] = Param( | ||
"", valType="str", inputType="choice", categ="Device", | ||
allowedVals=getPorts, | ||
label=_translate("COM port"), | ||
hint=_translate( | ||
"Serial port to connect to" | ||
) | ||
) | ||
|
||
return params, order | ||
|
||
def addRequirements(self): | ||
""" | ||
Add any required module/package imports for this backend | ||
""" | ||
self.exp.requireImport( | ||
importName="tpad", | ||
importFrom="psychopy_bbtk" | ||
) | ||
|
||
def writeInitCode(self, buff): | ||
# get inits | ||
inits = getInitVals(self.params) | ||
# make Keyboard object | ||
code = ( | ||
"%(name)s = tpad.TPadButtonGroup(\n" | ||
" %(serialPort)s,\n" | ||
" channels=%(nButtons)s\n" | ||
")\n" | ||
) | ||
buff.writeIndentedLines(code % inits) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters