Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

146 lines
4.8KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library 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 Library 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. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <SdFat.h>
  21. #ifndef PSTR
  22. #define PSTR(x) x
  23. #define PGM_P const char*
  24. #endif
  25. //------------------------------------------------------------------------------
  26. static void pstrPrint(PGM_P str) {
  27. for (uint8_t c; (c = pgm_read_byte(str)); str++) SdFat::stdOut()->write(c);
  28. }
  29. //------------------------------------------------------------------------------
  30. static void pstrPrintln(PGM_P str) {
  31. pstrPrint(str);
  32. SdFat::stdOut()->println();
  33. }
  34. //------------------------------------------------------------------------------
  35. /** %Print any SD error code and halt. */
  36. void SdFat::errorHalt() {
  37. errorPrint();
  38. while (1) {}
  39. }
  40. //------------------------------------------------------------------------------
  41. /** %Print msg, any SD error code, and halt.
  42. *
  43. * \param[in] msg Message to print.
  44. */
  45. void SdFat::errorHalt(char const* msg) {
  46. errorPrint(msg);
  47. while (1) {}
  48. }
  49. //------------------------------------------------------------------------------
  50. /** %Print msg, any SD error code, and halt.
  51. *
  52. * \param[in] msg Message in program space (flash memory) to print.
  53. */
  54. void SdFat::errorHalt_P(PGM_P msg) {
  55. errorPrint_P(msg);
  56. while (1) {}
  57. }
  58. //------------------------------------------------------------------------------
  59. /** %Print any SD error code. */
  60. void SdFat::errorPrint() {
  61. if (!m_card.errorCode()) return;
  62. pstrPrint(PSTR("SD errorCode: 0X"));
  63. m_stdOut->print(m_card.errorCode(), HEX);
  64. pstrPrint(PSTR(",0X"));
  65. m_stdOut->println(m_card.errorData(), HEX);
  66. }
  67. //------------------------------------------------------------------------------
  68. /** %Print msg, any SD error code.
  69. *
  70. * \param[in] msg Message to print.
  71. */
  72. void SdFat::errorPrint(char const* msg) {
  73. pstrPrint(PSTR("error: "));
  74. m_stdOut->println(msg);
  75. errorPrint();
  76. }
  77. //------------------------------------------------------------------------------
  78. /** %Print msg, any SD error code.
  79. *
  80. * \param[in] msg Message in program space (flash memory) to print.
  81. */
  82. void SdFat::errorPrint_P(PGM_P msg) {
  83. pstrPrint(PSTR("error: "));
  84. pstrPrintln(msg);
  85. errorPrint();
  86. }
  87. //------------------------------------------------------------------------------
  88. /** %Print error details and halt after SdFat::init() fails. */
  89. void SdFat::initErrorHalt() {
  90. initErrorPrint();
  91. while (1) {}
  92. }
  93. //------------------------------------------------------------------------------
  94. /**Print message, error details, and halt after SdFat::init() fails.
  95. *
  96. * \param[in] msg Message to print.
  97. */
  98. void SdFat::initErrorHalt(char const *msg) {
  99. m_stdOut->println(msg);
  100. initErrorHalt();
  101. }
  102. //------------------------------------------------------------------------------
  103. /**Print message, error details, and halt after SdFat::init() fails.
  104. *
  105. * \param[in] msg Message in program space (flash memory) to print.
  106. */
  107. void SdFat::initErrorHalt_P(PGM_P msg) {
  108. pstrPrintln(msg);
  109. initErrorHalt();
  110. }
  111. //------------------------------------------------------------------------------
  112. /** Print error details after SdFat::init() fails. */
  113. void SdFat::initErrorPrint() {
  114. if (m_card.errorCode()) {
  115. pstrPrintln(PSTR("Can't access SD card. Do not reformat."));
  116. if (m_card.errorCode() == SD_CARD_ERROR_CMD0) {
  117. pstrPrintln(PSTR("No card, wrong chip select pin, or SPI problem?"));
  118. }
  119. errorPrint();
  120. } else if (m_vol.fatType() == 0) {
  121. pstrPrintln(PSTR("Invalid format, reformat SD."));
  122. } else if (!m_vwd.isOpen()) {
  123. pstrPrintln(PSTR("Can't open root directory."));
  124. } else {
  125. pstrPrintln(PSTR("No error found."));
  126. }
  127. }
  128. //------------------------------------------------------------------------------
  129. /**Print message and error details and halt after SdFat::init() fails.
  130. *
  131. * \param[in] msg Message to print.
  132. */
  133. void SdFat::initErrorPrint(char const *msg) {
  134. m_stdOut->println(msg);
  135. initErrorPrint();
  136. }
  137. //------------------------------------------------------------------------------
  138. /**Print message and error details after SdFat::init() fails.
  139. *
  140. * \param[in] msg Message in program space (flash memory) to print.
  141. */
  142. void SdFat::initErrorPrint_P(PGM_P msg) {
  143. pstrPrintln(msg);
  144. initErrorHalt();
  145. }