annotate py/gpio_rpi.py @ 282:d3ebdb1d6a55

fridge sensor is back
author Matt Johnston <matt@ucc.asn.au>
date Mon, 30 Nov 2015 21:40:57 +0800
parents d9b819dcac53
children b0c0e88cbfcc
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 import RPi.GPIO as GPIO
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 from utils import L,D,EX,W
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 __all__ = ["Gpio"]
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 class Gpio(object):
243
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
10 SYS_GPIO_BASE = '/sys/class/gpio/gpio'
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
11 def __init__(self, pin, name):
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
12 self.pin = pin
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
13 self.name = name
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
14
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
15 dir_fn = '%s%d/direction' % (self.SYS_GPIO_BASE, pin)
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
16 with open(dir_fn, 'w') as f:
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
17 # make sure it doesn't start "on"
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
18 f.write('low')
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
19 val_fn = '%s%d/value' % (self.SYS_GPIO_BASE, pin)
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
20 self.value_file = open(val_fn, 'r+')
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
21
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
22 def turn(self, value):
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
23 self.value_file.seek(0)
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
24 self.value_file.write('1' if value else '0')
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
25 self.value_file.flush()
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
243
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
27 def get_state(self):
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
28 self.value_file.seek(0)
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
29 buf = self.value_file.read().strip()
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
30 if buf == '0':
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
31 return False
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
32 if buf != '1':
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
33 E("Bad value read from gpio '%s': '%s'"
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
34 % (self.value_file.name, buf))
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
35 return True
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
36
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
243
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
38 def main():
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
39 g = Gpio(17, 'f')
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
40 g.turn(1)
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 print(g.get_state())
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 g.turn(0)
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 print(g.get_state())
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
47
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
48 if __name__ == '__main__':
d9b819dcac53 fix gpio
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
49 main()