diff options
| author | Louis Burda <quent.burda@gmail.com> | 2022-11-20 16:35:30 +0100 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2022-11-20 16:35:44 +0100 |
| commit | 570c88be0e06de5a36c8af0de7b112e9509325df (patch) | |
| tree | 4c5d4a9f36db5cf6e920d06104f4be66fa2a1766 /src/util.c | |
| parent | 5c2cb697aa8d03eed27b45f8dc67957c45a3d722 (diff) | |
| download | sxkbd-570c88be0e06de5a36c8af0de7b112e9509325df.tar.gz sxkbd-570c88be0e06de5a36c8af0de7b112e9509325df.zip | |
Add CDC stdio driver and logging + panic handlers
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..5a6cf2d --- /dev/null +++ b/src/util.c @@ -0,0 +1,84 @@ +#include "util.h" +#include "board.h" +#include "neopix.h" + +#include "tusb.h" +#include "bsp/board.h" +#include "tusb_config.h" + +#include <stdio.h> + +int loglevel = LOG_WARN; + +static void +__attribute__((format(printf, 1, 0))) +panic_task(const char *fmtstr, va_list ap) +{ + va_list cpy; + char c; + + if (!tud_cdc_available()) + return; + + if (!tud_cdc_read(&c, 1)) + return; + + va_copy(cpy, ap); + vprintf(fmtstr, cpy); + printf("\n\r"); +} + +static void +blink_task(struct neopix *pix, uint32_t blink_ms) +{ + static uint32_t start_ms = 0; + static bool led_state = false; + + if (!start_ms) start_ms = board_millis(); + + if (board_millis() - start_ms < blink_ms) + return; + + neopix_put(pix, neopix_u32rgb(255 * led_state, 0, 0)); + + led_state ^= true; + start_ms += blink_ms; +} + +void +__attribute__ ((format (printf, 2, 3))) +stdio_log(int level, const char *fmtstr, ...) +{ + va_list ap; + + if (!tud_cdc_available()) + return; + + if (level > loglevel) + return; + + va_start(ap, fmtstr); + vprintf(fmtstr, ap); + va_end(ap); + printf("\n\r"); +} + +void +__attribute__((format(printf, 1, 2))) +blink_panic(const char *fmtstr, ...) +{ + va_list ap; + + va_start(ap, fmtstr); + + if (!onboard_led.init) + neopix_init(&onboard_led, pio0, 0, ONBOARD_LED_PIN); + + while (1) { + tud_task(); + panic_task(fmtstr, ap); + blink_task(&onboard_led, 200); + } + + va_end(ap); +} |
