UnicodeFilenames.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Simple test of Unicode filename.
  2. // Unicode is supported as UTF-8 encoded strings.
  3. #include "SdFat.h"
  4. // USE_UTF8_LONG_NAMES must be non-zero in SdFat/src/SdFatCongfig.h
  5. #if USE_UTF8_LONG_NAMES
  6. #define UTF8_FOLDER u8"😀"
  7. const char* names[] = {u8"россиянин", u8"très élégant", u8"狗.txt", nullptr};
  8. // Remove files if non-zero.
  9. #define REMOVE_UTF8_FILES 1
  10. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  11. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  12. #define SD_FAT_TYPE 0
  13. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  14. #ifndef SDCARD_SS_PIN
  15. const uint8_t SD_CS_PIN = SS;
  16. #else // SDCARD_SS_PIN
  17. // Assume built-in SD is used.
  18. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  19. #endif // SDCARD_SS_PIN
  20. // Try to select the best SD card configuration.
  21. #if HAS_SDIO_CLASS
  22. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  23. #elif ENABLE_DEDICATED_SPI
  24. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(16))
  25. #else // HAS_SDIO_CLASS
  26. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(16))
  27. #endif // HAS_SDIO_CLASS
  28. #if SD_FAT_TYPE == 0
  29. SdFat sd;
  30. File file;
  31. #elif SD_FAT_TYPE == 1
  32. SdFat32 sd;
  33. File32 file;
  34. #elif SD_FAT_TYPE == 2
  35. SdExFat sd;
  36. ExFile file;
  37. #elif SD_FAT_TYPE == 3
  38. SdFs sd;
  39. FsFile file;
  40. #else // SD_FAT_TYPE
  41. #error Invalid SD_FAT_TYPE
  42. #endif // SD_FAT_TYPE
  43. void setup() {
  44. Serial.begin(9600);
  45. while (!Serial) {
  46. yield();
  47. }
  48. Serial.println("Type any character to begin");
  49. while (!Serial.available()) {
  50. yield();
  51. }
  52. if (!sd.begin(SD_CONFIG)) {
  53. sd.initErrorHalt(&Serial);
  54. }
  55. if (!sd.exists(UTF8_FOLDER)) {
  56. if (!sd.mkdir(UTF8_FOLDER)) {
  57. Serial.println("sd.mkdir failed");
  58. return;
  59. }
  60. }
  61. if (!sd.chdir(UTF8_FOLDER)) {
  62. Serial.println("sd.chdir failed");
  63. return;
  64. }
  65. for (uint8_t i = 0; names[i]; i++) {
  66. if (!file.open(names[i], O_WRONLY | O_CREAT)) {
  67. Serial.println("file.open failed");
  68. return;
  69. }
  70. file.println(names[i]);
  71. file.close();
  72. }
  73. Serial.println("ls:");
  74. sd.ls("/", LS_SIZE | LS_R);
  75. #if REMOVE_UTF8_FILES // For debug test of remove and rmdir.
  76. for (uint8_t i = 0; names[i]; i++) {
  77. sd.remove(names[i]);
  78. }
  79. sd.chdir();
  80. sd.rmdir(UTF8_FOLDER);
  81. Serial.println("After remove and rmdir");
  82. sd.ls(LS_SIZE | LS_R);
  83. #endif // REMOVE_UTF8_FILES
  84. Serial.println("Done!");
  85. }
  86. void loop() {}
  87. #else // USE_UTF8_LONG_NAMES
  88. #error USE_UTF8_LONG_NAMES must be non-zero in SdFat/src/SdFatCongfig.h
  89. #endif // USE_UTF8_LONG_NAMES