You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VolumeFreeSpace.ino 2.4KB

пре 9 година
пре 9 година
пре 9 година
пре 9 година
пре 9 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This program demonstrates the freeClusterCount() call.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. /*
  7. * SD chip select pin. Common values are:
  8. *
  9. * Arduino Ethernet shield, pin 4.
  10. * SparkFun SD shield, pin 8.
  11. * Adafruit Datalogging shield, pin 10.
  12. * Default SD chip select is the SPI SS pin.
  13. */
  14. const uint8_t chipSelect = SS;
  15. #define TEST_FILE "CLUSTER.TST"
  16. // file system
  17. SdFat sd;
  18. // test file
  19. SdFile file;
  20. // Serial output stream
  21. ArduinoOutStream cout(Serial);
  22. //------------------------------------------------------------------------------
  23. void printFreeSpace() {
  24. cout << F("freeClusterCount() call time: ");
  25. uint32_t m = micros();
  26. uint32_t volFree = sd.vol()->freeClusterCount();
  27. cout << micros() - m << F(" micros\n");
  28. cout << F("freeClusters: ") << volFree << setprecision(3) << endl;
  29. float fs = 0.000512*volFree*sd.vol()->blocksPerCluster();
  30. cout << F("freeSpace: ") << fs << F(" MB (MB = 1,000,000 bytes)\n\n");
  31. }
  32. //------------------------------------------------------------------------------
  33. void setup() {
  34. Serial.begin(9600);
  35. while (!Serial) {} // wait for Leonardo
  36. if (!MAINTAIN_FREE_CLUSTER_COUNT) {
  37. cout << F("Please edit SdFatConfig.h and set\n");
  38. cout << F("MAINTAIN_FREE_CLUSTER_COUNT nonzero for\n");
  39. cout << F("maximum freeClusterCount() performance.\n\n");
  40. }
  41. // F stores strings in flash to save RAM
  42. cout << F("Type any character to start\n");
  43. while (Serial.read() <= 0) {}
  44. delay(400); // catch Due reset problem
  45. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  46. // breadboards. use SPI_FULL_SPEED for better performance.
  47. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  48. sd.initErrorHalt();
  49. }
  50. // Insure no TEST_FILE.
  51. sd.remove(TEST_FILE);
  52. cout << F("\nFirst call to freeClusterCount scans the FAT.\n\n");
  53. printFreeSpace();
  54. cout << F("Create and write to ") << TEST_FILE << endl;
  55. if (!file.open(TEST_FILE, O_WRITE | O_CREAT)) {
  56. sd.errorHalt(F("Create failed"));
  57. }
  58. file.print(F("Cause a cluster to be allocated"));
  59. file.close();
  60. cout << F("\nSecond freeClusterCount call is faster if\n");
  61. cout << F("MAINTAIN_FREE_CLUSTER_COUNT is nonzero.\n\n");
  62. printFreeSpace();
  63. cout << F("Remove ") << TEST_FILE << endl << endl;
  64. sd.remove(TEST_FILE);
  65. printFreeSpace();
  66. }
  67. //------------------------------------------------------------------------------
  68. void loop() {}