Solo Controller Documentation 1.0
Documentation for the Solo Controller
 
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1/******************************************************************************/
5
6#ifndef BUTTON_INCLUDED
7#define BUTTON_INCLUDED
8
9#include "ButtonState.h"
10
14template <uint8_t NUM>
16{
17 uint8_t borderWidth;
18 uint16_t borderColor;
19 unsigned short cornerRadius;
20};
21
26 3, // Border width
27 COLORS.WHITE, // Border Color
28 25 // Corner Radius0
29};
30
35 2, // Border width
36 COLORS.WHITE, // Border Color
37 15 // Corner Radius0
38};
39
40/******************************************************************************/
41/*** ***/
42/*** Class Definition: Button ***/
43/*** ***/
44/******************************************************************************/
54class Button
55{
56public:
66
67private:
68 bool _enabled = false; // Determines whether the button is enabled
69 uint8_t _state = 0; // Tracks the current state of the button
70 const char *_BUTTON_NAME; // Name of the button (used for error messages)
71 callback_type _whenCallback; // Defines when the callback function is called
72
73 // Destination where the button should be copied to
74 Canvas &_DestCanvas;
75 const uint16_t _DEST_X;
76 const uint16_t _DEST_Y;
77
78 // Size of the button
79 const uint16_t _WIDTH;
80 const uint16_t _HEIGHT;
81
82 // Array of button states
83 uint8_t _nStates = 0; // Maximum number of states supported by this button
84 ButtonState **_buttonState = nullptr; // Array of pointers to ButtonState objects, provided by derived classes
85
86 // Internal method that verifies that the indicated state is valid
87 bool _isValidState(uint8_t state);
88
89public:
91 Button(void) = delete;
92
96
107 Button(const char *buttonName, Canvas &DestCanvas, uint16_t dest_x, uint16_t dest_y, uint16_t width, uint16_t height,
108 callback_type whenCallback)
109 : _BUTTON_NAME{buttonName},
110 _whenCallback{whenCallback},
111 _DestCanvas{DestCanvas},
112 _DEST_X{dest_x},
113 _DEST_Y{dest_y},
114 _WIDTH{width},
115 _HEIGHT{height} {};
116
122 virtual ~Button(void) = default;
123
127
133 virtual void begin(void);
134
139 virtual bool press(FT5316::TouchLocation loc);
140
147 virtual void release(void);
148
154 void enable(void)
155 {
156 _enabled = true;
157 };
158
164 void disable(void)
165 {
166 _enabled = false;
167 };
168
173 void setState(uint8_t state)
174 {
175 if (state < 0 || state >= _nStates)
176 return;
177 _state = state;
178 };
179
185 void nextState(void)
186 {
187 if (++_state >= _nStates)
188 {
189 _state = 0;
190 }
191 };
192
198 void draw(void)
199 {
200 _buttonState[_state]->draw();
201 }
202
207 void draw(Canvas &newCanvas)
208 {
209 _buttonState[_state]->draw(newCanvas);
210 }
211
213
217
224 inline bool isEnabled(void) const
225 {
226 return _enabled;
227 };
228
235 inline uint16_t width(void) const
236 {
237 return _WIDTH;
238 }
239
246 inline uint16_t height(void) const
247 {
248 return _HEIGHT;
249 }
250
257 inline uint8_t state(void) const
258 {
259 return _state;
260 }
261
263
264protected:
270 void _validateObject(void);
271
277 void _registerButtonStates(ButtonState **buttonStatePtrArr, uint8_t numStates);
278
284 {
285 _whenCallback = type;
286 }
287};
288
289/******************************************************************************/
290/*** ***/
291/*** Class Definition: ToggleButton ***/
292/*** ***/
293/******************************************************************************/
300class ToggleButton : public Button
301{
302 static const uint8_t _NUM_STATES = 2;
303 ButtonState *_ButtonState[_NUM_STATES];
304
305public:
307 ToggleButton(void) = delete;
308
312
329 ToggleButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc,
330 const char *buttonText0, FontList::FontID Font0, uint16_t fillColor0, ButtonState::buttonCallbackFunction callback0,
331 const char *buttonText1, FontList::FontID Font1, uint16_t fillColor1, ButtonState::buttonCallbackFunction callback1,
332 uint16_t fontBaseline, const ButtonConfig_t<2> config = DEFAULT_BUTTON_CONFIG)
333 : Button(buttonName, Destination, DestLoc.x, DestLoc.y, DestLoc.width, DestLoc.height, Button::callback_type::ON_PRESS)
334 {
335 _ButtonState[0] = new TextButtonState(Destination, DestLoc, buttonText0, Font0, callback0, fontBaseline,
336 config.cornerRadius, config.borderWidth, config.borderColor, fillColor0);
337 _ButtonState[1] = new TextButtonState(Destination, DestLoc, buttonText1, Font1, callback1, fontBaseline,
338 config.cornerRadius, config.borderWidth, config.borderColor, fillColor1);
339 };
340
360 ToggleButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc,
361 ROM_Image ButtonImg0, uint16_t ButtonImgColor0, uint16_t ButtonFillColor0, ButtonState::buttonCallbackFunction callback0,
362 ROM_Image ButtonImg1, uint16_t ButtonImgColor1, uint16_t ButtonFillColor1, ButtonState::buttonCallbackFunction callback1,
364 : Button{buttonName, Destination, DestLoc.x, DestLoc.y, DestLoc.width, DestLoc.height, Button::callback_type::ON_PRESS}
365 {
366 _ButtonState[0] = new ImageButtonState(Destination, DestLoc, callback0, ButtonImg0, ButtonImgColor0,
367 config.cornerRadius, config.borderWidth, config.borderColor, ButtonFillColor0);
368 _ButtonState[1] = new ImageButtonState(Destination, DestLoc, callback1, ButtonImg1, ButtonImgColor1,
369 config.cornerRadius, config.borderWidth, config.borderColor, ButtonFillColor1);
370 }
371
377 ~ToggleButton(void) = default;
378
380
384
385 void begin(void) override;
386
388};
389
390/******************************************************************************/
391/*** ***/
392/*** Class Definition: MomentaryButton ***/
393/*** ***/
394/******************************************************************************/
402class MomentaryButton : public ToggleButton
403{
404 // ButtonStates define appearance and functionality of the Button
405 static const uint8_t _NUM_STATES = 1;
406 ButtonState *_ButtonState[_NUM_STATES];
407
408public:
410 MomentaryButton(void) = delete;
411
415
431 MomentaryButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc,
432 const char *buttonText0, FontList::FontID Font0, uint16_t fillColor0,
433 const char *buttonText1, FontList::FontID Font1, uint16_t fillColor1,
434 ButtonState::buttonCallbackFunction callback, uint16_t fontBaseline, const ButtonConfig_t<2> config = DEFAULT_BUTTON_CONFIG)
435 : ToggleButton(buttonName, Destination, DestLoc,
436 buttonText0, Font0, fillColor0, nullptr, buttonText1, Font1, fillColor1, callback,
437 fontBaseline, config)
438 {
440 };
441
460 MomentaryButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc,
461 ROM_Image ButtonImg0, uint16_t ButtonImgColor0, uint16_t ButtonFillColor0,
462 ROM_Image ButtonImg1, uint16_t ButtonImgColor1, uint16_t ButtonFillColor1,
464 : ToggleButton(buttonName, Destination, DestLoc,
465 ButtonImg0, ButtonImgColor0, ButtonFillColor0, nullptr, ButtonImg1, ButtonImgColor1, ButtonFillColor1, callback,
466 config)
467 {
469 };
470
476 ~MomentaryButton(void) = default;
477
479
485 void release(void) override;
486};
487
488/******************************************************************************/
489/*** ***/
490/*** Class Definition: TextBoxButton ***/
491/*** ***/
492/******************************************************************************/
498class TextBoxButton : public Button
499{
500 // ButtonStates that define appearance and functionality of the Button
501 static const uint8_t _NUM_STATES = 1;
502 TextBoxButtonState *_ButtonState[_NUM_STATES];
503
504public:
506 TextBoxButton(void) = delete;
507
511
519 TextBoxButton(const char *buttonName, Canvas &Destination,
521 : Button(buttonName, Destination, config.x, config.y, config.textWidth + config.UnitsFont, config.shape.height,
522 Button::callback_type::ON_RELEASE)
523 {
524 _ButtonState[0] = new TextBoxButtonState(Destination, config, callback);
525 };
526
532 ~TextBoxButton(void) = default;
533
535
539
545 void begin(void) override;
546
552 inline void update(const char *text)
553 {
554 _ButtonState[0]->update(text);
555 _ButtonState[0]->draw();
556 }
557
563 inline void update(int value)
564 {
565 _ButtonState[0]->update(value);
566 _ButtonState[0]->draw();
567 }
568
574 inline void setUnits(const char *text)
575 {
576 _ButtonState[0]->setUnits(text);
577 }
578
580};
581
582/******************************************************************************/
583/*** ***/
584/*** Class Definition: ImmediateButton ***/
585/*** ***/
586/******************************************************************************/
594/*class ImmediateButton : public Button
595{
596
597 // Canvases used to store the on/off button imagery. These images are copied
598 // onto _DestCanvas as needed.
599 static const uint8_t _NUM_STATES = 1;
600 const DRAM_Canvas _Img[_NUM_STATES];
601
602 // Two callbacks are store (on and off)
603 const buttonCallbackFunction _callback;
604
605public: */
607// ImmediateButton(void) = delete;
608
612
619// ImmediateButton(Canvas &DestCanvas, const Block_Image &DestLoc, buttonCallbackFunction callback)
620// : Button(DestCanvas, DestLoc, _Img, _NUM_STATES), _Img{DRAM_Canvas(DestCanvas, DestLoc)}, _callback{callback} {}
621
627//~ImmediateButton(void) = default;
628
630
634
639// bool press(FT5316::TouchLocation loc) override;
640
642
643#endif
const ButtonConfig_t< 2 > DEFAULT_BUTTON_CONFIG
Defines the configuration parameters for a standard button.
Definition Button.h:25
const ButtonConfig_t< 2 > DEFAULT_SM_BUTTON_CONFIG
Defines the configuration parameters for a small button.
Definition Button.h:34
Generic Button State class that defines properties of a button state.
const struct @335246042156366161160006341037173214026114026241 COLORS
Colors used by the application.
Abstract class that provides basic button functionality.
Definition Button.h:55
virtual void begin(void)
Initializes the button. This method should be called before first use.
Definition Button.cpp:36
uint8_t state(void) const
Returns the current state of the button.
Definition Button.h:257
void _registerButtonStates(ButtonState **buttonStatePtrArr, uint8_t numStates)
Registers the ButtonState objects that define this button.
Definition Button.cpp:112
void _validateObject(void)
Validates that all ButtonState objects have been defined.
Definition Button.cpp:91
void nextState(void)
Advances the button to the next state.
Definition Button.h:185
callback_type
Type definition that defines when the callback function is called relative to button press/release.
Definition Button.h:62
@ ON_RELEASE
Definition Button.h:64
@ ON_PRESS
Definition Button.h:63
uint16_t height(void) const
Returns the height of the button in pixels.
Definition Button.h:246
virtual void release(void)
Called when the button is released.
Definition Button.cpp:82
void setState(uint8_t state)
Sets the button to the specified state.
Definition Button.h:173
void _changeCallback(callback_type type)
Convenience method that changes when the callback is called.
Definition Button.h:283
void draw(Canvas &newCanvas)
Draws the image from the current ButtonState onto the specified canvas.
Definition Button.h:207
uint16_t width(void) const
Returns the width of the button in pixels.
Definition Button.h:235
virtual ~Button(void)=default
Default destructor.
void disable(void)
Disables the button, so that it ignores button presses.
Definition Button.h:164
void draw(void)
Draws the image from the current ButtonState onto the destination canvas.
Definition Button.h:198
void enable(void)
Enables the button, making it accept button presses.
Definition Button.h:154
virtual bool press(FT5316::TouchLocation loc)
Presses the button if loc is within the bounds of the button.
Definition Button.cpp:48
Button(const char *buttonName, Canvas &DestCanvas, uint16_t dest_x, uint16_t dest_y, uint16_t width, uint16_t height, callback_type whenCallback)
Constructor for creating a Button object.
Definition Button.h:107
bool isEnabled(void) const
Indicates whether the Button is enabled.
Definition Button.h:224
Base class used by all derived Button State classes.
Definition ButtonState.h:27
void(* buttonCallbackFunction)(void)
Defines the form of the callback functions expected by the Button class.
Definition ButtonState.h:29
Provides higher level TFT graphic functionality used by the menu system.
Definition Canvas.h:34
FontID
Enumeration listing available fonts.
Definition FontList.h:35
Button State for a button that uses imagery as its button graphics.
Definition ButtonState.h:201
~MomentaryButton(void)=default
Default destructor.
MomentaryButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc, const char *buttonText0, FontList::FontID Font0, uint16_t fillColor0, const char *buttonText1, FontList::FontID Font1, uint16_t fillColor1, ButtonState::buttonCallbackFunction callback, uint16_t fontBaseline, const ButtonConfig_t< 2 > config=DEFAULT_BUTTON_CONFIG)
Constructor for a Momentary Button that displays text in each state.
Definition Button.h:431
void release(void) override
Releases the button if it has been pressed.
Definition Button.cpp:147
MomentaryButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc, ROM_Image ButtonImg0, uint16_t ButtonImgColor0, uint16_t ButtonFillColor0, ROM_Image ButtonImg1, uint16_t ButtonImgColor1, uint16_t ButtonFillColor1, ButtonState::buttonCallbackFunction callback, const ButtonConfig_t< 2 > config=DEFAULT_BUTTON_CONFIG)
Constructor for a Momentary Button that displays images in each state.
Definition Button.h:460
void update(int value)
Updates the numerical value (int) inside the button.
Definition Button.h:563
~TextBoxButton(void)=default
Default destructor.
void begin(void) override
Initializes the button. This method should be called before first use of the object.
Definition Button.cpp:160
TextBoxButton(const char *buttonName, Canvas &Destination, const ConfigTextBox_t &config, ButtonState::buttonCallbackFunction callback)
Constructor for button containing text with both a value and units component.
Definition Button.h:519
void update(const char *text)
Updates the value inside the button. Assumes that the string is null terminated.
Definition Button.h:552
void setUnits(const char *text)
Updates the units text inside the button. Assumes that the string is null terminated.
Definition Button.h:574
Button State for a button that uses TextBox to display a value and units inside the button.
Definition ButtonState.h:310
Button State for a button that displays text in the button.
Definition ButtonState.h:250
~ToggleButton(void)=default
Default destructor.
ToggleButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc, const char *buttonText0, FontList::FontID Font0, uint16_t fillColor0, ButtonState::buttonCallbackFunction callback0, const char *buttonText1, FontList::FontID Font1, uint16_t fillColor1, ButtonState::buttonCallbackFunction callback1, uint16_t fontBaseline, const ButtonConfig_t< 2 > config=DEFAULT_BUTTON_CONFIG)
Constructor for a Toggle Button that displays text in each state.
Definition Button.h:329
ToggleButton(const char *buttonName, Canvas &Destination, const Block_Image &DestLoc, ROM_Image ButtonImg0, uint16_t ButtonImgColor0, uint16_t ButtonFillColor0, ButtonState::buttonCallbackFunction callback0, ROM_Image ButtonImg1, uint16_t ButtonImgColor1, uint16_t ButtonFillColor1, ButtonState::buttonCallbackFunction callback1, const ButtonConfig_t< 2 > config=DEFAULT_BUTTON_CONFIG)
Constructor for a Toggle Button that displays images in each state.
Definition Button.h:360
void begin(void) override
Initializes the button. This method should be called before first use.
Definition Button.cpp:133
Structure to define sub-images. Generally associated with a ROM_Image.
Definition ROM_Images.h:22
Generic data structure used to store graphical configuration of buttons.
Definition Button.h:16
uint8_t borderWidth
Definition Button.h:17
unsigned short cornerRadius
Definition Button.h:19
uint16_t borderColor
Definition Button.h:18
Configuration information for a TextBox object.
Definition Definitions.h:38
Definition TFTM070A1.h:435
Structure to define the size and location of an image in Flash.
Definition ROM_Images.h:14