S&B Volcano vaporizer remote control with Pi Pico W
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

log.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * log.c
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include "hardware/watchdog.h"
  20. #include "ff.h"
  21. #include "config.h"
  22. #include "main.h"
  23. #include "usb_cdc.h"
  24. #include "serial.h"
  25. #include "ring.h"
  26. #include "log.h"
  27. static uint8_t log_buff[4096] = {0};
  28. static struct ring_buffer log = RB_INIT(log_buff, sizeof(log_buff));
  29. static uint8_t line_buff[256] = {0};
  30. static volatile bool got_input = false;
  31. static FIL log_file_fat;
  32. static void add_to_log(const uint8_t *buff, size_t len) {
  33. rb_add(&log, buff, len);
  34. }
  35. static void log_dump_to_x(void (*write)(const uint8_t *, size_t)) {
  36. if (rb_len(&log) == 0) {
  37. return;
  38. }
  39. int l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nbuffered log output:\r\n");
  40. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  41. write(line_buff, l);
  42. }
  43. rb_dump(&log, write);
  44. l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nlive log:\r\n");
  45. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  46. write(line_buff, l);
  47. }
  48. }
  49. void log_dump_to_usb(void) {
  50. log_dump_to_x(usb_cdc_write);
  51. }
  52. void log_dump_to_uart(void) {
  53. log_dump_to_x(serial_write);
  54. }
  55. static void log_file_write_callback(const uint8_t *data, size_t len) {
  56. UINT bw;
  57. FRESULT res = f_write(&log_file_fat, data, len, &bw);
  58. if ((res != FR_OK) || (bw != len)) {
  59. debug("error: f_write %u returned %d", len, res);
  60. }
  61. }
  62. void log_dump_to_disk(void) {
  63. FRESULT res = f_open(&log_file_fat, "log.txt", FA_CREATE_ALWAYS | FA_WRITE);
  64. if (res != FR_OK) {
  65. debug("error: f_open returned %d", res);
  66. return;
  67. }
  68. rb_dump(&log, log_file_write_callback);
  69. res = f_close(&log_file_fat);
  70. if (res != FR_OK) {
  71. debug("error: f_close returned %d", res);
  72. }
  73. }
  74. void debug_log_va(bool log, const char *format, va_list args) {
  75. int l = vsnprintf((char *)line_buff, sizeof(line_buff), format, args);
  76. if (l < 0) {
  77. // encoding error
  78. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: encoding error\r\n", __func__);
  79. } else if (l >= (ssize_t)sizeof(line_buff)) {
  80. // not enough space for string
  81. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: message too long (%d)\r\n", __func__, l);
  82. }
  83. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  84. usb_cdc_write(line_buff, l);
  85. serial_write(line_buff, l);
  86. if (log) {
  87. add_to_log(line_buff, l);
  88. }
  89. }
  90. }
  91. void debug_log(bool log, const char* format, ...) {
  92. va_list args;
  93. va_start(args, format);
  94. debug_log_va(log, format, args);
  95. va_end(args);
  96. }
  97. void debug_handle_input(const uint8_t *buff, size_t len) {
  98. (void)buff;
  99. if (len > 0) {
  100. got_input = true;
  101. }
  102. }
  103. void debug_wait_input(const char *format, ...) {
  104. va_list args;
  105. va_start(args, format);
  106. debug_log_va(false, format, args);
  107. va_end(args);
  108. got_input = false;
  109. usb_cdc_set_reroute(true);
  110. serial_set_reroute(true);
  111. while (!got_input) {
  112. main_loop_hw();
  113. }
  114. usb_cdc_set_reroute(false);
  115. serial_set_reroute(false);
  116. }