util.c (1629B)
1#include "util.h" 2 3#include "split.h" 4#include "board.h" 5#include "class/cdc/cdc_device.h" 6#include "led.h" 7#include "ws2812.h" 8 9#include "pico/stdio.h" 10#include "tusb.h" 11#include "bsp/board.h" 12#include "tusb_config.h" 13 14#include <stdio.h> 15 16char warnlog[256]; 17int log_level_min = LOG_INFO; 18int log_group_mask = LOG_ALL; 19 20static void 21__attribute__((format(printf, 1, 0))) 22panic_task(const char *fmtstr, va_list ap, uint32_t sleep_ms) 23{ 24 static uint32_t start_ms = 0; 25 va_list cpy; 26 27 if (!tud_cdc_connected()) 28 return; 29 30 if (!start_ms) start_ms = board_millis(); 31 32 if (board_millis() < start_ms + sleep_ms) 33 return; 34 35 va_copy(cpy, ap); 36 vprintf(fmtstr, cpy); 37 printf("\n"); 38 tud_cdc_write_flush(); 39 40 start_ms += sleep_ms; 41} 42 43void 44__attribute__ ((format (printf, 3, 4))) 45stdio_log(int facility, int level, const char *fmtstr, ...) 46{ 47 va_list ap, cpy; 48 49 if (!(facility & log_group_mask)) 50 return; 51 52 if (level < log_level_min) 53 return; 54 55 if (level == LOG_WARN && !*warnlog) { 56 led_start_blip(HARD_RED, 100); 57 va_copy(cpy, ap); 58 va_start(cpy, fmtstr); 59 vsnprintf(warnlog, sizeof(warnlog), fmtstr, cpy); 60 va_end(cpy); 61 62 if (split_role == SLAVE) 63 split_warn_master(warnlog); 64 } 65 66 if (!tud_cdc_connected()) 67 return; 68 69 va_start(ap, fmtstr); 70 vprintf(fmtstr, ap); 71 va_end(ap); 72 printf("\n"); 73 tud_cdc_write_flush(); 74} 75 76void 77__attribute__((format(printf, 3, 4))) 78blink_panic(uint32_t blink_ms, uint32_t rgb, const char *fmtstr, ...) 79{ 80 va_list ap; 81 82 led_blink_ms = blink_ms; 83 led_rgb = rgb; 84 led_mode = LED_BLINK; 85 86 va_start(ap, fmtstr); 87 while (1) { 88 tud_task(); 89 panic_task(fmtstr, ap, 1000); 90 led_task(); 91 } 92 va_end(ap); 93}