Use definitions in Boards for pots/sliders config

This commit is contained in:
Raphael Coeffic
2021-12-31 10:53:10 +01:00
parent d37b5d3c57
commit 56997f4d3c
4 changed files with 148 additions and 49 deletions
@@ -403,8 +403,10 @@ Node convert<ModelData>::encode(const ModelData& rhs)
node["toplcdTimer"] = rhs.toplcdTimer; node["toplcdTimer"] = rhs.toplcdTimer;
// B&W only
// customScreens // customScreens
// topBarData // topBarData
node["view"] = rhs.view; node["view"] = rhs.view;
node["modelRegistrationID"] = rhs.registrationId; node["modelRegistrationID"] = rhs.registrationId;
@@ -58,6 +58,7 @@ std::string YamlRawSwitchEncode(const RawSwitch& rhs)
switch (rhs.type) { switch (rhs.type) {
case SWITCH_TYPE_SWITCH: case SWITCH_TYPE_SWITCH:
sw_str += 'S'; sw_str += 'S';
// TODO: use lookup of switches in Boards ???
sw_str += 'A' + (sval - 1) / 3; sw_str += 'A' + (sval - 1) / 3;
sw_str += std::to_string((sval - 1) % 3); sw_str += std::to_string((sval - 1) % 3);
break; break;
@@ -129,6 +130,7 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
} else if (val_len >= 3 && val[0] == 'S' && } else if (val_len >= 3 && val[0] == 'S' &&
(val[1] >= 'A' && val[1] <= 'Z') && (val[1] >= 'A' && val[1] <= 'Z') &&
(val[2] >= '0' && val[2] <= '2')) { (val[2] >= '0' && val[2] <= '2')) {
// TODO: use lookup of switches in Boards ???
rhs = RawSwitch(SWITCH_TYPE_SWITCH, (val[1] - 'A') * 3 + val[2] - '0'); rhs = RawSwitch(SWITCH_TYPE_SWITCH, (val[1] - 'A') * 3 + val[2] - '0');
} else { } else {
// TODO: check if we have a match? // TODO: check if we have a match?
@@ -19,19 +19,9 @@
*/ */
#include "yaml_switchconfig.h" #include "yaml_switchconfig.h"
#include "boards.h" #include "boards.h"
#include "eeprominterface.h"
const YamlLookupTable sticksLut = {
{0, "0"},
{1, "1"},
{2, "2"},
{3, "3"},
};
const YamlLookupTable switchesLut = {
{0, "SA"}, {1, "SB"}, {2, "SC"}, {3, "SD"}, {4, "SE"},
{5, "SF"}, {6, "SG"}, {7, "SH"}, {8, "SI"}, {9, "SJ"},
};
const YamlLookupTable switchConfigLut = { const YamlLookupTable switchConfigLut = {
{Board::SWITCH_NOT_AVAILABLE, "none"}, {Board::SWITCH_NOT_AVAILABLE, "none"},
@@ -40,10 +30,6 @@ const YamlLookupTable switchConfigLut = {
{Board::SWITCH_3POS, "3pos"}, {Board::SWITCH_3POS, "3pos"},
}; };
const YamlLookupTable potsLut = {
{0, "S1"}, {1, "6POS"}, {2, "S2"},
{3, "EXT1"}, {4, "EXT2"}, {5, "EXT3"}, {6, "EXT4"},
};
const YamlLookupTable potConfigLut = { const YamlLookupTable potConfigLut = {
{Board::POT_NONE, "none"}, {Board::POT_NONE, "none"},
@@ -62,3 +48,91 @@ const YamlLookupTable sliderConfigLut = {
{Board::SLIDER_WITH_DETENT, "with_detent"}, {Board::SLIDER_WITH_DETENT, "with_detent"},
}; };
int YamlStickLookup::name2idx(const std::string& name)
{
int idx = std::stoi(name);
if (idx < 4) {
return idx;
}
return -1;
}
std::string YamlStickLookup::idx2name(unsigned int idx)
{
if (idx < 4) {
return std::to_string(idx);
}
return std::string();
}
int YamlSwitchLookup::name2idx(const std::string& name)
{
// TODO: replace this with a real lookup of available switches
if (name.size() == 2
&& name[0] == 'S'
&& (name[1] >= 'A' && name[1] <= 'J')) {
return name[1] - 'A';
}
return -1;
}
std::string YamlSwitchLookup::idx2name(unsigned int idx)
{
// TODO: replace this with a real lookup of available switches
if (idx < 10) {
return std::string("S") + (char)('A' + idx);
}
return std::string();
}
int YamlPotLookup::name2idx(const std::string& name)
{
auto fw = getCurrentFirmware();
int idx = fw->getAnalogInputIndex(name.c_str());
if (idx < 0) return idx;
int pots = Boards::getCapability(fw->getBoard(), Board::Pots);
if (idx >= pots) return -1;
return idx;
}
std::string YamlPotLookup::idx2name(unsigned int idx)
{
auto fw = getCurrentFirmware();
int pots = Boards::getCapability(fw->getBoard(), Board::Pots);
if (idx >= pots) return std::string();
return fw->getAnalogInputTag(idx);
}
int YamlSliderLookup::name2idx(const std::string& name)
{
auto fw = getCurrentFirmware();
int idx = fw->getAnalogInputIndex(name.c_str());
if (idx < 0) return idx;
int pots = Boards::getCapability(fw->getBoard(), Board::Pots);
if (idx < pots) return -1;
idx -= pots;
int sliders = Boards::getCapability(fw->getBoard(), Board::Sliders);
if (idx >= sliders) return -1;
return idx;
}
std::string YamlSliderLookup::idx2name(unsigned int idx)
{
auto fw = getCurrentFirmware();
int pots = Boards::getCapability(fw->getBoard(), Board::Pots);
int sliders = Boards::getCapability(fw->getBoard(), Board::Sliders);
if (idx >= sliders) return std::string();
return fw->getAnalogInputTag(idx + pots);
}
@@ -24,7 +24,12 @@
#include "generalsettings.h" #include "generalsettings.h"
#include <string.h> #include <string.h>
template <unsigned int N, const YamlLookupTable& name_lut> struct YamlStickLookup {
static int name2idx(const std::string& name);
static std::string idx2name(unsigned int idx);
};
template <unsigned int N, class name_lookup>
struct YamlNameConfig { struct YamlNameConfig {
char name[N][HARDWARE_NAME_LEN + 1]; char name[N][HARDWARE_NAME_LEN + 1];
@@ -41,19 +46,17 @@ struct YamlNameConfig {
} }
}; };
extern const YamlLookupTable sticksLut; typedef YamlNameConfig<CPN_MAX_SWITCHES, YamlStickLookup> YamlStickConfig;
typedef YamlNameConfig<CPN_MAX_SWITCHES, sticksLut> YamlStickConfig;
template <unsigned int N, const YamlLookupTable& name_lut, template <unsigned int N, class name_lookup, const YamlLookupTable& cfg_lut>
const YamlLookupTable& cfg_lut> struct YamlKnobConfig : public YamlNameConfig<N, name_lookup> {
struct YamlKnobConfig : public YamlNameConfig<N, name_lut> {
unsigned int config[N]; unsigned int config[N];
YamlKnobConfig() { memset(config, 0, sizeof(config)); } YamlKnobConfig() { memset(config, 0, sizeof(config)); }
YamlKnobConfig(const char in_names[N][HARDWARE_NAME_LEN + 1], YamlKnobConfig(const char in_names[N][HARDWARE_NAME_LEN + 1],
const unsigned int in_configs[N]) : const unsigned int in_configs[N]) :
YamlNameConfig<N, name_lut>(in_names) YamlNameConfig<N, name_lookup>(in_names)
{ {
memcpy(config, in_configs, sizeof(config)); memcpy(config, in_configs, sizeof(config));
} }
@@ -61,7 +64,7 @@ struct YamlKnobConfig : public YamlNameConfig<N, name_lut> {
void copy(char out_names[N][HARDWARE_NAME_LEN + 1], void copy(char out_names[N][HARDWARE_NAME_LEN + 1],
unsigned int out_configs[N]) unsigned int out_configs[N])
{ {
YamlNameConfig<N, name_lut>::copy(out_names); YamlNameConfig<N, name_lookup>::copy(out_names);
memcpy(out_configs, config, sizeof(config)); memcpy(out_configs, config, sizeof(config));
} }
}; };
@@ -69,71 +72,89 @@ struct YamlKnobConfig : public YamlNameConfig<N, name_lut> {
// SA: // SA:
// type: 3pos // type: 3pos
// name: // name:
extern const YamlLookupTable switchesLut; struct YamlSwitchLookup {
static int name2idx(const std::string& name);
static std::string idx2name(unsigned int idx);
};
extern const YamlLookupTable switchConfigLut; extern const YamlLookupTable switchConfigLut;
typedef YamlKnobConfig<CPN_MAX_SWITCHES, switchesLut, switchConfigLut> typedef YamlKnobConfig<CPN_MAX_SWITCHES, YamlSwitchLookup, switchConfigLut>
YamlSwitchConfig; YamlSwitchConfig;
// S1: // S1:
// type: with_detent // type: with_detent
// name: // name:
extern const YamlLookupTable potsLut; struct YamlPotLookup {
static int name2idx(const std::string& name);
static std::string idx2name(unsigned int idx);
};
extern const YamlLookupTable potConfigLut; extern const YamlLookupTable potConfigLut;
typedef YamlKnobConfig<CPN_MAX_POTS, potsLut, potConfigLut> YamlPotConfig; typedef YamlKnobConfig<CPN_MAX_POTS, YamlPotLookup, potConfigLut> YamlPotConfig;
// S1: // S1:
// type: with_detent // type: with_detent
// name: // name:
extern const YamlLookupTable slidersLut; struct YamlSliderLookup {
static int name2idx(const std::string& name);
static std::string idx2name(unsigned int idx);
};
extern const YamlLookupTable sliderConfigLut; extern const YamlLookupTable sliderConfigLut;
typedef YamlKnobConfig<CPN_MAX_SLIDERS, slidersLut, sliderConfigLut> typedef YamlKnobConfig<CPN_MAX_SLIDERS, YamlSliderLookup, sliderConfigLut>
YamlSliderConfig; YamlSliderConfig;
namespace YAML namespace YAML
{ {
template <unsigned int N, const YamlLookupTable& name_lut> template <unsigned int N, class name_lookup>
struct convert<YamlNameConfig<N, name_lut> > { struct convert<YamlNameConfig<N, name_lookup> > {
static Node encode(const YamlNameConfig<N, name_lut>& rhs) static Node encode(const YamlNameConfig<N, name_lookup>& rhs)
{ {
Node node; Node node;
for (const auto& kv : name_lut) { for (unsigned int i=0; i<N; i++) {
node[kv.second]["name"] = rhs.name[kv.first]; std::string name = name_lookup::idx2name(i);
if (!name.empty()) {
node[name]["name"] = rhs.name[i];
}
} }
return node; return node;
} }
static bool decode(const Node& node, YamlNameConfig<N, name_lut>& rhs) static bool decode(const Node& node, YamlNameConfig<N, name_lookup>& rhs)
{ {
if (!node.IsMap()) return false; if (!node.IsMap()) return false;
int idx = 0; int idx = 0;
for (const auto& kv : node) { for (const auto& kv : node) {
kv.first >> name_lut >> idx; idx = name_lookup::name2idx(kv.first.Scalar());
kv.second["name"] >> rhs.name[idx]; if (idx >= 0) {
kv.second["name"] >> rhs.name[idx];
}
} }
return true; return true;
} }
}; };
template <unsigned int N, const YamlLookupTable& name_lut, template <unsigned int N, class name_lookup, const YamlLookupTable& cfg_lut>
const YamlLookupTable& cfg_lut> struct convert<YamlKnobConfig<N, name_lookup, cfg_lut> > {
struct convert<YamlKnobConfig<N, name_lut, cfg_lut> > { static Node encode(const YamlKnobConfig<N, name_lookup, cfg_lut>& rhs)
static Node encode(const YamlKnobConfig<N, name_lut, cfg_lut>& rhs)
{ {
Node node; Node node;
for (const auto& kv : name_lut) { for (unsigned int i=0; i<N; i++) {
node[kv.second]["type"] = cfg_lut << rhs.config[kv.first]; std::string name = name_lookup::idx2name(i);
node[kv.second]["name"] = rhs.name[kv.first]; if (!name.empty()) {
node[name]["type"] = cfg_lut << rhs.config[i];
node[name]["name"] = rhs.name[i];
}
} }
return node; return node;
} }
static bool decode(const Node& node, static bool decode(const Node& node,
YamlKnobConfig<N, name_lut, cfg_lut>& rhs) YamlKnobConfig<N, name_lookup, cfg_lut>& rhs)
{ {
if (!node.IsMap()) return false; if (!node.IsMap()) return false;
int idx = 0; int idx = 0;
for (const auto& kv : node) { for (const auto& kv : node) {
kv.first >> name_lut >> idx; idx = name_lookup::name2idx(kv.first.Scalar());
kv.second["type"] >> cfg_lut >> rhs.config[idx]; if (idx >= 0) {
kv.second["name"] >> rhs.name[idx]; kv.second["type"] >> cfg_lut >> rhs.config[idx];
kv.second["name"] >> rhs.name[idx];
}
} }
return true; return true;
} }