Teensy 4.1 core updated for C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

145 行
3.1KB

  1. /* Experimental File & FS base classes.
  2. */
  3. #ifndef FS_H
  4. #define FS_H
  5. #ifdef __cplusplus
  6. #include <Arduino.h>
  7. #define FILE_READ 0
  8. #define FILE_WRITE 1
  9. enum SeekMode {
  10. SeekSet = 0,
  11. SeekCur = 1,
  12. SeekEnd = 2
  13. };
  14. class File : public Stream {
  15. public:
  16. constexpr File() : f(nullptr) { }
  17. File(File *file) {
  18. // "file" must only be a class derived from File
  19. // can we use is_same or is_polymorphic with static_assert?
  20. // or is_base_of
  21. //static_assert(std::is_same<decltype(*file),File>::value,
  22. //"File(File *file) constructor only accepts pointers "
  23. //"to derived classes, not File itself");
  24. f = file;
  25. if (f) f->refcount++;
  26. }
  27. File(const File &file) {
  28. //Serial.println("File copy constructor");
  29. //static int copycount=0;
  30. //if (++copycount > 20) while (1) ;
  31. f = file.f;
  32. if (f) f->refcount++;
  33. }
  34. File& operator = (const File &file) {
  35. //Serial.println("File assignment");
  36. //static int assigncount=0;
  37. //if (++assigncount > 20) while (1) ;
  38. invalidate();
  39. f = file.f;
  40. if (f) f->refcount++;
  41. return *this;
  42. }
  43. virtual ~File() {
  44. invalidate();
  45. }
  46. #if 1
  47. virtual void whoami() { // testing only
  48. Serial.printf(" File this=%x, f=%x\n", (int)this, (int)f);
  49. if (f) f->whoami();
  50. }
  51. unsigned int getRefcount() { // testing only
  52. return refcount;
  53. }
  54. #endif
  55. virtual size_t read(void *buf, size_t nbyte) {
  56. return (f) ? f->read(buf, nbyte) : 0;
  57. }
  58. virtual size_t write(const void *buf, size_t size) {
  59. return (f) ? f->write(buf, size) : 0;
  60. }
  61. virtual int available() {
  62. return (f) ? f->available() : 0;
  63. }
  64. virtual int read() {
  65. return (f) ? f->read() : -1;
  66. }
  67. virtual int peek() {
  68. return (f) ? f->peek() : -1;
  69. }
  70. virtual void flush() {
  71. if (f) f->flush();
  72. }
  73. virtual bool seek(uint32_t pos, int mode) {
  74. return (f) ? f->seek(pos, mode) : false;
  75. }
  76. virtual uint32_t position() const {
  77. return (f) ? f->position() : 0;
  78. }
  79. virtual uint32_t size() const {
  80. return (f) ? f->size() : 0;
  81. }
  82. virtual void close() {
  83. if (f) f->close();
  84. }
  85. virtual operator bool() const {
  86. return (f) ? (bool)*f : false;
  87. }
  88. virtual const char* name() {
  89. return (f) ? f->name() : "";
  90. }
  91. virtual bool isDirectory() {
  92. return (f) ? f->isDirectory() : false;
  93. }
  94. virtual File openNextFile(uint8_t mode=0) {
  95. return (f) ? f->openNextFile(mode) : *this;
  96. }
  97. virtual void rewindDirectory(void) {
  98. if (f) f->rewindDirectory();
  99. }
  100. bool seek(uint32_t pos) {
  101. return seek(pos, SeekSet);
  102. }
  103. size_t write(uint8_t b) {
  104. return write(&b, 1);
  105. }
  106. size_t write(const char *str) {
  107. return write(str, strlen(str));
  108. }
  109. size_t readBytes(char *buffer, size_t length) {
  110. return read(buffer, length);
  111. }
  112. private:
  113. void invalidate() {
  114. if (f && --(f->refcount) == 0) delete f;
  115. }
  116. union {
  117. // instances of base File class use this pointer
  118. File *f;
  119. // instances of derived classes (which actually access media)
  120. // use this reference count which is managed by the base class
  121. unsigned int refcount;
  122. };
  123. };
  124. class FS
  125. {
  126. public:
  127. FS() {}
  128. File open(const char *filename, uint8_t mode = FILE_READ);
  129. bool exists(const char *filepath);
  130. bool mkdir(const char *filepath);
  131. bool remove(const char *filepath);
  132. bool rmdir(const char *filepath);
  133. };
  134. #endif // __cplusplus
  135. #endif // FS_H