Skip to content

Commit 27833d4

Browse files
committed
Add web based samples
Read Temperature, Brightness, humidity http://learn.linksprite.com/pcduino/python/use-flask-server-on-pcduino3- to-build-web-based-sensor-reading/
1 parent a90bff9 commit 27833d4

29 files changed

Lines changed: 927 additions & 0 deletions

File tree

‎README.md‎

100644100755
File mode changed.

‎Samples/adc_test/adc.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pinmap import PinMap
2+
3+
pins = PinMap('/proc', 'adc', 6)
4+
5+
def analog_read(channel):
6+
"""Return the integer value of an adc pin.
7+
8+
adc0 and adc1 have 6 bit resolution.
9+
adc2 through adc5 have 12 bit resolution.
10+
11+
"""
12+
with open(pins.get_path(channel), 'r') as f:
13+
return int(f.read(32).split(':')[1].strip())

‎Samples/adc_test/gpio/__init__.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# io test code for pcDuino ( http://www.pcduino.com )
4+
#
5+
__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digitalWrite', 'digitalRead', "pinMode"]
6+
7+
_GPIO_PINS = ('gpio0','gpio1','gpio2','gpio3','gpio4','gpio5','gpio6','gpio7',
8+
'gpio8', 'gpio9', 'gpio10', 'gpio11', 'gpio12', 'gpio13',
9+
'gpio14', 'gpio15', 'gpio16', 'gpio17', 'gpio18', 'gpio19')
10+
11+
_PIN_FD_PATH = '/sys/devices/virtual/misc/gpio/pin/%s'
12+
_MODE_FD_PATH = '/sys/devices/virtual/misc/gpio/mode/%s'
13+
HIGH = 1
14+
LOW = 0
15+
INPUT = 0
16+
OUTPUT = 1
17+
18+
class InvalidChannelException(Exception):
19+
"""The channel sent is invalid on pcDuino board """
20+
pass
21+
22+
def _GetValidId(channel):
23+
if channel in _GPIO_PINS:
24+
return channel
25+
else:
26+
raise InvalidChannelException
27+
28+
def digitalWrite(channel, value):
29+
"""Write to a GPIO channel"""
30+
id = _GetValidId(channel)
31+
with open(_PIN_FD_PATH % id, 'w') as f:
32+
f.write('1' if value == HIGH else '0')
33+
34+
def digitalRead(channel):
35+
"""Read from a GPIO channel"""
36+
id = _GetValidId(channel)
37+
with open(_PIN_FD_PATH % id, 'r') as f:
38+
return f.read(1) == '1'
39+
40+
def pinMode(channel, mode):
41+
""" Set Mode of a GPIO channel """
42+
id = _GetValidId(channel)
43+
with open(_MODE_FD_PATH % id, 'w') as f:
44+
f.write('0' if mode == INPUT else '1')

‎Samples/adc_test/pinmap.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os.path
2+
3+
4+
class InvalidChannelException(ValueError):
5+
"""The channel sent was invalid."""
6+
7+
def __init__(self, pin):
8+
super(InvalidChannelException, self).__init__("pin %s not found" % pin)
9+
10+
11+
class PinMap(object):
12+
def __init__(self, path, prefix, count):
13+
self.pins = ['%s%s' % (prefix, i) for i in xrange(count)]
14+
self.path = path
15+
16+
def get_path(self, pin, path=None):
17+
"""Get path of pin fd.
18+
19+
pin can either be the pin basename (i.e. 'adc2') or pin number (i.e. 2)
20+
if prefix is supplied, override the default path prefix.
21+
22+
"""
23+
if not path:
24+
path = self.path
25+
26+
if isinstance(pin, int):
27+
try:
28+
pin = self.pins[pin]
29+
except IndexError:
30+
raise InvalidChannelException(pin)
31+
32+
if not pin in self.pins:
33+
raise InvalidChannelException(pin)
34+
return os.path.join(path, pin)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# io test code for pcDuino ( http://www.pcduino.com )
4+
#
5+
__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digitalWrite', 'digitalRead', "pinMode"]
6+
7+
_GPIO_PINS = ('gpio0','gpio1','gpio2','gpio3','gpio4','gpio5','gpio6','gpio7',
8+
'gpio8', 'gpio9', 'gpio10', 'gpio11', 'gpio12', 'gpio13',
9+
'gpio14', 'gpio15', 'gpio16', 'gpio17', 'gpio18', 'gpio19')
10+
11+
_PIN_FD_PATH = '/sys/devices/virtual/misc/gpio/pin/%s'
12+
_MODE_FD_PATH = '/sys/devices/virtual/misc/gpio/mode/%s'
13+
HIGH = 1
14+
LOW = 0
15+
INPUT = 0
16+
OUTPUT = 1
17+
18+
class InvalidChannelException(Exception):
19+
"""The channel sent is invalid on pcDuino board """
20+
pass
21+
22+
def _GetValidId(channel):
23+
if channel in _GPIO_PINS:
24+
return channel
25+
else:
26+
raise InvalidChannelException
27+
28+
def digitalWrite(channel, value):
29+
"""Write to a GPIO channel"""
30+
id = _GetValidId(channel)
31+
with open(_PIN_FD_PATH % id, 'w') as f:
32+
f.write('1' if value == HIGH else '0')
33+
34+
def digitalRead(channel):
35+
"""Read from a GPIO channel"""
36+
id = _GetValidId(channel)
37+
with open(_PIN_FD_PATH % id, 'r') as f:
38+
return f.read(1) == '1'
39+
40+
def pinMode(channel, mode):
41+
""" Set Mode of a GPIO channel """
42+
id = _GetValidId(channel)
43+
with open(_MODE_FD_PATH % id, 'w') as f:
44+
f.write('0' if mode == INPUT else '1')
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from time import sleep, time
2+
from SimpleCV import Camera, Image, Display
3+
import gpio
4+
5+
myCamera = Camera(prop_set={'width':177, 'height': 150})
6+
myDisplay = Display(resolution=(177, 150))
7+
stache = Image("abc.jpg")
8+
9+
#stacheMask = \
10+
# stache.createBinaryMask(color1=(0,0,0), color2=(254,254,254))
11+
#stacheMask = stacheMask.invert()
12+
13+
gpio.pinMode("gpio13", gpio.INPUT)
14+
15+
def mustachify(frame):
16+
faces = frame.findHaarFeatures('face')
17+
if faces:
18+
for face in faces:
19+
print "Face at: " + str(face.coordinates())
20+
myFace = face.crop()
21+
noses = myFace.findHaarFeatures('nose')
22+
if noses:
23+
nose = noses.sortArea()[-1]
24+
print "Nose at: " + str(nose.coordinates())
25+
xmust = face.points[0][0] + nose.x - (stache.width/2)
26+
ymust = face.points[0][1] + nose.y + (stache.height/3)
27+
else:
28+
return frame
29+
frame = frame.blit(stache, pos=(xmust, ymust), mask=stacheMask)
30+
return frame
31+
else:
32+
return frame
33+
34+
while not myDisplay.isDone():
35+
inputValue = gpio.digitalRead("gpio13")
36+
frame = myCamera.getImage()
37+
if inputValue == True:
38+
frame = frame.blit(stache, pos=(0,0), mask=stache)
39+
#frame = mustachify(frame)
40+
frame.save("photo-" + str(time()) + ".jpg")
41+
frame = frame.flipHorizontal()
42+
frame.show()
43+
sleep(3)
44+
else:
45+
frame = frame.flipHorizontal()
46+
frame.save(myDisplay)
47+
sleep(.05)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from SimpleCV import *
2+
from time import sleep
3+
import gpio
4+
5+
servo_pin = "gpio2"
6+
7+
myCamera = Camera(prop_set={'width':480,'height':480})
8+
myDisplay = Display(resolution=(480,480))
9+
10+
def delayMicroseconds(us):
11+
sleep(1.0*us/1000/1000)
12+
13+
def delay(ms):
14+
sleep(1.0*ms/1000)
15+
16+
def setup():
17+
gpio.pinMode(servo_pin, gpio.OUTPUT)
18+
set_servo(1500)
19+
20+
def set_servo(x):
21+
duty = x
22+
for j in range(20):
23+
gpio.digitalWrite(servo_pin, gpio.HIGH)
24+
delayMicroseconds(duty)
25+
gpio.digitalWrite(servo_pin, gpio.LOW)
26+
delayMicroseconds(20000-duty)
27+
28+
def loop():
29+
while not myDisplay.isDone():
30+
old_x = 1450
31+
duty = 1500
32+
frame = myCamera.getImage()
33+
faces = frame.findHaarFeatures('face')
34+
if faces:
35+
for face in faces:
36+
new_x = (face.points[1][0]+face.points[0][0])/2
37+
new_y = (face.points[3][1]+face.points[0][1])/2
38+
if new_x != old_x :
39+
if new_x > old_x :
40+
duty -= 50
41+
set_servo(duty)
42+
if new_x < old_x :
43+
duty +=50
44+
set_servo(duty)
45+
facelayer = DrawingLayer((frame.width, frame.height))
46+
facelayer.circle( (new_x,new_y),(face.points[3][1]-face.points[0][1])/2,color=Color.RED )
47+
#facelayer.circle( (face.points[0][0],face.points[0][1]),2,color=Color.RED )
48+
#facelayer.circle( (face.points[1][0],face.points[1][1]),2,color=Color.RED )
49+
#facelayer.circle( (face.points[2][0],face.points[2][1]),2,color=Color.RED )
50+
#facelayer.circle( (face.points[3][0],face.points[3][1]),2,color=Color.RED )
51+
#facebox_dim = (200,200)
52+
#center_point = (frame.width/2, frame.height/2)
53+
#facebox = facelayer.centeredRectangle(center_point,facebox_dim)
54+
#facelayer.text("GO", (0,0), color=Color.WHITE)
55+
#frame.addDrawingLayer(newlayer)
56+
frame.addDrawingLayer(facelayer)
57+
frame.applyLayers()
58+
frame.show()
59+
old_x = new_x
60+
old_y = new_y
61+
else:
62+
gpio.digitalWrite(servo_pin, gpio.LOW)
63+
#set_servo(1500)
64+
frame.save(myDisplay)
65+
sleep(.1)
66+
67+
def main():
68+
setup()
69+
loop()
70+
71+
main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import gpio
2+
import time
3+
4+
servo_pin = "gpio2"
5+
6+
def delayMicroseconds(us):
7+
time.sleep(1.0*us/1000/1000)
8+
9+
def delay(ms):
10+
time.sleep(1.0*ms/1000)
11+
12+
def setup():
13+
gpio.pinMode(servo_pin, gpio.OUTPUT)
14+
15+
def loop():
16+
duty = 0
17+
while True:
18+
for i in range(5):
19+
duty +=500
20+
for j in range(20):
21+
gpio.digitalWrite(servo_pin, gpio.HIGH)
22+
delayMicroseconds(duty)
23+
gpio.digitalWrite(servo_pin, gpio.LOW)
24+
delayMicroseconds(20000-duty)
25+
delay(500)
26+
time.sleep(1.5)
27+
28+
for i in range(5):
29+
duty -=500
30+
for j in range(20):
31+
gpio.digitalWrite(servo_pin, gpio.HIGH)
32+
delayMicroseconds(duty)
33+
gpio.digitalWrite(servo_pin, gpio.LOW)
34+
delayMicroseconds(20000-duty)
35+
delay(500)
36+
time.sleep(1.5)
37+
38+
def main():
39+
setup()
40+
loop()
41+
42+
main()

‎Samples/hello-flask.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Flask
2+
app = Flask(__name__)
3+
4+
@app.route("/")
5+
def hello():
6+
return "Welcome to pcDuino !"
7+
8+
if __name__ == "__main__" :
9+
app.run (host='0.0.0.0',port=80,debug=True)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# io test code for pcDuino ( http://www.pcduino.com )
4+
#
5+
__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digitalWrite', 'digitalRead', "pinMode"]
6+
7+
_GPIO_PINS = ('gpio0','gpio1','gpio2','gpio3','gpio4','gpio5','gpio6','gpio7',
8+
'gpio8', 'gpio9', 'gpio10', 'gpio11', 'gpio12', 'gpio13',
9+
'gpio14', 'gpio15', 'gpio16', 'gpio17', 'gpio18', 'gpio19')
10+
11+
_PIN_FD_PATH = '/sys/devices/virtual/misc/gpio/pin/%s'
12+
_MODE_FD_PATH = '/sys/devices/virtual/misc/gpio/mode/%s'
13+
HIGH = 1
14+
LOW = 0
15+
INPUT = 0
16+
OUTPUT = 1
17+
18+
class InvalidChannelException(Exception):
19+
"""The channel sent is invalid on pcDuino board """
20+
pass
21+
22+
def _GetValidId(channel):
23+
if channel in _GPIO_PINS:
24+
return channel
25+
else:
26+
raise InvalidChannelException
27+
28+
def digitalWrite(channel, value):
29+
"""Write to a GPIO channel"""
30+
id = _GetValidId(channel)
31+
with open(_PIN_FD_PATH % id, 'w') as f:
32+
f.write('1' if value == HIGH else '0')
33+
34+
def digitalRead(channel):
35+
"""Read from a GPIO channel"""
36+
id = _GetValidId(channel)
37+
with open(_PIN_FD_PATH % id, 'r') as f:
38+
return f.read(1) == '1'
39+
40+
def pinMode(channel, mode):
41+
""" Set Mode of a GPIO channel """
42+
id = _GetValidId(channel)
43+
with open(_MODE_FD_PATH % id, 'w') as f:
44+
f.write('0' if mode == INPUT else '1')

0 commit comments

Comments
 (0)