-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.py
113 lines (95 loc) · 2.45 KB
/
gpio.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
import RPi.GPIO as GPIO
import signal
import sys
import picamera
import time
import zbar
import io
from PIL import Image
from mpdclient import mpdclient
def led_all_off():
for led in led_all:
GPIO.output(led, GPIO.LOW)
def get_qr_content(string):
qr_content = string.split(',')
print qr_content
dict = {}
for item in qr_content:
row = item.split('=')
if len(row) == 2:
dict[ row[ 0 ].strip() ] = row[ 1 ].strip("\"")
else:
print "invalid row"
print dict
return dict
def cam_capture():
# Camera warm-up time
# camera.capture('qr.jpg', resize=(320, 240))
stream = io.BytesIO()
camera.capture(stream, format='jpeg')
stream.seek(0)
pil = Image.open(stream)
scanner =zbar.ImageScanner()
scanner.parse_config('enable')
pil = pil.convert('L')
width, height = pil.size
raw = pil.tostring()
image = zbar.Image(width, height, 'Y800', raw)
scanner.scan(image)
for symbol in image:
# print 'decode', symbol.type, 'symbol', '"%s"' % symbol.data
print type(symbol.type)
if symbol.type == 64:
print ("QR detected")
dict = get_qr_content(symbol.data)
client.add(dict['folder'])
else:
print ("no QR code, type: %s" % symbol.type )
del image
stream.close()
def printFunction(channel):
print ("edge: %d" % channel)
if (GPIO.input(sens) == 1):
GPIO.output(led_red, GPIO.HIGH)
print ("QR tag detected")
cam_capture()
print ("image take")
else:
GPIO.output(led_red, GPIO.LOW)
client.pause()
print ("QR tag removed")
def signal_handler(signal, frame):
print ("Ctrl-C detected")
led_all_off()
GPIO.cleanup()
sys.exit()
# supposed to use the board numbering
GPIO.setmode(GPIO.BCM)
signal.signal(signal.SIGINT, signal_handler)
# configure GPIOs
sens = 17
led_yellow = 21
led_green = 22
led_red = 23
led_all = [led_yellow, led_green, led_red]
sens_bounce_ms = 150
GPIO.setup(sens, GPIO.IN)
GPIO.setup(led_yellow, GPIO.OUT)
GPIO.setup(led_green, GPIO.OUT)
GPIO.setup(led_red, GPIO.OUT)
print "initiated: GPIO"
camera = picamera.PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()
time.sleep(2)
print "initiated: camera module"
client = mpdclient()
# client.add("Alexi Murdoch/Towards The Sun")
print "initiated: MPD"
if (GPIO.input(sens) == 1):
print ("enabled FALLING")
GPIO.add_event_detect(sens, GPIO.FALLING, callback=printFunction, bouncetime=sens_bounce_ms)
else:
print ("enabled RISING")
GPIO.add_event_detect(sens, GPIO.RISING, callback=printFunction, bouncetime=sens_bounce_ms)
signal.pause()