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);
5252void 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//--------------------------------------------------------------------+
110103static unsigned frame_num = 0 ;
111104static unsigned tx_busy = 0 ;
112105static 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 )
119113static struct {
120114 uint32_t size ;
121115 uint8_t const * buffer ;
@@ -129,29 +123,30 @@ 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
135131static uint8_t frame_buffer [FRAME_WIDTH * FRAME_HEIGHT * 16 / 8 ];
136- static void fill_color_bar (uint8_t * buffer , unsigned start_position )
137- {
138- /* EBU color bars
139- * See also https://stackoverflow.com/questions/6939422 */
132+
133+ static void fill_color_bar (uint8_t * buffer , unsigned start_position ) {
134+ /* EBU color bars: https://stackoverflow.com/questions/6939422 */
140135 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 */
136+ /* Y, U, Y, V */
137+ { 235 , 128 , 235 , 128 }, /* 100% White */
138+ { 219 , 16 , 219 , 138 }, /* Yellow */
139+ { 188 , 154 , 188 , 16 }, /* Cyan */
140+ { 173 , 42 , 173 , 26 }, /* Green */
141+ { 78 , 214 , 78 , 230 }, /* Magenta */
142+ { 63 , 102 , 63 , 240 }, /* Red */
143+ { 32 , 240 , 32 , 118 }, /* Blue */
144+ { 16 , 128 , 16 , 128 }, /* Black */
150145 };
151- uint8_t * p ;
146+ uint8_t * p ;
152147
153148 /* Generate the 1st line */
154- uint8_t * end = & buffer [FRAME_WIDTH * 2 ];
149+ uint8_t * end = & buffer [FRAME_WIDTH * 2 ];
155150 unsigned idx = (FRAME_WIDTH / 2 - 1 ) - (start_position % (FRAME_WIDTH / 2 ));
156151 p = & buffer [idx * 4 ];
157152 for (unsigned i = 0 ; i < 8 ; ++ i ) {
@@ -163,39 +158,40 @@ static void fill_color_bar(uint8_t *buffer, unsigned start_position)
163158 }
164159 }
165160 }
161+
166162 /* Duplicate the 1st line to the others */
167163 p = & buffer [FRAME_WIDTH * 2 ];
168164 for (unsigned i = 1 ; i < FRAME_HEIGHT ; ++ i ) {
169165 memcpy (p , buffer , FRAME_WIDTH * 2 );
170166 p += FRAME_WIDTH * 2 ;
171167 }
172168}
169+
173170#endif
174171
175- void video_task (void )
176- {
172+ void video_task (void ) {
177173 static unsigned start_ms = 0 ;
178174 static unsigned already_sent = 0 ;
179175
180176 if (!tud_video_n_streaming (0 , 0 )) {
181- already_sent = 0 ;
182- frame_num = 0 ;
177+ already_sent = 0 ;
178+ frame_num = 0 ;
183179 return ;
184180 }
185181
186182 if (!already_sent ) {
187183 already_sent = 1 ;
188184 start_ms = board_millis ();
189185#ifdef CFG_EXAMPLE_VIDEO_READONLY
190- # if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG )
186+ # if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG )
191187 tud_video_n_frame_xfer (0 , 0 , (void * )(uintptr_t )& frame_buffer [(frame_num % (FRAME_WIDTH / 2 )) * 4 ],
192188 FRAME_WIDTH * FRAME_HEIGHT * 16 /8 );
193- # else
189+ # else
194190 tud_video_n_frame_xfer (0 , 0 , (void * )(uintptr_t )frames [frame_num % 8 ].buffer , frames [frame_num % 8 ].size );
195- # endif
191+ # endif
196192#else
197193 fill_color_bar (frame_buffer , frame_num );
198- tud_video_n_frame_xfer (0 , 0 , (void * )frame_buffer , FRAME_WIDTH * FRAME_HEIGHT * 16 / 8 );
194+ tud_video_n_frame_xfer (0 , 0 , (void * ) frame_buffer , FRAME_WIDTH * FRAME_HEIGHT * 16 / 8 );
199195#endif
200196 }
201197
@@ -205,30 +201,30 @@ void video_task(void)
205201 start_ms += interval_ms ;
206202
207203#ifdef CFG_EXAMPLE_VIDEO_READONLY
208- # if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG )
204+ # if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG )
209205 tud_video_n_frame_xfer (0 , 0 , (void * )(uintptr_t )& frame_buffer [(frame_num % (FRAME_WIDTH / 2 )) * 4 ],
210206 FRAME_WIDTH * FRAME_HEIGHT * 16 /8 );
211- # else
207+ # else
212208 tud_video_n_frame_xfer (0 , 0 , (void * )(uintptr_t )frames [frame_num % 8 ].buffer , frames [frame_num % 8 ].size );
213- # endif
209+ # endif
214210#else
215211 fill_color_bar (frame_buffer , frame_num );
216- tud_video_n_frame_xfer (0 , 0 , (void * )frame_buffer , FRAME_WIDTH * FRAME_HEIGHT * 16 / 8 );
212+ tud_video_n_frame_xfer (0 , 0 , (void * ) frame_buffer , FRAME_WIDTH * FRAME_HEIGHT * 16 / 8 );
217213#endif
218214}
219215
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 ;
216+ void tud_video_frame_xfer_complete_cb (uint_fast8_t ctl_idx , uint_fast8_t stm_idx ) {
217+ ( void ) ctl_idx ;
218+ (void ) stm_idx ;
223219 tx_busy = 0 ;
224220 /* flip buffer */
225221 ++ frame_num ;
226222}
227223
228224int 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 ;
225+ video_probe_and_commit_control_t const * parameters ) {
226+ ( void ) ctl_idx ;
227+ (void ) stm_idx ;
232228 /* convert unit to ms from 100 ns */
233229 interval_ms = parameters -> dwFrameInterval / 10000 ;
234230 return VIDEO_ERROR_NONE ;
@@ -237,13 +233,12 @@ int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
237233//--------------------------------------------------------------------+
238234// BLINKING TASK
239235//--------------------------------------------------------------------+
240- void led_blinking_task (void )
241- {
236+ void led_blinking_task (void ) {
242237 static uint32_t start_ms = 0 ;
243238 static bool led_state = false;
244239
245240 // Blink every interval ms
246- if ( board_millis () - start_ms < blink_interval_ms ) return ; // not enough time
241+ if (board_millis () - start_ms < blink_interval_ms ) return ; // not enough time
247242 start_ms += blink_interval_ms ;
248243
249244 board_led_write (led_state );
0 commit comments