forked from quicksilv3rflash/Precision_tES_tACS_tRNS_tDCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KludgeGUI.py
475 lines (416 loc) · 18.5 KB
/
KludgeGUI.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#this is my horribly coded but thoroughly debugged GUI
#it'll work but I'm writing the core functions into a separate library
#with better comments and clearer code flow
#This is spaghetti code. I have verified that it controls the device
#accurately with an oscilloscope. The GUI also reflects the artifacts
#that appear in the device's output waveform as frequency increases ^.^
whichport = input("Enter the serial port location the Arduino is connected to,\nfor example, COM6. This location is displayed in the Arduino IDE\nunder Tools>PORT:")
#certified tested + functional 2017-02-26 04h55
#SYSTEM DEVELOPED ON SOFTWARE VERSIONS:
# * Arduino 1.8.1
# * Python 3.2.5
# * PySerial 2.6
# * PyGame 1.9.2pre
# * SYSTEM DEVELOPED ON ARDUINO HARDWARE:
# * "MINI USB Nano V3.0 ATmega328P CH340G 5V 16M"
# */ <- lol C comment fragments
#assume 512 microseconds / sample!
#main loop iterates every 65.536 ms (approximately 15.25 FPS)
import random, pygame, sys, math, time, serial
from time import sleep
from pygame.locals import *
#generate waveform produces sine, square, triangle, ramp_up
#offset = dc bias
def generate_wave_points(waveform,frequency,amplitude,offset):
if(offset > 2.002):
offset = 2.001
if(offset < -2.002):
offset = -2.001
if(amplitude > 2.002):
amplitude = 2.001
samples_per_cycle = int(round(1/(float(frequency) * .000512)))
wave_list = [0]* samples_per_cycle
if(waveform == "sine"):
for x in range (0, samples_per_cycle):
wave_list[x] = amplitude*(math.sin(2*(math.pi)*(x/samples_per_cycle))) + offset
wave_list[samples_per_cycle - 1] = (wave_list[0] + wave_list[samples_per_cycle - 2])/2.0
if(waveform == "square"):
for x in range (0, samples_per_cycle):
if(x < round(samples_per_cycle/2)):
wave_list[x] = amplitude + offset
if(x >= round(samples_per_cycle/2)):
wave_list[x] = (-1*amplitude) + offset
if(waveform == "triangle"):
for x in range (0, samples_per_cycle):
if(x < round(samples_per_cycle/4)):
wave_list[x] = amplitude * (x/(samples_per_cycle/4)) + offset
if((x>=round(samples_per_cycle/4)) and (x<round((3*samples_per_cycle) / 4))):
wave_list[x] = (amplitude + offset)- amplitude*((x - (samples_per_cycle /4))/(samples_per_cycle / 4))
if(x >= round((3*samples_per_cycle)/4)):
wave_list[x] = (-amplitude + offset) + amplitude *((x - ((3*samples_per_cycle) /4))/(samples_per_cycle / 4))
if(waveform == "ramp_up"):
for x in range (0, samples_per_cycle):
wave_list[x] = (-amplitude) + amplitude*(x/(samples_per_cycle/2.0)) + offset
if(waveform == "ramp_down"):
for x in range (0, samples_per_cycle):
wave_list[x] = (-1)*((-amplitude) + amplitude*(x/(samples_per_cycle/2.0))) + offset
if(waveform == "random"):
for x in range (0, samples_per_cycle):
if((x % 2)==0):
wave_list[x] = (1.0 - (random.random()*2))*amplitude + offset
else:
wave_list[x] = wave_list[x-1]
for x in range(0, len(wave_list)):
if(wave_list[x] > 2.002): #check positive limit
wave_list[x] = 2.001
if(wave_list[x] < -2.002):#check negative limit
wave_list[x] = -2.001
return wave_list
#this function converts mA values to appropriate DAC write values
def mA_2_DAC_write(value_in_mA):
if(value_in_mA > 2.002): #check positive limit
value_in_mA = 2.001
if(value_in_mA < -2.002):#check negative limit
value_in_mA = -2.001
dacwrite = (int(round(((16383*1.0866)/5)*(2.5-value_in_mA)))).to_bytes(2,byteorder="big",signed=False)
#That horrible line of code converts mA values into correct format
#for the DAC. First it runs the values through the linear equation
#that converts them to appropriate DAC values, then it ensures the
#data are formatted in 16-bit unsigned integers.
return dacwrite
#This function fills half of the 0.131072 seconds of buffer on the device.
#It requires a list of 128 values in mA;
# output sample rate: one value per 512us.
def tx_128_mA_values(listof128_values_in_mA):
data_to_transmit = [0] * 128
for x in range(0, 128):
data_to_transmit[x] = mA_2_DAC_write(listof128_values_in_mA[x])
data_sent = 0
while (data_sent < 1):
if (ser.inWaiting() > 0):
for x in range(0, 128):
ser.write(data_to_transmit[x])
ser.read(ser.inWaiting())
data_sent += 1
return
def addindicator(indicated_mode):
returnstring = " "
if (which_mode == indicated_mode):
returnstring += "<<"
return returnstring
def setactivecolor(indicated_mode):
returntuple = (160,100,100)
if (which_mode == indicated_mode):
returntuple = (255,0,0)
return returntuple
def updateGUI():
global displaysurface
global wave_points
global which_mode
global frequency
global amplitude
global dc_bias
global halted
updatewave = 0
displaysurface.fill((0,0,0))
pygame.draw.line(displaysurface,(255,0,0),(0,230),(305,230),2)
pygame.draw.line(displaysurface,(255,0,0),(305,230),(305,480),2)
pygame.draw.line(displaysurface,(100,100,160),(325,310),(640,310),2)
pygame.draw.line(displaysurface,(100,100,160),(325,395),(640,395),2)
pygame.draw.line(displaysurface,(0,0,255),(325,230),(640,230),2)
pygame.draw.line(displaysurface,(0,0,255),(325,230),(325,480),2)
font = pygame.font.Font(None, 50)
if(halted != 0):
introtext = font.render("[ RUN STIMULATOR ]",0,(0,255,0))
statustext = font.render("HALTED",0,(100,100,160))
if(halted == 0):
introtext = font.render("[HALT STIMULATOR]",0,(255,0,0))
statustext = font.render("RUNNING",0,(100,100,160))
runstop = displaysurface.blit(introtext, (5,190))
displaysurface.blit(statustext,(400,190))
font = pygame.font.Font(None, 150)
if(halted != 0):
introtext = font.render("STOPPED",0,(100,100,160))
else:
introtext = font.render(" ",0,(0,0,0))
displaysurface.blit(introtext, (50,50))
font = pygame.font.Font(None, 36)
introtext = font.render(" - - SET MODE - -", 0,(200,50,50))
displaysurface.blit(introtext, (5,240))
introtext = font.render("[ DIRECT CURRENT ]" + addindicator("dc"), 0, setactivecolor("dc"))
dcbutton = displaysurface.blit(introtext, (5,270))
introtext = font.render("[ SINE WAVE ]" + addindicator("sine"), 0,setactivecolor("sine"))
sinebutton = displaysurface.blit(introtext, (5, 300))
introtext = font.render("[ SQUARE WAVE ]" + addindicator("square"), 0,setactivecolor("square"))
squarebutton = displaysurface.blit(introtext, (5, 330))
introtext = font.render("[ TRIANGLE WAVE ]" + addindicator("triangle"), 0,setactivecolor("triangle"))
trianglebutton = displaysurface.blit(introtext, (5, 360))
introtext = font.render("[ SAWTOOTH (UP) ]" + addindicator("ramp_up"), 0,setactivecolor("ramp_up"))
sawtoothupbutton = displaysurface.blit(introtext, (5, 390))
introtext = font.render("[SAWTOOTH(DOWN)]" + addindicator("ramp_down"), 0,setactivecolor("ramp_down"))
sawtoothdownbutton = displaysurface.blit(introtext, (5, 420))
introtext = font.render("[ RANDOM NOISE ]" + addindicator("random"), 0,setactivecolor("random"))
randombutton = displaysurface.blit(introtext, (5, 450))
introtext = font.render("[10]",0,(0,0,255))
hzmns10 = displaysurface.blit(introtext, (340,235))
introtext = font.render("[1]",0,(0,0,255))
hzmns1 = displaysurface.blit(introtext, (395,235))
introtext = font.render("[.1]",0,(0,0,255))
hzmnspt1 = displaysurface.blit(introtext, (435,235))
introtext = font.render("-|+",0,(100,100,160))
displaysurface.blit(introtext, (475,235))
introtext = font.render("[.1]",0,(0,0,255))
hzplspt1 = displaysurface.blit(introtext, (510,235))
introtext = font.render("[1]",0,(0,0,255))
hzpls1 = displaysurface.blit(introtext, (555,235))
introtext = font.render("[10]",0,(0,0,255))
hzpls10 = displaysurface.blit(introtext, (590,235))
introtext = font.render("[1]",0,(0,0,255))
ampmns1 = displaysurface.blit(introtext, (340,320))
introtext = font.render("[.1]",0,(0,0,255))
ampmnspt1 = displaysurface.blit(introtext, (380,320))
introtext = font.render("[.01]",0,(0,0,255))
ampmnspt01 = displaysurface.blit(introtext, (420,320))
introtext = font.render("-|+",0,(100,100,160))
displaysurface.blit(introtext, (475,320))
introtext = font.render("[.01]",0,(0,0,255))
ampplspt01 = displaysurface.blit(introtext, (510,320))
introtext = font.render("[.1]",0,(0,0,255))
ampplspt1 = displaysurface.blit(introtext, (565,320))
introtext = font.render("[1]",0,(0,0,255))
amppls1 = displaysurface.blit(introtext, (605,320))
introtext = font.render("[1]",0,(0,0,255))
dcbmns1 = displaysurface.blit(introtext, (340,405))
introtext = font.render("[.1]",0,(0,0,255))
dcbmnspt1 = displaysurface.blit(introtext, (380,405))
introtext = font.render("[.01]",0,(0,0,255))
dcbmnspt01 = displaysurface.blit(introtext, (420,405))
introtext = font.render("-|+",0,(100,100,160))
displaysurface.blit(introtext, (475,405))
introtext = font.render("[.01]",0,(0,0,255))
dcbplspt01 = displaysurface.blit(introtext, (510,405))
introtext = font.render("[.1]",0,(0,0,255))
dcbplspt1 = displaysurface.blit(introtext, (565,405))
introtext = font.render("[1]",0,(0,0,255))
dcbpls1 = displaysurface.blit(introtext, (605,405))
if(which_mode != "dc" and which_mode != "random"):
introtext = font.render("FREQUENCY(Hz):", 0, (100,100,160))
displaysurface.blit(introtext, (340,260))
if((frequency > 0.009) and (frequency < 0.011)):
lagwarning = " (expect lag)"
else:
lagwarning = " "
introtext = font.render(" " + str(round(frequency,2)) + lagwarning,0,(180,100,180))
displaysurface.blit(introtext, (340,285))
if(which_mode != "dc"):
introtext = font.render("AMPLITUDE(mA peak):",0, (100,100,160))
displaysurface.blit(introtext, (340,345))
introtext = font.render(" " + str(round(amplitude,2)),0,(180,100,180))
displaysurface.blit(introtext, (340,370))
introtext = font.render("DC BIAS(mA):",0, (100,100,160))
displaysurface.blit(introtext, (340,430))
introtext = font.render(" " + str(round(dc_bias,2)),0, (180,100,180))
displaysurface.blit(introtext, (340,455))
pygame.draw.line(displaysurface,(100,70,70),(10,20),(560,20),3)
pygame.draw.line(displaysurface,(100,70,70),(10,180),(560,180),3)
pygame.draw.line(displaysurface,(100,70,70),(10,100),(560,100),3)
if((which_mode != "dc") and (which_mode != "random")):
pygame.draw.line(displaysurface,(100,70,70),(505,30),(505,170),3)
pygame.draw.line(displaysurface,(100,70,70),(303,30),(303,170),3)
pygame.draw.line(displaysurface,(100,70,70),(303,140),(505,140),3)
introtext = font.render("+2mA", 0, (120,120,120))
displaysurface.blit(introtext, (560,20))
introtext = font.render("+0mA", 0, (120,120,120))
displaysurface.blit(introtext, (560,100))
introtext = font.render("-2mA", 0, (120,120,120))
displaysurface.blit(introtext, (560,180))
listlength = len(wave_points)
if(which_mode != "random"):
for x in range (0,300):
pygame.draw.line(displaysurface,(240,240,255),(10+(5*(x/3)),100 - (40*wave_points[round((x/3)*(listlength/40)) % listlength])),(15 +(5*(x/3)),100 - (40*wave_points[round(((x/3)+1)*(listlength/40)) % listlength])),2)
else:
for x in range (0,100):
pygame.draw.line(displaysurface,(240,240,255),(10+(5*x),100 - (40*wave_points[round(x*(listlength/40)) % listlength])),(15 +(5*x),100 - (40*wave_points[round(x*(listlength/40)) % listlength])),2)
if(which_mode == "random"):
introtext = font.render("1 value per 1024 us", 0, (120,120,120))
else:
if(which_mode == "dc"):
introtext = font.render(" ",0,(0,0,0))
else:
if(frequency < 1):
introtext = font.render(str(round(1/frequency,2)) + " seconds",0,(120,120,120))
else:
introtext = font.render(str(round(1000/frequency,2)) + " milliseconds",0,(120,120,120))
displaysurface.blit(introtext,(305,143))
pygame.display.update()
#quit cleanly. The user input event handler lives here too.
for event in pygame.event.get():
if(event.type == QUIT):
pygame.quit()
sys.exit()
if(event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
cursor = pygame.mouse.get_pos()
if(dcbutton.collidepoint(cursor)):
which_mode = "dc"
updatewave = 1
if(sinebutton.collidepoint(cursor)):
wave_points = generate_wave_points("sine",frequency,2,0)
which_mode = "sine"
updatewave = 1
if(squarebutton.collidepoint(cursor)):
wave_points = generate_wave_points("square",frequency,2,0)
which_mode = "square"
updatewave = 1
if(trianglebutton.collidepoint(cursor)):
wave_points = generate_wave_points("triangle",frequency,2,0)
which_mode = "triangle"
updatewave = 1
if(sawtoothupbutton.collidepoint(cursor)):
wave_points = generate_wave_points("ramp_up",frequency,2,0)
which_mode = "ramp_up"
updatewave = 1
if(sawtoothdownbutton.collidepoint(cursor)):
wave_points = generate_wave_points("ramp_down",frequency,2,0)
which_mode = "ramp_down"
updatewave = 1
if(randombutton.collidepoint(cursor)):
which_mode = "random"
updatewave = 1
if(hzpls10.collidepoint(cursor)):
if(frequency <= 0.09):
frequency = 10
else:
frequency += 10
updatewave = 1
if(hzpls1.collidepoint(cursor)):
if(frequency <= 0.9):
frequency = 1
else:
frequency += 1
updatewave = 1
if(hzplspt1.collidepoint(cursor)):
if(frequency <= 0.09):
frequency = 0.1
else:
frequency += 0.1
updatewave = 1
if(hzmnspt1.collidepoint(cursor)):
if(frequency >= 0.11):
frequency -= 0.1
else:
frequency = 0.01
updatewave = 1
if(hzmns1.collidepoint(cursor)):
if(frequency >= 1.01):
frequency -= 1
else:
frequency = 0.01
updatewave = 1
if(hzmns10.collidepoint(cursor)):
if(frequency >= 10.01):
frequency -= 10
else:
frequency = 0.01
updatewave = 1
if(amppls1.collidepoint(cursor)):
amplitude += 1
updatewave = 1
if(ampplspt1.collidepoint(cursor)):
amplitude +=0.1
updatewave = 1
if(ampplspt01.collidepoint(cursor)):
amplitude += 0.01
updatewave = 1
if(ampmnspt01.collidepoint(cursor)):
amplitude -= 0.01
updatewave = 1
if(ampmnspt1.collidepoint(cursor)):
amplitude -= 0.1
updatewave = 1
if(ampmns1.collidepoint(cursor)):
amplitude -= 1
updatewave = 1
if(dcbpls1.collidepoint(cursor)):
dc_bias += 1
updatewave = 1
if(dcbplspt1.collidepoint(cursor)):
dc_bias +=0.1
updatewave = 1
if(dcbplspt01.collidepoint(cursor)):
dc_bias += 0.01
updatewave = 1
if(dcbmnspt01.collidepoint(cursor)):
dc_bias -= 0.01
updatewave = 1
if(dcbmnspt1.collidepoint(cursor)):
dc_bias -= 0.1
updatewave = 1
if(dcbmns1.collidepoint(cursor)):
dc_bias -= 1
updatewave = 1
if(runstop.collidepoint(cursor)):
if(halted != 0):
halted = 0
else:
halted = 1
if(updatewave == 1):
if(which_mode == "dc"):
wave_points = generate_wave_points("square",50,0,dc_bias)
else:
if(which_mode == "random"):
wave_points = generate_wave_points("random",0.1,amplitude,dc_bias)
else:
wave_points = generate_wave_points(which_mode,frequency,amplitude,dc_bias)
return
def initializeGUI():
pygame.init()
global displaysurface
displaysurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption("tES device control program")
return
def initializebuffer(): #loads the device's buffer with "output zero mA" commands
for x in (0,256):
ser.write(mA_2_DAC_write(0.0))
return
ser = serial.Serial(whichport, 115200) # Establish the connection on a specific port
sleep(2) #wait 2 seconds for the connection to settle
ticks = 0
global halted
halted = 1
global which_mode
which_mode = "sine"
global frequency
frequency = 0.1
global amplitude
amplitude = 0.5
global dc_bias
dc_bias = 0.0
global wave_points
wave_points = generate_wave_points("sine",0.1,0.5,0)
initializeGUI()
initializebuffer()
while True:
listlength = len(wave_points)
txlist = [0]*128
for x in range (0, 128):
if(halted == 0):
txlist[x] = wave_points[(ticks + x)% listlength]
else:
txlist[x] = 0
tx_128_mA_values(txlist) #if this function is called often enough it'll guarantee 65.5ms for each iteration of the loop
updateGUI()
if(frequency > 976.6):
frequency = 976.5625
if(frequency < 0.01):
frequency = 0.01
if(dc_bias > 2.002):
dc_bias = 2.001
if(dc_bias <-2.002):
dc_bias = -2.001
if(amplitude > 2.002):
amplitude = 2.001
if(amplitude < 0):
amplitude = 0
ticks += 128
if((ticks % listlength) == 0):
ticks = 0