Skip to content

Commit 9445a96

Browse files
committed
updated examples
1 parent 3782684 commit 9445a96

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

assets/ugly_logo.png

2.63 KB
Loading
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <ArduinoJson.h>
2+
#include <YAMLDuino.h>
3+
#include <M5Stack.h>
4+
5+
6+
const char* yaml_example_str = R"_YAML_STRING_(
7+
8+
my_setting: "false"
9+
10+
flag1: "true"
11+
flag2: "true"
12+
13+
settings1:
14+
just_a_string: "I am a string"
15+
integer: 12345
16+
float: 12.3323
17+
18+
settings2:
19+
nope: ["n","o","p","e"]
20+
21+
22+
)_YAML_STRING_";
23+
24+
25+
const char* config_file = "/config.yml";
26+
const char* nodename = "my_setting"; // property name in the config
27+
const bool default_value = false; // default value for property
28+
bool current_value = default_value;
29+
bool config_loaded = false; // prevent updates if config isn't loaded
30+
DynamicJsonDocument json_doc(2048);
31+
JsonObject myConfig; // json accessor
32+
33+
34+
bool writeTestYaml( fs::FS &fs, const char* path )
35+
{
36+
fs::File file = fs.open( path, FILE_WRITE );
37+
if( !file ) {
38+
Serial.println("Can't open file for writing");
39+
return false;
40+
}
41+
size_t written = file.write( (const uint8_t*)yaml_example_str, strlen( yaml_example_str ) );
42+
file.close();
43+
//Serial.printf("Example file created (%d bytes)\n", written);
44+
return written > 0;
45+
}
46+
47+
48+
bool loadYamlConfig()
49+
{
50+
fs::File file = SD.open( config_file );
51+
if( !file ) {
52+
Serial.println("Can't open test file for writing :-(");
53+
return false;
54+
}
55+
auto err = deserializeYml( json_doc, file ); // convert yaml to json
56+
file.close();
57+
if( err ) {
58+
Serial.printf("Unable to deserialize YAML to JsonDocument: %s\n", err.c_str() );
59+
return false;
60+
}
61+
myConfig = json_doc.as<JsonObject>();
62+
current_value = myConfig[nodename].as<String>() == "true";
63+
//serializeJson( myConfig, Serial );
64+
//Serial.println();
65+
return true;
66+
}
67+
68+
69+
bool saveYamlConfig()
70+
{
71+
fs::File file = SD.open( config_file, FILE_WRITE);
72+
if( !file ) {
73+
Serial.println("Can't open file for writing");
74+
return false;
75+
}
76+
const size_t bytes_out = serializeYml( myConfig, file );
77+
file.close();
78+
//Serial.printf("Written %d bytes\n", bytes_out );
79+
return bytes_out > 0;
80+
}
81+
82+
83+
bool toggleYamlProperty()
84+
{
85+
if( !config_loaded ) return false;
86+
//Serial.printf("Initial value: [%s] = %s\n", nodename, current_value ? "true" : "false" );
87+
current_value = !current_value;
88+
Serial.printf("New value: [%s] = %s\n", nodename, current_value ? "true" : "false" );
89+
// ArduinoJson @bool is borked up so we use a string
90+
myConfig[nodename] = current_value ? "true" : "false";
91+
return saveYamlConfig();
92+
}
93+
94+
95+
void setup()
96+
{
97+
M5.begin();
98+
99+
if( M5.BtnA.isPressed() ) {
100+
SD.remove( config_file );
101+
Serial.println("Deleted config file");
102+
while( M5.BtnA.isPressed() ) { M5.update(); } // wait for release
103+
ESP.restart();
104+
}
105+
106+
_load_config:
107+
config_loaded = loadYamlConfig();
108+
109+
if( !config_loaded ) {
110+
Serial.printf("Ceating config file %s\n", config_file );
111+
if( !writeTestYaml( SD, config_file ) ) {
112+
Serial.println("Could not create config file, aborting");
113+
while(1) vTaskDelay(1);
114+
}
115+
// write succeeded, reload config
116+
goto _load_config;
117+
}
118+
119+
Serial.printf("Current config value: [%s] = %s\n", nodename, current_value ? "true" : "false" );
120+
}
121+
122+
123+
124+
void loop()
125+
{
126+
M5.update();
127+
128+
if( M5.BtnB.wasPressed() ) {
129+
if( !toggleYamlProperty() ) {
130+
Serial.println("Failed to save property");
131+
}
132+
}
133+
}
134+

examples/test/test.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ blah:
2525
prop4: wat
2626
integer: 12345
2727
float: 12.3323
28-
inline_json_for_the_haters: { "hello":"json", "nested":[3,2,1,"moon"] }
28+
inline_json_for_the_haters: { "hello":"json", "nested":[3,2,"1","moon"] }
2929
whatever:
3030
nope: ["n","o","p","e"]
3131
last: "true"
@@ -56,7 +56,7 @@ const char* json_example_str = R"_JSON_STRING_(
5656
],
5757
"integer": 12345,
5858
"float": 12.3323,
59-
"inline_json_for_the_haters": { "hello": "json", "nested": [ 3, 2, 1, "moon" ] }
59+
"inline_json_for_the_haters": { "hello": "json", "nested": [ 3, 2, "1", "moon" ] }
6060
},
6161
"whatever": { "nope": [ "n", "o", "p", "e" ] },
6262
"last": "true"

0 commit comments

Comments
 (0)