Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Selector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Selectable.hpp"
5#include <AH/Debug/Debug.hpp>
6#include <Def/Def.hpp>
7
9
14enum class Wrap : bool {
15 Clamp = false,
17 Wrap = true,
19 NoWrap = false,
20};
21
25 void begin() {}
27 void update() {}
29 void update(setting_t oldSetting, setting_t newSetting) {
30 (void)oldSetting, (void)newSetting;
31 }
32};
33
40 protected:
42 SelectorBase() = default;
43
44 public:
46 setting_t get() const { return setting; }
47
48 protected:
53};
54
55template <setting_t N, class Callback = EmptySelectorCallback>
56class GenericSelector : public SelectorBase, public AH::Updatable<> {
57 public:
71
72 void begin() override {
73 callback.begin();
74 reset();
75 }
76
77 void update() override { callback.update(); }
78
80 void reset() {
81 setting_t initialSelection = selectable.getInitialSelection();
82 selectable.select(initialSelection);
83 callback.update(initialSelection, initialSelection);
84 this->setting = initialSelection;
85 }
86
93 void set(setting_t newSetting) {
94 newSetting = selectable.validateSetting(newSetting);
95 selectable.select(newSetting);
96 if (get() != newSetting) {
97 callback.update(get(), newSetting);
98 this->setting = newSetting;
99 }
100 }
101
109 void increment(Wrap wrap) {
110 setting_t setting = this->get();
111 setting++;
112 if (setting == N) {
113 if (wrap == Wrap::Wrap)
114 setting = 0;
115 else
116 return;
117 }
118 this->set(setting);
119 }
120
128 void decrement(Wrap wrap) {
129 setting_t setting = this->get();
130 if (setting == 0) {
131 if (wrap == Wrap::Wrap)
132 setting = N;
133 else
134 return;
135 }
136 setting--;
137 this->set(setting);
138 }
139
140 private:
142
143 public:
144 Callback callback;
145};
146
153template <setting_t N>
154class Selector : public GenericSelector<N> {
155 public:
158};
159
uint8_t setting_t
The type used for Selectors.
Definition Def.hpp:53
Wrap
An enumeration to set the behavior of selectors that are incremented (decremented) beyond their maxim...
Definition Selector.hpp:14
@ Clamp
When the maximum (minimum) setting is reached, clamp to the maximum (minimum) setting.
@ NoWrap
@ Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
A super class for object that have to be updated regularly.
void increment(Wrap wrap)
Add one to the setting, wrap around or clamp, depending on the parameter, if the new setting would be...
Definition Selector.hpp:109
void set(setting_t newSetting)
Select the given selection.
Definition Selector.hpp:93
void decrement(Wrap wrap)
Subtract one from the setting, wrap around or clamp, depending on the parameter, if the new setting w...
Definition Selector.hpp:128
Callback callback
Definition Selector.hpp:144
void update() override
Update this updatable.
Definition Selector.hpp:77
Selectable< N > & selectable
Definition Selector.hpp:141
void begin() override
Initialize this updatable.
Definition Selector.hpp:72
void reset()
Reset the selection to the initial selection.
Definition Selector.hpp:80
GenericSelector(Selectable< N > &selectable, const Callback &callback)
Constructor.
Definition Selector.hpp:69
Base class for all Selectors exposing the get method, so it can be used by display elements etc,...
Definition Selector.hpp:39
SelectorBase()=default
Constructor.
setting_t setting
The selection of the selector.
Definition Selector.hpp:52
setting_t get() const
Get the current selection/setting.
Definition Selector.hpp:46
A Selector with an empty callback.
Definition Selector.hpp:154
Selector(Selectable< N > &selectable)
Constructor.
Definition Selector.hpp:157
A callback for the GenericSelector class that does nothing.
Definition Selector.hpp:23
void update(setting_t oldSetting, setting_t newSetting)
Called when the setting changes.
Definition Selector.hpp:29
void begin()
Initialize.
Definition Selector.hpp:25
void update()
Refresh, called periodically.
Definition Selector.hpp:27