Files
tinyUSB/examples/device/board_test/src/main.c

78 lines
2.1 KiB
C
Raw Normal View History

/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2023-08-03 15:44:05 +07:00
#include "bsp/board_api.h"
/* Blink pattern
2019-08-31 17:39:47 +07:00
* - 250 ms : button is not pressed
* - 1000 ms : button is pressed (and hold)
*/
2023-10-31 11:39:20 +07:00
enum {
2019-08-31 17:39:47 +07:00
BLINK_PRESSED = 250,
BLINK_UNPRESSED = 1000
};
2020-03-14 14:22:08 -04:00
#define HELLO_STR "Hello from TinyUSB\r\n"
2023-10-31 11:39:20 +07:00
int main(void) {
board_init();
2021-11-05 11:40:53 +07:00
board_led_write(true);
2019-08-31 17:39:47 +07:00
uint32_t start_ms = 0;
bool led_state = false;
2023-10-31 11:39:20 +07:00
while (1) {
2019-08-31 17:39:47 +07:00
uint32_t interval_ms = board_button_read() ? BLINK_PRESSED : BLINK_UNPRESSED;
2025-07-02 15:38:14 +07:00
int ch = board_getchar();
if (ch > 0) {
board_putchar(ch);
}
// Blink and print every interval ms
2023-10-31 11:39:20 +07:00
if (!(board_millis() - start_ms < interval_ms)) {
2019-08-31 17:39:47 +07:00
start_ms = board_millis();
2025-07-02 15:38:14 +07:00
if (ch < 0) {
// skip if echoing
printf(HELLO_STR);
board_uart_write(HELLO_STR, strlen(HELLO_STR));
}
2019-08-31 17:39:47 +07:00
board_led_write(led_state);
led_state = 1 - led_state; // toggle
}
}
}
2025-07-02 15:38:14 +07:00
#ifdef ESP_PLATFORM
2023-10-31 11:39:20 +07:00
void app_main(void) {
main();
}
#endif