Skip to content

Commit 9b5d8e5

Browse files
committed
.
1 parent 25daf35 commit 9b5d8e5

File tree

5 files changed

+261
-168
lines changed

5 files changed

+261
-168
lines changed

src/app_cam.cpp

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,60 +81,57 @@ int CLAppCam::stop() {
8181
}
8282

8383
int CLAppCam::loadPrefs() {
84-
jparse_ctx_t jctx;
85-
int ret = parsePrefs(&jctx);
84+
JsonDocument json;
85+
int ret = parsePrefs(json);
8686
if(ret != OS_SUCCESS) {
8787
return ret;
8888
}
8989

9090
// process local settings
9191

92-
json_obj_get_int(&jctx, (char*)"frame_rate", &frameRate);
93-
json_obj_get_int(&jctx, (char*)"xclk", &xclk);
94-
json_obj_get_int(&jctx, (char*)"rotate", &myRotation);
92+
this->frameRate = json["frame_rate"];
93+
this->xclk = json["xclk"];
94+
this->myRotation = json["rotate"];
9595

9696
// get sensor reference
9797
sensor_t * s = esp_camera_sensor_get();
9898
// process camera settings
9999
if(s) {
100-
s->set_framesize(s, (framesize_t)readJsonIntVal(&jctx, "framesize"));
101-
s->set_quality(s, readJsonIntVal(&jctx, "quality"));
100+
s->set_framesize(s, (framesize_t)json["framesize"]);
101+
s->set_quality(s, json["quality"]);
102102
s->set_xclk(s, LEDC_TIMER_0, xclk);
103-
s->set_brightness(s, readJsonIntVal(&jctx, "brightness"));
104-
s->set_contrast(s, readJsonIntVal(&jctx, "contrast"));
105-
s->set_saturation(s, readJsonIntVal(&jctx, "saturation"));
106-
s->set_sharpness(s, readJsonIntVal(&jctx, "sharpness"));
107-
s->set_denoise(s, readJsonIntVal(&jctx, "denoise"));
108-
s->set_special_effect(s, readJsonIntVal(&jctx, "special_effect"));
109-
s->set_wb_mode(s, readJsonIntVal(&jctx, "wb_mode"));
110-
s->set_whitebal(s, readJsonIntVal(&jctx, "awb"));
111-
s->set_awb_gain(s, readJsonIntVal(&jctx, "awb_gain"));
112-
s->set_exposure_ctrl(s, readJsonIntVal(&jctx, "aec"));
113-
s->set_aec2(s, readJsonIntVal(&jctx, "aec2"));
114-
s->set_ae_level(s, readJsonIntVal(&jctx, "ae_level"));
115-
s->set_aec_value(s, readJsonIntVal(&jctx, "aec_value"));
116-
s->set_gain_ctrl(s, readJsonIntVal(&jctx, "agc"));
117-
s->set_agc_gain(s, readJsonIntVal(&jctx, "agc_gain"));
118-
s->set_gainceiling(s, (gainceiling_t)readJsonIntVal(&jctx, "gainceiling"));
119-
s->set_bpc(s, readJsonIntVal(&jctx, "bpc"));
120-
s->set_wpc(s, readJsonIntVal(&jctx, "wpc"));
121-
s->set_raw_gma(s, readJsonIntVal(&jctx, "raw_gma"));
122-
s->set_lenc(s, readJsonIntVal(&jctx, "lenc"));
123-
s->set_vflip(s, readJsonIntVal(&jctx, "vflip"));
124-
s->set_hmirror(s, readJsonIntVal(&jctx, "hmirror"));
125-
s->set_dcw(s, readJsonIntVal(&jctx, "dcw"));
126-
s->set_colorbar(s, readJsonIntVal(&jctx, "colorbar"));
103+
s->set_brightness(s, json["brightness"]);
104+
s->set_contrast(s, json["contrast"]);
105+
s->set_saturation(s, json["saturation"]);
106+
s->set_sharpness(s, json["sharpness"]);
107+
s->set_denoise(s, json["denoise"]);
108+
s->set_special_effect(s, json["special_effect"]);
109+
s->set_wb_mode(s, json["wb_mode"]);
110+
s->set_whitebal(s, json["awb"]);
111+
s->set_awb_gain(s, json["awb_gain"]);
112+
s->set_exposure_ctrl(s, json["aec"]);
113+
s->set_aec2(s, json["aec2"]);
114+
s->set_ae_level(s, json["ae_level"]);
115+
s->set_aec_value(s, json["aec_value"]);
116+
s->set_gain_ctrl(s, json["agc"]);
117+
s->set_agc_gain(s, json["agc_gain"]);
118+
s->set_gainceiling(s, (gainceiling_t)json["gainceiling"]);
119+
s->set_bpc(s, json["bpc"]);
120+
s->set_wpc(s, json["wpc"]);
121+
s->set_raw_gma(s, json["raw_gma"]);
122+
s->set_lenc(s, json["lenc"]);
123+
s->set_vflip(s, json["vflip"]);
124+
s->set_hmirror(s, json["hmirror"]);
125+
s->set_dcw(s, json["dcw"]);
126+
s->set_colorbar(s, json["colorbar"]);
127+
128+
if(json["debug_mode"]) setDebugMode(json["debug_mode"]);
127129

128-
bool dbg;
129-
if(json_obj_get_bool(&jctx, (char*)"debug_mode", &dbg) == OS_SUCCESS)
130-
setDebugMode(dbg);
131130
}
132131
else {
133132
Serial.println("Failed to get camera handle. Camera settings skipped");
134133
}
135-
136-
// close the file
137-
json_parse_end(&jctx);
134+
138135
return ret;
139136
}
140137

src/app_component.cpp

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ int CLAppComponent::removePrefs() {
5454
return OS_SUCCESS;
5555
}
5656

57+
int CLAppComponent::parsePrefs(JsonDocument& json) {
58+
char* conn_file = getPrefsFileName();
59+
60+
if (Storage.exists(conn_file)) {
61+
//file exists, reading and loading
62+
Serial.printf("Open config file %s \r\n", conn_file);
63+
64+
File configFile = Storage.open(conn_file);
65+
if (configFile) {
66+
Serial.printf("Config file %s opened \r\n", conn_file);
67+
68+
DeserializationError error = deserializeJson(json, configFile);
69+
70+
if (!error) {
71+
serializeJsonPretty(json, Serial);
72+
return OS_SUCCESS;
73+
}
74+
} else {
75+
Serial.printf("Failed to open the connection settings from %s \r\n", conn_file);
76+
}
77+
} else {
78+
Serial.printf("Preference file %s not exists.\r\n", conn_file);
79+
}
80+
return OS_FAIL;
81+
}
82+
5783
int CLAppComponent::parsePrefs(jparse_ctx_t *jctx) {
5884
char *conn_file = getPrefsFileName();
5985

@@ -75,7 +101,78 @@ int CLAppComponent::parsePrefs(jparse_ctx_t *jctx) {
75101
return ret;
76102
}
77103

78-
int CLAppComponent::urlDecode(char * decoded, char * source, size_t len) {
104+
unsigned char CLAppComponent::hex2int(char c) {
105+
if (c >= '0' && c <='9'){
106+
return((unsigned char)c - '0');
107+
}
108+
if (c >= 'a' && c <='f'){
109+
return((unsigned char)c - 'a' + 10);
110+
}
111+
if (c >= 'A' && c <='F'){
112+
return((unsigned char)c - 'A' + 10);
113+
}
114+
return(0);
115+
}
116+
117+
int CLAppComponent::urlDecode(String* decoded, String* source) {
118+
char c;
119+
char code0;
120+
char code1;
121+
for (int i =0; i < source->length(); i++){
122+
c=decoded->charAt(i);
123+
if (c == '+'){
124+
decoded += ' ';
125+
}else if (c == '%') {
126+
i++;
127+
code0=source->charAt(i);
128+
i++;
129+
code1=source->charAt(i);
130+
c = (this->hex2int(code0) << 4) | this->hex2int(code1);
131+
decoded += c;
132+
} else{
133+
134+
decoded += c;
135+
}
136+
137+
yield();
138+
}
139+
140+
return OS_SUCCESS;
141+
}
142+
143+
int CLAppComponent::urlEncode(String* encoded, String* source) {
144+
char c;
145+
char code0;
146+
char code1;
147+
char code2;
148+
for (int i =0; i < source->length(); i++){
149+
c=source->charAt(i);
150+
if (c == ' '){
151+
encoded += '+';
152+
} else if (isalnum(c)){
153+
encoded +=c;
154+
} else{
155+
code1=(c & 0xf)+'0';
156+
if ((c & 0xf) >9){
157+
code1=(c & 0xf) - 10 + 'A';
158+
}
159+
c=(c>>4)&0xf;
160+
code0=c+'0';
161+
if (c > 9){
162+
code0=c - 10 + 'A';
163+
}
164+
code2='\0';
165+
encoded +='%';
166+
encoded +=code0;
167+
encoded +=code1;
168+
//encodedString+=code2;
169+
}
170+
yield();
171+
}
172+
return OS_SUCCESS;
173+
}
174+
175+
int CLAppComponent::urlDecode(char* decoded, char * source, size_t len) {
79176
char temp[] = "0x00";
80177
int i=0;
81178
char * ptr = decoded;

src/app_component.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "json_generator.h"
55
#include "json_parser.h"
6+
#include <ArduinoJson.h>
67

78
#if __has_include("../myconfig.h")
89
#include "../myconfig.h"
@@ -51,10 +52,15 @@ class CLAppComponent {
5152
int readJsonIntVal(jparse_ctx_t *jctx, const char* token);
5253

5354
int parsePrefs(jparse_ctx_t *jctx);
55+
int parsePrefs(JsonDocument& json);
5456

5557
int urlDecode(char * decoded, char * source, size_t len);
5658
int urlEncode(char * encoded, char * source, size_t len);
5759

60+
unsigned char hex2int(char c);
61+
int urlDecode(String* decoded, String* source);
62+
int urlEncode(String* encoded, String* source);
63+
5864

5965
private:
6066
// prefix for forming preference file name of this class

0 commit comments

Comments
 (0)