This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Selector.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Selectable.hpp"
4 #include <Def/Def.hpp>
5 #include <Helpers/Debug.hpp>
6 #include <Helpers/Updatable.hpp>
7 
9 
14 enum class Wrap : bool {
15  Clamp = false,
16  Wrap = true,
19  NoWrap = false,
21 };
22 
26  void begin() {}
28  void update() {}
30  void update(setting_t oldSetting, setting_t newSetting) {
31  (void)oldSetting, (void)newSetting;
32  }
33 };
34 
40 class SelectorBase {
41  protected:
43  SelectorBase() = default;
44 
45  public:
47  setting_t get() const { return setting; }
48 
49  protected:
54 };
55 
56 template <setting_t N, class Callback = EmptySelectorCallback>
57 class GenericSelector : public SelectorBase, public Updatable<> {
58  public:
72 
73  void begin() override {
74  callback.begin();
75  reset();
76  }
77 
78  void update() override { callback.update(); }
79 
81  void reset() { set(selectable.getInitialSelection()); }
82 
89  void set(setting_t newSetting) {
90  newSetting = selectable.validateSetting(newSetting);
91  selectable.select(newSetting);
92  if (get() != newSetting) {
93  callback.update(get(), newSetting);
94  setting = newSetting;
95  }
96  }
97 
105  void increment(Wrap wrap) {
106  setting_t setting = this->get();
107  setting++;
108  if (setting == N) {
109  if (wrap == Wrap::Wrap)
110  setting = 0;
111  else
112  return;
113  }
114  this->set(setting);
115  }
116 
124  void decrement(Wrap wrap) {
125  setting_t setting = this->get();
126  if (setting == 0) {
127  if (wrap == Wrap::Wrap)
128  setting = N;
129  else
130  return;
131  }
132  setting--;
133  this->set(setting);
134  }
135 
136  private:
138 
139  public:
140  Callback callback;
141 };
142 
149 template <setting_t N>
150 class Selector : public GenericSelector<N> {
151  public:
154 };
155 
Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:20
Selector
A Selector with an empty callback.
Definition: Selector.hpp:150
EmptySelectorCallback::update
void update()
Refresh, called periodically.
Definition: Selector.hpp:28
EmptySelectorCallback::update
void update(setting_t oldSetting, setting_t newSetting)
Called when the setting changes.
Definition: Selector.hpp:30
SelectorBase::setting
setting_t setting
The selection of the selector.
Definition: Selector.hpp:53
GenericSelector::decrement
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:124
GenericSelector::selectable
Selectable< N > & selectable
Definition: Selector.hpp:137
SelectorBase::get
setting_t get() const
Get the current selection/setting.
Definition: Selector.hpp:47
Selector::Selector
Selector(Selectable< N > &selectable)
Constructor.
Definition: Selector.hpp:153
SelectorBase::SelectorBase
SelectorBase()=default
Constructor.
Updatable.hpp
SelectorBase
Base class for all Selectors exposing the get method, so it can be used by display elements etc,...
Definition: Selector.hpp:40
Def.hpp
Selectable.hpp
Selectable
Definition: Selectable.hpp:11
EmptySelectorCallback
A callback for the GenericSelector class that does nothing.
Definition: Selector.hpp:24
Wrap::NoWrap
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
GenericSelector::set
void set(setting_t newSetting)
Select the given selection.
Definition: Selector.hpp:89
EmptySelectorCallback::begin
void begin()
Initialize.
Definition: Selector.hpp:26
GenericSelector::GenericSelector
GenericSelector(Selectable< N > &selectable, const Callback &callback)
Constructor.
Definition: Selector.hpp:70
Wrap
Wrap
An enumeration to set the behavior of selectors that are incremented (decremented) beyond their maxim...
Definition: Selector.hpp:14
GenericSelector
Definition: Selector.hpp:57
GenericSelector::update
void update() override
Update this updatable.
Definition: Selector.hpp:78
GenericSelector::callback
Callback callback
Definition: Selector.hpp:140
GenericSelector::increment
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:105
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
setting_t
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:63
Wrap::Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
Debug.hpp
GenericSelector::begin
void begin() override
Initialize this updatable.
Definition: Selector.hpp:73
Wrap::Clamp
When the maximum (minimum) setting is reached, clamp to the maximum (minimum) setting.
GenericSelector::reset
void reset()
Reset the selection to the initial selection.
Definition: Selector.hpp:81