-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputTransducers.py
More file actions
59 lines (42 loc) · 1.57 KB
/
InputTransducers.py
File metadata and controls
59 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import RPi.GPIO as GPIO
from time import sleep
class InputTransducer:
def __init__( self, *, name="InputTransducer", pin=0 ):
self.name = name
self.pin = pin
def getValue():
return 0
class AnalougeInput( InputTransducer ):
def getValue():
lastMeasure = 0
# Discharge capacitor
GPIO.setup( self.pin, GPIO.OUT )
GPIO.output( self.pin, GPIO.LOW )
sleep( 0.1 )
GPIO.setup( self.pin, GPIO.IN )
# Count loops until voltage across
# capacitor reads high on GPIO
# Unreliable, but perhaps better than enforcing delays
while ( GPIO.input( self.pin ) == GPIO.LOW ):
lastMeasure += 1
return lastMeasure
class DigitalInput( InputTransducer ):
def __init__( self, *, id=None ):
self.deviceID == id
def getValue():
try:
mytemp = 0
filename = 'w1_slave'
f = open( '/sys/bus/w1/devices/' + self.deviceID + '/' + filename, 'r' )
line = f.readline() # read 1st line
crc = line.rsplit( ' ',1 )
crc = crc[ 1 ].replace( '\n', '' )
if crc=='YES':
line = f.readline() # read 2nd line
mytemp = line.rsplit( 't=',1 ) / float( 1000 )
else:
mytemp = -1
f.close()
return int( mytemp[ 1 ] )
except:
return -1