├── .gitattributes ├── .gitignore ├── README.md ├── Samples ├── adc_test │ ├── adc.py │ ├── adc │ │ └── __init__.py │ ├── adc_test.py │ ├── gpio │ │ └── __init__.py │ ├── pinmap.py │ └── xively-temp.py ├── blink_led │ ├── blink_led.py │ └── gpio │ │ └── __init__.py ├── face-tracking-robot │ ├── gpio │ │ └── __init__.py │ ├── photobooth.py │ ├── track-face.py │ └── tracking-robot.py ├── hello-flask.py ├── hello-gpio │ ├── gpio │ │ └── __init__.py │ ├── hello-gpio.py │ └── templates │ │ ├── main.html │ │ └── pin.html ├── photobooth │ ├── abc.jpg │ ├── gpio │ │ └── __init__.py │ └── photobooth.py └── web-led │ ├── gpio │ └── __init__.py │ ├── templates │ ├── 1.gif │ └── pin.html │ └── web-led.py ├── hello-temp ├── adc.py ├── gpio │ └── __init__.py ├── hello-temp.py └── pinmap.py ├── pcduino ├── __init__.py ├── adc.py ├── exceptions.py ├── gpio.py ├── pinmap.py └── pwm.py ├── setup.py └── weblamp ├── gpio └── __init__.py ├── templates └── pin.html └── weblamp.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pcduino/python-pcduino/a174682a203f51169580fac7bc899967cc8080ba/README.md -------------------------------------------------------------------------------- /Samples/adc_test/adc.py: -------------------------------------------------------------------------------- 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()) 14 | -------------------------------------------------------------------------------- /Samples/adc_test/adc/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | _PIN_FD_PATH = '/proc/adc%s' 4 | 5 | 6 | def analog_read(channel): 7 | """Return the integer value of an adc pin. 8 | 9 | adc0 and adc1 have 6 bit resolution. 10 | adc2 through adc5 have 12 bit resolution. 11 | 12 | """ 13 | 14 | 15 | with open(_PIN_FD_PATH % channel, 'r') as f: 16 | return int(f.read(32).split(':')[1].strip()) -------------------------------------------------------------------------------- /Samples/adc_test/adc_test.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adc import analog_read 3 | 4 | def delay(ms): 5 | time.sleep(1.0*ms/1000) 6 | 7 | def setup(): 8 | print "read channel ADC2 value ,the V-REF = 3.3V" 9 | delay(3000) 10 | 11 | def loop(): 12 | while(1): 13 | value = analog_read(2) 14 | voltage = (value * 3.3)/4096 15 | print ("value = %4d"%value) 16 | print ("voltage = %4.3f V" %voltage) 17 | delay(100) 18 | 19 | def main(): 20 | setup() 21 | loop() 22 | 23 | main() 24 | -------------------------------------------------------------------------------- /Samples/adc_test/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/adc_test/pinmap.py: -------------------------------------------------------------------------------- 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) 35 | -------------------------------------------------------------------------------- /Samples/adc_test/xively-temp.py: -------------------------------------------------------------------------------- 1 | # part of the python code is copied from page 82 of Getting Started with BeagleBone, by Matt Richardson 2 | # Jingfeng Liu 3 | # LinkSprite.com/pcDuino.com 4 | 5 | from adc import analog_read 6 | import time 7 | import datetime 8 | import xively 9 | from requests import HTTPError 10 | 11 | api =xively.XivelyAPIClient("APIKEY") 12 | feed=api.feeds.get(FEEDID) 13 | 14 | 15 | def delay(ms): 16 | time.sleep(1.0*ms/1000) 17 | 18 | def setup(): 19 | print "read channel ADC0 value ,the V-REF = 3.3V" 20 | delay(3000) 21 | 22 | def loop(): 23 | 24 | while True: 25 | value = analog_read(5) 26 | temp = value*(3.3/4096*100) 27 | print ("value = %4d"%value) 28 | print ("temperature = %4.3f V" %temp) 29 | 30 | now=datetime.datetime.utcnow() 31 | feed.datastreams=[ xively.Datastream(id='office_temp', current_value=temp, at=now) 32 | ] 33 | 34 | try: 35 | feed.update() 36 | print "Value pushed to Xively: " + str(temp) 37 | except HTTPError as e: 38 | print "Error connecting to Xively: " + str (e) 39 | 40 | time.sleep(20) 41 | 42 | def main(): 43 | setup() 44 | loop() 45 | 46 | main() 47 | -------------------------------------------------------------------------------- /Samples/blink_led/blink_led.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # blink_led.py 3 | # gpio test code for pcduino ( http://www.pcduino.com ) 4 | # 5 | import gpio 6 | import time 7 | 8 | led_pin = "gpio18" 9 | 10 | def delay(ms): 11 | time.sleep(1.0*ms/1000) 12 | 13 | def setup(): 14 | gpio.pinMode(led_pin, gpio.OUTPUT) 15 | 16 | def loop(): 17 | while(1): 18 | gpio.digitalWrite(led_pin, gpio.HIGH) 19 | delay(200) 20 | gpio.digitalWrite(led_pin, gpio.LOW) 21 | delay(100) 22 | 23 | def main(): 24 | setup() 25 | loop() 26 | 27 | main() 28 | -------------------------------------------------------------------------------- /Samples/blink_led/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/face-tracking-robot/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/face-tracking-robot/photobooth.py: -------------------------------------------------------------------------------- 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) 48 | -------------------------------------------------------------------------------- /Samples/face-tracking-robot/track-face.py: -------------------------------------------------------------------------------- 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() 72 | -------------------------------------------------------------------------------- /Samples/face-tracking-robot/tracking-robot.py: -------------------------------------------------------------------------------- 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() 43 | -------------------------------------------------------------------------------- /Samples/hello-flask.py: -------------------------------------------------------------------------------- 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) -------------------------------------------------------------------------------- /Samples/hello-gpio/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/hello-gpio/hello-gpio.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | import datetime 3 | import gpio 4 | 5 | app = Flask(__name__) 6 | 7 | channel = { 0:'gpio0', 1:'gpio1', 2:'gpio2', 3:'gpio3', 4:'gpio4', 8 | 5:'gpio5', 6:'gpio6', 7:'gpio7', 8:'gpio8', 9:'gpio9', 9 | 10:'gpio10', 11:'gpio11', 12:'gpio12', 13:'gpio13' 10 | } 11 | 12 | @app.route("/") 13 | def hello(): 14 | 15 | now = datetime.datetime.now() 16 | timeString = now.strftime("%Y/%m/%d %H:%M:%S") 17 | templateData = { 18 | 'title':'HELLO!', 19 | 'time':timeString 20 | } 21 | return render_template('main.html',**templateData) 22 | 23 | @app.route("/readpin/") 24 | def readPin(pin): 25 | 26 | gpio.pinMode(channel[int(pin)],gpio.INPUT) 27 | value = " " 28 | 29 | if (gpio.digitalRead(channel[int(pin)]) == gpio.HIGH) : 30 | value = "Read GPIO" + pin + " is high !" 31 | else : 32 | value = "Read GPIO" + pin +" is low !" 33 | templateData = { 34 | 'title' : 'Status of GPIO' + pin , 35 | 'value' : value 36 | } 37 | return render_template('pin.html',**templateData) 38 | 39 | if __name__ == "__main__" : 40 | app.run (host='0.0.0.0',port=80,debug=True) -------------------------------------------------------------------------------- /Samples/hello-gpio/templates/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ title }} 4 | 5 | 6 | 7 |
8 |

Welcome to pcDuino ! 9 |

The date and time on the server is :{{ time }}

10 |
11 | 12 | -------------------------------------------------------------------------------- /Samples/hello-gpio/templates/pin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ title }} 4 | 5 | 6 | 7 |
8 |

Pin Status

9 |

{{ value }}

10 |
11 | 12 | pcDuino.org 13 | 14 |
15 | 16 | -------------------------------------------------------------------------------- /Samples/photobooth/abc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pcduino/python-pcduino/a174682a203f51169580fac7bc899967cc8080ba/Samples/photobooth/abc.jpg -------------------------------------------------------------------------------- /Samples/photobooth/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/photobooth/photobooth.py: -------------------------------------------------------------------------------- 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) 48 | -------------------------------------------------------------------------------- /Samples/web-led/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /Samples/web-led/templates/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pcduino/python-pcduino/a174682a203f51169580fac7bc899967cc8080ba/Samples/web-led/templates/1.gif -------------------------------------------------------------------------------- /Samples/web-led/templates/pin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Current Status 4 | 5 | 6 | 7 |
8 |

Device Listing and Status

9 | 10 | 11 |

The {{ pins['gpio0'].name }} 12 | {% if pins['gpio0'].state == true %} 13 | is currently on ! (turn off) 14 | {% else %} 15 | is currently off ! (turn on) 16 | {% endif %} 17 |

18 |
19 | 20 | The {{ pins['gpio1'].name }} 22 | {% if pins['gpio1'].state == true %} 23 | is currently on ! (turn off) 24 | {% else %} 25 | is currently off ! (turn on) 26 | {% endif %} 27 |

28 |
29 | 30 | {% if message %} 31 |

{{ message }}

32 | {% endif %} 33 | 34 | 35 |
36 | 37 | pcDuino.org 38 | 39 |
40 | 41 | 42 | -------------------------------------------------------------------------------- /Samples/web-led/web-led.py: -------------------------------------------------------------------------------- 1 | import gpio 2 | from flask import Flask, render_template, request 3 | 4 | app = Flask(__name__) 5 | 6 | pins = { 7 | 'gpio0':{'name':'Red_LED','state':False}, 8 | 'gpio1':{'name':'Blue_LED','state':True} 9 | } 10 | 11 | for pin in pins: 12 | gpio.pinMode(pin,gpio.OUTPUT) 13 | gpio.digitalWrite(pin,gpio.LOW) 14 | 15 | @app.route("/") 16 | def main(): 17 | for pin in pins : 18 | pins[pin]['state'] = gpio.digitalRead(pin) 19 | templateData = { 20 | 'pins':pins 21 | } 22 | return render_template('pin.html',**templateData) 23 | 24 | @app.route("//") 25 | def action(changepin,value): 26 | setpin = changepin 27 | message = " " 28 | deviceName = pins[setpin]['name'] 29 | 30 | if value == "on" : 31 | gpio.digitalWrite(setpin,gpio.HIGH) 32 | message = "turned " + deviceName + " on." 33 | 34 | if value == "off" : 35 | gpio.digitalWrite(setpin,gpio.LOW) 36 | message = "turned " + deviceName + " off." 37 | 38 | if value == "toggle" : 39 | gpio.digitalWrite(setpin,not gpio.digitalRead(setpin)) 40 | message = "toggled " + deviceName + " ." 41 | 42 | for pin in pins: 43 | pins[pin]['state'] = gpio.digitalRead(pin) 44 | 45 | templateData = { 46 | 'message' : message , 47 | 'pins' : pins 48 | } 49 | return render_template('pin.html',**templateData) 50 | 51 | if __name__ == "__main__" : 52 | app.run (host='0.0.0.0',port=80,debug=True) 53 | -------------------------------------------------------------------------------- /hello-temp/adc.py: -------------------------------------------------------------------------------- 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()) 14 | -------------------------------------------------------------------------------- /hello-temp/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /hello-temp/hello-temp.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from adc import analog_read 3 | import datetime 4 | 5 | app = Flask(__name__) 6 | 7 | #temperature = 2 8 | #brightness = 3 9 | #humidity =4 10 | 11 | @app.route('/') 12 | def now_time(): 13 | now = datetime.datetime.now() 14 | timeString = now.strftime("%Y/%m/%d %H:%M:%S") 15 | return "Time :%s " % timeString 16 | 17 | @app.route('/temperature') 18 | def temp(): 19 | temp = analog_read(2) 20 | return "Temperature :%d " % temp 21 | 22 | @app.route('/brightness') 23 | def brig(): 24 | brig = analog_read(3) 25 | return "Brightness : %d " % brig 26 | 27 | @app.route('/humidity') 28 | def humi(): 29 | humi = analog_read(4) 30 | return "humi : %d " % humi 31 | 32 | if __name__ == "__main__" : 33 | app.run (host='0.0.0.0',port=80,debug=True) -------------------------------------------------------------------------------- /hello-temp/pinmap.py: -------------------------------------------------------------------------------- 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) 35 | -------------------------------------------------------------------------------- /pcduino/__init__.py: -------------------------------------------------------------------------------- 1 | from pcduino.gpio import * 2 | from pcduino.adc import analog_read 3 | from pcduino.pwm import analog_write 4 | -------------------------------------------------------------------------------- /pcduino/adc.py: -------------------------------------------------------------------------------- 1 | from pcduino.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()) 14 | -------------------------------------------------------------------------------- /pcduino/exceptions.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pcduino/python-pcduino/a174682a203f51169580fac7bc899967cc8080ba/pcduino/exceptions.py -------------------------------------------------------------------------------- /pcduino/gpio.py: -------------------------------------------------------------------------------- 1 | from pcduino.pinmap import PinMap 2 | import os.path 3 | 4 | 5 | __all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digital_write', 'digital_read', 6 | "pin_mode"] 7 | 8 | HIGH = 1 9 | LOW = 0 10 | INPUT = 0 11 | OUTPUT = 1 12 | 13 | gpio_pins = PinMap( 14 | '/sys/devices/virtual/misc/gpio/pin', 15 | 'gpio', 16 | 20 17 | ) 18 | 19 | gpio_mode_pins = PinMap( 20 | '/sys/devices/virtual/misc/gpio/mode/', 21 | 'gpio', 22 | 20 23 | ) 24 | 25 | def digital_write(channel, value): 26 | """Write to a GPIO channel""" 27 | path = gpio_pins.get_path(channel) 28 | with open(path, 'w') as f: 29 | f.write('1' if value == HIGH else '0') 30 | 31 | def digital_read(channel): 32 | """Read from a GPIO channel""" 33 | path = gpio_pins.get_path(channel) 34 | with open(path, 'r') as f: 35 | return f.read(1) == '1' 36 | 37 | def pin_mode(channel, mode): 38 | """ Set Mode of a GPIO channel """ 39 | 40 | # has to disable new sysfs pwm 41 | pwmPath = '/sys/class/misc/pwmtimer/enable/' 42 | ending = 'pwm' + str(channel) 43 | if os.path.isfile(os.path.join(pwmPath ,ending)): 44 | with open(os.path.join(pwmPath, ending), 'w+') as f: 45 | f.write('0') 46 | 47 | path = gpio_mode_pins.get_path(channel) 48 | with open(path, 'w+') as f: 49 | f.write('0' if mode == INPUT else '1') 50 | -------------------------------------------------------------------------------- /pcduino/pinmap.py: -------------------------------------------------------------------------------- 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 range(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) 35 | -------------------------------------------------------------------------------- /pcduino/pwm.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | from os import listdir 3 | 4 | MAX_PWM_LEVEL = 255 5 | 6 | def analog_write(pin, value): 7 | """Write to one of the pwm pins. 8 | 9 | value can be an number between 0 and 255. 10 | 11 | """ 12 | 13 | path = '/sys/class/misc/pwmtimer/' 14 | 15 | ending = 'pwm' + str(pin) 16 | 17 | pins = listdir(os.path.join(path, 'enable')) 18 | if not ending in pins: 19 | raise ValueError("Pin not found, PWM only possible on " + " ".join(str(p) for p in pins) + ".") 20 | 21 | with open(os.path.join(path, 'max_level',ending)) as f: 22 | max_value = int(f.read()) 23 | 24 | if value < 0 or value > MAX_PWM_LEVEL: 25 | raise ValueError("value must be between 0 and %s" % MAX_PWM_LEVEL) 26 | 27 | map_level = ((max_value-1) * value) // MAX_PWM_LEVEL 28 | #-1 because if it puts max_value the duty cycle somehow becomes 0 (overflow) 29 | 30 | #disable -> change level -> enable , as requested by documentation 31 | with open(os.path.join(path, 'enable', ending), 'w+') as f: 32 | f.write("0\n") 33 | 34 | with open(os.path.join(path, 'level', ending), 'w+') as f: 35 | f.write("%d\n" % map_level) 36 | 37 | with open(os.path.join(path, 'enable', ending), 'w+') as f: 38 | f.write("1\n") 39 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name="pcduino", 5 | version="0.1.0", 6 | author="Matthew Hooker", 7 | author_email="mwhooker@gmail.com", 8 | url="https://github.com/pcduino/python-pcduino", 9 | ) 10 | -------------------------------------------------------------------------------- /weblamp/gpio/__init__.py: -------------------------------------------------------------------------------- 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') 45 | -------------------------------------------------------------------------------- /weblamp/templates/pin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Current Status 4 | 5 | 6 | 7 |
8 | 9 |

Device Listing and Status

10 |
11 | 12 | 13 | {% for pin in pins %} 14 |

The {{ pins[pin].name }} 15 | {% if pins[pin].state == true %} 16 | is currently on (turn off) 17 | {% else %} 18 | is currently off (turn on) 19 | {% endif %} 20 |

21 | {% endfor %} 22 | 23 | 24 | {% if message %} 25 |

{{ message }}

26 | {% endif %} 27 |
28 | 29 |
30 | 31 | pcDuino.org 32 | 33 |
34 | 35 | 36 | -------------------------------------------------------------------------------- /weblamp/weblamp.py: -------------------------------------------------------------------------------- 1 | import gpio 2 | from flask import Flask, render_template, request 3 | 4 | app = Flask(__name__) 5 | 6 | pins = { 7 | 'gpio0':{'name':'Red_LED','state':False}, 8 | 'gpio1':{'name':'Blue_LED','state':True} 9 | } 10 | 11 | for pin in pins: 12 | gpio.pinMode(pin,gpio.OUTPUT) 13 | gpio.digitalWrite(pin,gpio.LOW) 14 | 15 | @app.route("/") 16 | def main(): 17 | for pin in pins : 18 | pins[pin]['state'] = gpio.digitalRead(pin) 19 | templateData = { 20 | 'pins':pins 21 | } 22 | return render_template('pin.html',**templateData) 23 | 24 | @app.route("//") 25 | def action(changepin,value): 26 | setpin = changepin 27 | message = " " 28 | deviceName = pins[setpin]['name'] 29 | 30 | if value == "on" : 31 | gpio.digitalWrite(setpin,gpio.HIGH) 32 | message = "turned " + deviceName + " on." 33 | 34 | if value == "off" : 35 | gpio.digitalWrite(setpin,gpio.LOW) 36 | message = "turned " + deviceName + " off." 37 | 38 | if value == "toggle" : 39 | gpio.digitalWrite(setpin,not gpio.digitalRead(setpin)) 40 | message = "toggled " + deviceName + " ." 41 | 42 | for pin in pins: 43 | pins[pin]['state'] = gpio.digitalRead(pin) 44 | 45 | templateData = { 46 | 'message' : message , 47 | 'pins' : pins 48 | } 49 | return render_template('pin.html',**templateData) 50 | 51 | if __name__ == "__main__" : 52 | app.run (host='0.0.0.0',port=80,debug=True) 53 | --------------------------------------------------------------------------------