Skip to content

Commit

Permalink
Added new parameter values_wrap to enable/disable value wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Auer committed Oct 14, 2024
1 parent 142ae8a commit fbfc58e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 46 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
+ Fixed parameter default_encoder_mode, as there was a spelling mistake
+ Internal refactoring
+ Added HIDAPI as external library and started preparations for HID support
+ Added new parameter values_wrap to enable/disable value wrapping

-----------------------------------------------------------------------------------------------------------------------

Expand Down
7 changes: 4 additions & 3 deletions docs/inbound_mapping_dataref.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ Toggles a given Dataref between two or more values. This is very useful when no

## Optional Paramaters

| Parameter | Description |
|-----------|---------------------------------------------------------------------------------------------------|
| mode | Defines how the dataref should be changed. Possible values are `toggle` (default) and `momentary` |
| Parameter | Description |
|-------------|---------------------------------------------------------------------------------------------------------|
| mode | Defines how the dataref should be changed. Possible values are `toggle` (default) and `momentary` |
| values_wrap | Enable wrapping of values (e.g. if the end of the values list is reached, the first value will be taken |

### Dataref Mode
By default, the values for a Dataref are toggled (`mode = "toggle"`). That means, if you define two values pressing a button on your MIDI
Expand Down
17 changes: 10 additions & 7 deletions src/device/map_in/map_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,35 @@ map_param_in* map_in::get_param_in(map_param* in_param) {
/**
* Toggle dataref between values
*/
std::string map_in::toggle_dataref(text_logger& in_log, std::string_view in_dataref, std::vector<std::string>& in_values)
std::string map_in::toggle_dataref(text_logger& in_log, std::string_view in_dataref, std::vector<std::string>& in_values, bool in_wrap)
{
std::string new_value {};

if (in_values.size() == 2) {
new_value = env().drf().toggle(in_log, in_dataref, in_values[0], in_values[1]);
} else {
// get current value
std::string value;
env().drf().read(in_log, in_dataref, value);
std::string cur_value;
env().drf().read(in_log, in_dataref, cur_value);

// search for the value in the values list
auto it = std::find(in_values.begin(), in_values.end(), value);
auto it = std::find(in_values.begin(), in_values.end(), cur_value);

if (it != in_values.end()) {
auto idx = std::distance(in_values.begin(), it);

if (idx < static_cast<int>(in_values.size()) - 1)
idx++;
else
else if (in_wrap)
idx = 0;

new_value = in_values[idx];
} else {
// value not found, let's take the first one of the list
new_value = in_values[0];
// we are at the end if the list or did not find the value
if (in_wrap)
new_value = in_values[0];
else
new_value = cur_value;
}

in_log.debug(fmt::format(" --> Change dataref '{}' to value '{}'", in_dataref, new_value));
Expand Down
42 changes: 23 additions & 19 deletions src/device/map_in/map_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ namespace xmidictrl {
//---------------------------------------------------------------------------------------------------------------------

// Inbound mapping types
enum class map_in_type {
none,
command,
dataref,
encoder,
push_pull,
slider
enum class map_in_type
{
none,
command,
dataref,
encoder,
push_pull,
slider
};


Expand All @@ -54,29 +55,32 @@ enum class map_in_type {

class map_in : public map {
public:
explicit map_in(environment& in_env);
~map_in() override = default;
explicit map_in(environment& in_env);
~map_in() override = default;

virtual map_in_type type();
virtual map_in_type type();

virtual void read_config(text_logger& in_log, toml::value& in_data, toml::value& in_config);
virtual void read_config(text_logger& in_log, toml::value& in_data, toml::value& in_config);

virtual std::string map_text_label() = 0;
virtual std::string map_text_cmd_drf() = 0;
virtual std::string map_text_parameter() = 0;
virtual std::string map_text_label() = 0;
virtual std::string map_text_cmd_drf() = 0;
virtual std::string map_text_parameter() = 0;

protected:
environment& env();
environment& env();

map_param_in* get_param_in(map_param* in_param);

virtual std::string toggle_dataref(text_logger& in_log, std::string_view in_dataref, std::vector<std::string>& in_values);
virtual std::string toggle_dataref(text_logger& in_log,
std::string_view in_dataref,
std::vector<std::string>& in_values,
bool in_wrap);

private:
// members
environment& m_env;
// members
environment& m_env;
};

} // Namespace xmiditrl
} // namespace xmidictrl

#endif // XMC_MAP_IN_H
5 changes: 4 additions & 1 deletion src/device/map_in/map_in_drf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ void map_in_drf::read_config(text_logger& in_log, toml::value& in_data, toml::va
if (!value.empty())
m_values.push_back(value);
}

// check if we should wrap values
m_values_wrap = toml_utils::read_bool(in_log, in_data, c_cfg_values_wrap, true);
}


Expand Down Expand Up @@ -156,7 +159,7 @@ std::unique_ptr<map_result> map_in_drf::execute(map_param* in_param)
env().drf().write(param_in->msg().log(), m_dataref, value);
display_label(param_in->msg().log(), value);
} else {
toggle_dataref(param_in->msg().log(), m_dataref, m_values);
toggle_dataref(param_in->msg().log(), m_dataref, m_values, m_values_wrap);
}

return result;
Expand Down
2 changes: 2 additions & 0 deletions src/device/map_in/map_in_drf.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ class map_in_drf : public map_in_label {
static constexpr std::string_view c_cfg_values {"values"};
static constexpr std::string_view c_cfg_value_on {"value_on"};
static constexpr std::string_view c_cfg_value_off {"value_off"};
static constexpr std::string_view c_cfg_values_wrap { "values_wrap" };

// members
dataref_mode m_mode {dataref_mode::toggle};

std::string m_dataref {};

std::vector<std::string> m_values {};
bool m_values_wrap {true};

// functions
[[nodiscard]] dataref_mode dataref_mode_from_code(std::string_view in_mode) const;
Expand Down
5 changes: 3 additions & 2 deletions src/device/map_in/map_in_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ std::string map_in_label::map_text_label()
*/
std::string map_in_label::toggle_dataref(text_logger& in_log,
std::string_view in_dataref,
std::vector<std::string>& in_values)
std::vector<std::string>& in_values,
bool in_wrap)
{
auto new_value = map_in::toggle_dataref(in_log, in_dataref, in_values);
auto new_value = map_in::toggle_dataref(in_log, in_dataref, in_values, in_wrap);
display_label(in_log, new_value);

return new_value;
Expand Down
27 changes: 15 additions & 12 deletions src/device/map_in/map_in_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,36 @@ namespace xmidictrl {

class map_in_label : public map_in {
public:
explicit map_in_label(environment& in_env);
~map_in_label() override = default;
explicit map_in_label(environment& in_env);
~map_in_label() override = default;

void read_config(text_logger& in_log, toml::value& in_data, toml::value& in_config) override;
void read_config(text_logger& in_log, toml::value& in_data, toml::value& in_config) override;

std::string map_text_label() override;
std::string map_text_label() override;

protected:
std::string toggle_dataref(text_logger& in_log, std::string_view in_dataref, std::vector<std::string>& in_values) override;
std::string toggle_dataref(text_logger& in_log,
std::string_view in_dataref,
std::vector<std::string>& in_values,
bool in_wrap) override;

void display_label(text_logger& in_log, float in_value);
void display_label(text_logger& in_log, std::string_view in_value);
void display_label(text_logger& in_log, float in_value);
void display_label(text_logger& in_log, std::string_view in_value);

private:
// constants
static constexpr std::string_view c_cfg_label {"label"};
// constants
static constexpr std::string_view c_cfg_label {"label"};
static constexpr std::string_view c_cfg_text {"text"};
static constexpr std::string_view c_cfg_value {"value"};
static constexpr std::string_view c_cfg_values {"values"};

// members
std::unique_ptr<label> m_label {nullptr};
// members
std::unique_ptr<label> m_label {nullptr};

// functions
void read_label(text_logger& in_log, toml::value& in_data, toml::value& in_config);
};

} // Namespace xmiditrl
} // namespace xmidictrl

#endif // XMC_MAP_IN_LABEL_H
4 changes: 2 additions & 2 deletions src/device/map_in/map_in_pnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ std::unique_ptr<map_result> map_in_pnp::execute(map_param* in_param)

if (elapsed_seconds.count() > 0.5f || m_time_released.load() == time_point::min()) {
if (!m_dataref_pull.empty()) {
toggle_dataref(param_in->msg().log(), m_dataref_pull, m_values_pull);
toggle_dataref(param_in->msg().log(), m_dataref_pull, m_values_pull, true);
} else if (!m_command_pull.empty()) {
param_in->msg().log().debug(fmt::format(" --> Begin pull command '{}'", m_command_pull));
m_command_type = command_type::pull;
Expand All @@ -239,7 +239,7 @@ std::unique_ptr<map_result> map_in_pnp::execute(map_param* in_param)
}
} else {
if (!m_dataref_push.empty()) {
toggle_dataref(param_in->msg().log(), m_dataref_push, m_values_push);
toggle_dataref(param_in->msg().log(), m_dataref_push, m_values_push, true);
} else if (!m_command_push.empty()) {
param_in->msg().log().debug(fmt::format(" --> Begin push command '{}'", m_command_push));
m_command_type = command_type::push;
Expand Down

0 comments on commit fbfc58e

Please sign in to comment.