prjbrook

helperCode.h for spudSensor94

Sep 17th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /* old tryme.txt code
  2. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor9\helperCode.h
  3. */
  4. int t=0;
  5. void tme(void) {
  6. Serial.println("Im in the helperCode.h INC file ");
  7. }
  8. void doLED2() {
  9. t++; if(t==2) t=0;
  10. // Serial.print("t3=");
  11. // Serial.println(t);
  12. //Serial.print(LED_BUILTIN);
  13.  
  14. if (t==1) digitalWrite(LED_BUILTIN, HIGH);
  15. if (t==0) digitalWrite(LED_BUILTIN, LOW);
  16. }
  17.  
  18. void listDir(const char * dirname) {
  19. Serial.printf("Listing directory: %s\n", dirname);
  20.  
  21. Dir root = LittleFS.openDir(dirname);
  22.  
  23. while (root.next()) {
  24. File file = root.openFile("r");
  25. Serial.print(" FILE: ");
  26. Serial.print(root.fileName());
  27. Serial.print(" SIZE: ");
  28. Serial.print(file.size());
  29. time_t cr = file.getCreationTime();
  30. time_t lw = file.getLastWrite();
  31. file.close();
  32. struct tm * tmstruct = localtime(&cr);
  33. Serial.printf(" CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
  34. tmstruct = localtime(&lw);
  35. Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
  36. }
  37. }
  38.  
  39. void readFile(const char * path) {
  40. Serial.printf("Reading file: %s\n", path);
  41.  
  42. File file = LittleFS.open(path, "r");
  43. if (!file) {
  44. Serial.println("Failed to open file for reading");
  45. return;
  46. }
  47.  
  48. Serial.print("Read from file: ");
  49. while (file.available()) {
  50. Serial.write(file.read());
  51. }
  52. file.close();
  53. }
  54.  
  55. void writeFile(const char * path, const char * message) {
  56. Serial.printf("Writing file: %s\n", path);
  57.  
  58. File file = LittleFS.open(path, "w");
  59. if (!file) {
  60. Serial.println("Failed to open file for writing");
  61. return;
  62. }
  63. if (file.print(message)) {
  64. Serial.println("File written");
  65. } else {
  66. Serial.println("Write failed");
  67. }
  68. delay(2000); // Make sure the CREATE and LASTWRITE times are different
  69. file.close();
  70. }
  71.  
  72. void appendFile(const char * path, const char * message) {
  73. Serial.printf("Appending to file: %s\n", path);
  74.  
  75. File file = LittleFS.open(path, "a");
  76. if (!file) {
  77. Serial.println("Failed to open file for appending");
  78. return;
  79. }
  80. if (file.print(message)) {
  81. Serial.println("Message appended");
  82. } else {
  83. Serial.println("Append failed");
  84. }
  85. file.close();
  86. }
  87.  
  88. void renameFile(const char * path1, const char * path2) {
  89. Serial.printf("Renaming file %s to %s\n", path1, path2);
  90. if (LittleFS.rename(path1, path2)) {
  91. Serial.println("File renamed");
  92. } else {
  93. Serial.println("Rename failed");
  94. }
  95. }
  96.  
  97. void deleteFile(const char * path) {
  98. Serial.printf("Deleting file: %s\n", path);
  99. if (LittleFS.remove(path)) {
  100. Serial.println("File deleted");
  101. } else {
  102. Serial.println("Delete failed");
  103. }
  104. }
  105.  
  106. //uint32_t uxt=1455451200; //Arbitrary early date.
  107. uint32_t uxt=1455451200 + millis(); //Millis added,just to make a difference
  108. time_t myTimeCallback() {
  109. // return 1455451200; // UNIX timestamp
  110. return uxt ; // UNIX timestamp
  111. }
Advertisement
Add Comment
Please, Sign In to add comment