A set of data recording functions to locally store data in a circular buffer, with functions for offloading to an SD Card when convenient. dataRecorderr.h shows accessible functions to the main program - all direct SD operations are abstracted away by the library. When using this library, #include dataRecorder.h
Dependencies: sd-driver_compatible_with_MAX32630FTHR
Fork of CircularBufferSDCardLib by
sdCardReader.cpp
- Committer:
- DVLevine
- Date:
- 2018-04-03
- Revision:
- 0:ebe71c7e7854
- Child:
- 1:45627bbdeb69
File content as of revision 0:ebe71c7e7854:
#include "sdCardReader.h" //SDCardReader::SDCardReader(){ //this = new SDBlockDevice(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); //} int SDCardReader::writeBlock(uint8_t* blockToWrite, int dataLength){ //printf("%i\n",dataLength); if ( 0 != this->init()) { printf("Init failed \n"); return -1; } printf("lul"); // set the frequency if ( 0 != this->frequency(5000000)) { printf("Error setting frequency \n"); } printf("huh"); // uint8_t lala[512] = "NOOMBA"; // blockToWrite = lala; dataLength = 4096; //blockToWrite = uint8 lala[512] printf("%i\n",this->program(blockToWrite, 0, dataLength)); if (0 == this->program(blockToWrite, 0, dataLength)){ printf("Block Written!"); //read back contents of block if ( 0 == this->read(blockToWrite, 0, dataLength)) { // print the contents of the block printf("%s", blockToWrite); } } printf("what?"); // call the SDBlockDevice instance de-initialisation method. this->deinit(); return 0; } //use circular buffers instead of strings int SDCardReader::writeData(string title, string dataToWrite){ /* uint8_t m_title[512];// = reinterpret_cast<const uint8_t*>(&title[0]); uint8_t m_data[1024];// = reinterpret_cast<const uint8_t*>(&dataToWrite[0]); m_title = reinterpret_cast<const uint8_t*>(&title[0]); m_data = reinterpret_cast<const uint8_t*>(&dataToWrite[0]);*/ vector<uint8_t> myTitleVector(title.begin(), title.end()); uint8_t *m_title = &myTitleVector[0]; vector<uint8_t> myDataVector(dataToWrite.begin(), dataToWrite.end()); uint8_t *m_data = &myDataVector[0]; if ( 0 != this->init()) { printf("Init failed \n"); return -1; } // set the frequency if ( 0 != this->frequency(5000000)) { printf("Error setting frequency \n"); } printf("writing"); //Writing title // Write some the data block to the device if ( 0 == this->program(m_title, 0, 512)) { // read the data block from the device if ( 0 == this->read(m_title, 0, 512)) { // print the contents of the block printf("%s", m_title); } } //Writing data if ( 0 == this->program(m_data, 0, 1024)) { // read the data block from the device if ( 0 == this->read(m_data, 0, 1024)) { // print the contents of the block printf("%s", m_data); } } // call the SDBlockDevice instance de-initialisation method. this->deinit(); return 0; } int SDCardReader::eraseData(){ printf("Initializing the block device... "); fflush(stdout); int err = this.init(); printf("%s\n", (err ? "Fail :(" : "OK")); if (err) { error("error: %s (%d)\n", strerror(-err), err); } printf("Erasing the block device... "); fflush(stdout); err = this.erase(0, this.size()); printf("%s\n", (err ? "Fail :(" : "OK")); if (err) { error("error: %s (%d)\n", strerror(-err), err); } printf("Deinitializing the block device... "); fflush(stdout); err = this.deinit(); printf("%s\n", (err ? "Fail :(" : "OK")); if (err) { error("error: %s (%d)\n", strerror(-err), err); } } // Try to mount the filesystem void SDCardReader::mountFileSystem(){ printf("Mounting the filesystem... "); fflush(stdout); int err = fs.mount(&this); printf("%s\n", (err ? "Fail :(" : "OK")); if (err) { // Reformat if we can't mount the filesystem // this should only happen on the first boot printf("No filesystem found, formatting... "); fflush(stdout); err = fs.reformat(&this); printf("%s\n", (err ? "Fail :(" : "OK")); if (err) { error("error: %s (%d)\n", strerror(-err), err); } } } // Open file FILE * SDCardReader::writeFile(string filename, dataRecorder* myDataRecorder); printf("Opening \"/fs/%s\"... ",filename); fflush(stdout); FILE *f = fopen("/fs/"+filename, "r+"); printf("%s\n", (!f ? "Fail :(" : "OK")); if (!f) { // Create the file if it doesn't exist printf("No file found, creating a new file... "); fflush(stdout); f = fopen("/fs/"+filename, "w+"); printf("%s\n", (!f ? "Fail :(" : "OK")); if (!f) { error("error: %s (%d)\n", strerror(errno), -errno); } for (int i = 0; i < 10; i++) { printf("\rWriting numbers (%d/%d)... ", i, 10); fflush(stdout); err = fprintf(f, " %d\n", i); if (err < 0) { printf("Fail :(\n"); error("error: %s (%d)\n", strerror(errno), -errno); } } printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); printf("Seeking file... "); fflush(stdout); err = fseek(f, 0, SEEK_SET); printf("%s\n", (err < 0 ? "Fail :(" : "OK")); if (err < 0) { error("error: %s (%d)\n", strerror(errno), -errno); } } // Go through and increment the numbers for (int i = 0; i < 10; i++) { printf("\rIncrementing numbers (%d/%d)... ", i, 10); fflush(stdout); // Get current stream position long pos = ftell(f); // Parse out the number and increment int32_t number; fscanf(f, "%d", &number); number += 1; // Seek to beginning of number fseek(f, pos, SEEK_SET); // Store number fprintf(f, " %d\n", number); // Flush between write and read on same file fflush(f); }