S&B Volcano vaporizer remote control with Pi Pico W
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <string.h>
  20. #include "hardware/watchdog.h"
  21. #include "ff.h"
  22. #include "config.h"
  23. #include "main.h"
  24. #include "usb_cdc.h"
  25. #include "serial.h"
  26. #include "ring.h"
  27. #include "log.h"
  28. static uint8_t log_buff[4096] = {0};
  29. static struct ring_buffer log_rb = RB_INIT(log_buff, sizeof(log_buff), 1);
  30. static uint8_t line_buff[256] = {0};
  31. static volatile bool got_input = false;
  32. #ifndef PICOWOTA
  33. static FIL log_file_fat;
  34. #endif // PICOWOTA
  35. static void add_to_log(const void *buff, size_t len) {
  36. rb_add(&log_rb, buff, len);
  37. }
  38. struct ring_buffer *log_get(void) {
  39. return &log_rb;
  40. }
  41. #ifndef PICOWOTA
  42. static void log_dump_to_x(void (*write)(const void *, size_t)) {
  43. if (rb_len(&log_rb) == 0) {
  44. return;
  45. }
  46. int l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nbuffered log output:\r\n");
  47. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  48. write(line_buff, l);
  49. }
  50. rb_dump(&log_rb, write, 0);
  51. l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nlive log:\r\n");
  52. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  53. write(line_buff, l);
  54. }
  55. }
  56. void log_dump_to_usb(void) {
  57. log_dump_to_x(usb_cdc_write);
  58. }
  59. void log_dump_to_uart(void) {
  60. #ifndef NDEBUG
  61. log_dump_to_x(serial_write);
  62. #endif // NDEBUG
  63. }
  64. static void log_file_write_callback(const void *data, size_t len) {
  65. UINT bw;
  66. FRESULT res = f_write(&log_file_fat, data, len, &bw);
  67. if ((res != FR_OK) || (bw != len)) {
  68. debug("error: f_write %u returned %d", len, res);
  69. }
  70. }
  71. void log_dump_to_disk(void) {
  72. FRESULT res = f_open(&log_file_fat, "log.txt", FA_CREATE_ALWAYS | FA_WRITE);
  73. if (res != FR_OK) {
  74. debug("error: f_open returned %d", res);
  75. return;
  76. }
  77. rb_dump(&log_rb, log_file_write_callback, 0);
  78. res = f_close(&log_file_fat);
  79. if (res != FR_OK) {
  80. debug("error: f_close returned %d", res);
  81. }
  82. }
  83. #endif // PICOWOTA
  84. void debug_log_va(bool do_log, const char *format, va_list args) {
  85. int l = vsnprintf((char *)line_buff, sizeof(line_buff), format, args);
  86. if (l < 0) {
  87. // encoding error
  88. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: encoding error\r\n", __func__);
  89. } else if (l >= (ssize_t)sizeof(line_buff)) {
  90. // not enough space for string
  91. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: message too long (%d)\r\n", __func__, l);
  92. }
  93. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  94. #ifndef PICOWOTA
  95. usb_cdc_write(line_buff, l);
  96. #ifndef NDEBUG
  97. serial_write(line_buff, l);
  98. #endif // NDEBUG
  99. #endif // PICOWOTA
  100. if (do_log) {
  101. add_to_log(line_buff, l);
  102. }
  103. }
  104. }
  105. void debug_log(bool do_log, const char* format, ...) {
  106. va_list args;
  107. va_start(args, format);
  108. debug_log_va(do_log, format, args);
  109. va_end(args);
  110. }
  111. #ifndef PICOWOTA
  112. void debug_handle_input(const void *buff, size_t len) {
  113. (void)buff;
  114. if (len > 0) {
  115. got_input = true;
  116. }
  117. }
  118. void debug_wait_input(const char *format, ...) {
  119. va_list args;
  120. va_start(args, format);
  121. debug_log_va(false, format, args);
  122. va_end(args);
  123. got_input = false;
  124. usb_cdc_set_reroute(true);
  125. serial_set_reroute(true);
  126. while (!got_input) {
  127. main_loop_hw();
  128. }
  129. usb_cdc_set_reroute(false);
  130. serial_set_reroute(false);
  131. }
  132. #endif // PICOWOTA