Skip to content

Commit b47c68a

Browse files
authored
Merge pull request #4 from ronron-gh/support_sdupdater
Modifications for SDUpdater compatibility
2 parents 8456089 + db41227 commit b47c68a

File tree

4 files changed

+173
-23
lines changed

4 files changed

+173
-23
lines changed

src/SDUpdater.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// --- for SD-Updater -------
2+
// SDU.cpp
3+
// NoRi 2024-06-04
4+
// -------------------------
5+
#if defined( ENABLE_SD_UPDATER )
6+
7+
#include <Arduino.h>
8+
#include <M5Unified.h>
9+
#include <ESP32-targz.h>
10+
#include <M5StackUpdater.h>
11+
#include "SDUpdater.h"
12+
13+
14+
//#define TFCARD_CS_PIN 4
15+
#define SDU_SKIP_TMR 5000 // skip timer : ms
16+
//#define SERVO_TXT "/servo.txt"
17+
18+
//bool USE_SERVO_ST;
19+
//int SERVO_PIN_X;
20+
//int SERVO_PIN_Y;
21+
22+
//bool SdBegin();
23+
24+
25+
void SDU_lobby(String PROG_NAME)
26+
{
27+
SDUCfg.setAppName(PROG_NAME.c_str()); // lobby screen label: application name
28+
SDUCfg.setLabelMenu("< Menu"); // BtnA label: load menu.bin
29+
30+
checkSDUpdater(
31+
SD, // filesystem (default=SD)
32+
MENU_BIN, // path to binary (default=/menu.bin, empty string=rollback only)
33+
SDU_SKIP_TMR, // wait delay, (default=0, will be forced to 2000 upon ESP.restart() )
34+
TFCARD_CS_PIN // usually default=4 but your mileage may vary
35+
);
36+
37+
Serial.println("SDU_lobby done");
38+
}
39+
40+
41+
#if 0
42+
bool SdBegin()
43+
{
44+
// --- SD begin -------
45+
int i = 0;
46+
bool success = false;
47+
Serial.println("SD.begin Start ...");
48+
49+
while (i < 3)
50+
{ // SDカードマウント待ち
51+
success = SD.begin(GPIO_NUM_4, SPI, 15000000);
52+
if (success)
53+
return true;
54+
55+
Serial.println("SD Wait...");
56+
delay(500);
57+
i++;
58+
}
59+
60+
if (i >= 3)
61+
{
62+
Serial.println("SD.begin faile ...");
63+
return false;
64+
}
65+
else
66+
return true;
67+
}
68+
69+
70+
bool servoTxtSDRead()
71+
{
72+
if (!SdBegin())
73+
{
74+
SD.end();
75+
return false;
76+
}
77+
78+
File fs = SD.open(SERVO_TXT, FILE_READ);
79+
if (!fs)
80+
{
81+
SD.end();
82+
return false;
83+
}
84+
85+
size_t sz = fs.size();
86+
char buf[sz + 1];
87+
fs.read((uint8_t *)buf, sz);
88+
buf[sz] = 0;
89+
fs.close();
90+
91+
int y = 0;
92+
int z = 0;
93+
for (int x = 0; x < sz; x++)
94+
{
95+
if (buf[x] == 0x0a || buf[x] == 0x0d)
96+
buf[x] = 0;
97+
else if (!y && x > 0 && !buf[x - 1] && buf[x])
98+
y = x;
99+
else if (!z && x > 0 && !buf[x - 1] && buf[x])
100+
z = x;
101+
}
102+
103+
String SV_ON_OFF = "";
104+
String SV_PORT_X = "";
105+
String SV_PORT_Y = "";
106+
107+
SV_ON_OFF = String(buf);
108+
SV_PORT_X = String(&buf[y]);
109+
SV_PORT_Y = String(&buf[z]);
110+
111+
if (SV_ON_OFF == "on" || SV_ON_OFF == "ON")
112+
USE_SERVO_ST = true;
113+
else
114+
USE_SERVO_ST = false;
115+
116+
int sx = SV_PORT_X.toInt();
117+
int sy = SV_PORT_Y.toInt();
118+
if (sx != 0 && sy != 0)
119+
{
120+
SERVO_PIN_X = sx;
121+
SERVO_PIN_Y = sy;
122+
}
123+
124+
Serial.println("USE_SERVO = " + SV_ON_OFF);
125+
Serial.println("SERVO PIN_X = " + String(SERVO_PIN_X, 10));
126+
Serial.println("SERVO PIN_Y = " + String(SERVO_PIN_Y, 10));
127+
128+
return true;
129+
}
130+
#endif
131+
132+
#endif

src/SDUpdater.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef _SD_UPDATER_H
2+
#define _SD_UPDATER_H
3+
4+
#if defined( ENABLE_SD_UPDATER )
5+
6+
extern bool USE_SERVO_ST;
7+
extern int SERVO_PIN_X;
8+
extern int SERVO_PIN_Y;
9+
10+
extern void SDU_lobby(String PROG_NAME);
11+
extern bool servoTxtSDRead();
12+
13+
#endif // ENABLE_SD_UPDATER
14+
15+
#endif // _SD_UPDATER_H

src/Stackchan_system_config.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ void StackchanSystemConfig::setDefaultParameters() {
6565
_servo[AXIS_X].start_degree = 90;
6666
_servo[AXIS_Y].start_degree = 90;
6767
_secret_config_show = false;
68-
_extend_config_filename = "";
69-
_extend_config_filesize = 0;
70-
_secret_config_filename = "";
71-
_secret_config_filesize = 0;
7268

7369
}
7470

75-
void StackchanSystemConfig::loadConfig(fs::FS& fs, const char *app_yaml_filename, uint32_t app_yaml_filesize, const char* basic_yaml_filename, uint32_t basic_yaml_filesize) {
71+
void StackchanSystemConfig::loadConfig(fs::FS& fs, const char *app_yaml_filename, uint32_t app_yaml_filesize,
72+
const char* secret_yaml_filename, uint32_t secret_yaml_filesize,
73+
const char* basic_yaml_filename, uint32_t basic_yaml_filesize) {
7674
M5_LOGI("----- StackchanSystemConfig::loadConfig:%s\n", basic_yaml_filename);
7775
M5_LOGI("----- app_yaml_filename:%s\n", app_yaml_filename);
7876
fs::File file = fs.open(basic_yaml_filename);
@@ -89,11 +87,12 @@ void StackchanSystemConfig::loadConfig(fs::FS& fs, const char *app_yaml_filename
8987
Serial.println("ConfigFile Not Found. Default Parameters used.");
9088
// YAMLファイルが見つからない場合はデフォルト値を利用します。
9189
setDefaultParameters();
90+
basicConfigNotFoundCallback();
9291
}
93-
if (_secret_config_filesize > 0) {
94-
loadSecretConfig(fs, _secret_config_filename.c_str(), _secret_config_filesize);
92+
if (secret_yaml_filesize > 0) {
93+
loadSecretConfig(fs, secret_yaml_filename, secret_yaml_filesize);
9594
}
96-
if (_extend_config_filesize > 0) {
95+
if (app_yaml_filename > 0) {
9796
loadExtendConfig(fs, app_yaml_filename, app_yaml_filesize);
9897
}
9998
printAllParameters();
@@ -109,19 +108,26 @@ void StackchanSystemConfig::loadSecretConfig(fs::FS& fs, const char* yaml_filena
109108
M5_LOGE("yaml file read error: %s\n", yaml_filename);
110109
M5_LOGE("error%s\n", err.c_str());
111110
}
111+
else{
112+
setSecretConfig(doc);
113+
}
114+
112115
if (_secret_config_show) {
113116
// 個人的な情報をログに表示する。
114117
M5_LOGI("=======================================================================================");
115118
M5_LOGI("下記の情報は公開してはいけません。(The following information must not be disclosed.)");
116119
M5_LOGI("");
117120
serializeJsonPretty(doc, Serial);
118-
setSecretConfig(doc);
119121
M5_LOGI("");
120122
printSecretParameters();
123+
M5_LOGI("");
121124
M5_LOGI("ここまでの情報は公開してはいけません。(No information should be disclosed so far.)");
122125
M5_LOGI("=======================================================================================");
123126
}
124127
}
128+
else{
129+
secretConfigNotFoundCallback();
130+
}
125131
}
126132

127133
void StackchanSystemConfig::setSystemConfig(DynamicJsonDocument doc) {
@@ -176,10 +182,6 @@ void StackchanSystemConfig::setSystemConfig(DynamicJsonDocument doc) {
176182
_servo[AXIS_Y].start_degree = 90;
177183
}
178184
_secret_config_show = doc["secret_config_show"].as<bool>();
179-
_secret_config_filename = doc["secret_config_filename"].as<String>();
180-
_secret_config_filesize = doc["secret_config_filesize"];
181-
_extend_config_filename = doc["extend_config_filename"].as<String>();
182-
_extend_config_filesize = doc["extend_config_filesize"];
183185
}
184186

185187
void StackchanSystemConfig::setSecretConfig(DynamicJsonDocument doc) {
@@ -230,10 +232,6 @@ void StackchanSystemConfig::printAllParameters() {
230232
M5_LOGI("use takao_base:%s", _takao_base ? "true":"false");
231233
M5_LOGI("ServoTypeStr:%s", _servo_type_str.c_str());
232234
M5_LOGI("ServoType: %d", _servo_type);
233-
M5_LOGI("SecretConfigFileName: %s", _secret_config_filename.c_str());
234-
M5_LOGI("SecretConfigFileSize: %d", _secret_config_filesize);
235-
M5_LOGI("ExtendConfigFileName: %s", _extend_config_filename.c_str());
236-
M5_LOGI("ExtendConfigFileSize: %d", _extend_config_filesize);
237235
M5_LOGI("secret_config_show:%s", _secret_config_show ? "true":"false");
238236

239237
printExtParameters();
@@ -247,6 +245,10 @@ void StackchanSystemConfig::printSecretParameters() {
247245
M5_LOGI("apikey_tts: %s", _secret_config.api_key.tts.c_str());
248246
}
249247
void StackchanSystemConfig::loadExtendConfig(fs::FS& fs, const char* filename, uint32_t yaml_size) { };
250-
void StackchanSystemConfig::setExtendSettings(DynamicJsonDocument doc) { if ( _extend_config_filename == "" ) return; };
248+
void StackchanSystemConfig::setExtendSettings(DynamicJsonDocument doc) { };
251249
void StackchanSystemConfig::printExtParameters(void) {};
250+
251+
void StackchanSystemConfig::basicConfigNotFoundCallback(void) {};
252+
void StackchanSystemConfig::secretConfigNotFoundCallback(void) {};
253+
252254
#endif

src/Stackchan_system_config.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ class StackchanSystemConfig {
6767
bool _takao_base; // Takao_Baseを使い後ろから給電する場合にtrue
6868
String _servo_type_str;
6969
uint8_t _servo_type; // サーボの種類 (0: PWMサーボ, 1: Feetech SCS0009)
70-
String _extend_config_filename; // 使用するアプリ側で拡張した設定が必要な場合に使用
71-
uint32_t _extend_config_filesize; // 拡張設定ファイルのサイズ
72-
String _secret_config_filename; // 個人情報の設定値を定義したファイル
73-
uint32_t _secret_config_filesize; // 個人情報設定ファイルのサイズ
7470
secret_config_s _secret_config; // 個人情報の構造体
7571
bool _secret_config_show; // 個人情報をログに出すかどうか
7672
void setDefaultParameters();
@@ -82,7 +78,9 @@ class StackchanSystemConfig {
8278
public:
8379
StackchanSystemConfig();
8480
~StackchanSystemConfig();
85-
void loadConfig(fs::FS& fs, const char *app_yaml_filename, uint32_t app_yaml_filesize=2048, const char* basic_yaml_filename = "/yaml/SC_BasicConfig.yaml", uint32_t basic_yaml_filesize=2048);
81+
void loadConfig(fs::FS& fs, const char *app_yaml_filename, uint32_t app_yaml_filesize=2048,
82+
const char* secret_yaml_filename = "/yaml/SC_SecConfig.yaml", uint32_t secret_yaml_filesize=2048,
83+
const char* basic_yaml_filename = "/yaml/SC_BasicConfig.yaml", uint32_t basic_yaml_filesize=2048);
8684

8785
void printAllParameters();
8886

@@ -104,6 +102,9 @@ class StackchanSystemConfig {
104102
virtual void setExtendSettings(DynamicJsonDocument doc);
105103
virtual void printExtParameters(void);
106104

105+
virtual void basicConfigNotFoundCallback(void);
106+
virtual void secretConfigNotFoundCallback(void);
107+
107108
};
108109

109110
#endif // __STACKCHAN_SYSTEM_CONFIG_H__

0 commit comments

Comments
 (0)