annotate py/gpio_rpi.py @ 298:b0c0e88cbfcc

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