Skip to content

Commit de12666

Browse files
committed
clean up video example for readability
1 parent 09ae917 commit de12666

File tree

5 files changed

+96
-146
lines changed

5 files changed

+96
-146
lines changed

examples/device/video_capture/src/images.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
2+
// uncopmressed frame
23
static const unsigned char frame_buffer[128 * (96 + 1) * 2] = {
34
/* 0 */
45
0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80,
@@ -1650,8 +1651,10 @@ static const unsigned char frame_buffer[128 * (96 + 1) * 2] = {
16501651
0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80,
16511652
0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80,
16521653
};
1654+
16531655
#else
16541656

1657+
// mpeg compressed data (not CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
16551658
#define color_bar_0_jpg_len 511
16561659
#define color_bar_1_jpg_len 512
16571660
#define color_bar_2_jpg_len 511

examples/device/video_capture/src/main.c

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* - 1000 ms : device mounted
4141
* - 2500 ms : device is suspended
4242
*/
43-
enum {
43+
enum {
4444
BLINK_NOT_MOUNTED = 250,
4545
BLINK_MOUNTED = 1000,
4646
BLINK_SUSPENDED = 2500,
@@ -52,8 +52,7 @@ void led_blinking_task(void);
5252
void video_task(void);
5353

5454
/*------------- MAIN -------------*/
55-
int main(void)
56-
{
55+
int main(void) {
5756
board_init();
5857

5958
// init device stack on configured roothub port
@@ -63,8 +62,7 @@ int main(void)
6362
board_init_after_tusb();
6463
}
6564

66-
while (1)
67-
{
65+
while (1) {
6866
tud_task(); // tinyusb device task
6967
led_blinking_task();
7068

@@ -77,45 +75,41 @@ int main(void)
7775
//--------------------------------------------------------------------+
7876

7977
// Invoked when device is mounted
80-
void tud_mount_cb(void)
81-
{
78+
void tud_mount_cb(void) {
8279
blink_interval_ms = BLINK_MOUNTED;
8380
}
8481

8582
// Invoked when device is unmounted
86-
void tud_umount_cb(void)
87-
{
83+
void tud_umount_cb(void) {
8884
blink_interval_ms = BLINK_NOT_MOUNTED;
8985
}
9086

9187
// Invoked when usb bus is suspended
9288
// remote_wakeup_en : if host allow us to perform remote wakeup
9389
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
94-
void tud_suspend_cb(bool remote_wakeup_en)
95-
{
90+
void tud_suspend_cb(bool remote_wakeup_en) {
9691
(void) remote_wakeup_en;
9792
blink_interval_ms = BLINK_SUSPENDED;
9893
}
9994

10095
// Invoked when usb bus is resumed
101-
void tud_resume_cb(void)
102-
{
96+
void tud_resume_cb(void) {
10397
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
10498
}
10599

106-
107100
//--------------------------------------------------------------------+
108101
// USB Video
109102
//--------------------------------------------------------------------+
110103
static unsigned frame_num = 0;
111104
static unsigned tx_busy = 0;
112105
static unsigned interval_ms = 1000 / FRAME_RATE;
113106

114-
/* YUY2 frame buffer */
115107
#ifdef CFG_EXAMPLE_VIDEO_READONLY
108+
// For mcus that does not have enough SRAM for frame buffer, we use fixed frame data.
109+
// To further reduce the size, we use MJPEG format instead of YUY2.
116110
#include "images.h"
117111

118-
# if !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
112+
#if !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
119113
static struct {
120114
uint32_t size;
121115
uint8_t const *buffer;
@@ -129,29 +123,31 @@ static struct {
129123
{color_bar_6_jpg_len, color_bar_6_jpg},
130124
{color_bar_7_jpg_len, color_bar_7_jpg},
131125
};
132-
# endif
126+
#endif
133127

134128
#else
129+
130+
// YUY2 frame buffer
135131
static uint8_t frame_buffer[FRAME_WIDTH * FRAME_HEIGHT * 16 / 8];
136-
static void fill_color_bar(uint8_t *buffer, unsigned start_position)
137-
{
132+
133+
static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
138134
/* EBU color bars
139135
* See also https://stackoverflow.com/questions/6939422 */
140136
static uint8_t const bar_color[8][4] = {
141-
/* Y, U, Y, V */
142-
{ 235, 128, 235, 128}, /* 100% White */
143-
{ 219, 16, 219, 138}, /* Yellow */
144-
{ 188, 154, 188, 16}, /* Cyan */
145-
{ 173, 42, 173, 26}, /* Green */
146-
{ 78, 214, 78, 230}, /* Magenta */
147-
{ 63, 102, 63, 240}, /* Red */
148-
{ 32, 240, 32, 118}, /* Blue */
149-
{ 16, 128, 16, 128}, /* Black */
137+
/* Y, U, Y, V */
138+
{ 235, 128, 235, 128}, /* 100% White */
139+
{ 219, 16, 219, 138}, /* Yellow */
140+
{ 188, 154, 188, 16}, /* Cyan */
141+
{ 173, 42, 173, 26}, /* Green */
142+
{ 78, 214, 78, 230}, /* Magenta */
143+
{ 63, 102, 63, 240}, /* Red */
144+
{ 32, 240, 32, 118}, /* Blue */
145+
{ 16, 128, 16, 128}, /* Black */
150146
};
151-
uint8_t *p;
147+
uint8_t* p;
152148

153149
/* Generate the 1st line */
154-
uint8_t *end = &buffer[FRAME_WIDTH * 2];
150+
uint8_t* end = &buffer[FRAME_WIDTH * 2];
155151
unsigned idx = (FRAME_WIDTH / 2 - 1) - (start_position % (FRAME_WIDTH / 2));
156152
p = &buffer[idx * 4];
157153
for (unsigned i = 0; i < 8; ++i) {
@@ -163,23 +159,24 @@ static void fill_color_bar(uint8_t *buffer, unsigned start_position)
163159
}
164160
}
165161
}
162+
166163
/* Duplicate the 1st line to the others */
167164
p = &buffer[FRAME_WIDTH * 2];
168165
for (unsigned i = 1; i < FRAME_HEIGHT; ++i) {
169166
memcpy(p, buffer, FRAME_WIDTH * 2);
170167
p += FRAME_WIDTH * 2;
171168
}
172169
}
170+
173171
#endif
174172

175-
void video_task(void)
176-
{
173+
void video_task(void) {
177174
static unsigned start_ms = 0;
178175
static unsigned already_sent = 0;
179176

180177
if (!tud_video_n_streaming(0, 0)) {
181-
already_sent = 0;
182-
frame_num = 0;
178+
already_sent = 0;
179+
frame_num = 0;
183180
return;
184181
}
185182

@@ -195,7 +192,7 @@ void video_task(void)
195192
# endif
196193
#else
197194
fill_color_bar(frame_buffer, frame_num);
198-
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);
195+
tud_video_n_frame_xfer(0, 0, (void*) frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
199196
#endif
200197
}
201198

@@ -213,22 +210,22 @@ void video_task(void)
213210
# endif
214211
#else
215212
fill_color_bar(frame_buffer, frame_num);
216-
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);
213+
tud_video_n_frame_xfer(0, 0, (void*) frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
217214
#endif
218215
}
219216

220-
void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
221-
{
222-
(void)ctl_idx; (void)stm_idx;
217+
void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
218+
(void) ctl_idx;
219+
(void) stm_idx;
223220
tx_busy = 0;
224221
/* flip buffer */
225222
++frame_num;
226223
}
227224

228225
int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
229-
video_probe_and_commit_control_t const *parameters)
230-
{
231-
(void)ctl_idx; (void)stm_idx;
226+
video_probe_and_commit_control_t const* parameters) {
227+
(void) ctl_idx;
228+
(void) stm_idx;
232229
/* convert unit to ms from 100 ns */
233230
interval_ms = parameters->dwFrameInterval / 10000;
234231
return VIDEO_ERROR_NONE;
@@ -237,13 +234,12 @@ int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
237234
//--------------------------------------------------------------------+
238235
// BLINKING TASK
239236
//--------------------------------------------------------------------+
240-
void led_blinking_task(void)
241-
{
237+
void led_blinking_task(void) {
242238
static uint32_t start_ms = 0;
243239
static bool led_state = false;
244240

245241
// Blink every interval ms
246-
if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
242+
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
247243
start_ms += blink_interval_ms;
248244

249245
board_led_write(led_state);

examples/device/video_capture/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
#define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256
102102

103103
// use bulk endpoint for streaming interface
104-
#define CFG_TUD_VIDEO_STREAMING_BULK 0
104+
#define CFG_TUD_VIDEO_STREAMING_BULK 1
105105

106106
#ifdef __cplusplus
107107
}

0 commit comments

Comments
 (0)