@@ -63,7 +63,8 @@ def __init__(self, brick, port, check_compatible=True):
6363 """
6464 super (BaseDigitalSensor , self ).__init__ (brick , port )
6565 self .set_input_mode (Type .LOW_SPEED_9V , Mode .RAW )
66- self .lastpoll = None
66+ self .last_poll = time ()
67+ self .poll_delay = 0.01
6768 sleep (0.1 ) # Give I2C time to initialize
6869 #Don't do type checking if this class has no compatible sensors listed.
6970 try : self .compatible_sensors
@@ -95,7 +96,11 @@ def _i2c_command(self, address, value, format):
9596 """
9697 value = struct .pack (format , * value )
9798 msg = chr (self .I2C_DEV ) + chr (address ) + value
99+ if self .last_poll + self .poll_delay > time ():
100+ diff = time () - self .last_poll
101+ sleep (self .poll_delay - diff )
98102 self .brick .ls_write (self .port , msg , 0 )
103+ self .last_poll = time ()
99104
100105 def _i2c_query (self , address , format ):
101106 """Reads an i2c value from given address, and returns a value unpacked
@@ -104,14 +109,13 @@ def _i2c_query(self, address, format):
104109 """
105110 n_bytes = struct .calcsize (format )
106111 msg = chr (self .I2C_DEV ) + chr (address )
107- if not self .lastpoll : self .lastpoll = time ()
108- if self .lastpoll + 0.02 > time ():
109- diff = time () - self .lastpoll
110- sleep (0.02 - diff )
112+ if self .last_poll + self .poll_delay > time ():
113+ diff = time () - self .last_poll
114+ sleep (self .poll_delay - diff )
111115 self .brick .ls_write (self .port , msg , n_bytes )
116+ self .last_poll = time ()
112117 self ._ls_get_status (n_bytes )
113118 data = self .brick .ls_read (self .port )
114- self .lastpoll = time ()
115119 if len (data ) < n_bytes :
116120 raise I2CError , 'Read failure'
117121 return struct .unpack (format , data [- n_bytes :]) # TODO: why could there be more than n_bytes?
0 commit comments