Skip to content

Commit 6b7ceed

Browse files
authored
Merge pull request #2516 from hathach/minor-code-format
Minor code format
2 parents a0e5626 + 2a4b27e commit 6b7ceed

File tree

4 files changed

+366
-507
lines changed

4 files changed

+366
-507
lines changed

examples/device/cdc_dual_ports/src/main.c

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@
3131
#include "bsp/board_api.h"
3232
#include "tusb.h"
3333

34-
//------------- prototypes -------------//
34+
/* Blink pattern
35+
* - 250 ms : device not mounted
36+
* - 1000 ms : device mounted
37+
* - 2500 ms : device is suspended
38+
*/
39+
enum {
40+
BLINK_NOT_MOUNTED = 250,
41+
BLINK_MOUNTED = 1000,
42+
BLINK_SUSPENDED = 2500,
43+
};
44+
45+
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
46+
47+
static void led_blinking_task(void);
3548
static void cdc_task(void);
3649

3750
/*------------- MAIN -------------*/
38-
int main(void)
39-
{
51+
int main(void) {
4052
board_init();
4153

4254
// init device stack on configured roothub port
@@ -46,28 +58,23 @@ int main(void)
4658
board_init_after_tusb();
4759
}
4860

49-
while (1)
50-
{
61+
while (1) {
5162
tud_task(); // tinyusb device task
5263
cdc_task();
64+
led_blinking_task();
5365
}
5466
}
5567

5668
// echo to either Serial0 or Serial1
5769
// with Serial0 as all lower case, Serial1 as all upper case
58-
static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count)
59-
{
70+
static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count) {
6071
uint8_t const case_diff = 'a' - 'A';
6172

62-
for(uint32_t i=0; i<count; i++)
63-
{
64-
if (itf == 0)
65-
{
73+
for (uint32_t i = 0; i < count; i++) {
74+
if (itf == 0) {
6675
// echo back 1st port as lower case
6776
if (isupper(buf[i])) buf[i] += case_diff;
68-
}
69-
else
70-
{
77+
} else {
7178
// echo back 2nd port as upper case
7279
if (islower(buf[i])) buf[i] -= case_diff;
7380
}
@@ -77,21 +84,29 @@ static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count)
7784
tud_cdc_n_write_flush(itf);
7885
}
7986

87+
// Invoked when device is mounted
88+
void tud_mount_cb(void) {
89+
blink_interval_ms = BLINK_MOUNTED;
90+
}
91+
92+
// Invoked when device is unmounted
93+
void tud_umount_cb(void) {
94+
blink_interval_ms = BLINK_NOT_MOUNTED;
95+
}
96+
97+
8098
//--------------------------------------------------------------------+
8199
// USB CDC
82100
//--------------------------------------------------------------------+
83-
static void cdc_task(void)
84-
{
101+
static void cdc_task(void) {
85102
uint8_t itf;
86103

87-
for (itf = 0; itf < CFG_TUD_CDC; itf++)
88-
{
104+
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
89105
// connected() check for DTR bit
90106
// Most but not all terminal client set this when making connection
91107
// if ( tud_cdc_n_connected(itf) )
92108
{
93-
if ( tud_cdc_n_available(itf) )
94-
{
109+
if (tud_cdc_n_available(itf)) {
95110
uint8_t buf[64];
96111

97112
uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
@@ -103,3 +118,18 @@ static void cdc_task(void)
103118
}
104119
}
105120
}
121+
122+
//--------------------------------------------------------------------+
123+
// BLINKING TASK
124+
//--------------------------------------------------------------------+
125+
void led_blinking_task(void) {
126+
static uint32_t start_ms = 0;
127+
static bool led_state = false;
128+
129+
// Blink every interval ms
130+
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
131+
start_ms += blink_interval_ms;
132+
133+
board_led_write(led_state);
134+
led_state = 1 - led_state; // toggle
135+
}

0 commit comments

Comments
 (0)