@@ -60,7 +60,10 @@ def __init__(self, dht11, pin, trig_wait):
60
60
self ._last_called = 0
61
61
self ._humidity = None
62
62
self ._temperature = None
63
-
63
+ # We don't use a context because linux-based systems are sluggish
64
+ # and we're better off having a running process
65
+ if _USE_PULSEIO :
66
+ self .pulse_in = pulseio .PulseIn (self ._pin , 81 , True )
64
67
65
68
def _pulses_to_binary (self , pulses , start , stop ):
66
69
"""Takes pulses, a list of transition times, and converts
@@ -102,25 +105,22 @@ def _get_pulses_pulseio(self):
102
105
pulses will have 81 elements for the DHT11/22 type devices.
103
106
"""
104
107
pulses = array .array ('H' )
105
- # create the PulseIn object using context manager
106
- with pulseio .PulseIn (self ._pin , 81 , True ) as pulse_in :
108
+ if _USE_PULSEIO :
107
109
# The DHT type device use a specialize 1-wire protocol
108
110
# The microprocessor first sends a LOW signal for a
109
111
# specific length of time. Then the device sends back a
110
112
# series HIGH and LOW signals. The length the HIGH signals
111
113
# represents the device values.
112
- pulse_in .pause ()
113
- pulse_in .clear ()
114
- pulse_in .resume (self ._trig_wait )
114
+ self .pulse_in .pause ()
115
+ self .pulse_in .clear ()
116
+ self .pulse_in .resume (self ._trig_wait )
117
+
115
118
# loop until we get the return pulse we need or
116
119
# time out after 1/4 second
117
- tmono = time .monotonic ()
118
- while time .monotonic () - tmono < 0.25 :
119
- pass # time out after 1/4 seconds
120
- pulse_in .pause ()
121
- while pulse_in :
122
- pulses .append (pulse_in .popleft ())
123
- pulse_in .resume ()
120
+ time .sleep (0.25 )
121
+ self .pulse_in .pause ()
122
+ while self .pulse_in :
123
+ pulses .append (self .pulse_in .popleft ())
124
124
return pulses
125
125
126
126
def _get_pulses_bitbang (self ):
0 commit comments