forked from directive0/picorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·820 lines (620 loc) · 24.4 KB
/
main.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#!/usr/bin/python
# Picorder - Version 2a!
# Program collects sensor data and displays it in sliders and graphs.
# Plays a video using OMX
# Displays modes using three LEDs
# Changes modes using three microswitches
# Uses interval timers to direct flow of program
# The following are some necessary modules for the Picorder.
import pygame
import os
import time
from filehandling import *
# If running on Pi use these imports:
#from gpiobasics import *
#from sensehatbasics import *
# If testing on PC use these imports:
from gpiodummy import *
from getcpu import *
#from screens import *
# The following commands initiate a pygame environment.
pygame.init()
pygame.font.init()
# The following commands disable the mouse and cursor.
pygame.event.set_blocked(pygame.MOUSEMOTION)
pygame.mouse.set_visible(0)
# The following lists are for my colour standards.
red = (255,0,0)
green = (106,255,69)
blue = (99,157,255)
yellow = (255,221,5)
black = (0,0,0)
white = (255,255,255)
# The following lists/objects are for UI elements.
titleFont = "assets/babs.otf"
titleFont = "assets/babs.otf"
blueInsignia = pygame.image.load('assets/insigniablue.png')
backplane = pygame.image.load('assets/background.png')
backgraph = pygame.image.load('assets/backgraph.png')
slider = pygame.image.load('assets/slider.png')
sliderb = pygame.image.load('assets/slider2.png')
status = "startup"
set_logging = False
# The following is a simple object that I can use to maintain toggled states. There's probably a better way to do it, but this is what I did!
class toggle(object):
def __init__(self):
self.setting = False
def read(self):
return self.setting
def flip(self):
if self.setting == True:
self.setting = False
else:
self.setting = True
# The following class is used to display text
class Label(object):
def __init__(self):
self.x = 0
self.y = 0
self.color = white
self.fontSize = 33
self.myfont = pygame.font.Font(titleFont, self.fontSize)
def update(self, content, fontSize, nx, ny, fontType, color):
self.x = nx
self.y = ny
self.content = content
self.fontSize = fontSize
self.myfont = pygame.font.Font(fontType, self.fontSize)
self.color = color
def draw(self, surface):
label = self.myfont.render(self.content, 1, self.color)
surface.blit(label, (self.x, self.y))
# the following class is used to display images
class Image(object):
def __init__(self):
self.x = 258
self.y = 66
self.Img = blueInsignia
def update(self, image, nx, ny):
self.x = nx
self.y = ny
self.Img = image
def draw(self, surface):
surface.blit(self.Img, (self.x,self.y))
# The following class is to keep track of whether a graph has been initiated or is to be reset. This allows me to refresh graphs if need be.
class initial(object):
def __init__(self):
self.go = 0
def logstart(self):
self.go = 1
def reset(self):
self.go = 0
def get(self):
return self.go
# the following class is used to make items flash by providing a timed pulse bit object.
class flash(object):
def __init__(self):
self.value = 0
self.timelast = 0
self.timenow = 0
def pulse(self):
if (self.timelast == 0):
self.timelast = time.time()
self.timenow = time.time()
if ((self.timenow - self.timelast) >= 1):
if (self.value == 1):
self.value = 0
else:
self.value = 1
self.timelast = time.time()
def display(self):
return self.value
# The following class is used to prepare sensordata for display on the graph.
class graphlist(object):
# the following is constructor code to give each object a list suitable for storing all our graph data, in this case it is 145 spaces.
def __init__(self):
self.glist = []
for i in range(145):
self.glist.append(110.5)
# the following function returns the list.
def grablist(self):
return self.glist
# the following appends data to the list.
def updatelist(self, data):
#grabs a simple 15 wide tuple for our values
#puts a new sensor value at the end
self.buffer.append(data)
#pop the oldest value off
self.buffer.pop(0)
# the following pairs the list of values with coordinates on the X axis. The supplied variables are the starting X coordinates and spacing between each point.
def graphprep(self,list):
linepoint = 15
jump = 2
self.newlist = []
for i in range(145):
self.newlist.append((linepoint,list[i]))
linepoint = linepoint + jump
return self.newlist
# The following class is to handle interval timers.
class timer(object):
# Constructor code logs the time it was instantiated.
def __init__(self):
self.timeInit = time.time()
# The following funtion returns the last logged value.
def timestart(self):
return self.timeInit
# the following function updates the time log with the current time.
def logtime(self):
self.lastTime = time.time()
# the following function returns the interval that has elapsed since the last log.
def timelapsed(self):
self.timeLapse = time.time() - self.lastTime
#print(self.timeLapse)
return self.timeLapse
# The following function defines button behaviours and allows the program to query the button events and act accordingly.
def butswitch(status,graphinit,moire,rot,buttons):
pygame.event.get()
key = pygame.key.get_pressed()
if key[pygame.K_RIGHT]:
if (status == "slidergo"):
graphinit.reset()
status = "graphgo"
elif(status == "graphgo"):
graphinit.reset()
status = "magnetgo"
elif (status == "magnetgo"):
graphinit.reset()
status = "slidergo"
if key[pygame.K_LEFT]:
if (status == "slidergo"):
graphinit.reset()
status = "magnetgo"
elif(status == "graphgo"):
graphinit.reset()
status = "slidergo"
elif (status == "magnetgo"):
graphinit.reset()
status = "graphgo"
button_readings = buttons.read()
if (button_readings['buta']==True) and (button_readings['butb']==False) and (button_readings['butc']==False):
if (status == "graphgo") or (status == "magnetgo"):
graphinit.reset()
status = "slidergo"
if (button_readings['buta']==False) and (button_readings['butb']==True) and (button_readings['butc']==False):
if (status == "slidergo"):
graphinit.reset()
status = "graphgo"
elif(status == "graphgo"):
graphinit.reset()
status = "magnetgo"
elif (status == "magnetgo"):
graphinit.reset()
status = "graphgo"
if (button_readings['butc']==True):
if (status == "slidergo"):
rot.flip()
elif(status == "graphgo"):
moire.toggle()
elif (status == "magnetgo"):
moire.toggle()
#os.system("omxplayer ekmd.mov")
if (button_readings['buta']==False) and (button_readings['butb']==True) and (button_readings['butc']==True):
os.system("omxplayer ekmd.mov")
if (button_readings['buta']==True) and (button_readings['butb']==False) and (button_readings['butc']==True):
moire.toggle()
return status
# the following function maps a value from the target range onto the desination range
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
# the following function runs the startup animation
def startUp(surface, timeSinceStart):
#This function draws the opening splash screen for the program that provides the user with basic information.
#Sets a black screen ready for our UI elements
surface.fill(black)
#Instantiates the components of the scene
insignia = Image()
mainTitle = Label()
secTitle = Label()
#sets out UI objects with the appropriate data
insignia.update(blueInsignia, 115, 24)
mainTitle.update("TR-108 Environmental Tricorder",25,22,181,titleFont,white)
secTitle.update("STARFLEET R&D - Toronto - CLASS M ONLY",19,37,210,titleFont,blue)
#writes our objects to the buffer
insignia.draw(surface)
#checks time
timenow = time.time()
#compares time just taken with time of start to animate the apperance of text
if (timenow - timeSinceStart) > .5:
mainTitle.draw(surface)
if (timenow - timeSinceStart) > 1:
secTitle.draw(surface)
pygame.display.flip()
#waits for 2 seconds to elapse before returning the state that will take us to the sensor readout
if (timenow - timeSinceStart) < 2.5:
return "startup"
else:
return "slidergo"
# the following function draws the main sensor readout screen
def sliderScreen(surface,moire,tock,buttons,csvfile,rot,graphinit,humidGraph,tempGraph,pressGraph,graphtock):
# This function draws the main 3-slider interface, modelled after McCoy's tricorder in "Plato's Stepchildren". It displays temperature, humidity and pressure.
if (tock.timelapsed() >= 0.4):
# Sets a black screen ready for our UI elements
surface.fill(black)
# Cycles the Moire
moire.animate()
# Instantiates the components of the scene
templabel = Label()
presslabel = Label()
humidlabel = Label()
backPlane = Image()
slider1 = Image()
slider2 = Image()
slider3 = Image()
# Grabs data from our sensor/weather package (depends on what module is imported at the top)
senseData = sensorget()
# parses dictionary of data from sensor/weather
tempData = str(int(senseData['temp']))
pressData = str(int(senseData['pressure']))
humidData = str(int(senseData['humidity']))
# data labels
templabel.update(tempData + "\xb0",19,47,215,titleFont,yellow)
presslabel.update(pressData,19,152,215,titleFont,yellow)
humidlabel.update(humidData,19,254,215,titleFont,yellow)
# slider data adjustment
tempslide = translate(senseData['temp'], -40, 120, 204, 15)
pressslide = translate(senseData['pressure'], 260, 1260, 204, 15)
humidslide = translate(senseData['humidity'], 0, 100, 204, 15)
# Updates our UI objects with data parsed from sensor/weather
backPlane.update(backplane, 0, 0)
slider1.update(slider, 70, tempslide)
slider2.update(slider, 172, pressslide)
slider3.update(slider, 276, humidslide)
# draws the graphic UI to the buffer
backPlane.draw(surface)
slider1.draw(surface)
slider2.draw(surface)
slider3.draw(surface)
# draws the labels to the buffer
templabel.draw(surface)
presslabel.draw(surface)
humidlabel.draw(surface)
# draws UI to frame buffer
if (rot.read() == True):
surface.blit(pygame.transform.rotate(surface, 180), (0, 0))
pygame.display.flip()
tock.logtime()
if graphtock.timelapsed() >= 64:
humidData = float(senseData['humidity'])
#scales the data to the limits of our screen
humidgraph = translate(humidData, 0, 100, 204, 17)
#grabs a simple 61 wide tuple for our values
humidbuffer = humidGraph.grablist()
#puts a new sensor value at the end
humidbuffer.append(humidgraph)
#pop the oldest value off
humidbuffer.pop(0)
#preps the list by adding the X coordinate to every sensor value
humidcords = humidGraph.graphprep(humidbuffer)
tempData = float(senseData['temp'])
tempgraph = translate(tempData, -40, 120, 204, 17)
tempbuffer = tempGraph.grablist()
tempbuffer.append(tempgraph)
tempbuffer.pop(0)
tempcords = tempGraph.graphprep(tempbuffer)
pressData = float(senseData['pressure'])
pressgraph = translate(pressData, 260, 1260, 204, 17)
pressbuffer = pressGraph.grablist()
pressbuffer.append(pressgraph)
pressbuffer.pop(0)
presscords = pressGraph.graphprep(pressbuffer)
if set_logging == True:
csvfile.logvalues(senseData)
graphtock.logtime()
status = "slidergo"
status = butswitch(status,graphinit,moire,rot,buttons)
#returns state to main loop
return status
# The following function plots the magneto sensors to an onscreen graph
def magnetScreen(surface,xGraph,yGraph,zGraph,moire,tock,graphinit,buttons,csvfile,rot):
interval = (1)
if (tock.timelapsed() >= interval):
moire.animate()
#Sets a black screen ready for our UI elements
surface.fill(black)
#draws Background gridplane
graphback = Image()
graphback.update(backgraph, 0, 0)
graphback.draw(surface)
#instantiates 3 labels for our readout
xlabel = Label()
ylabel = Label()
zlabel = Label()
intervallabel = Label()
intervallabelshadow = Label()
slider1 = Image()
slider2 = Image()
slider3 = Image()
#gets our data
senseData = sensorget()["compass"]
if set_logging == True:
csvfile.logvalues(senseData)
#parses dictionary of data from sensor/weather
#converts humid data to float
xData = float(senseData['x'])
#scales the data to the limits of our screen
xgraph = translate(xData, -500, 500, 204, 17)
#grabs a simple 61 wide tuple for our values
xbuffer = xGraph.grablist()
#puts a new sensor value at the end
xbuffer.append(xgraph)
#pop the oldest value off
xbuffer.pop(0)
#preps the list by adding the X coordinate to every sensor value
xcords = xGraph.graphprep(xbuffer)
#repeat for each sensor
yData = float(senseData['y'])
ygraph = translate(yData, -500, 500, 204, 17)
ybuffer = yGraph.grablist()
ybuffer.append(ygraph)
ybuffer.pop(0)
ycords = yGraph.graphprep(ybuffer)
zData = float(senseData['z'])
zgraph = translate(zData, -500, 500, 204, 17)
zbuffer = zGraph.grablist()
zbuffer.append(zgraph)
zbuffer.pop(0)
zcords = zGraph.graphprep(zbuffer)
xslide = translate(senseData['x'], -500, 500, 194, 7)
yslide = translate(senseData['y'], -500, 500, 194, 7)
zslide = translate(senseData['z'], -500, 500, 194, 7)
slider1.update(sliderb, 283, xslide)
slider2.update(sliderb, 283, yslide)
slider3.update(sliderb, 283, zslide)
xcontent = str(int(xData))
xlabel.update(xcontent + ' uT',30,35,205,titleFont,blue)
ycontent = str(int(yData))
ylabel.update(ycontent + ' uT',30,145,205,titleFont,green)
zcontent = str(int(zData))
zlabel.update(zcontent + ' uT',30,235,205,titleFont,white)
#templabel.update(tempData + "\xb0",16,15,212,titleFont,yellow)
##setting = str(float((interval/60)*64))
setting = str(float(interval))
intervaltext = (setting + ' sec')
interx= (22)
intery= (21)
# intervallabel.update("~"+setting + " Hrs",30,22,167,titleFont,white)
#intervallabelshadow.update(setting + " Hrs",30,24,169,titleFont,(100,100,100))
intervallabel.update(intervaltext,30,interx,intery,titleFont,white)
intervallabelshadow.update(intervaltext, 30, interx + 2, intery + 2 ,titleFont,(100,100,100))
#draw the lines
pygame.draw.lines(surface, blue, False, xcords, 3)
pygame.draw.lines(surface, green, False, ycords, 3)
pygame.draw.lines(surface, white, False, zcords, 3)
slider1.draw(surface)
slider2.draw(surface)
slider3.draw(surface)
xlabel.draw(surface)
ylabel.draw(surface)
zlabel.draw(surface)
intervallabelshadow.draw(surface)
intervallabel.draw(surface)
#draws UI to frame buffer
if (rot.read() == True):
surface.blit(pygame.transform.rotate(surface, 180), (0, 0))
pygame.display.flip()
status = "magnetgo"
#returns state to main loop
status = butswitch(status,graphinit,moire,rot,buttons)
#returns state to main loop
return status
# the following function plots the environment sensors to an onscreen graph
def graphScreen(surface,humidGraph,tempGraph,pressGraph,moire,tock,graphinit,buttons,csvfile,rot):
status = "graphgo"
drawinterval = 1 #64
senseinterval = 10
#337
# Because the graph screen is slow to update it needs to pop a reading onto screen as soon as it is initiated I draw a value once and wait for the interval to lapse for the next draw. Once the interval has lapsed pop another value on screen.
moire.animate()
if (graphinit.get() == 0) or (tock.timelapsed() >= drawinterval):
#Sets a black screen ready for our UI elements
surface.fill(black)
#draws Background gridplane
graphback = Image()
graphback.update(backgraph, 0, 0)
graphback.draw(surface)
#instantiates 3 labels for our readout
templabel = Label()
humidlabel = Label()
presslabel = Label()
intervallabel = Label()
intervallabelshadow = Label()
slider1 = Image()
slider2 = Image()
slider3 = Image()
#gets our data
senseData = sensorget()
if set_logging == True:
csvfile.logvalues(senseData)
#parses dictionary of data from sensor/weather.
#converts humid data to float
humidData = float(senseData['humidity'])
#scales the data to the limits of our screen
humidgraph = translate(humidData, 0, 100, 204, 17)
#grabs a simple 61 wide tuple for our values
humidbuffer = humidGraph.grablist()
#puts a new sensor value at the end
humidbuffer.append(humidgraph)
#pop the oldest value off
humidbuffer.pop(0)
#preps the list by adding the X coordinate to every sensor value
humidcords = humidGraph.graphprep(humidbuffer)
#repeat for each sensor
tempData = float(senseData['temp'])
tempgraph = translate(tempData, -40, 120, 204, 17)
tempbuffer = tempGraph.grablist()
tempbuffer.append(tempgraph)
tempbuffer.pop(0)
tempcords = tempGraph.graphprep(tempbuffer)
pressData = float(senseData['pressure'])
pressgraph = translate(pressData, 260, 1260, 204, 17)
pressbuffer = pressGraph.grablist()
pressbuffer.append(pressgraph)
pressbuffer.pop(0)
presscords = pressGraph.graphprep(pressbuffer)
tempcontent = str(int(tempData))
templabel.update(tempcontent + "\xb0" + " c",30,35,205,titleFont,red)
presscontent = str(int(pressData))
presslabel.update(presscontent + " hpa",30,114,205,titleFont,yellow)
humidcontent = str(int(humidData))
humidlabel.update(humidcontent + " %",30,246,205,titleFont,green)
#templabel.update(tempData + "\xb0",16,15,212,titleFont,yellow)
setting = str(float(drawinterval))
intervaltext = (setting + ' sec')
interx= (22)
intery= (21)
# intervallabel.update("~"+setting + " Hrs",30,22,167,titleFont,white)
#intervallabelshadow.update(setting + " Hrs",30,24,169,titleFont,(100,100,100))
intervallabel.update(intervaltext,30,interx,intery,titleFont,white)
intervallabelshadow.update(intervaltext, 30, interx + 2, intery + 2 ,titleFont,(100,100,100))
tempslide = translate(senseData['temp'], -40, 120, 194, 7)
pressslide = translate(senseData['pressure'], 260, 1260, 194, 7)
humidslide = translate(senseData['humidity'], 0, 100, 194, 7)
slider1.update(sliderb, 283, tempslide)
slider2.update(sliderb, 283, pressslide)
slider3.update(sliderb, 283, humidslide)
#draw the lines
pygame.draw.lines(surface, red, False, tempcords, 3)
pygame.draw.lines(surface, green, False, humidcords, 3)
pygame.draw.lines(surface, yellow, False, presscords, 3)
templabel.draw(surface)
presslabel.draw(surface)
humidlabel.draw(surface)
intervallabelshadow.draw(surface)
intervallabel.draw(surface)
#draws UI to frame buffer
slider1.draw(surface)
slider2.draw(surface)
slider3.draw(surface)
if (rot.read() == True):
surface.blit(pygame.transform.rotate(surface, 180), (0, 0))
pygame.display.flip()
if (graphinit.get() == 0):
graphinit.logstart()
if (tock.timelapsed() >= 10):
tock.logtime()
#status = "graphgo"
#returns state to main loop
status = butswitch(status,graphinit,moire,rot,buttons)
#button_readings = buttons.read()
#returns state to main loop
return status
# the following function is our main object, it contains all the flow for our program.
class Main(object):
# set up an interval timer object, one for logging values, the other for drawing the screen.
tock = timer()
graphtock = timer()
rot = toggle()
# set up a file to store our sensor values to disk.
csvfile = write_values()
# set the screen resolution
screenSize = (320,240)
# I forget, probably colour depth?
modes = pygame.display.list_modes(16)
# instantiate a pygame display with the name "surface"
surface = pygame.display.set_mode(screenSize)
# surface = pygame.display.set_mode(screenSize, pygame.FULLSCREEN)
# Create a time index to work from for the splash screen.
timeSinceStart = time.time()
# The Moire object controls the SenseHat's 8x8 LED Display.
moire = led_display()
# The Buttons object handles button events.
buttons = debounce()
# I put the sensor data into objects so they can be passed into functions and retain information outside the scope.
humidGraph = graphlist()
tempGraph = graphlist()
pressGraph = graphlist()
xGraph = graphlist()
yGraph = graphlist()
zGraph = graphlist()
# tock.logtime() logs a starting point for my program. It could replace timeSinceStart and probably will one day.
tock.logtime()
graphtock.logtime()
# graphinit is an object to make sure the graph screen never shows up blank or lags on being displayed when you switch to it.
graphinit = initial()
# The following while loop catches ctrl-c exceptions. I use this structure so that status changes will loop back around and have a chance to activate different functions. It gets a little weird going forward, bear with me.
while (status != "quit"):
# Using this loop as an example. This loop runs the startup animation played when you first boot the program.
while(status == "startup"):
# We activate each LED for a nice lamp test.
#leda_on()
#ledb_on()
#ledc_on()
# A simple pygame.time.wait allows the program to take a break through the cycle so it's not loading up the CPU with draws to screen. I just saved 75% of my CPU by switching to this.
pygame.time.wait(33)
# This next item grabs the pygame events, which includes keyboard events.
pygame.event.get()
# this next item checks to see if the q key was pressed
key = pygame.key.get_pressed()
# and if so breaks the loop and gets us to the shut down commands.
if key[pygame.K_q]:
cleangpio()
clearled()
status = "quit"
else:
# otherwise it updates the status by calling on the startUp function, which is passed our pygame display object, and the time since we booted our program.
status = startUp(surface,timeSinceStart)
# The rest of these loops all handle a different mode, switched by buttons within the functions.
while(status == "slidergo"):
leda_on()
ledb_off()
ledc_off()
pygame.event.get()
pygame.time.wait(50)
key = pygame.key.get_pressed()
if key[pygame.K_q]:
cleangpio()
clearled()
status = "quit"
else:
status = sliderScreen(surface,moire,tock,buttons,csvfile,rot,graphinit,humidGraph,tempGraph,pressGraph,graphtock)
while(status == "graphgo"):
leda_off()
ledb_on()
ledc_off()
pygame.event.get()
pygame.time.wait(50)
key = pygame.key.get_pressed()
if key[pygame.K_q]:
cleangpio()
clearled()
status = "quit"
else:
status = graphScreen(surface,humidGraph,tempGraph,pressGraph,moire,graphtock,graphinit,buttons,csvfile,rot)
while (status == "magnetgo"):
leda_off()
ledb_on()
ledc_off()
pygame.event.get()
pygame.time.wait(50)
key = pygame.key.get_pressed()
if key[pygame.K_q]:
cleangpio()
clearled()
status = "quit"
else:
#timeStart=time.time()
status = magnetScreen(surface,xGraph,yGraph,zGraph,moire,tock,graphinit,buttons,csvfile,rot)
# If CTRL-C is received the program gracefully turns off the LEDs and resets the GPIO.
try:
# the following call starts our program
Main()
except KeyboardInterrupt:
clearled()
cleangpio()
status = "quit"