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
9
static
const
char
*
FILE_PATH
=
"/value.bin"
;
10
static
const
uint8_t
VALUE_TO_WRITE
= 31;
// change as needed
11
bool
success
;
12
13
void
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
67
void
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
}
Arduino.h
Serial
static SerialStub Serial
Definition
Arduino.h:128
SPIFFS.h
delay
void delay(int ms)
Definition
actuator_nc.cpp:30
literals::json_literals::n
*brief user defined string literal for JSON values *sa std::size_t n
Definition
json.hpp:24474
setup
void setup()
Definition
spiffs_write_byte.cpp:13
success
bool success
Definition
spiffs_write_byte.cpp:11
VALUE_TO_WRITE
static const uint8_t VALUE_TO_WRITE
Definition
spiffs_write_byte.cpp:10
FILE_PATH
static const char * FILE_PATH
ESP32 S3: write a single 8-bit integer to SPIFFS (root).
Definition
spiffs_write_byte.cpp:9
loop
void loop()
Definition
spiffs_write_byte.cpp:67
firmware
Burn_ID
src
spiffs_write_byte.cpp
Generated by
1.15.0