Skip to content

Commit 9e07197

Browse files
author
marcusw
committed
Fixed Issue 5 in the trunk in a much better way according to comment 4 on the ticket.
1 parent 218f144 commit 9e07197

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

nxt/sensor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(cls, name, bases, dict):
9393
q = _make_query(address, n_bytes)
9494
setattr(cls, 'get_' + name, q)
9595

96+
import time
9697
class DigitalSensor(Sensor):
9798
'Object for digital sensors'
9899

@@ -102,6 +103,7 @@ class DigitalSensor(Sensor):
102103

103104
def __init__(self, brick, port):
104105
super(DigitalSensor, self).__init__(brick, port)
106+
self.lastpoll = None
105107

106108
def _ls_get_status(self, n_bytes):
107109
for n in range(3):
@@ -119,9 +121,15 @@ def i2c_command(self, address, value):
119121

120122
def i2c_query(self, address, n_bytes):
121123
msg = chr(DigitalSensor.I2C_DEV) + chr(address)
124+
if not self.lastpoll: self.lastpoll = time.time()
125+
if self.lastpoll+0.02 > time.time():
126+
diff = time.time() - self.lastpoll
127+
print diff
128+
time.sleep(0.02 - diff)
122129
self.brick.ls_write(self.port, msg, n_bytes)
123130
self._ls_get_status(n_bytes)
124131
data = self.brick.ls_read(self.port)
132+
self.lastpoll = time.time()
125133
if len(data) < n_bytes:
126134
raise I2CError, 'Read failure'
127135
return data[-n_bytes:]
@@ -241,7 +249,6 @@ def set_adjusted(self, active):
241249
self.sensor_type = Type.SOUND_DB
242250
self.set_input_mode()
243251

244-
245252
class UltrasonicSensor(DigitalSensor):
246253
'Object for ultrasonic sensors'
247254

0 commit comments

Comments
 (0)