BuGUI
BUtton Grid User Interface
Loading...
Searching...
No Matches
buttons.hpp
1#pragma once
2#include "init.hpp"
3#include "clear.hpp"
4#include <bugui/hardware/common.hpp>
5#include <bugui/hardware/base_hardware.hpp>
6#include <bugui/hardware/protocol/protocol.hpp>
7#include <bugui/io/button_presser.hpp>
8#include <bugui/color/color_converter.hpp>
9#include <bugui/concepts/device.hpp>
10#include <bugui/labels.hpp>
11
12namespace bugui
13{
16template <typename Device, typename Controller>
17struct buttons { };
18
21template <has_buttons Device, typename Controller>
22struct buttons<Device, Controller>
23 : virtual base_hardware
24 , virtual common<Controller>
25 , virtual init<component<Device, class Device::buttons>>
26 , virtual clear<component<Device, class Device::buttons>>
27 , virtual protocol<component<Device, class Device::buttons>>
28{
29 void set_button(const button_painter& p) override
30 {
31 this->send([&p, this](auto& formater)
32 {
33 color_cnvrtr b{p.get_color()};
34
35#define reseter(F) \
36 if constexpr(device_##F##_button<Device>) \
37 if (p.get_label() == labels::F && F != b) \
38 { \
39 formater.set(Device::buttons::F##_id(), b); \
40 F = b; \
41 }
42// FIXME : find a more succinct macro
43#define CALL_MACRO(r, data, elem) reseter(elem)
44 FOR_EACH_LABEL(CALL_MACRO)
45#undef CALL_MACRO
46#undef call_func
47 });
48 }
49
50protected:
51 explicit buttons()
52 {
53 this->on_message_received
54 ([this](auto id, auto value)
55 { return set(id, value); });
56 }
57
58 void component_reset()
59 {
60 if constexpr(has_clear<typename Device::buttons>)
61 this->clear<class component<Device, class Device::buttons>>
62 ::component_reset();
63 else
64 this->send
65 ([this](auto& formater)
66 {
67#define call_func(F) \
68 if constexpr(device_##F##_button<Device>) \
69 if (!F.is_clear()) \
70 { \
71 F.clear(); \
72 formater.set(Device::buttons::F##_id(), F); \
73 }
74// FIXME : find a more succinct macro
75#define CALL_MACRO(r, data, elem) call_func(elem)
76 FOR_EACH_LABEL(CALL_MACRO)
77#undef CALL_MACRO
78#undef reseters
79 });
80 }
81
82private:
83 using color_cnvrtr = color_converter
84 <typename color_t<Device, class Device::buttons>::type>;
85
86 bool set(uint8_t id, uint8_t midi)
87 { // FIXME : Only set presser if the receiver returns true
88 btn_prsr.set_midi(midi);
89 return on_button_received(id);
90 }
91
92 bool set(auto id, bool boolean)
93 { // FIXME : Only set presser if the receiver returns true
94 btn_prsr.set_bool(boolean);
95 return on_button_received(id);
96 }
97
98 bool on_button_received(auto id)
99 {
100#define call_ctrlr(F) \
101 if constexpr(F##_button<Device, Controller>) \
102 if (id == Device::buttons::F##_id()) \
103 { \
104 this->controller->on_##F(btn_prsr); \
105 this->controller->set_##F##_held(btn_prsr); \
106 return true; \
107 }
108// FIXME : find more succinct macro
109#define CALL_MACRO(r, data, elem) call_ctrlr(elem)
110 FOR_EACH_LABEL(CALL_MACRO)
111#undef CALL_MACRO
112#undef call_ctrlr
113
114 return false;
115 }
116
117#define converters(L) color_cnvrtr L{};
118// FIXME : find a more succinct macro
119#define CALL_MACRO(r, data, elem) converters(elem)
120 FOR_EACH_LABEL(CALL_MACRO)
121#undef CALL_MACRO
122#undef converters
123
124 button_presser btn_prsr;
125};
126
127} // namespace bugui
Conveys button colors from the controller to the base_hardware.
Definition button_painter.hpp:10
color get_color() const
Retieves the current color.
Definition button_painter.hpp:40
void set_button(const button_painter &p) override
Pass a reference to the button_painter object and display it's content to the device's buttons.
Definition buttons.hpp:29
Empty struct for devices that do not feature labelled buttons.
Definition buttons.hpp:17
Empty struct for devices or componants that do not provide a command to clear all feedback (LED's,...
Definition clear.hpp:10
Empty struct for devices that do not provide an initialisation command.
Definition init.hpp:10
Inherits from different protocols, depending on the device's specification.
Definition protocol.hpp:13