本文整理汇总了C++中SdFile类的典型用法代码示例。如果您正苦于以下问题:C++ SdFile类的具体用法?C++ SdFile怎么用?C++ SdFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SdFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dirAllocTest
/*
* create enough files to force a cluster to be allocated to dir.
*/
void dirAllocTest(SdFile &dir) {
char buf[13], name[13];
SdFile file;
uint16_t n;
uint32_t size = dir.fileSize();
// create files and write name to file
for (n = 0; ; n++){
// make file name
sprintf(name, "%u.TXT", n);
// open start time
uint32_t t0 = millis();
if (!file.open(&dir, name, O_WRITE | O_CREAT | O_EXCL)) {
error("open for write failed");
}
// open end time and write start time
uint32_t t1 = millis();
// write file name to file
file.print(name);
if (!file.close()) error("close write");
// write end time
uint32_t t2 = millis();
PgmPrint("WR ");
Serial.print(n);
Serial.print(' ');
// print time to create file
Serial.print(t1 - t0);
Serial.print(' ');
// print time to write file
Serial.println(t2 - t1);
// directory size will change when a cluster is added
if (dir.fileSize() != size) break;
}
// read files and check content
for (uint16_t i = 0; i <= n; i++) {
sprintf(name, "%u.TXT", i);
// open start time
uint32_t t0 = millis();
if (!file.open(&dir, name, O_READ)) {
error("open for read failed");
}
// open end time and read start time
uint32_t t1 = millis();
int16_t nr = file.read(buf, 13);
if (nr < 5) error("file.read failed");
// read end time
uint32_t t2 = millis();
// check file content
if (strlen(name) != (uint16_t)nr || strncmp(name, buf, nr)) {
error("content compare failed");
}
if (!file.close()) error("close read failed");
PgmPrint("RD ");
Serial.print(i);
Serial.print(' ');
// print open time
Serial.print(t1 - t0);
Serial.print(' ');
// print read time
Serial.println(t2 - t1);
}
}
示例2: init
namespace sd {
#ifdef SDCARD
SdFat sd;
SdFile file;
#endif
uint32_t syncTime = 0; // time of last sync(), in millis()
//void error_P(const char* str) {
// Serial.print("error: ");
// Serial.print(str);
//
// // Stop the SD card
// close();
//}
void init() {
close();
#ifdef SDCARD
if (!sd.begin(SD_CS, SPI_FULL_SPEED)) {
// error_P("card.init");
return;
}
#endif
}
bool open(char* fileName) {
// Create LDxxxx.CSV for the lowest value of x.
#ifdef SDCARD
uint16_t i = 0;
do {
fileName[2] = (i/1000) % 10 + '0';
fileName[3] = (i/100) % 10 + '0';
fileName[4] = (i/10) % 10 + '0';
fileName[5] = i % 10 + '0';
i++;
}
while(sd.exists(fileName));
if(!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
// error_P("file open");
return false;
}
file.clearWriteError();
// write data header
file.print("time (s)");
#endif
Serial.print("v");
Serial.println(FIRMWARE_VERSION);
Serial.print("File: ");
Serial.println(fileName);
// write data header
Serial.print("time (s)");
/* We are no longer using the junction temperature in our data output.
// write data header
file.print("time (s), ambient");
Serial.print("time (s), ambient");
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
file.print(" (C)");
Serial.print(" (C)");
break;
case TEMPERATURE_UNITS_F:
file.print(" (F)");
Serial.print(" (F)");
break;
case TEMPERATURE_UNITS_K:
file.print(" (K)");
Serial.print(" (K)");
break;
}*/
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
#ifdef SDCARD
file.print(", temp_");
file.print(i, DEC);
#endif
Serial.print(", temp_");
Serial.print(i, DEC);
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
#ifdef SDCARD
file.print(" (C)");
#endif
Serial.print(" (C)");
break;
case TEMPERATURE_UNITS_F:
//.........这里部分代码省略.........
示例3: setup_sendbuf
//.........这里部分代码省略.........
ResetEEPROM();
ReloadEvents();
ServeHeader(pFile, 200, "OK", false);
}
else if (strcmp(sPage, "bin/reset") == 0)
{
ServeHeader(pFile, 200, "OK", false);
bReset = true;
}
else if (strcmp(sPage, "json/schedules") == 0)
{
JSONSchedules(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/zones") == 0)
{
JSONZones(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/settings") == 0)
{
JSONSettings(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/state") == 0)
{
JSONState(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/schedule") == 0)
{
JSONSchedule(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/wcheck") == 0)
{
JSONwCheck(key_value_pairs, pFile);
}
#ifdef LOGGING
else if (strcmp(sPage, "json/logs") == 0)
{
JSONLogs(key_value_pairs, pFile);
}
else if (strcmp(sPage, "json/tlogs") == 0)
{
JSONtLogs(key_value_pairs, pFile);
}
#endif
else if (strcmp(sPage, "ShowSched") == 0)
{
freeMemory();
ServeSchedPage(pFile);
}
else if (strcmp(sPage, "ShowZones") == 0)
{
freeMemory();
ServeZonesPage(pFile);
}
else if (strcmp(sPage, "ShowEvent") == 0)
{
ServeEventPage(pFile);
}
else if (strcmp(sPage, "ReloadEvent") == 0)
{
ReloadEvents(true);
ServeEventPage(pFile);
}
else
{
if (strlen(sPage) == 0)
strcpy(sPage, "index.htm");
// prepend path
memmove(sPage + 5, sPage, sizeof(sPage) - 5);
memcpy(sPage, "/web/", 5);
sPage[sizeof(sPage)-1] = 0;
trace(F("Serving Page: %s\n"), sPage);
SdFile theFile;
if (!theFile.open(sPage, O_READ))
Serve404(pFile);
else
{
if (theFile.isFile())
ServeFile(pFile, sPage, theFile, client);
else
Serve404(pFile);
theFile.close();
}
}
}
#ifdef ARDUINO
flush_sendbuf(client);
// give the web browser time to receive the data
delay(1);
#else
fflush(pFile);
fclose(pFile);
#endif
// close the connection:
client.stop();
if (bReset)
sysreset();
}
}
示例4: handleConsoleInput
void handleConsoleInput() {
uint8_t retval;
uint8_t index=0;
uint8_t ch='0';
SdFile finger;
switch (console.keyValue()) {
case _DIR_:
console.printf("SOD:\r\n");
root.ls(LS_DATE | LS_SIZE);
console.printf("EOD:\r\n");
break;
case _LSV_:
console.printf("LSV:" BOM_VERSION "\r\n");
break;
case _TYP_:
typeFile(console.arguments());
break;
case _NSC_:
console.printf("SOD:\r\n");
retval=networkScan();
console.printf("EOD:\r\n");
console.printf("\nDBG: found=%d\r\n",retval);
break;
case _NJN_:
//console.printf("SOD:\r\n");
retval=networkJoin(console.arguments());
//console.printf("EOD:\r\n");
console.printf("\nDBG: joined=%s\r\n",retval?"TRUE":"FALSE");
break;
case _NPW_:
//console.printf("SOD:\r\n");
retval=networkSetPassword(console.arguments());
//console.printf("EOD:\r\n");
console.printf("\nDBG: pwd set=%s\r\n",retval?"TRUE":"FALSE");
break;
case _NST_:
retval=networkStatus();
console.printf("NST: %s\r\n",retval?"CONNECTED":"NOT CONNECTED");
break;
case _FMT_: // there really should be some REALLY do you mean this here but.....
root.openRoot(&volume);
if (finger.open(&root, ".", O_WRITE|O_READ)) {
console.printf("\nDBG: Opened / \r\n");
finger.rmRfStar();
} else {
console.printf("\nDBG: FAIL \r\n");
}
break;
case _TPT_:
toPachube(1, console.arguments());
break;
case _TX2_:
radio.printf("%s",console.arguments());
index=0;
// delay(1000);
while (radio.available()) {
inBuffer[index++]=ch=radio.read();
if( index>= 99 || ((ch== '\n') || ch !='\r')) {
inBuffer[index]='\0';
console.puts(inBuffer);
index=0;
delay(100);
}
}
inBuffer[index]='\0';
console.puts(inBuffer);
console.puts((char *) "\r\n");
break;
// set to one to test output for.
#if 1
case _TS1_: toPachube(0, console.arguments()); break;
case _TS2_: toPachube(1, console.arguments()); break;
case _TS3_: toPachube(2, console.arguments()); break;
case _TS4_: toPachube(3, console.arguments()); break;
case _TS5_: toPachube(4, console.arguments()); break;
case _FAN_: toPachube(5, console.arguments()); break;
case _CHL_: toPachube(6, console.arguments()); break;
case _STC_: toPachube(7, console.arguments()); break;
#endif
case _PKY_:
strncpy(pachubeKey, console.arguments(), MAX_PATCHUBE_KEY_LENGHT-1);
stripcrlf(pachubeKey);
break;
case _PFD_:
strncpy(pachubeFeed, console.arguments(), MAX_PATCHUBE_FEED_LENGHT-1);
stripcrlf(pachubeFeed);
break;
case _SGT_ :
readSettings();
break;
case _SSV_ :
writeSettings();
break;
default:
console.printf("DBG: forwarding (%s) to device\r\n",console.key());
device.puts(console.line());
//.........这里部分代码省略.........
示例5: ECHO_LMV
void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/, bool lcd_status/*=true*/) {
if (!cardOK) return;
if (file.isOpen()) { //replacing current file by new file, or subfile call
if (!replace_current) {
if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
ECHO_LMV(ER, MSG_SD_MAX_DEPTH, SD_PROCEDURE_DEPTH);
kill(PSTR(MSG_KILLED));
return;
}
ECHO_SMV(DB, "SUBROUTINE CALL target:\"", name);
ECHO_M("\" parent:\"");
//store current filename and position
getAbsFilename(filenames[file_subcall_ctr]);
ECHO_V(filenames[file_subcall_ctr]);
ECHO_EMV("\" pos", sdpos);
filespos[file_subcall_ctr] = sdpos;
file_subcall_ctr++;
}
else {
ECHO_LMV(DB, "Now doing file: ", name);
}
file.close();
}
else { // opening fresh file
file_subcall_ctr = 0; // resetting procedure depth in case user cancels print while in procedure
ECHO_LMV(DB, "Now fresh file: ", name);
}
sdprinting = false;
SdFile myDir;
curDir = &root;
char *fname = name;
char *dirname_start, *dirname_end;
if (name[0] == '/') {
dirname_start = &name[1];
while (dirname_start > 0) {
dirname_end = strchr(dirname_start, '/');
if (dirname_end > 0 && dirname_end > dirname_start) {
char subdirname[FILENAME_LENGTH];
strncpy(subdirname, dirname_start, dirname_end - dirname_start);
subdirname[dirname_end - dirname_start] = 0;
ECHO_EV(subdirname);
if (!myDir.open(curDir, subdirname, O_READ)) {
ECHO_MV(MSG_SD_OPEN_FILE_FAIL, subdirname);
ECHO_C('.');
return;
}
else {
//ECHO_EM("dive ok");
}
curDir = &myDir;
dirname_start = dirname_end + 1;
}
else { // the remainder after all /fsa/fdsa/ is the filename
fname = dirname_start;
//ECHO_EM("remainder");
//ECHO_EV(fname);
break;
}
}
}
else { //relative path
curDir = &workDir;
}
if (read) {
if (file.open(curDir, fname, O_READ)) {
filesize = file.fileSize();
ECHO_MV(MSG_SD_FILE_OPENED, fname);
ECHO_EMV(MSG_SD_SIZE, filesize);
sdpos = 0;
ECHO_EM(MSG_SD_FILE_SELECTED);
getfilename(0, fname);
if(lcd_status) lcd_setstatus(longFilename[0] ? longFilename : fname);
}
else {
ECHO_MV(MSG_SD_OPEN_FILE_FAIL, fname);
ECHO_PGM(".\n");
}
}
else { //write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
ECHO_MV(MSG_SD_OPEN_FILE_FAIL, fname);
ECHO_PGM(".\n");
}
else {
saving = true;
ECHO_EMV(MSG_SD_WRITE_TO_FILE, name);
if(lcd_status) lcd_setstatus(fname);
}
}
}
示例6:
void MP3Player::Halt()
{
Timer1.detachInterrupt();
PLAY=false;
while(!myFile.close())
{
#if DEBUG
Serial.println("close audio file");
#endif
}
//AMPSHUTDOWN();
#if DEBUG
Serial.println("finish");
#endif
delayMicroseconds(100000);
name = "";
if(isPlayAll)
PlayTrack("",0,"");
}
示例7: wcs
int wcs(const char *oname)
{
// char name[PATHLEN];
// strcpy(name, oname);
Eofseen = 0;
// vpos = 0;
switch (wctxpn(oname)) {
case ERROR:
return ERROR;
case ZSKIP:
return OK;
}
// ++Filcnt;
if(!Zmodem && wctx(fout.fileSize())==ERROR)
return ERROR;
return 0;
}
示例8: setup
void setup() {
Serial.begin(115200);
while (!Serial.available()) SPARK_WLAN_Loop();
while (Serial.available()) Serial.read();
// initialize the SD card at SPI_FULL_SPEED for best performance.
// try SPI_HALF_SPEED if bus errors occur.
// Initialize HARDWARE SPI with user defined chipSelect
if (!card.init(SPI_FULL_SPEED, chipSelect)) error("card.init failed");
// Initialize SOFTWARE SPI
//if (!card.init(mosiPin, misoPin, clockPin, chipSelect)) error("card.init failed");
// initialize a FAT volume
if (!volume.init(&card)) error("volume.init failed!");
Serial.print("Type is FAT");
Serial.println(volume.fatType(), DEC);
if (!root.openRoot(&volume)) error("openRoot failed");
}
示例9: printCardInfo
void printCardInfo() {
Serial.print(F("Data logging is "));
if (disableLogging) {
Serial.println(F("DISABLED"));
} else {
Serial.println(F("ENABLED"));
}
Serial.println();
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
unsigned long volumesize;
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize /= 2;
volumesize /= 1024;
Serial.print("Volume size: ");
Serial.print(volumesize, DEC);
Serial.println("MB");
Serial.println("name\tdate\tsize");
root.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println();
}
示例10: setup
void setup(void) {
Serial.begin(BPS_115200);
Serial.println();
PgmPrintln("Type any character to start");
while (!Serial.available());
//PgmPrint("FreeRam: ");
//Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
// initialize a FAT volume
if (!volume.init(&card)) error("volume.init failed");
// open the root directory
if (!root.openRoot(&volume)) error("openRoot failed");
strcpy_P(buf, PSTR("APPEND.TXT"));
// open for read
if (!from.open(&root, buf, O_READ)) {
PgmPrint("Can't open ");
Serial.println(buf);
PgmPrintln("Run the append example to create the file.");
error("from.open failed");
}
strcpy_P(buf, PSTR("ACOPY.TXT"));
// create if needed, truncate to zero length, open for write
if (!copy.open(&root, buf, O_CREAT | O_TRUNC | O_WRITE)) {
error("copy.open failed");
}
// count for printing periods
uint16_t p = 0;
int16_t n;
while ((n = from.read(buf, sizeof(buf))) > 0) {
if (copy.write(buf, n) != n) error("write failed");
// print progress periods
if (!(p++ % 25)) Serial.print('.');
if (!(p % 500)) Serial.println();
}
Serial.println();
if (n != 0) error ("read");
// force write of directory entry and last data
if (!copy.close()) error("copy.close failed");
PgmPrintln("Copy done.");
}
示例11: setupLog
void setupLog() {
if (!card.init(SPI_HALF_SPEED, SD_CS)) {
Serial.println(F("No SD card inserted, disabling data logger."));
disableLogging = true;
}
if (!disableLogging && !volume.init(card)) {
Serial.println(F("Unable to initialize SD volume"));
disableLogging = true;
}
if (!disableLogging && !root.openRoot(volume)) {
Serial.println(F("Unable to open volume root"));
disableLogging = true;
}
#ifdef DEBUG
printCardInfo();
#endif
if (!disableLogging) {
openLog();
}
}
示例12: SD_ListFiles
void SD_ListFiles(void)
{
Serial.begin(9600);
while (!Serial) {}
delay(1000);
Serial.println();
while (file.openNext(sdf.vwd(), O_READ)) {
file.printFileSize(&Serial);
Serial.write(' ');
file.printModifyDateTime(&Serial);
Serial.write(' ');
file.printName(&Serial);
if (file.isDir()) {
// Indicate a directory.
Serial.write('/');
}
Serial.println();
file.close();
}
}
示例13: writeSettings
void writeSettings() {
char wrtBuffer[80];
if (!settingsFile.open(&root, settingsFileName, O_WRITE|O_CREAT)) {
// bitch.... ();
return;
}
console.printf("DBG: Opened Settings File: %s\r\n",settingsFileName);
snprintf(wrtBuffer,80,"NPW!%s\n",networkPassword);
settingsFile.write(wrtBuffer,strlen(wrtBuffer));
snprintf(wrtBuffer,80,"NJN!%s\n",networkSSID);
settingsFile.write(wrtBuffer,strlen(wrtBuffer));
snprintf(wrtBuffer,80,"PKY!%s\n",pachubeKey);
settingsFile.write(wrtBuffer,strlen(wrtBuffer));
snprintf(wrtBuffer,80,"PFD!%s\n",pachubeFeed);
settingsFile.write(wrtBuffer,strlen(wrtBuffer));
settingsFile.close();
console.printf("DBG: Closing Settings File: %s\r\n",settingsFileName);
}
示例14: open
bool open(char* fileName) {
// Create LDxxxx.CSV for the lowest value of x.
#ifdef SDCARD
uint16_t i = 0;
do {
fileName[2] = (i/1000) % 10 + '0';
fileName[3] = (i/100) % 10 + '0';
fileName[4] = (i/10) % 10 + '0';
fileName[5] = i % 10 + '0';
i++;
}
while(sd.exists(fileName));
if(!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
// error_P("file open");
return false;
}
file.clearWriteError();
// write data header
file.print("time (s)");
#endif
Serial.print("v");
Serial.println(FIRMWARE_VERSION);
Serial.print("File: ");
Serial.println(fileName);
// write data header
Serial.print("time (s)");
/* We are no longer using the junction temperature in our data output.
// write data header
file.print("time (s), ambient");
Serial.print("time (s), ambient");
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
file.print(" (C)");
Serial.print(" (C)");
break;
case TEMPERATURE_UNITS_F:
file.print(" (F)");
Serial.print(" (F)");
break;
case TEMPERATURE_UNITS_K:
file.print(" (K)");
Serial.print(" (K)");
break;
}*/
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
#ifdef SDCARD
file.print(", temp_");
file.print(i, DEC);
#endif
Serial.print(", temp_");
Serial.print(i, DEC);
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
#ifdef SDCARD
file.print(" (C)");
#endif
Serial.print(" (C)");
break;
case TEMPERATURE_UNITS_F:
#ifdef SDCARD
file.print(" (F)");
#endif
Serial.print(" (F)");
break;
case TEMPERATURE_UNITS_K:
#ifdef SDCARD
file.print(" (K)");
#endif
Serial.print(" (K)");
break;
}
}
#ifdef SDCARD
file.println();
file.flush();
#endif
Serial.println();
#ifdef SDCARD
return (file.getWriteError() == false);
#else
return true;
#endif
}
示例15: close
void close() {
#ifdef SDCARD
file.close();
#endif
}