예제 #1
0
 def getScreenshot(self) -> QtGui.QPixmap:
     logger.debug("Capturing screenshot...")
     if not self.serial.is_open:
         return QtGui.QPixmap()
     try:
         with self.app.serialLock:
             drain_serial(self.serial)
             timeout = self.serial.timeout
             self.serial.write("capture\r".encode('ascii'))
             self.serial.timeout = 4
             self.serial.readline()
             image_data = self.serial.read(self.screenwidth *
                                           self.screenheight * 2)
             self.serial.timeout = timeout
         rgb_data = struct.unpack(
             f">{self.screenwidth * self.screenheight}H", image_data)
         rgb_array = np.array(rgb_data, dtype=np.uint32)
         rgba_array = (0xFF000000 + ((rgb_array & 0xF800) << 8) +
                       ((rgb_array & 0x07E0) << 5) +
                       ((rgb_array & 0x001F) << 3))
         image = QtGui.QImage(rgba_array, self.screenwidth,
                              self.screenheight, QtGui.QImage.Format_ARGB32)
         logger.debug("Captured screenshot")
         return QtGui.QPixmap(image)
     except serial.SerialException as exc:
         logger.exception("Exception while capturing screenshot: %s", exc)
     return QtGui.QPixmap()
예제 #2
0
    def getScreenshot(self) -> QtGui.QPixmap:
        logger.debug("Capturing screenshot...")
        if not self.serial.is_open:
            return QtGui.QPixmap()
        try:
            with self.app.serialLock:
                drain_serial(self.serial)
                self.serial.write("capture\r".encode('ascii'))
                timeout = self.serial.timeout
                self.serial.timeout = 4
                self.serial.readline()
                image_data = self.serial.read(
                    self.screenwidth * self.screenheight * 2)
                self.serial.timeout = timeout
            rgb_data = struct.unpack(
                f"<{self.screenwidth * self.screenheight}H", image_data)
            rgb_array = np.array(rgb_data, dtype=np.uint32)
            rgba_array = (0xFF000000 +
                            ((rgb_array & 0xF800) << 8) +  # G?!
                            ((rgb_array & 0x07E0) >> 3) +  # B
                            ((rgb_array & 0x001F) << 11))  # G

            unwrapped_array = np.empty(
                self.screenwidth*self.screenheight,
                dtype=np.uint32)
            for y in range(self.screenheight // 2):
                for x in range(self.screenwidth // 2):
                    unwrapped_array[
                        2 * x + 2 * y * self.screenwidth
                    ] = rgba_array[x + y * self.screenwidth]
                    unwrapped_array[
                        (2 * x) + 1 + 2 * y * self.screenwidth
                    ] = rgba_array[
                        x + (self.screenheight//2 + y) * self.screenwidth
                    ]
                    unwrapped_array[
                        2 * x + (2 * y + 1) * self.screenwidth
                    ] = rgba_array[
                        x + self.screenwidth // 2 + y * self.screenwidth
                    ]
                    unwrapped_array[
                        (2 * x) + 1 + (2 * y + 1) * self.screenwidth
                    ] = rgba_array[
                        x + self.screenwidth // 2 +
                        (self.screenheight//2 + y) * self.screenwidth
                    ]

            image = QtGui.QImage(
                unwrapped_array,
                self.screenwidth, self.screenheight,
                QtGui.QImage.Format_ARGB32)
            logger.debug("Captured screenshot")
            return QtGui.QPixmap(image)
        except serial.SerialException as exc:
            logger.exception("Exception while capturing screenshot: %s", exc)
        return QtGui.QPixmap()
예제 #3
0
 def _capture_data(self) -> bytes:
     timeout = self.serial.timeout
     with self.serial.lock:
         drain_serial(self.serial)
         timeout = self.serial.timeout
         self.serial.write("capture\r".encode('ascii'))
         self.serial.readline()
         self.serial.timeout = 4
         image_data = self.serial.read(self.screenwidth *
                                       self.screenheight * 2)
         self.serial.timeout = timeout
     self.serial.timeout = timeout
     return image_data
예제 #4
0
 def readFirmware(self) -> str:
     try:
         with self.app.serialLock:
             drain_serial(self.serial)
             self.serial.write("info\r".encode('ascii'))
             result = ""
             data = ""
             sleep(0.01)
             while data != "ch> ":
                 data = self.serial.readline().decode('ascii')
                 result += data
         return result
     except serial.SerialException as exc:
         logger.exception("Exception while reading firmware data: %s", exc)
     return ""
예제 #5
0
 def readFromCommand(self, command) -> str:
     try:
         with self.app.serialLock:
             drain_serial(self.serial)
             self.serial.write(f"{command}\r".encode('ascii'))
             result = ""
             data = ""
             sleep(0.01)
             while data != "ch> ":
                 data = self.serial.readline().decode('ascii')
                 result += data
         return result
     except serial.SerialException as exc:
         logger.exception("Exception while reading %s: %s", command, exc)
     return ""
예제 #6
0
def detect_version(serial_port: serial.Serial) -> str:
    data = ""
    for i in range(RETRIES):
        drain_serial(serial_port)
        serial_port.write("\r".encode("ascii"))
        data = serial_port.read(128).decode("ascii")
        if data.startswith("ch> "):
            return "v1"
        # -H versions
        if data.startswith("\r\nch> "):
            return "vh"
        if data.startswith("2"):
            return "v2"
        logger.debug("Retry detection: %s", i + 1)
    logger.error('No VNA detected. Hardware responded to CR with: %s', data)
    return ""
예제 #7
0
 def readValues(self, value) -> List[str]:
     logger.debug("VNA reading %s", value)
     try:
         with self.app.serialLock:
             drain_serial(self.serial)
             self.serial.write(f"{value}\r".encode('ascii'))
             result = ""
             data = ""
             sleep(0.05)
             while data != "ch> ":
                 data = self.serial.readline().decode('ascii')
                 result += data
         values = result.split("\r\n")
         logger.debug("VNA done reading %s (%d values)", value,
                      len(values) - 2)
         return values[1:-1]
     except serial.SerialException as exc:
         logger.exception("Exception while reading %s: %s", value, exc)
     return []
예제 #8
0
 def _capture_data(self) -> bytes:
     timeout = self.serial.timeout
     with self.serial.lock:
         drain_serial(self.serial)
         timeout = self.serial.timeout
         self.serial.write("capture\r".encode('ascii'))
         self.serial.readline()
         self.serial.timeout = 4
         image_data = self.serial.read(
             self.screenwidth * self.screenheight * 2)
         self.serial.timeout = timeout
     self.serial.timeout = timeout
     rgb_data = struct.unpack(
         f">{self.screenwidth * self.screenheight}H",
         image_data)
     rgb_array = np.array(rgb_data, dtype=np.uint32)
     return (0xFF000000 +
             ((rgb_array & 0xF800) << 8) +
             ((rgb_array & 0x07E0) << 5) +
             ((rgb_array & 0x001F) << 3))
예제 #9
0
 def getCalibration(self) -> str:
     logger.debug("Reading calibration info.")
     if not self.serial.is_open:
         return "Not connected."
     with self.app.serialLock:
         try:
             drain_serial(self.serial)
             self.serial.write("cal\r".encode('ascii'))
             result = ""
             data = ""
             sleep(0.1)
             while "ch>" not in data:
                 data = self.serial.readline().decode('ascii')
                 result += data
             values = result.splitlines()
             return values[1]
         except serial.SerialException as exc:
             logger.exception(
                 "Exception while reading calibration info: %s", exc)
     return "Unknown"
예제 #10
0
 def readVersion(self):
     logger.debug("Reading version info.")
     if not self.serial.is_open:
         return ""
     try:
         with self.app.serialLock:
             drain_serial(self.serial)
             self.serial.write("version\r".encode('ascii'))
             result = ""
             data = ""
             sleep(0.1)
             while "ch>" not in data:
                 data = self.serial.readline().decode('ascii')
                 result += data
         values = result.splitlines()
         logger.debug("Found version info: %s", values[1])
         return values[1]
     except serial.SerialException as exc:
         logger.exception("Exception while reading firmware version: %s",
                          exc)
     return ""
예제 #11
0
def get_info(serial_port: serial.Serial) -> str:
    for _ in range(RETRIES):
        drain_serial(serial_port)
        serial_port.write("info\r".encode("ascii"))
        lines = []
        retries = 0
        while True:
            line = serial_port.readline()
            line = line.decode("ascii").strip()
            if not line:
                retries += 1
                if retries > RETRIES:
                    return ""
                sleep(WAIT)
                continue
            if line == "info":  # suppress echo
                continue
            if line.startswith("ch>"):
                logger.debug("Needed retries: %s", retries)
                break
            lines.append(line)
        return "\n".join(lines)
예제 #12
0
 def exec_command(self, command: str, wait: float = WAIT) -> Iterator[str]:
     logger.debug("exec_command(%s)", command)
     with self.serial.lock:
         drain_serial(self.serial)
         self.serial.write(f"{command}\r".encode('ascii'))
         sleep(wait)
         retries = 0
         max_retries = _max_retries(self.bandwidth, self.datapoints)
         logger.debug("Max retries: %s", max_retries)
         while True:
             line = self.serial.readline()
             line = line.decode("ascii").strip()
             if not line:
                 retries += 1
                 if retries > max_retries:
                     raise IOError("too many retries")
                 sleep(wait)
                 continue
             if line == command:  # suppress echo
                 continue
             if line.startswith("ch>"):
                 logger.debug("Needed retries: %s", retries)
                 break
             yield line