-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpygame_example.py
85 lines (60 loc) · 1.88 KB
/
pygame_example.py
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
# # # # #
# CONSTANTS
DUMMY = False
RES = (640,480)
# # # # #
# IMPORTS
# external
import pygame
# custom
if not DUMMY:
from mpydev import BioPac
# # # # #
# STARTUP
# Initialize PyGame.
pygame.init()
# Create a new display.
disp = pygame.display.set_mode(RES, pygame.RESIZABLE)
# Initialise a font to display text.
font = pygame.font.Font(pygame.font.get_default_font(), 24)
# Start communication with a BioPac MP150 device.
if not DUMMY:
mp = BioPac("MP150", n_channels=3, samplerate=200, logfile="test", overwrite=True)
# # # # #
# RUN
# Run until the user quits.
quited = False
while not quited:
# Start recording data to file.
if not DUMMY:
mp.start_recording()
# Get the newest sample. Note that you DO NOT have to do this to record
# data! Data is recorded in the background after calling 'start_recording',
# the sample method is only here to use samples within the experiment,
# for example to create a visual effect.
if not DUMMY:
sample = mp.sample()
else:
sample = 16 * [0.0]
# Render text.
textsurf = font.render("sample = %.3f, %.3f, %.3f" % (sample[0], sample[1], sample[2]), False, (255,255,255))
blitpos = (int(RES[0]/2 - textsurf.get_width()/2), int(RES[1]/2 - textsurf.get_height()/2))
# Blit text to the display Surface.
disp.fill((0,0,0))
disp.blit(textsurf, blitpos)
# Update the display.
pygame.display.flip()
# Check if there was any keyboard input.
for event in pygame.event.get(pygame.KEYDOWN):
if pygame.key.name(event.key) == 'escape':
quited = True
# Stop recording data.
if not DUMMY:
mp.stop_recording()
# # # # #
# CLOSE
# Close the connection to the BIOPAC device.
if not DUMMY:
mp.close()
# Close the display.
pygame.display.quit()