-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvanced.py
92 lines (73 loc) · 2.59 KB
/
advanced.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
import picamera
from PIL import Image, ImageFont, ImageDraw
import RPi.GPIO as GPIO
import time
import os
import sys
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("advanced.log"),
logging.StreamHandler(sys.stdout)
]
)
camera = picamera.PiCamera()
camera.resolution = (1920, 1080)
camera.rotation = 180
camera.framerate = 30
camera.start_preview()
img = Image.open('overlay.png')
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 34)
draw.text((10, 5), "RPI 3A+ HQ Cam Microscope v0.1", font=font, align ="left")
pad = Image.new('RGBA', (
((img.size[0] + 31) // 32) * 32,
((img.size[1] + 15) // 16) * 16,
))
pad.paste(img, (0, 0))
o = camera.add_overlay(pad.tobytes(), size=img.size, layer=3, alpha = 254)
#camera.remove_overlay(o)
# gpio buttons of the pcb adapter board
BTN_CAPTURE = 17
BTN_MODE = 27
BTN_INCREASE = 22
BTN_DECREASE = 19
BTN_SHUTDOWN = 26
# path to capture. depending to username and your custom needs
PATH_CAPTURE = "/home/micro/capture/"
GPIO.setmode(GPIO.BCM)
GPIO.setup(BTN_MODE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_INCREASE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_DECREASE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_CAPTURE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_SHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def mode_select(channel):
logging.info("Pressed mode")
time.sleep(1)
def increase(channel):
logging.info("Increase value")
time.sleep(1)
def decrease(channel):
logging.info("Decrease value")
time.sleep(1)
def capture_photo(channel):
logging.info("Capture photo")
t = time.localtime()
filename = PATH_CAPTURE + "capture" + time.strftime('-%Y-%m-%d_%H-%M-%S', t) + ".jpg"
camera.capture(filename)
# owner:group can vary depending to in sd card setup given username
os.system("chown micro:micro " + filename)
def shutdown(channel):
logging.info("Shutting down")
time.sleep(1)
os.system("sudo shutdown -h now")
GPIO.add_event_detect(BTN_MODE, GPIO.FALLING, callback=mode_select, bouncetime=1100)
GPIO.add_event_detect(BTN_INCREASE, GPIO.FALLING, callback=increase, bouncetime=1100)
GPIO.add_event_detect(BTN_DECREASE, GPIO.FALLING, callback=decrease, bouncetime=1100)
GPIO.add_event_detect(BTN_CAPTURE, GPIO.FALLING, callback=capture_photo, bouncetime=1100)
GPIO.add_event_detect(BTN_SHUTDOWN, GPIO.FALLING, callback=shutdown, bouncetime=1100)
logging.info("Advanced script started")
while 1:
time.sleep(1)