S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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), 1);
  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 void *buff, size_t len) {
  33. rb_add(&log, buff, len);
  34. }
  35. static void log_dump_to_x(void (*write)(const void *, 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. #ifndef NDEBUG
  54. log_dump_to_x(serial_write);
  55. #endif
  56. }
  57. static void log_file_write_callback(const void *data, size_t len) {
  58. UINT bw;
  59. FRESULT res = f_write(&log_file_fat, data, len, &bw);
  60. if ((res != FR_OK) || (bw != len)) {
  61. debug("error: f_write %u returned %d", len, res);
  62. }
  63. }
  64. void log_dump_to_disk(void) {
  65. FRESULT res = f_open(&log_file_fat, "log.txt", FA_CREATE_ALWAYS | FA_WRITE);
  66. if (res != FR_OK) {
  67. debug("error: f_open returned %d", res);
  68. return;
  69. }
  70. rb_dump(&log, log_file_write_callback);
  71. res = f_close(&log_file_fat);
  72. if (res != FR_OK) {
  73. debug("error: f_close returned %d", res);
  74. }
  75. }
  76. void debug_log_va(bool log, const char *format, va_list args) {
  77. int l = vsnprintf((char *)line_buff, sizeof(line_buff), format, args);
  78. if (l < 0) {
  79. // encoding error
  80. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: encoding error\r\n", __func__);
  81. } else if (l >= (ssize_t)sizeof(line_buff)) {
  82. // not enough space for string
  83. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: message too long (%d)\r\n", __func__, l);
  84. }
  85. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  86. usb_cdc_write(line_buff, l);
  87. #ifndef NDEBUG
  88. serial_write(line_buff, l);
  89. #endif
  90. if (log) {
  91. add_to_log(line_buff, l);
  92. }
  93. }
  94. }
  95. void debug_log(bool log, const char* format, ...) {
  96. va_list args;
  97. va_start(args, format);
  98. debug_log_va(log, format, args);
  99. va_end(args);
  100. }
  101. void debug_handle_input(const void *buff, size_t len) {
  102. (void)buff;
  103. if (len > 0) {
  104. got_input = true;
  105. }
  106. }
  107. void debug_wait_input(const char *format, ...) {
  108. va_list args;
  109. va_start(args, format);
  110. debug_log_va(false, format, args);
  111. va_end(args);
  112. got_input = false;
  113. usb_cdc_set_reroute(true);
  114. serial_set_reroute(true);
  115. while (!got_input) {
  116. main_loop_hw();
  117. }
  118. usb_cdc_set_reroute(false);
  119. serial_set_reroute(false);
  120. }