MCPcopy Create free account
hub / github.com/SmingHub/Sming / writeToFile

Function writeToFile

samples/SDCard/app/application.cpp:31–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29namespace
30{
31void writeToFile(const String& filename, uint32_t totalBytes, uint32_t bytesPerRound)
32{
33 char* buf = new char[totalBytes];
34 if(buf == nullptr) {
35 Serial.println(_F("Failed to allocate heap"));
36 return;
37 }
38
39 Serial << _F("Write ") << totalBytes / 1024 << _F(" kBytes in ") << bytesPerRound << _F(" Bytes increment:")
40 << endl;
41 for(unsigned i = 0; i < totalBytes; i++) {
42 buf[i] = (i % 10) + '0';
43 }
44
45 OneShotFastUs timer;
46
47 FIL file;
48 FRESULT fRes = f_open(&file, filename.c_str(), FA_WRITE | FA_CREATE_ALWAYS);
49
50 if(fRes == FR_OK) {
51 unsigned i = 0;
52 do {
53 uint32_t remainingBytes = totalBytes - i > bytesPerRound ? bytesPerRound : totalBytes - i;
54 uint32_t bytesWritten = 0;
55 f_write(&file, buf + i, remainingBytes, &bytesWritten);
56 if(bytesWritten != remainingBytes) {
57 Serial << _F("Only written ") << i + bytesWritten << " bytes" << endl;
58 break;
59 }
60 i += bytesWritten;
61 } while(i < totalBytes);
62
63 f_close(&file);
64
65 //get the time at test end
66 unsigned elapsed = timer.elapsedTime();
67
68 Serial << (i / 1024.0f) * 1000000.0f / elapsed << _F(" kB/s") << endl;
69 } else {
70 Serial << _F("fopen FAIL: ") << fRes << endl;
71 }
72
73 delete[] buf;
74}
75
76void stat_file(char* fname)
77{

Callers 1

speedTestFunction · 0.85

Calls 6

f_openFunction · 0.85
f_writeFunction · 0.85
f_closeFunction · 0.85
printlnMethod · 0.80
elapsedTimeMethod · 0.80
c_strMethod · 0.45

Tested by

no test coverage detected