annotate py/gpio_rpi.py @ 579:4a6e75ed2ed4

fix gpio chown script
author Matt Johnston <matt@ucc.asn.au>
date Mon, 30 Nov 2015 21:40:45 +0800
parents 92632a6d1dc6
children b0c0e88cbfcc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import RPi.GPIO as GPIO
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 from utils import L,D,EX,W
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 __all__ = ["Gpio"]
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 class Gpio(object):
537
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
10 SYS_GPIO_BASE = '/sys/class/gpio/gpio'
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
11 def __init__(self, pin, name):
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
12 self.pin = pin
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
13 self.name = name
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
14
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
15 dir_fn = '%s%d/direction' % (self.SYS_GPIO_BASE, pin)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
16 with open(dir_fn, 'w') as f:
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
17 # make sure it doesn't start "on"
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
18 f.write('low')
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
19 val_fn = '%s%d/value' % (self.SYS_GPIO_BASE, pin)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
20 self.value_file = open(val_fn, 'r+')
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
21
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
22 def turn(self, value):
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
23 self.value_file.seek(0)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
24 self.value_file.write('1' if value else '0')
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
25 self.value_file.flush()
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
537
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
27 def get_state(self):
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
28 self.value_file.seek(0)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
29 buf = self.value_file.read().strip()
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
30 if buf == '0':
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
31 return False
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
32 if buf != '1':
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
33 E("Bad value read from gpio '%s': '%s'"
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
34 % (self.value_file.name, buf))
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
35 return True
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
36
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
537
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
38 def main():
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
39 g = Gpio(17, 'f')
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
40 g.turn(1)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
41
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
42 print(g.get_state())
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
43
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
44 g.turn(0)
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
45
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
46 print(g.get_state())
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
47
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
48 if __name__ == '__main__':
92632a6d1dc6 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
49 main()