-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathFileWrite.ino
executable file
·50 lines (38 loc) · 1.25 KB
/
FileWrite.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// This example depends on Paul Stoffregen's SerialFlash library
// Download at https://github.com/PaulStoffregen/SerialFlash
#include <CurieSerialFlash.h>
#include <SPI.h>
#define FSIZE 256
const char *filename = "myfile.txt";
const char *contents = "0123456789ABCDEF";
void setup() {
Serial.begin(9600);
// wait for Arduino Serial Monitor
while (!Serial) ;
delay(100);
// Init. SPI Flash chip
if (!SerialFlash.begin(ONBOARD_FLASH_SPI_PORT, ONBOARD_FLASH_CS_PIN)) {
Serial.println("Unable to access SPI Flash chip");
}
SerialFlashFile file;
// Create the file if it doesn't exist
if (!create_if_not_exists(filename)) {
Serial.println("Not enough space to create file " + String(filename));
return;
}
// Open the file and write test data
file = SerialFlash.open(filename);
file.write(contents, strlen(contents) + 1);
Serial.println("String \"" + String(contents) + "\" written to file " + String(filename));
file.close();
}
bool create_if_not_exists (const char *filename) {
if (!SerialFlash.exists(filename)) {
Serial.println("Creating file " + String(filename));
return SerialFlash.create(filename, FSIZE);
}
Serial.println("File " + String(filename) + " already exists");
return true;
}
void loop() {
}