mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #2267 from openmv/usbdbg_state
misc/usbdbg: Implement USB debug GET_STATE command.
This commit is contained in:
commit
674cb76499
@ -191,6 +191,49 @@ void usbdbg_data_in(void *buffer, int length) {
|
|||||||
cmd = USBDBG_NONE;
|
cmd = USBDBG_NONE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case USBDBG_GET_STATE:
|
||||||
|
// Clear flags
|
||||||
|
((uint32_t *) buffer)[0] = 0;
|
||||||
|
|
||||||
|
// Set script running flag
|
||||||
|
if (script_running) {
|
||||||
|
((uint32_t *) buffer)[0] |= USBDBG_STATE_FLAGS_SCRIPT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set text buf valid flag.
|
||||||
|
uint32_t tx_buf_len = usb_cdc_buf_len();
|
||||||
|
if (tx_buf_len) {
|
||||||
|
((uint32_t *) buffer)[0] |= USBDBG_STATE_FLAGS_TEXT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to lock FB. If header size == 0 frame is not ready
|
||||||
|
if (mutex_try_lock_alternate(&JPEG_FB()->lock, MUTEX_TID_IDE)) {
|
||||||
|
// If header size == 0 frame is not ready
|
||||||
|
if (JPEG_FB()->size == 0) {
|
||||||
|
// unlock FB
|
||||||
|
mutex_unlock(&JPEG_FB()->lock, MUTEX_TID_IDE);
|
||||||
|
} else {
|
||||||
|
// Set frame width, height and size/bpp
|
||||||
|
((uint32_t *) buffer)[1] = JPEG_FB()->w;
|
||||||
|
((uint32_t *) buffer)[2] = JPEG_FB()->h;
|
||||||
|
((uint32_t *) buffer)[3] = JPEG_FB()->size;
|
||||||
|
|
||||||
|
// Set valid frame flag.
|
||||||
|
((uint32_t *) buffer)[0] |= USBDBG_STATE_FLAGS_FRAME;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The rest of this packet is packed with text buffer.
|
||||||
|
if (tx_buf_len) {
|
||||||
|
tx_buf_len = OMV_MIN(tx_buf_len, (40 - 1));
|
||||||
|
usb_cdc_get_buf((uint8_t *) buffer + 24, tx_buf_len);
|
||||||
|
((uint8_t *) buffer)[24 + tx_buf_len] = 0; // Null-terminate
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = USBDBG_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
default: /* error */
|
default: /* error */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -431,6 +474,11 @@ void usbdbg_control(void *buffer, uint8_t request, uint32_t length) {
|
|||||||
xfer_length = length;
|
xfer_length = length;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case USBDBG_GET_STATE:
|
||||||
|
xfer_bytes = 0;
|
||||||
|
xfer_length = length;
|
||||||
|
break;
|
||||||
|
|
||||||
default: /* error */
|
default: /* error */
|
||||||
cmd = USBDBG_NONE;
|
cmd = USBDBG_NONE;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -49,6 +49,13 @@ enum usbdbg_cmd {
|
|||||||
USBDBG_SENSOR_ID =0x90,
|
USBDBG_SENSOR_ID =0x90,
|
||||||
USBDBG_TX_INPUT =0x11,
|
USBDBG_TX_INPUT =0x11,
|
||||||
USBDBG_SET_TIME =0x12,
|
USBDBG_SET_TIME =0x12,
|
||||||
|
USBDBG_GET_STATE =0x93,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum usbdbg_state_flags {
|
||||||
|
USBDBG_STATE_FLAGS_SCRIPT = (1 << 0),
|
||||||
|
USBDBG_STATE_FLAGS_TEXT = (1 << 1),
|
||||||
|
USBDBG_STATE_FLAGS_FRAME = (1 << 2),
|
||||||
};
|
};
|
||||||
|
|
||||||
void usbdbg_init();
|
void usbdbg_init();
|
||||||
|
|||||||
@ -37,6 +37,11 @@ __USBDBG_SYS_RESET_TO_BL= 0x0E
|
|||||||
__USBDBG_FB_ENABLE = 0x0D
|
__USBDBG_FB_ENABLE = 0x0D
|
||||||
__USBDBG_TX_BUF_LEN = 0x8E
|
__USBDBG_TX_BUF_LEN = 0x8E
|
||||||
__USBDBG_TX_BUF = 0x8F
|
__USBDBG_TX_BUF = 0x8F
|
||||||
|
__USBDBG_GET_STATE = 0x93
|
||||||
|
|
||||||
|
__USBDBG_STATE_FLAGS_SCRIPT = (1 << 0)
|
||||||
|
__USBDBG_STATE_FLAGS_TEXT = (1 << 1)
|
||||||
|
__USBDBG_STATE_FLAGS_FRAME = (1 << 2)
|
||||||
|
|
||||||
ATTR_CONTRAST =0
|
ATTR_CONTRAST =0
|
||||||
ATTR_BRIGHTNESS =1
|
ATTR_BRIGHTNESS =1
|
||||||
@ -70,6 +75,44 @@ def fb_size():
|
|||||||
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_FRAME_SIZE, __FB_HDR_SIZE))
|
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_FRAME_SIZE, __FB_HDR_SIZE))
|
||||||
return struct.unpack("III", __serial.read(12))
|
return struct.unpack("III", __serial.read(12))
|
||||||
|
|
||||||
|
def read_state():
|
||||||
|
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_GET_STATE, 64))
|
||||||
|
flags, w, h, size, res0, res1, text_buf = struct.unpack("IIIIII40s", __serial.read(64))
|
||||||
|
|
||||||
|
text = None
|
||||||
|
if flags & __USBDBG_STATE_FLAGS_TEXT:
|
||||||
|
text = text_buf.split(b'\0', 1)[0].decode()
|
||||||
|
|
||||||
|
if flags & __USBDBG_STATE_FLAGS_FRAME == 0:
|
||||||
|
return 0, 0, None, text
|
||||||
|
|
||||||
|
num_bytes = size if size > 2 else (w * h * size)
|
||||||
|
|
||||||
|
# read fb data
|
||||||
|
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_FRAME_DUMP, num_bytes))
|
||||||
|
buff = __serial.read(num_bytes)
|
||||||
|
|
||||||
|
if size == 1: # Grayscale
|
||||||
|
y = np.fromstring(buff, dtype=np.uint8)
|
||||||
|
buff = np.column_stack((y, y, y))
|
||||||
|
elif size == 2: # RGB565
|
||||||
|
arr = np.fromstring(buff, dtype=np.uint16).newbyteorder('S')
|
||||||
|
r = (((arr & 0xF800) >>11)*255.0/31.0).astype(np.uint8)
|
||||||
|
g = (((arr & 0x07E0) >>5) *255.0/63.0).astype(np.uint8)
|
||||||
|
b = (((arr & 0x001F) >>0) *255.0/31.0).astype(np.uint8)
|
||||||
|
buff = np.column_stack((r,g,b))
|
||||||
|
else: # JPEG
|
||||||
|
try:
|
||||||
|
buff = np.asarray(Image.frombuffer("RGB", (w, h), buff, "jpeg", "RGB", ""))
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"JPEG decode error (%e)")
|
||||||
|
|
||||||
|
if (buff.size != (w*h*3)):
|
||||||
|
raise ValueError(f"Unexpected frame size. Expected: {w*h*3} received: {buff.size}")
|
||||||
|
|
||||||
|
return w, h, buff.reshape((h, w, 3)), text
|
||||||
|
|
||||||
|
|
||||||
def fb_dump():
|
def fb_dump():
|
||||||
size = fb_size()
|
size = fb_size()
|
||||||
|
|
||||||
|
|||||||
@ -28,8 +28,10 @@ sensor.skip_frames(time = 2000) # Wait for settings take effect.
|
|||||||
clock = time.clock() # Create a clock object to track the FPS.
|
clock = time.clock() # Create a clock object to track the FPS.
|
||||||
|
|
||||||
while(True):
|
while(True):
|
||||||
|
clock.tick()
|
||||||
img = sensor.snapshot() # Take a picture and return the image.
|
img = sensor.snapshot() # Take a picture and return the image.
|
||||||
sensor.flush()
|
sensor.flush()
|
||||||
|
print(clock.fps(), " FPS")
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# init pygame
|
# init pygame
|
||||||
@ -55,7 +57,7 @@ for i in range(10):
|
|||||||
sleep(0.100)
|
sleep(0.100)
|
||||||
|
|
||||||
if not connected:
|
if not connected:
|
||||||
print ( "Failed to connect to OpenMV's serial port.\n"
|
print("Failed to connect to OpenMV's serial port.\n"
|
||||||
"Please install OpenMV's udev rules first:\n"
|
"Please install OpenMV's udev rules first:\n"
|
||||||
"sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n"
|
"sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n"
|
||||||
"sudo udevadm control --reload-rules\n\n")
|
"sudo udevadm control --reload-rules\n\n")
|
||||||
@ -73,18 +75,20 @@ screen = None
|
|||||||
IMAGE_SCALE = 4
|
IMAGE_SCALE = 4
|
||||||
|
|
||||||
Clock = pygame.time.Clock()
|
Clock = pygame.time.Clock()
|
||||||
font = pygame.font.SysFont("monospace", 15)
|
font = pygame.font.SysFont("monospace", 25)
|
||||||
|
|
||||||
while running:
|
try:
|
||||||
Clock.tick(100)
|
while running:
|
||||||
|
Clock.tick(30)
|
||||||
|
# Read state
|
||||||
|
w, h, data, text = pyopenmv.read_state()
|
||||||
|
|
||||||
# read framebuffer
|
if text is not None:
|
||||||
fb = pyopenmv.fb_dump()
|
print(text, end="")
|
||||||
if fb is not None:
|
|
||||||
|
if data is not None:
|
||||||
fps = Clock.get_fps()
|
fps = Clock.get_fps()
|
||||||
w, h, data = fb[0], fb[1], fb[2]
|
# Create image from RGB888
|
||||||
|
|
||||||
# create image from RGB888
|
|
||||||
image = pygame.image.frombuffer(data.flat[0:], (w, h), 'RGB')
|
image = pygame.image.frombuffer(data.flat[0:], (w, h), 'RGB')
|
||||||
image = pygame.transform.scale(image, (w * IMAGE_SCALE, h * IMAGE_SCALE))
|
image = pygame.transform.scale(image, (w * IMAGE_SCALE, h * IMAGE_SCALE))
|
||||||
|
|
||||||
@ -94,7 +98,6 @@ while running:
|
|||||||
# blit stuff
|
# blit stuff
|
||||||
screen.blit(image, (0, 0))
|
screen.blit(image, (0, 0))
|
||||||
screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0))
|
screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0))
|
||||||
|
|
||||||
# update display
|
# update display
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
@ -106,6 +109,8 @@ while running:
|
|||||||
running = False
|
running = False
|
||||||
if event.key == pygame.K_c:
|
if event.key == pygame.K_c:
|
||||||
pygame.image.save(image, "capture.png")
|
pygame.image.save(image, "capture.png")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
pyopenmv.stop_script()
|
pyopenmv.stop_script()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user