AwesomeStudioPedal
A programmable, multi-profile foot controller for DAWs, score readers, and studio automation
Loading...
Searching...
No Matches
config_loader_merge.cpp
Go to the documentation of this file.
1#include "config.h"
2#include "config_loader.h"
3#include "profile.h"
4#include "profile_manager.h"
5#include <ArduinoJson.h>
6#include <memory>
7
8using namespace ArduinoJson;
9
10// ---- mergeConfig helpers ----
11
12bool ConfigLoader::profileExistsByName(const ProfileManager& profileManager, const char* name) const
13{
14 for (uint8_t i = 0; i < hardwareConfig.numProfiles; i++)
15 {
16 const Profile* p = profileManager.getProfile(i);
17 if (p && p->getName() == name)
18 {
19 return true;
20 }
21 }
22 return false;
23}
24
25uint8_t ConfigLoader::findEmptyProfileSlot(const ProfileManager& profileManager)
26{
27 for (uint8_t i = 0; i < hardwareConfig.numProfiles; i++)
28 {
30 {
31 return i;
32 }
33 }
34 return 255;
35}
36
50 IBleKeyboard* keyboard,
51 const std::string& jsonConfig)
52{
53 DynamicJsonDocument doc(8192);
54 DeserializationError error = deserializeJson(doc, jsonConfig);
55
56 if (error)
57 {
58 logger_->log("JSON parsing failed:", error.c_str());
59 return false;
60 }
61
62 JsonArray profiles = doc["profiles"];
63
64 for (uint8_t newIdx = 0; newIdx < profiles.size() && newIdx < hardwareConfig.numProfiles;
65 newIdx++)
66 {
67 JsonObject profileJson = profiles[newIdx];
68 const char* profileName = profileJson["name"] | "";
69 const char* profileDescription = profileJson["description"] | "";
70
71 if (profileExistsByName(profileManager, profileName))
72 {
73 logger_->log("Profile already exists, skipping: ", profileName);
74 continue;
75 }
76
77 uint8_t targetIndex = findEmptyProfileSlot(profileManager);
78 if (targetIndex == 255)
79 {
80 logger_->log("No empty profile slots available for merging");
81 continue;
82 }
83
84 auto newProfile = std::unique_ptr<Profile>(new Profile(profileName));
85 newProfile->setDescription(profileDescription);
86 populateProfileFromJson(*newProfile, profileJson["buttons"], keyboard);
87 profileManager.addProfile(targetIndex, std::move(newProfile));
88 logger_->log("Added profile: ", profileName);
89 }
90
91 logLoadedConfig(profileManager);
92 return true;
93}
94
109 IBleKeyboard* keyboard,
110 uint8_t profileIndex,
111 const std::string& jsonConfig)
112{
113 if (profileIndex >= hardwareConfig.numProfiles)
114 {
115 logger_->log("Invalid profile index");
116 return false;
117 }
118
119 DynamicJsonDocument doc(8192);
120 DeserializationError error = deserializeJson(doc, jsonConfig);
121
122 if (error)
123 {
124 logger_->log("JSON parsing failed:", error.c_str());
125 return false;
126 }
127
128 JsonArray profiles = doc["profiles"];
129 if (profiles.size() == 0)
130 {
131 logger_->log("No profiles found in JSON config");
132 return false;
133 }
134
135 JsonObject profileJson = profiles[0];
136 const char* profileName = profileJson["name"] | "";
137 const char* profileDescription = profileJson["description"] | "";
138
139 auto newProfile = std::unique_ptr<Profile>(new Profile(profileName));
140 newProfile->setDescription(profileDescription);
141 populateProfileFromJson(*newProfile, profileJson["buttons"], keyboard);
142 profileManager.addProfile(profileIndex, std::move(newProfile));
143 logger_->log("Replaced profile with: ", profileName);
144 logLoadedConfig(profileManager);
145
146 return true;
147}
bool replaceProfile(ProfileManager &profileManager, IBleKeyboard *keyboard, uint8_t profileIndex, const std::string &jsonConfig)
Replaces a specific profile with configuration from JSON.
bool mergeConfig(ProfileManager &profileManager, IBleKeyboard *keyboard, const std::string &jsonConfig)
Merges configuration from JSON into existing profiles.
Interface for Bluetooth LE keyboard functionality.
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
void addProfile(uint8_t profileIndex, std::unique_ptr< Profile > profile)
Represents a single button configuration profile.
Definition profile.h:15
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
uint8_t numProfiles
Number of active profiles (1..MAX_PROFILES)
Definition config.h:19