STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
spiffs_write_byte.cpp
Go to the documentation of this file.
1/**
2 * ESP32 S3: write a single 8-bit integer to SPIFFS (root).
3 * Standalone .cpp — no PlatformIO project; add to your build as needed.
4 */
5
6#include <Arduino.h>
7#include <SPIFFS.h>
8
9static const char* FILE_PATH = "/value.bin";
10static const uint8_t VALUE_TO_WRITE = 31; // change as needed
12
13void setup() {
14 Serial.begin(115200);
15 delay(500);
16 // Wait for serial (USB CDC) so output and user are ready before burning
17 while (!Serial) {
18 delay(10);
19 }
20 delay(500);
21
22 if (!SPIFFS.begin(true)) {
23 Serial.println("SPIFFS mount failed");
24 return;
25 }
26
27 File f = SPIFFS.open(FILE_PATH, "w");
28 if (!f) {
29 Serial.println("Failed to open file for writing");
30 SPIFFS.end();
31 return;
32 }
33
34 size_t written = f.write(&VALUE_TO_WRITE, 1);
35 f.close();
36
37 if (written != 1) {
38 Serial.println("Write failed");
39 SPIFFS.end();
40 return;
41 }
42 Serial.printf("Wrote byte %u to %s\n", (unsigned)VALUE_TO_WRITE, FILE_PATH);
43
44 // Read back to verify
45 File fr = SPIFFS.open(FILE_PATH, "r");
46 if (!fr || fr.size() < 1) {
47 Serial.println("Read-back failed: could not open or empty file");
48 SPIFFS.end();
49 return;
50 }
51 uint8_t readBack = 0;
52 size_t n = fr.read(&readBack, 1);
53 fr.close();
54 SPIFFS.end();
55
56 if (n == 1 && readBack == VALUE_TO_WRITE) {
57 Serial.printf("Read-back OK: %s contains %u\n", FILE_PATH,
58 (unsigned)readBack);
59 success = true;
60 } else {
61 Serial.printf("Read-back FAIL: got %u (expected %u)\n",
62 (unsigned)readBack, (unsigned)VALUE_TO_WRITE);
63 success = false;
64 }
65}
66
67void loop() {
68 if (success) {
69 Serial.printf("Burn in succeeded: ID %u\n", (unsigned)VALUE_TO_WRITE);
70 } else {
71 Serial.println("Burn in failed");
72 }
73 delay(500);
74}
static SerialStub Serial
Definition Arduino.h:128
void delay(int ms)
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
void setup()
bool success
static const uint8_t VALUE_TO_WRITE
static const char * FILE_PATH
ESP32 S3: write a single 8-bit integer to SPIFFS (root).
void loop()