-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
95 lines (74 loc) · 2.18 KB
/
monitor.py
File metadata and controls
95 lines (74 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# monitor.py
#
# (c) 2012 Konstantin Sering <konstantin.sering [aet] gmail.com>
#
# GPL 3.0+ or (cc) by-sa (http://creativecommons.org/licenses/by-sa/3.0/)
#
# content: (1) class Monitor
#
# input: --
# output: --
#
# created 2012-05-29 KS
# last mod 2013-04-16 16:02 KS
"""
This module provides the class Monitor, which encapsulates the presentation
of a color stimuli on the monitor.
It is deprecated to use any other code than this in order to present one
color and collect key presses.
"""
from psychopy import visual, event, core
class Event(object):
"""
Very small class to store a key stroke.
"""
def __init__(self):
"""
Add attribute key.
"""
self.key = ""
class Monitor(object):
"""
Provides a convenient interface to present a color on the monitor and
get key strokes.
Example:
>>> mon = Monitor()
>>> mon.setColor("#00FF00FF")
"""
def __init__(self, psychopy_win=None):
self.psychopy_win = psychopy_win
if not psychopy_win:
self.psychopy_win = visual.Window(size=(800, 600))
self.patch_stim = visual.GratingStim(self.psychopy_win, tex=None,
size=(2, 2), color=0, colorSpace="rgb255")
self.e = Event() # small object storing one key
def setColor(self, psychopy_color, colorSpace="rgb255"):
"""
Presents one color at the monitor.
"""
self.patch_stim.setColor(psychopy_color, colorSpace=colorSpace)
self.patch_stim.draw()
self.psychopy_win.flip()
def waitForButtonPress(self):
"""
Waits for a key press and stores button press in self.e.key.
"""
event.clearEvents()
self.e.key = ""
while not self.e.key:
core.wait(0.01)
keys = event.getKeys()
if keys:
self.e.key = keys[0]
def checkForButtonPress(self):
"""
Checks for a key press and stores key press in self.e.key.
"""
self.e.key = ""
core.wait(0.01)
keys = event.getKeys()
if keys:
self.e.key = keys[0]
event.clearEvents()