14
14
# text of the GNU General Public License at
15
15
# <http://www.gnu.org/licenses/gpl.txt> for the details.
16
16
17
+ # Listing of /sys/class/gpio
18
+ # export gpio107 gpio66 gpio69 gpio72 gpio97 gpiochip32 unexport
19
+ # gpio100 gpio64 gpio67 gpio70 gpio73 gpio98 gpiochip64
20
+
17
21
def decode_pin_name (pin ):
18
22
names = {
19
23
'LED' : 107 , # PC11
@@ -53,12 +57,16 @@ def analogRead(pin):
53
57
def analogWrite ():
54
58
pass
55
59
60
+ # Reads a value from a specified digital pin
61
+ # Returns '0' or '1'
56
62
def digitalRead (pin ):
57
63
pin = decode_pin_name (pin )
58
64
with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'r' ) as f :
59
65
reading = f .read ()
60
66
return reading .strip ()
61
67
68
+ # Write a HIGH or LOW value to a digital pin
69
+ # E.g. digitalWrite('11', 'HIGH')
62
70
def digitalWrite (pin , state ):
63
71
pin = decode_pin_name (pin )
64
72
with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'w' ) as f :
@@ -67,6 +75,32 @@ def digitalWrite(pin, state):
67
75
else :
68
76
f .write ('0' )
69
77
78
+ # Configures the specified pin to behave either as an input or an output
79
+ # E.g. pinMode('5', 'INPUT')
80
+ def pinMode (pin , mode ):
81
+ pin = decode_pin_name (pin )
82
+ with open ('/sys/class/gpio/gpio' + str (pin ) + '/direction' , 'w' ) as f :
83
+ if (mode == 'INPUT' ):
84
+ f .write ('in' )
85
+ else :
86
+ f .write ('out' )
87
+
88
+ # Called with list of pins e.g. [ 'LED' ]
89
+ # Returns dictionary of tuples { 'pin': (direction, value) }
90
+ def readPins (pinlist ):
91
+ pins = {}
92
+ for pin in pinlist :
93
+ syspin = str (decode_pin_name (pin ))
94
+ try :
95
+ with open ('/sys/class/gpio/gpio' + syspin + '/direction' , 'r' ) as f :
96
+ direction = f .read ().strip ()
97
+ with open ('/sys/class/gpio/gpio' + syspin + '/value' , 'r' ) as f :
98
+ value = f .read ()
99
+ pins [pin ] = (direction .strip (), value .strip ())
100
+ except :
101
+ print ('## readPins ## Cannot access pin {0} ({1})' .format (pin , syspin ))
102
+ return pins
103
+
70
104
def i2cRead ():
71
105
pass
72
106
0 commit comments