-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpitimelapse.py
More file actions
executable file
·801 lines (663 loc) · 19.5 KB
/
pitimelapse.py
File metadata and controls
executable file
·801 lines (663 loc) · 19.5 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
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
#!/usr/bin/python3
"""
piTimeLapse is a program to run on a Raspberry PI and a piTFT display module with some additional
electronics to drive a stepper motor and control a DSLR camera to produce moving time lapse movies.
(c) 2015 by Matthias Cramer, cramer@freestone.net
"""
import os
import sys
import pygame
import time
import socket
from subprocess import call
from pygame.locals import *
from pygame.compat import unichr_, unicode_
try:
import RPi.GPIO as GPIO
gpio=True
except ImportError:
print("no GPIO module")
gpio=False
from itertools import chain
os.environ["SDL_FBDEV"] = "/dev/fb1"
SCREEN_SIZE = (320, 240)
buttoncolor = 50,50,255
headercolor = 255,50,50
wincolor = 40, 40, 90
menucolor = [[50,50,100],[255,50,50],[50,50,50]]
pulslength=0.0035
rewindpulslength=0.0025
# Raspberry PI revision (GPIO has changed between 1 and 2)
if gpio:
endStop = { 'Left': 12, 'Right': 13}
camFocus=16
camShutter=19
motorPulse=20
motorDir=21
motorEna=6
if GPIO.RPI_REVISION == 1:
# buttonPins = [17, 22, 23, 21]
print("This does not work on a revison 1 board, sorry!")
exit()
else:
buttonPins = [17, 22, 23, 27]
movesize=0.2
buttonStateOld = [1, 1, 1, 1]
tlSet = { 'Intervall' : 10,
'Stepsize' : 10,
'Direction' : 0
}
dirList = ['Left','Right']
esState = { 'Left' : 0,
'Right': 0
}
tlUnits = { 'Intervall' : 's',
'Stepsize' : 'mm',
}
tlPos = { 'Position' : 0,
'Starttime' : 0,
'PictureCount' : 0
}
started = False
pause = False
posInc=0.01
# Display the splash screen
def splash():
newScreen("")
font = pygame.font.SysFont('matthiascramerhandwriting', 24)
message = "Welcome to piTime"
label = font.render(message, True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = screen_rect.center
# Blit image and text to the target surface.
screen.blit(label, label_rect)
pygame.display.flip()
time.sleep(1)
def butondemo():
for i in range (0, 4):
time.sleep(0.5)
buttonpress(i,1)
time.sleep(1)
buttonpress(i,0)
# Prepare a new screen
def newScreen(title):
screen.fill(wincolor)
if (title != ""):
header = pygame.Rect(0, 0, screen_rect.width, 20)
screen.fill(headercolor, header)
font = pygame.font.SysFont('ubuntu', 16)
label = font.render(title, True, (255,255,255))
label_rect = label.get_rect()
label_rect.x = 4
label_rect.y = 2
screen.blit(label, label_rect)
# Draw the button labels
def buttons(labels):
btn = pygame.Rect(0, 0, screen_rect.width/4 - 6, 20)
font = pygame.font.Font('FreeSans.ttf', 14)
for i in range (0, 4):
btn.x=3+((screen_rect.width/4)*i)
btn.y=screen_rect.height-20
screen.fill(buttoncolor, btn)
label = font.render(labels[i], True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = btn.center
screen.blit(label, label_rect)
# Mark a pressed button
def buttonPress(num,toggle):
width=(screen_rect.width/4)-6
height=20
x=3+((screen_rect.width/4)*num)
y=screen_rect.height-20
if (toggle):
pygame.draw.polygon(screen, (255,255,255), [[x,y],[x+width,y],[x+width,y+height],[x,y+height]], 1)
else:
pygame.draw.polygon(screen, buttoncolor, [[x,y],[x+width,y],[x+width,y+height],[x,y+height]], 1)
pygame.display.flip()
# Framebuffer init
def fbInit():
disp_no = os.getenv("DISPLAY")
if disp_no:
print("I'm running under X display = {0}".format(disp_no))
screen = pygame.display.set_mode(SCREEN_SIZE)
return(screen)
else:
drivers = ['fbcon', 'directfb', 'svgalib']
found = False
for driver in drivers:
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print('Driver: {0} failed.'.format(driver))
continue
found = True
break
if not found:
raise(Exception('No suitable video driver found!'))
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
print("Framebuffer size: %d x %d" % (size[0], size[1]))
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
return(screen)
# GPIO Init for Buttons
def gpioInit():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(endStop['Left'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(endStop['Right'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(camFocus, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(camShutter, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(motorPulse, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(motorDir, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(motorEna, GPIO.OUT, initial=GPIO.HIGH)
for i in buttonPins:
GPIO.setup(i, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print("GPIO {0} init".format(i))
# Get Gpio Button event
def gpioGetButtons():
buttonState = [0, 0, 0, 0]
global buttonStateOld
buttonevent = {"type": "none", "button": 99}
for i in range (0,4):
buttonState[i]=GPIO.input(buttonPins[i])
if (buttonStateOld[i] != buttonState[i]):
# print('Event on button {0} -> {1}'.format(buttonPins[i], buttonState[i]))
if (buttonState[i] == 0):
buttonevent["type"]=pygame.KEYDOWN
if (buttonState[i] == 1):
buttonevent["type"]=pygame.KEYUP
buttonevent["button"]=i
buttonPress(i,not buttonState[i])
buttonStateOld = buttonState[:]
return(buttonevent)
# End Program
def endProgram():
GPIO.cleanup()
exit()
# Get Button or Keypresses
def getButtonEvent():
clock.tick( 10 );
if gpio:
buttonevent = gpioGetButtons()
if buttonevent["type"] == pygame.KEYDOWN:
# print("Button {0} pressed".format(buttonevent["button"]))
return(buttonevent["button"])
# if buttonevent["type"] == pygame.KEYUP:
# print("Button {0} released".format(buttonevent["button"]))
events = pygame.event.get()
for event in events:
button=-1
if event.type == pygame.QUIT:
endProgram()
if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
if event.type == pygame.KEYDOWN:
toggle=1
if event.type == pygame.KEYUP:
toggle=0
if event.key == pygame.K_q:
exit()
if event.key == pygame.K_1:
button=0
if event.key == pygame.K_DOWN:
button=0
if event.key == pygame.K_2:
button=1
if event.key == pygame.K_UP:
button=1
if event.key == pygame.K_3:
button=2
if event.key == pygame.K_RETURN:
button=2
if event.key == pygame.K_4:
button=3
if event.key == pygame.K_ESCAPE:
button=3
buttonPress(button,toggle)
if (toggle==1):
return(button)
# Clear screen area
def clearScreen():
li = pygame.Rect(0, 20, screen_rect.width, screen_rect.height-40)
screen.fill(wincolor, li)
# Clear partial screen area
def clearTlScreen():
li = pygame.Rect(200, 20, screen_rect.width, screen_rect.height-40)
screen.fill(wincolor, li)
# Draw a select list menu
def drawSelectMenu(items,select):
li = pygame.Rect(0, 0, screen_rect.width*0.8 , 24)
font = pygame.font.SysFont('ubuntu', 18)
for i, val in enumerate(items):
li.x=(screen_rect.width*0.1)
li.y=((screen_rect.height/8) * i) + 40
screen.fill(menucolor[1 if i == select else 0], li)
label = font.render(val, True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = li.center
screen.blit(label, label_rect)
pygame.display.flip()
# Draw a settings menu
def drawSettingsMenu(settings,units,select,selected):
li = pygame.Rect(0, 0, screen_rect.width*0.8 , 24)
font = pygame.font.SysFont('ubuntu', 18)
i = 0
for key in settings:
li.x=(screen_rect.width*0.1)
li.y=((screen_rect.height/8) * i) + 40
screen.fill(menucolor[1 if i == select else 0], li)
label = font.render(key, True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = li.center
label_rect.x = li.x + 8
screen.blit(label, label_rect)
if (i == selected):
highlight=pygame.Rect(0, 0, screen_rect.width*0.2+4 , 24)
highlight.x=(screen_rect.width*0.7-4)
highlight.y=((screen_rect.height/8) * i) + 40
screen.fill(menucolor[2], highlight)
text=""
if key == "Direction":
text=dirList[settings[key] % 2]
else:
text=str(settings[key])+" "+units[key]
val = font.render(text, True, (255,255,255))
val_rect = label.get_rect()
val_rect.center = li.center
val_rect.x = screen_rect.width*0.7
screen.blit(val, val_rect)
i = i + 1
pygame.display.flip()
# Draw timelapse screen
def drawTimeLapseScreen(first,elapsed):
global started
global pause
global tlPos
font = pygame.font.SysFont('ubuntu', 22)
line_height=30;
if first==True:
clearScreen()
# Labels
label = font.render("Position", True, (255,255,255))
label_rect = label.get_rect()
label_rect.x = 6
label_rect.y = 10 + line_height
screen.blit(label, label_rect)
label = font.render("Picture Count", True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 6
label_rect.y = 10 + line_height * 2
screen.blit(label, label_rect)
label = font.render("Time Elapsed", True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 6
label_rect.y = 10 + line_height * 3
screen.blit(label, label_rect)
label = font.render("Direction", True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 6
label_rect.y = 10 + line_height * 4
screen.blit(label, label_rect)
else:
clearTlScreen()
# Values
label = font.render(str(tlPos['Position']), True, (255,255,255))
label_rect = label.get_rect()
label_rect.x = 200
label_rect.y = 10 + line_height
screen.blit(label, label_rect)
label = font.render(str(tlPos['PictureCount']), True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 200
label_rect.y = 10 + line_height * 2
screen.blit(label, label_rect)
tc=0
if started==True:
tc = time.time() - tlPos["Starttime"]
if pause==True:
tc = elapsed
label = font.render("{:.1f}".format(tc) + " s", True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 200
label_rect.y = 10 + line_height * 3
screen.blit(label, label_rect)
label = font.render(dirList[tlSet['Direction']], True, (255,255,255))
label_rect = label.get_rect()
label_rect = label.get_rect()
label_rect.x = 200
label_rect.y = 10 + line_height * 4
screen.blit(label, label_rect)
pygame.display.flip()
# Check overflow of menu selector
def checkOverflow(list, index):
if (index < 0): index=len(list)-1
if (index >= len(list)): index=0
return(index)
# Main Screen
def mainScreen():
select=0
while(1):
newScreen("piTimeLapse")
btn_labels=['▼ Down','▲ Up','⇒ Select','']
buttons(btn_labels)
menu=["Config","Start Timelapse","System"]
menu_f = { 0: configScreen, 1: timeLapseScreen, 2: systemScreen }
pygame.display.flip()
while (1):
drawSelectMenu(menu,select)
button=getButtonEvent()
if button == 0:
select=select+1
else:
if button == 1:
select=select-1
select=checkOverflow(menu,select)
if button == 2:
menu_f[select]()
break
#Config Screen
def configScreen():
newScreen("piTimeLapse - Config")
btn_labels=['▼ Down','▲ Up','⇒ Select','↩ Exit']
buttons(btn_labels)
select=0
selected=99
selkeys=list(tlSet.keys())
pygame.display.flip()
while (1):
drawSettingsMenu(tlSet,tlUnits,select,selected)
button=getButtonEvent()
if button == 0:
if selected==99:
select=select+1
else:
tlSet[selkeys[select]]-=1
else:
if button == 1:
if selected==99:
select=select-1
else:
tlSet[selkeys[select]]+=1
select=checkOverflow(tlSet,select)
if button == 2:
selected=select
if button == 3:
if selected==99:
return
else:
selected=99
# Take an Image
def takeImage():
global tlPos
if gpio:
GPIO.output(camFocus,1)
time.sleep(0.3)
GPIO.output(camShutter,1)
time.sleep(0.2)
GPIO.output(camShutter,0)
GPIO.output(camFocus,0)
else:
print("Cheese")
tlPos['PictureCount']+=1
# CallBack for Endstop
def cbEndStopEvent(pin):
global esState
lookup = {value: key for key, value in endStop.items()}
value=GPIO.input(pin);
esState[lookup[pin]]=value;
print("{0} Endstop has changed to {1}".format(lookup[pin],value))
# Register event for endstop detection
def checkEndStop():
global endStop
for i in endStop:
GPIO.add_event_detect(endStop[i], GPIO.BOTH)
GPIO.add_event_callback(endStop[i], callback=cbEndStopEvent)
# Remove endstop events
def removeCheckEndStop():
global esState
global endStop
for i in endStop:
GPIO.remove_event_detect(endStop[i])
esState = { 'Left' : 0,
'Right': 0
}
# Enable Stepper Motor
def motorEnable():
GPIO.output(motorEna,0)
# Disapble Stepper Motor
def motorDisable():
GPIO.output(motorEna,1)
# Move the camera on the rail
def moveCamera():
global tlSet
global tlPos
global esState
global started
posold=tlPos['Position']
tlPos['Position']+=tlSet['Stepsize']
if (gpio):
if (tlSet["Direction"] == 1):
GPIO.output(motorDir,1)
else:
GPIO.output(motorDir,0)
while(tlPos['Position']>=posold and esState[dirList[tlSet["Direction"]]] == 0):
GPIO.output(motorPulse,1)
time.sleep(pulslength)
GPIO.output(motorPulse,0)
time.sleep(pulslength)
posold=posold+movesize
if (esState[dirList[tlSet["Direction"]]] == 1):
print("Endstop reached!")
started=False
esReachedScreen()
btn_labels=['Start','','','↩ Exit']
buttons(btn_labels)
if gpio:
motorDisable()
removeCheckEndStop()
return
else:
print("moveCamera")
# rewind
def rewind():
global tlSet
global tlPos
global esState
print("Rewinding")
if (gpio):
if (tlSet["Direction"] == 1):
GPIO.output(motorDir,0)
else:
GPIO.output(motorDir,1)
while (esState[dirList[not tlSet["Direction"]]] == 0):
GPIO.output(motorPulse,1)
time.sleep(rewindpulslength)
GPIO.output(motorPulse,0)
time.sleep(rewindpulslength)
print("Rewinding done")
tlPos['Position']=0
return
# Maual Rewind
def manualRewind():
motorEnable()
checkEndStop()
rewind()
motorDisable()
removeCheckEndStop()
return
#TimeLapse Screen
def timeLapseScreen():
global started
global pause
global tlPos
newScreen("piTimeLapse - Time Lapse")
btn_labels=['Start','','','↩ Exit']
buttons(btn_labels)
pygame.display.flip()
lastimg=0
first=True
elapsed=0
while (1):
#print("Loop")
drawTimeLapseScreen(first,elapsed)
first=False
button=getButtonEvent()
if button == 0: # Pause/Start Button
if started==False: # Start
print("Start")
if pause==False:
tlPos["Starttime"]=time.time()
else:
pause=False
tlPos["Starttime"]=time.time()-elapsed
btn_labels[0]='Pause'
btn_labels[1]='Stop'
buttons(btn_labels)
started=True
if gpio:
motorEnable()
checkEndStop()
takeImage()
lastimg=time.time()
moveCamera()
if started==False:
first=True
else: # Pause
if started==True:
print("Pause")
pause=True
started=False
elapsed=time.time()-tlPos["Starttime"]
print(elapsed)
btn_labels[0]='Start'
buttons(btn_labels)
if gpio:
motorDisable();
removeCheckEndStop()
if button == 1:
started=False
pause=False
elapsed=0
btn_labels[0]='Start'
btn_labels[1]=''
buttons(btn_labels)
rewind()
tlPos["PictureCount"]=0
if gpio:
motorDisable();
removeCheckEndStop()
if button == 3:
first=True
return
if started==True:
print("Running...")
if lastimg+tlSet['Intervall'] <= time.time():
takeImage()
lastimg=time.time()
moveCamera()
if started==False:
first=True
# Get default IP address
def getDefaultIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0)) # connecting to a UDP address doesn't send packets
local_ip_address = s.getsockname()[0]
return(local_ip_address)
# EndStop reached screen
def esReachedScreen():
global started
started = False
newScreen("piTimeLapse - EndStop")
btn_labels=['','','','↩ Exit']
buttons(btn_labels)
font = pygame.font.SysFont('ubuntu', 18)
label = font.render("End stop reached!", True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = [160,120]
screen.blit(label, label_rect)
pygame.display.flip()
while (1):
button=getButtonEvent()
if button == 3:
return
# The info screen
def infoScreen():
newScreen("piTimeLapse - Info")
btn_labels=['','','','↩ Exit']
buttons(btn_labels)
font = pygame.font.SysFont('ubuntu', 18)
label = font.render("IP Address: "+getDefaultIP(), True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = [160,120]
screen.blit(label, label_rect)
pygame.display.flip()
while (1):
button=getButtonEvent()
if button == 3:
return
#systemScreen()
# The shutdown screen
def shutdownScreen():
newScreen("piTimeLapse - Shutdown")
btn_labels=['','Shutdown','','↩ Exit']
buttons(btn_labels)
font = pygame.font.SysFont('ubuntu', 18)
label = font.render("Confirm Shutdown", True, (255,255,255))
label_rect = label.get_rect()
label_rect.center = [160,120]
screen.blit(label, label_rect)
pygame.display.flip()
while (1):
button=getButtonEvent()
if button == 1:
print("Shutdown")
if os.getuid()==0:
pygame.display.quit()
call(["shutdown", "-h", "now"])
else:
print("Not running as root!")
if button == 3:
return
#systemScreen()
#System Screen
def systemScreen():
select=0
while(1):
newScreen("piTimeLapse - System")
btn_labels=['▼ Down','▲ Up','⇒ Select','↩ Exit']
buttons(btn_labels)
menu=["Info","Rewind","Shutdown"]
menu_f = { 0: infoScreen, 1: manualRewind ,2: shutdownScreen }
pygame.display.flip()
while (1):
drawSelectMenu(menu,select)
button=getButtonEvent()
if button == 0:
select=select+1
else:
if button == 1:
select=select-1
select=checkOverflow(menu,select)
if button == 2:
menu_f[select]()
break
if button == 3:
return
""" Main """
pygame.init()
screen=fbInit()
screen_rect = screen.get_rect()
pygame.mouse.set_visible(False)
pygame.display.set_caption('piTime')
clock = pygame.time.Clock()
splash()
if gpio:
gpioInit()
while 1:
mainScreen()