No Description
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.

debug.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * debug.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 <string.h>
  19. #include "pico/stdlib.h"
  20. #include "ff.h"
  21. #include "config.h"
  22. #include "log.h"
  23. #include "pmw3360.h"
  24. #include "debug.h"
  25. static FATFS fs;
  26. static bool mounted = false;
  27. int debug_msc_mount(void) {
  28. if (mounted) {
  29. debug("already mounted");
  30. return 0;
  31. }
  32. FRESULT res = f_mount(&fs, "", 0);
  33. if (res != FR_OK) {
  34. debug("error: f_mount returned %d", res);
  35. mounted = false;
  36. return -1;
  37. }
  38. mounted = true;
  39. return 0;
  40. }
  41. int debug_msc_unmount(void) {
  42. if (!mounted) {
  43. debug("already unmounted");
  44. return 0;
  45. }
  46. FRESULT res = f_mount(0, "", 0);
  47. if (res != FR_OK) {
  48. debug("error: f_mount returned %d", res);
  49. return -1;
  50. }
  51. mounted = false;
  52. return 0;
  53. }
  54. static void debug_msc_pmw_stats(void) {
  55. FIL file;
  56. FRESULT res = f_open(&file, "pmw_stats.txt", FA_CREATE_ALWAYS | FA_WRITE);
  57. if (res != FR_OK) {
  58. debug("error: f_open returned %d", res);
  59. return;
  60. }
  61. char status_buff[1024];
  62. pmw_print_status(status_buff, sizeof(status_buff));
  63. size_t len = strlen(status_buff);
  64. UINT bw;
  65. res = f_write(&file, status_buff, len, &bw);
  66. if ((res != FR_OK) || (bw != len)) {
  67. debug("error: f_write returned %d", res);
  68. }
  69. res = f_close(&file);
  70. if (res != FR_OK) {
  71. debug("error: f_close returned %d", res);
  72. }
  73. }
  74. void debug_msc_stats(void) {
  75. debug_msc_pmw_stats();
  76. log_dump_to_disk();
  77. }
  78. static void debug_msc_pmw3360_frame(void) {
  79. FIL file;
  80. FRESULT res = f_open(&file, "pmw_frame.bin", FA_CREATE_ALWAYS | FA_WRITE);
  81. if (res != FR_OK) {
  82. debug("error: f_open returned %d", res);
  83. return;
  84. }
  85. uint8_t frame[PMW_FRAME_CAPTURE_LEN];
  86. ssize_t r = pmw_frame_capture(frame, PMW_FRAME_CAPTURE_LEN);
  87. if (r != PMW_FRAME_CAPTURE_LEN) {
  88. debug("error: pmw_frame_capture %d != %d", r, PMW_FRAME_CAPTURE_LEN);
  89. } else {
  90. UINT bw;
  91. res = f_write(&file, frame, r, &bw);
  92. if ((res != FR_OK) || ((ssize_t)bw != r)) {
  93. debug("error: f_write returned %d", res);
  94. }
  95. }
  96. res = f_close(&file);
  97. if (res != FR_OK) {
  98. debug("error: f_close returned %d", res);
  99. }
  100. }
  101. void debug_msc_pmw3360(void) {
  102. pmw_dump_data(false);
  103. debug_msc_pmw3360_frame();
  104. }