import serial

class GridEye(object):
  def __init__(self, port, baudrate=115200):
    self.serial = serial.Serial(port, baudrate, timeout=0.1)
  
  def readData(self):
    datas = []
    for i in range(4):
      retry = True
      while retry:
        self.serial.reset_input_buffer()
        self.serial.reset_output_buffer()
        self.serial.write(bytes([ord('0')+i]))
        raw_data = self.serial.read(64*5+3)
        if raw_data.split(b':')[0] != bytes([ord('0')+i]):
          continue
        data = [int(x, 16)*0.25 for x in raw_data.split()[1:]]
        #data = self.serial.read(64*5+3)
        if b'FFFF' not in data:
          retry = False
        datas.append(data)

    return datas
