@@ -212,6 +212,29 @@ def read(self, length: int) -> bytes:
212212 '''
213213 return os .read (self .device , length )
214214
215+ def find_and_read (self , sub : bytes , length : int , max_search : int ):
216+ '''
217+ Find the first occurence of `sub` and read a message of `length` bytes from the device
218+
219+ Args:
220+ sub: the initial bytes to search for in the stream
221+ length: the length of the message to read, including the length of `sub`
222+ max_search: the maximum number of bytes to read when searching
223+
224+ Returns:
225+ bytes if successful, None if not found
226+ '''
227+ buf = b''
228+ while len (buf ) < max_search :
229+ buf += self .read (128 )
230+ if sub in buf :
231+ buf = buf [buf .index (sub ):]
232+ remaining = length - len (buf )
233+ if remaining > 0 :
234+ buf += self .read (remaining )
235+ buf = buf [:length ]
236+ return buf
237+
215238 def write (self , data : bytes ) -> int :
216239 '''
217240 Writes data to the I2C bus
@@ -361,19 +384,14 @@ def _query_i2c_path(cls, i2c_path):
361384 try :
362385 # open the I2C device using the host read address
363386 device = cls .I2CDevice (i2c_path , cls .HOST_ADDR_R )
364- # read some 512 bytes from the device
365- data = device .read ( 512 )
387+ # search for the EDID header within our 512 read bytes
388+ edid = device .find_and_read ( bytes . fromhex ( '00 FF FF FF FF FF FF 00' ), 128 , 512 )
366389 except IOError as e :
367390 cls ._logger .error (f'IOError reading from device { i2c_path } : { e } ' )
368391 return
369392
370- # search for the EDID header within our 512 read bytes
371- start = data .find (bytes .fromhex ('00 FF FF FF FF FF FF 00' ))
372- if start < 0 :
393+ if edid is None :
373394 return
374-
375- # grab 128 bytes of the edid
376- edid = data [start : start + 128 ]
377395 # parse the EDID
378396 manufacturer_id , manufacturer , model , name , serial = EDID .parse (edid )
379397 return {
0 commit comments