AwesomeStudioPedal
A programmable, multi-profile foot controller for DAWs, score readers, and studio automation
Loading...
Searching...
No Matches
config_loader_io.cpp
Go to the documentation of this file.
1#include "button_constants.h"
2#include "config.h"
3#include "config_loader.h"
4#include "profile_manager.h"
5#include <ArduinoJson.h>
6
7using namespace ArduinoJson;
8
19bool ConfigLoader::saveToFile(const ProfileManager& profileManager, const std::string& configPath)
20{
21 DynamicJsonDocument doc(8192);
22 JsonArray profiles = doc.createNestedArray("profiles");
23
24 for (uint8_t profileIndex = 0; profileIndex < hardwareConfig.numProfiles; profileIndex++)
25 {
26 const Profile* profile = profileManager.getProfile(profileIndex);
27 if (! profile)
28 {
29 continue;
30 }
31
32 JsonObject profileObj = profiles.createNestedObject();
33 profileObj["name"] = profile->getName().c_str();
34 profileObj["description"] = profile->getDescription().c_str();
35
36 JsonObject buttons = profileObj.createNestedObject("buttons");
37
38 char btnName[2];
39 for (uint8_t b = 0; b < hardwareConfig.numButtons; b++)
40 {
41 Btn::name(b, btnName);
42 Action* action = profile->getAction(b);
43 if (action)
44 {
45 JsonObject actionObj = buttons.createNestedObject(btnName);
46 actionToJson(action, actionObj);
47 }
48 }
49 }
50
51 std::string content;
52 serializeJson(doc, content);
53
54 if (! fileSystem_->writeFile(configPath.c_str(), content))
55 {
56 logger_->log("Failed to write config file");
57 return false;
58 }
59
60 return true;
61}
62
71void ConfigLoader::logLoadedConfig(const ProfileManager& profileManager) const
72{
73 logger_->log("--- Config loaded ---");
74 for (uint8_t i = 0; i < hardwareConfig.numProfiles; i++)
75 {
76 const Profile* profile = profileManager.getProfile(i);
77 if (! profile)
78 {
79 continue;
80 }
81
82 logger_->log("Profile: ", profile->getName().c_str());
83
84 char btnLabel[2];
85 for (uint8_t b = 0; b < hardwareConfig.numButtons; b++)
86 {
87 const Action* action = profile->getAction(b);
88 if (! action)
89 {
90 continue;
91 }
92
93 Btn::name(b, btnLabel);
94 const char* typeStr = ProfileManager::getActionTypeString(action->getType());
95
96 std::string line = " ";
97 line += btnLabel;
98 line += ": ";
99 line += typeStr;
100 if (action->hasName())
101 {
102 line += " [";
103 line += action->getName();
104 line += "]";
105 }
106 logger_->log(line.c_str());
107 }
108 }
109}
110
111void ConfigLoader::actionToJson(const Action* action, JsonObject& out)
112{
113 if (action->hasName())
114 {
115 out["name"] = action->getName().c_str();
116 }
117
118 switch (action->getType())
119 {
121 out["type"] = "SendStringAction";
122 action->getJsonProperties(out);
123 break;
125 out["type"] = "SendCharAction";
126 action->getJsonProperties(out);
127 break;
129 out["type"] = "SendKeyAction";
130 action->getJsonProperties(out);
131 break;
133 out["type"] = "SendMediaKeyAction";
134 action->getJsonProperties(out);
135 break;
137 out["type"] = "SerialOutputAction";
138 action->getJsonProperties(out);
139 break;
141 out["type"] = "DelayedAction";
142 action->getJsonProperties(out);
143 break;
144 default:
145 out["type"] = "UnknownAction";
146 break;
147 }
148}
Base class for all pedal actions.
Definition action.h:12
virtual Type getType() const
Definition action.h:29
virtual void getJsonProperties(JsonObject &json) const
Definition action.h:39
bool hasName() const
Definition action.h:27
const std::string & getName() const
Definition action.h:26
bool saveToFile(const ProfileManager &profileManager, const std::string &configPath)
Saves configuration to a file.
virtual bool writeFile(const char *path, const std::string &content)=0
Write content to a file.
virtual void log(const char *message)=0
Logs a single message.
Manages up to MAX_PROFILES profiles with LED feedback.
const Profile * getProfile(uint8_t profileIndex) const
static const char * getActionTypeString(Action::Type actionType)
Represents a single button configuration profile.
Definition profile.h:15
const std::string & getDescription() const
Gets the description of this profile.
Definition profile.cpp:49
Action * getAction(uint8_t button) const
Gets the action associated with a button in this profile.
Definition profile.cpp:28
const std::string & getName() const
Gets the name of this profile.
Definition profile.cpp:42
const HardwareConfig hardwareConfig
Global hardware configuration instance.
ProfileManager * profileManager
Definition main.cpp:72
void name(uint8_t index, char *buf)
Write the letter name for a button index into buf.
uint8_t numButtons
Action buttons wired (1..26, A–Z)
Definition config.h:21
uint8_t numProfiles
Number of active profiles (1..MAX_PROFILES)
Definition config.h:19