misc/usbdbg: Add GET_STATE command.

The GET_STATE command is a command that returns flags, frame width,
height, size, and the text buffer (up to 40 bytes), in a single 64
bytes packet to reduce the bandwidth/overhead of the protocol.
The packet format is:
word    word    word    word    2 words     40 bytes
<flags> <width> <height> <size> <reserved>  <null-terminated text>

The flags are mostly reserved, only the following bits are defined:
0x001 script running
0x010 text buffer valid.
0x100 JPEG frame buffer ready.
This commit is contained in:
iabdalkader 2024-07-09 19:18:51 +03:00
parent c15bc9cdf4
commit 0cef6239e0
2 changed files with 55 additions and 0 deletions

View File

@ -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;

View File

@ -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();