forked from eosfrontier/heartmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.py
executable file
·239 lines (224 loc) · 8.18 KB
/
heartbeat.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
import time, sys, os, random, smbus, socket
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
import pyaudio, wave
from neopixel import *
import MAX30102
import NFCReader
import Paddles
leds = Adafruit_NeoPixel(2, 13, channel=1)
mx = MAX30102.MAX30102()
beepwav = wave.open("audio/beep.wav","rb")
pa = pyaudio.PyAudio()
stream = pa.open(format = pa.get_format_from_width(beepwav.getsampwidth()),
channels = beepwav.getnchannels(),
rate = beepwav.getframerate(),
output = True)
beep = beepwav.readframes(4096)
i2c = smbus.SMBus(1)
mcp = 0x20
mcp_iodir = 0x00
mcp_gppu = 0x0c
mcp_gpio = 0x12
i2c.write_word_data(mcp, mcp_iodir, 0xFFFF)
i2c.write_word_data(mcp, mcp_gppu, 0xFFFF)
i2c.write_word_data(mcp, mcp_gpio, 0x0000)
leds.begin()
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
options.disable_hardware_pulsing = True
options.pixel_mapper_config = "Rotate:180"
#nfcRdr = NFCReader.NFCReader()
#nfcRdr.run()
paddles = Paddles.Paddles()
matrix = RGBMatrix(options = options)
acmefont = graphics.Font()
acmefont.LoadFont('/home/pi/src/rpi-rgb-led-matrix/fonts/6x13.bdf')
ipfont = graphics.Font()
ipfont.LoadFont('/home/pi/src/rpi-rgb-led-matrix/fonts/5x8.bdf')
def show_ipaddress(matrix, font):
ipaddress = ((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])
ypos = 64 - 5*len(ipaddress)
graphics.DrawText(matrix, font, ypos, 30, graphics.Color(255, 0, 255), ipaddress)
pixs = [255,255,255,255,255,0]
leds.setPixelColor(0, Color(pixs[2], pixs[1], pixs[0]))
leds.setPixelColor(1, Color(pixs[5], pixs[4], pixs[3]))
leds.show()
try:
matrix.Fill(0, 0, 0)
print "Starting process"
graphics.DrawText(matrix, acmefont, 2, 20, graphics.Color(255, 255, 0), 'ACME')
graphics.DrawText(matrix, acmefont, 32, 20, graphics.Color(0, 255, 255), 'Cura')
heartbeatdelay = 0
heartmode = 0
cur = 0.4
tgt = 0.4
spd = 0.1
zapcnt = 0
zaptry = 0
beat = False
plotpos = 0
while True:
"""
uid = nfcRdr.poll()
if uid:
print "Got NFC card "+uid
if uid == 'e4c3b6c3' and heartbeatdelay != 120:
print "Stop beat"
heartbeatdelay = 120
spd = 0.1
tgt = 0.4
if uid == '9349a524' and heartbeatdelay == 120:
print "Start beat"
heartbeatdelay = -100
spd = 0.1
tgt = 0.4
"""
if heartmode != 0:
for y in range(0, matrix.height):
#matrix.SetPixel(plotpos, y, 0, 0, 0)
#matrix.SetPixel(plotpos+1, y, 0, 0, 0)
#matrix.SetPixel(plotpos+2, y, 0, 0, 0)
matrix.SetPixel((plotpos+3)%matrix.width, y, 0, 0, 0)
matrix.SetPixel((plotpos+4)%matrix.width, y, 0, 0, 0)
if heartmode == 2:
mx.update()
tgt = 0.5 - (mx.ir / 1200.0)
spd = 1.0
if tgt > 1.0:
tgt = 1.0
if tgt < 0.0:
tgt = 0.0
if tgt > 0.8 and not beat:
beat = True
stream.write(beep)
if tgt < 0.4 and beat:
beat = False
elif heartmode == 1:
if heartbeatdelay == 10:
tgt = random.randint(15,25)/100.0
spd = 0.1
elif heartbeatdelay == 15:
tgt = random.randint(90,90)/100.0
spd = 0.3
stream.write(beep)
elif heartbeatdelay == 18:
tgt = random.randint(5,15)/100.0
spd = 0.2
elif heartbeatdelay == 22:
tgt = random.randint(50,60)/100.0
spd = 0.1
elif heartbeatdelay >= 27 and heartbeatdelay < 35:
tgt = random.randint(35,45)/100.0
spd = 0.1
elif heartbeatdelay ==35:
tgt = 0.4
spd = 0.1
elif heartbeatdelay == 40:
heartbeatdelay = random.randint(-5,5)
ypos1 = int(matrix.height/2 * (1.0-cur))
if cur < tgt:
cur = cur + spd
if cur > tgt:
cur = tgt
spd = 0.0
ypos2 = int(matrix.height/2 * (1.0-cur))
for y in range(ypos2, ypos1):
matrix.SetPixel(plotpos, y, 0, 255, 0)
elif cur > tgt:
cur = cur - spd
if cur < tgt:
cur = tgt
spd = 0.0
ypos2 = int(matrix.height/2 * (1.0-cur))
for y in range(ypos1, ypos2):
matrix.SetPixel(plotpos, y, 0, 255, 0)
matrix.SetPixel(plotpos, ypos1, 0, 200, 0)
if heartbeatdelay > -100 and heartbeatdelay <= 100:
heartbeatdelay += 1
try:
buttons = i2c.read_word_data(mcp, mcp_gpio)
"""
for x in range(0,16):
if ((buttons >> x) & 1) == 0:
if x >= 0 and x <= 4:
pixs[x] = 0
matrix.SetPixel(x*4+2, 30, 0, 0, 255)
matrix.SetPixel(x*4+3, 30, 0, 64, 64)
matrix.SetPixel(x*4+1, 30, 0, 64, 64)
matrix.SetPixel(x*4+2, 31, 0, 64, 64)
matrix.SetPixel(x*4+2, 29, 0, 64, 64)
else:
if x >= 0 and x <= 4:
pixs[x] += 2
if pixs[x] >= 255:
pixs[x] = 255
matrix.SetPixel(x*4+2, 30, 0, 0, 0)
matrix.SetPixel(x*4+3, 30, 0, 0, 0)
matrix.SetPixel(x*4+1, 30, 0, 0, 0)
matrix.SetPixel(x*4+2, 31, 0, 0, 0)
matrix.SetPixel(x*4+2, 29, 0, 0, 0)
"""
if (not buttons & 0b0000000000001000) and heartmode == 0:
print "Starting heart monitor"
heartmode = 1
elif (not buttons & 0b0000000000010000) and heartmode == 1:
print "Stopping heart monitor"
heartmode = 0
if (not buttons & 0b0000000010000000) and heartbeatdelay != 120:
print "Stop beat"
heartbeatdelay = 120
spd = 0.1
tgt = 0.4
zaptry = 0
elif (not buttons & 0b0000000000100000) and heartbeatdelay == 120:
print "Start beat"
heartbeatdelay = -10
spd = 0.1
tgt = 0.4
if (not buttons & 0b0000000000000010):
show_ipaddress(matrix, ipfont)
except:
pass
paddles.read()
buttons = paddles.buttons.values()
if len(buttons) == 2 and buttons[0] and buttons[1]:
if zapcnt == 0:
paddles.command("color 100,0,0")
paddles.command("sound")
zapcnt += 1
if zapcnt > 100:
paddles.command("color 0,100,0")
elif zapcnt > 100:
paddles.command("color 0,0,100")
paddles.command("flash")
paddles.command("color 0,0,0")
if heartmode != 0 and heartbeatdelay == 120:
zaptry += 1
if zapcnt < (80 + (zaptry * 30)):
print "It worked! Start beat!"
heartbeatdelay = -99
spd = 0.1
tgt = 0.4
zapcnt = 0
elif zapcnt > 0:
zapcnt = 0
paddles.command("color 0,0,0")
paddles.command("soundoff")
time.sleep(0.02)
if heartmode != 0:
plotpos = (plotpos + 1) % matrix.width
else:
plotpos = 0
except KeyboardInterrupt:
print "Stopping"
pass
finally:
leds.setPixelColor(0, Color(0, 0, 0))
leds.setPixelColor(1, Color(0, 0, 0))
leds.show()
paddles.command("color 0,0,0")
paddles.command("soundoff")
#mx.close()
sys.exit(0)