Advertisement
Guest User

Untitled

a guest
Dec 25th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include "mbed.h"
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <functional>
  5. #include <chrono>
  6. #define BUFFER_MAX_LEN 10
  7. #define FORCE_REFORMAT true
  8.  
  9. #include "SDBlockDevice.h"
  10. BlockDevice *sd = SDBlockDevice::get_default_instance();
  11. #include "FATFileSystem.h"
  12. FATFileSystem fs("fs");
  13.  
  14.  
  15.  
  16. volatile uint16_t fileBuffer = NULL;
  17. int bufferPtr = 0;
  18. AnalogIn adcz(A0);
  19. Ticker ticker;
  20. Timer fileOpenTimer;
  21.  
  22.  
  23. #define bufferSize 1024
  24. uint16_t sensorReading[bufferSize];
  25. unsigned int / = 0;
  26. volatile unsigned int writePointer = 0; // volatile so that the main loop knows to check for changes.
  27.  
  28. // opens the next unused file name in the format set.
  29. // This can be a little slow the first time if there are already lots of log files
  30. // since it tries each number in turn but the second call on will be fairly quick.
  31. int counter = 0;
  32. FILE *nextLogFile(void)
  33. {
  34. static unsigned int fileNumber = 0;
  35. char fileName[32];
  36. FILE *filePtr = NULL;
  37. do {
  38. if (filePtr != NULL)
  39. fclose(filePtr);
  40. sprintf(fileName,"/fs/log%04u.txt",counter++);
  41. filePtr = fopen(fileName,"r");
  42. } while (filePtr != NULL);
  43. return fopen( fileName,"w");
  44. }
  45.  
  46.  
  47.  
  48. int main()
  49. {
  50. int err = fs.mount(sd);
  51. if (err || FORCE_REFORMAT) {
  52. printf("formatting ");
  53. fflush(stdout);
  54. err = fs.reformat(sd);
  55. if (err) {
  56. printf("badreform");
  57. error("error: %s (%d)\n", strerror(-err), err);
  58. return 1;
  59. }
  60. }
  61. FILE *myLogFile;
  62. myLogFile = nextLogFile();
  63. if (!myLogFile) {
  64. printf("notlog");
  65. // ERROR failed to open the first log file for writing.
  66. // The SD card is missing, not working, read only or full?
  67.  
  68. return 1; // probably want to exit the program in this situation
  69. }
  70. fileOpenTimer.start();
  71.  
  72.  
  73. while (true) {
  74. sensorReading[writePointer++] = adcz.read_u16();
  75. if (writePointer == bufferSize)
  76. writePointer = 0;
  77. if (writePointer == readPointer) {
  78. // BUFFER OVERFLOW. You may want to print an error message or turn an LED on
  79. }
  80.  
  81. while (writePointer != readPointer) { // write any waiting data to the SD card
  82. fprintf(myLogFile,"%hu\r\n",sensorReading[readPointer++]);
  83. if (readPointer == bufferSize)
  84. readPointer = 0;
  85. }
  86.  
  87. if (std::chrono::duration_cast<std::chrono::milliseconds>(fileOpenTimer.elapsed_time()).count() > 1000*60) {
  88. fclose(myLogFile); // close the current file
  89. printf("done");
  90. myLogFile = nextLogFile(); // open a new file
  91. if (!myLogFile) {
  92. // ERROR failed to open the log file for writing.
  93. // card full maybe?
  94.  
  95. break; // exit the while(true) loop
  96. }
  97. fileOpenTimer.reset(); // restart the timer
  98. }
  99. }
  100.  
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement