Skip to content

Commit 21892ea

Browse files
committed
Item Review and Enhancements
1 parent ce996b9 commit 21892ea

File tree

6 files changed

+137
-36
lines changed

6 files changed

+137
-36
lines changed

data/yaml/SC_BasicConfig.yaml

Whitespace-only changes.

data/yaml/SC_ExterndConfig.yaml

Whitespace-only changes.

src/AIStackchan_config.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#include "AIStackchan_config.h"
3+
4+
AIStackchanConfig::AIStackchanConfig() {};
5+
AIStackchanConfig::~AIStackchanConfig() {};
6+
7+
void AIStackchanConfig::loadExtendConfig(fs::FS& fs, const char *yaml_filename, uint32_t yaml_size) {
8+
M5_LOGI("----- StackchanAIConfig::loadConfig:%s\n", yaml_filename);
9+
File file = fs.open(yaml_filename);
10+
if (file) {
11+
DynamicJsonDocument doc(yaml_size);
12+
auto err = deserializeYml( doc, file);
13+
if (err) {
14+
M5_LOGI("yaml file read error: %s\n", yaml_filename);
15+
M5_LOGI("error%s\n", err.c_str());
16+
}
17+
serializeJsonPretty(doc, Serial);
18+
setExtendSettings(doc);
19+
}
20+
}
21+
22+
void AIStackchanConfig::setExtendSettings(DynamicJsonDocument doc) {
23+
JsonObject apikeys = doc["apikeys"];
24+
_ai_parameters.stt = apikeys["stt_apikey"].as<String>();
25+
_ai_parameters.aiservice = apikeys["aiservice_apikey"].as<String>();
26+
_ai_parameters.tts = apikeys["tts_apikey"].as<String>();
27+
}
28+
29+
void AIStackchanConfig::printExtParameters(void) {
30+
M5_LOGI("stt_apikey:%s\n", _ai_parameters.stt.c_str());
31+
M5_LOGI("aiservice_apikey:%s\n", _ai_parameters.aiservice.c_str());
32+
M5_LOGI("tts_apikey:%s\n", _ai_parameters.tts.c_str());
33+
}

src/AIStackchan_config.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef __AISTACKCHAN_CONFIG_H__
2+
#define __AISTACKCHAN_CONFIG_H__
3+
4+
#include <Stackchan_system_config.h>
5+
6+
typedef struct AIConfig {
7+
String stt;
8+
String aiservice;
9+
String tts;
10+
} ai_config_s;
11+
12+
class AIStackchanConfig : public StackchanSystemConfig
13+
{
14+
protected:
15+
ai_config_s _ai_parameters;
16+
17+
public:
18+
AIStackchanConfig();
19+
~AIStackchanConfig();
20+
void loadExtendConfig(fs::FS& fs, const char *yaml_filename, uint32_t yaml_size) override;
21+
void setExtendSettings(DynamicJsonDocument doc) override;
22+
void printExtParameters(void) override;
23+
ai_config_s getAIConfig() { return _ai_parameters; }
24+
void setSTTApikey(String apikey) { _ai_parameters.stt = apikey; }
25+
void setAIServiceApikey(String apikey) { _ai_parameters.aiservice = apikey; }
26+
void setTTSApikey(String apikey) { _ai_parameters.tts = apikey; }
27+
};
28+
29+
#endif // __AISTACKCHAN_SYSTEM_CONFIG_H__

src/Stackchan_system_config.cpp

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#ifndef STACKCHAN_SYSTEM_CONFIG_CPP
2+
#define STACKCHAN_SYSTEM_CONFIG_CPP
13
#include "Stackchan_system_config.h"
24

3-
45
StackchanSystemConfig::StackchanSystemConfig() {
56

67
};
@@ -19,8 +20,11 @@ void StackchanSystemConfig::setDefaultParameters() {
1920
_servo[AXIS_X].pin = 22;
2021
_servo[AXIS_Y].pin = 21;
2122
break;
23+
case m5::board_t::board_M5StackCoreS3:
24+
_servo[AXIS_X].pin = 1;
25+
_servo[AXIS_Y].pin = 2;
2226
default:
23-
Serial.printf("UnknownBoard:%d\n", M5.getBoard());
27+
M5_LOGI("UnknownBoard:%d\n", M5.getBoard());
2428
_servo[AXIS_X].pin = 22;
2529
_servo[AXIS_Y].pin = 21;
2630
break;
@@ -60,17 +64,19 @@ void StackchanSystemConfig::setDefaultParameters() {
6064
_servo_type = 0;
6165
_servo[AXIS_X].start_degree = 90;
6266
_servo[AXIS_Y].start_degree = 90;
67+
_extend_config_filename = "";
68+
_extend_config_size = 0;
6369
}
6470

6571
void StackchanSystemConfig::loadConfig(fs::FS& fs, const char *yaml_filename) {
66-
Serial.printf("----- StackchanSystemConfig::loadConfig:%s\n", yaml_filename);
67-
File file = fs.open(yaml_filename);
72+
M5_LOGI("----- StackchanSystemConfig::loadConfig:%s\n", yaml_filename);
73+
fs::File file = fs.open(yaml_filename);
74+
DynamicJsonDocument doc(2048);
6875
if (file) {
69-
DynamicJsonDocument doc(2048);
70-
auto err = deserializeYml( doc, file);
76+
DeserializationError err = deserializeYml(doc, file);
7177
if (err) {
72-
Serial.printf("yaml file read error: %s\n", yaml_filename);
73-
Serial.printf("error%s\n", err.c_str());
78+
M5_LOGI("yaml file read error: %s\n", yaml_filename);
79+
M5_LOGI("error%s\n", err.c_str());
7480
}
7581
serializeJsonPretty(doc, Serial);
7682
setSystemConfig(doc);
@@ -79,6 +85,9 @@ void StackchanSystemConfig::loadConfig(fs::FS& fs, const char *yaml_filename) {
7985
// JSONファイルが見つからない場合はデフォルト値を利用します。
8086
setDefaultParameters();
8187
}
88+
if (_extend_config_size >= 0) {
89+
loadExtendConfig(fs, _extend_config_filename.c_str(), _extend_config_size);
90+
}
8291
printAllParameters();
8392
}
8493

@@ -109,13 +118,16 @@ void StackchanSystemConfig::setSystemConfig(DynamicJsonDocument doc) {
109118
_bluetooth.starting_state = doc["bluetooth"]["starting_state"];//.as<bool>();
110119
_bluetooth.start_volume = doc["bluetooth"]["start_volume"];
111120

121+
_wifi.ssid = doc["wifi"]["ssid"].as<String>();
122+
_wifi.pass = doc["wifi"]["pass"].as<String>();
123+
112124
_auto_power_off_time = doc["auto_power_off_time"];
113125
_font_language_code = doc["balloon"]["font_language"].as<String>();
114126

115127
JsonArray balloon_lyrics = doc["balloon"]["lyrics"];
116128

117129
_lyrics_num = balloon_lyrics.size();
118-
Serial.printf("lyrics_num:%d\n", _lyrics_num);
130+
M5_LOGI("lyrics_num:%d\n", _lyrics_num);
119131
for (int j=0;j<_lyrics_num;j++) {
120132
_lyrics[j] = balloon_lyrics[j].as<String>();
121133
}
@@ -133,6 +145,10 @@ void StackchanSystemConfig::setSystemConfig(DynamicJsonDocument doc) {
133145
_servo[AXIS_X].start_degree = 90;
134146
_servo[AXIS_Y].start_degree = 90;
135147
}
148+
149+
_extend_config_filename = doc["extend_config_filename"].as<String>();
150+
_extend_config_filesize = doc["extend_config_filesize"];
151+
136152

137153
}
138154

@@ -142,35 +158,46 @@ const lgfx::IFont* StackchanSystemConfig::getFont() {
142158
} else if (_font_language_code.compareTo("CN")) {
143159
return &fonts::efontCN_16;
144160
} else {
145-
Serial.printf("FontCodeError:%s\n", _font_language_code);
161+
M5_LOGI("FontCodeError:%s\n", _font_language_code);
146162
return &fonts::Font0;
147163
}
148164
}
149165

150166
void StackchanSystemConfig::printAllParameters() {
151-
Serial.printf("servo:pin_x:%d\n", _servo[AXIS_X].pin);
152-
Serial.printf("servo:pin_y:%d\n", _servo[AXIS_Y].pin);
153-
Serial.printf("servo:offset_x:%d\n", _servo[AXIS_X].offset);
154-
Serial.printf("servo:offset_y:%d\n", _servo[AXIS_Y].offset);
167+
M5_LOGI("servo:pin_x:%d\n", _servo[AXIS_X].pin);
168+
M5_LOGI("servo:pin_y:%d\n", _servo[AXIS_Y].pin);
169+
M5_LOGI("servo:offset_x:%d\n", _servo[AXIS_X].offset);
170+
M5_LOGI("servo:offset_y:%d\n", _servo[AXIS_Y].offset);
155171
for (int i=0;i<_mode_num;i++) {
156-
Serial.printf("mode:%s\n", _servo_interval[i].mode_name);
157-
Serial.printf("interval_min:%d\n", _servo_interval[i].interval_min);
158-
Serial.printf("interval_max:%d\n", _servo_interval[i].interval_max);
159-
Serial.printf("move_min:%d\n", _servo_interval[i].move_min);
160-
Serial.printf("move_max:%d\n", _servo_interval[i].move_max);
172+
M5_LOGI("mode:%s\n", _servo_interval[i].mode_name);
173+
M5_LOGI("interval_min:%d\n", _servo_interval[i].interval_min);
174+
M5_LOGI("interval_max:%d\n", _servo_interval[i].interval_max);
175+
M5_LOGI("move_min:%d\n", _servo_interval[i].move_min);
176+
M5_LOGI("move_max:%d\n", _servo_interval[i].move_max);
161177
}
162-
Serial.printf("mode_num:%d\n", _mode_num);
163-
Serial.printf("Bluetooth_device_name:%s\n", _bluetooth.device_name.c_str());
164-
Serial.printf("Bluetooth_starting_state:%s\n", _bluetooth.starting_state ? "true":"false");
165-
Serial.printf("Bluetooth_start_volume:%d\n", _bluetooth.start_volume);
166-
Serial.printf("auto_power_off_time:%d\n", _auto_power_off_time);
167-
Serial.printf("font_language:%s\n", _font_language_code);
178+
M5_LOGI("mode_num:%d\n", _mode_num);
179+
M5_LOGI("WiFi SSID: %s\n", _wifi.ssid.c_str());
180+
M5_LOGI("WiFi PASS: %s\n", _wifi.pass.c_str());
181+
M5_LOGI("Bluetooth_device_name:%s\n", _bluetooth.device_name.c_str());
182+
M5_LOGI("Bluetooth_starting_state:%s\n", _bluetooth.starting_state ? "true":"false");
183+
M5_LOGI("Bluetooth_start_volume:%d\n", _bluetooth.start_volume);
184+
M5_LOGI("auto_power_off_time:%d\n", _auto_power_off_time);
185+
M5_LOGI("font_language:%s\n", _font_language_code);
168186
for (int i=0;i<_lyrics_num;i++) {
169-
Serial.printf("lyrics:%d:%s\n", i, _lyrics[i].c_str());
187+
M5_LOGI("lyrics:%d:%s\n", i, _lyrics[i].c_str());
170188
}
171-
Serial.printf("led_lr:%d\n", _led_lr);
172-
Serial.printf("led_pin:%d\n", _led_pin);
173-
Serial.printf("use takao_base:%s\n", _takao_base ? "true":"false");
174-
Serial.printf("ServoTypeStr:%s\n", _servo_type_str.c_str());
175-
Serial.printf("ServoType: %d\n", _servo_type);
176-
}
189+
M5_LOGI("led_lr:%d\n", _led_lr);
190+
M5_LOGI("led_pin:%d\n", _led_pin);
191+
M5_LOGI("use takao_base:%s\n", _takao_base ? "true":"false");
192+
M5_LOGI("ServoTypeStr:%s\n", _servo_type_str.c_str());
193+
M5_LOGI("ServoType: %d\n", _servo_type);
194+
M5_LOGI("ExtendConfigFileName: %s\n", _extend_config_filename);
195+
M5_LOGI("ExtendConfigFileSize: %d\n", _extend_config_filesize);
196+
197+
printExtParameters();
198+
}
199+
200+
void StackchanSystemConfig::loadExtendConfig(fs::FS& fs, const char* filename, uint32_t yaml_size) { };
201+
void StackchanSystemConfig::setExtendSettings(DynamicJsonDocument doc) { if ( _extend_config_filename == "" ) return; };
202+
void StackchanSystemConfig::printExtParameters(void) {};
203+
#endif

src/Stackchan_system_config.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#ifndef __STACKCHAN_SYSTEM_CONFIG_H__
22
#define __STACKCHAN_SYSTEM_CONFIG_H__
33

4-
//#include <ArduinoJson.h>
4+
#include <ArduinoJson.h>
55
#include <M5Unified.h>
6-
#include <ArduinoYaml.h>
7-
6+
#include <YAMLDuino.h>
7+
#include <FS.h>
88
#include "Stackchan_servo.h"
99

1010
typedef struct ServoInterval {
@@ -22,6 +22,11 @@ typedef struct Bluetooth {
2222
uint8_t start_volume;
2323
} bluetooth_s;
2424

25+
typedef struct WiFi {
26+
String ssid;
27+
String password;
28+
} wifi_s;
29+
2530
typedef struct ServoInitialParam {
2631
uint8_t pin;
2732
uint16_t offset;
@@ -50,7 +55,9 @@ class StackchanSystemConfig {
5055
bool _takao_base; // Takao_Baseを使い後ろから給電する場合にtrue
5156
String _servo_type_str;
5257
uint8_t _servo_type; // サーボの種類 (0: PWMサーボ, 1: Feetech SCS0009)
53-
58+
wifi_s _wifi; // wifi ssid and pass
59+
String _extend_config_filename; // 使用するアプリ側で拡張した設定が必要な場合に使用
60+
uint32_t _extend_config_filesize; // 拡張設定ファイルのサイズ
5461
void setDefaultParameters();
5562
void setSystemConfig(DynamicJsonDocument doc);
5663

@@ -64,6 +71,7 @@ class StackchanSystemConfig {
6471
servo_initial_param_s* getServoInfo(uint8_t servo_axis_no) { return &_servo[servo_axis_no]; }
6572
servo_interval_s* getServoInterval(AvatarMode avatar_mode) { return &_servo_interval[avatar_mode]; }
6673
bluetooth_s* getBluetoothSetting() { return &_bluetooth; }
74+
wifi_s* getWiFiSetting() { return &_wifi; }
6775
String* getLyric(uint8_t no) { return &_lyrics[no]; }
6876
uint8_t getLyrics_num() { return _lyrics_num; }
6977
uint32_t getAutoPowerOffTime() { return _auto_power_off_time; }
@@ -72,6 +80,10 @@ class StackchanSystemConfig {
7280
int getLedPin() { return _led_pin; }
7381
bool getUseTakaoBase() { return _takao_base; }
7482
uint8_t getServoType() { return _servo_type; }
83+
virtual void loadExtendConfig(fs::FS& fs, const char* yaml_filename, uint32_t yaml_size);
84+
virtual void setExtendSettings(DynamicJsonDocument doc);
85+
virtual void printExtParameters(void);
86+
7587
};
7688

7789
#endif // __STACKCHAN_SYSTEM_CONFIG_H__

0 commit comments

Comments
 (0)