Solo Controller Documentation 1.0
Documentation for the Solo Controller
 
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1/*******************************************************************************
2 * @file Button.h
3 * @brief Defines the Button classes used by the Menu system
4 ******************************************************************************/
5
6#ifndef BUTTON_INCLUDED
7#define BUTTON_INCLUDED
8
9#include "Canvas.h"
10
11/******************************************************************************/
12/*** ***/
13/*** Class Definition: Button ***/
14/*** ***/
15/******************************************************************************
16 * @brief Provides buttons for the menu system
17 * @details
18 * This is the base class for all buttons. Derived classes are expected to
19 * provide the images for the button states. This class manages the button
20 * state and the copying of the button imagery onto the destination canvas.
21 * The derived classes are responsible for providing callback functions.
22 ******************************************************************************/
23class Button
24{
25
26 bool _enabled = false; // Determines whether the button is enabled
27 uint8_t _state = 0; // Tracks the state of the button
28 uint8_t _maxState; // The maximum allowed value for _state
29
30 // Destimation where the button should be copied
31 Canvas &_DestCanvas;
32 const uint16_t _DestX;
33 const uint16_t _DestY;
34
35 // Array of canvases that store the imagery by each button state. These images are
36 // provided by derived classes. This class only uses these images.
37 const DRAM_Canvas *_Img;
38
39 // Internal method for copying the button image for the current _state onto the
40 // destination canvas
41 inline void _updateDestCanvas(void)
42 {
43 _Img[_state].BTE_CopyFrom(_Img[_state], 0, 0, _DestX, _DestY, width(), height());
44 }
45
46 // Validates that this object is well formed
47 bool _validateObject(void);
48
49public:
51 Button(void) = delete;
52
56
69 Button(Canvas &DestCanvas, const Block_Image &DestLoc, const DRAM_Canvas *Imgs, uint8_t maxNumStates)
70 : _maxState{maxNumStates},
71 _DestCanvas{DestCanvas},
72 _DestX{DestLoc.x},
73 _DestY{DestLoc.y},
74 _Img{Imgs} {}
75
81 ~Button(void) = default;
82
84
86 typedef void (*callback_function)(void);
87
91
97 void begin(void);
98
103 virtual bool press(FT5316::TouchLocation loc);
104
110 virtual void release(void) {};
111
117 void enable(void)
118 {
119 _enabled = true;
120 };
121
127 void disable(void)
128 {
129 _enabled = false;
130 };
131
133
137
144 inline bool isEnabled(void) const
145 {
146 return _enabled;
147 };
148
155 inline uint16_t width(void) const
156 {
157 return _Img[0].width();
158 }
159
166 inline uint16_t height(void) const
167 {
168 return _Img[0].height();
169 }
170
177 inline uint8_t state(void) const
178 {
179 return _state;
180 }
181
183
184protected:
190 void _resetToState0(void)
191 {
192 _state = 0;
193 _DestCanvas.BTE_CopyFrom(_Img[_state], _DestX, _DestY);
194 }
195};
196
197/******************************************************************************/
198/*** ***/
199/*** Class Definition: ToggleButton ***/
200/*** ***/
201/******************************************************************************
202 * @brief Defines a button that toggles between two states.
203 * @details
204 * Each state has its own image and callback function. The callback function
205 * is called when when the users releases the button.
206 ******************************************************************************/
207class ToggleButton : public Button
208{
209
210 // Canvases used to store the on/off button imagery. These images are copied
211 // onto _DestCanvas as needed.
212 static const uint8_t _NUM_STATES = 2;
213 const DRAM_Canvas _Img[_NUM_STATES];
214
215 // Two callbacks functions are used: Botton on/off
216 const callback_function _callback[_NUM_STATES];
217
218public:
220 ToggleButton(void) = delete;
221
225
239 ToggleButton(Canvas &DestCanvas, const Block_Image &DestLoc,
240 callback_function Img0Callback,
241 callback_function Img1Callback)
242 : Button(DestCanvas, DestLoc, _Img, _NUM_STATES),
243 _Img{DRAM_Canvas(DestCanvas), DRAM_Canvas(DestCanvas)},
244 _callback{Img0Callback, Img1Callback} {}
245
251 ~ToggleButton(void) = default;
252
254
258
263 bool press(FT5316::TouchLocation loc) override;
264
266};
267
268/******************************************************************************/
269/*** ***/
270/*** Class Definition: MomentaryButton ***/
271/*** ***/
272/******************************************************************************
273 * @brief Momentary buttons have 2 states. Upon release, the button returns to state 0.
274 * @details
275 * This button has two images (pressed and unpressed). The button's single
276 * callback function is called when the button is released.
277 ******************************************************************************/
278class MomentaryButton : public Button
279{
280
281 // Canvases used to store the on/off button imagery. These images are copied
282 // onto _DestCanvas as needed.
283 static const uint8_t _NUM_STATES = 2;
284 const DRAM_Canvas _Img[_NUM_STATES];
285
286 // A single callback is used
287 const callback_function _callback;
288
289public:
291 MomentaryButton(void) = delete;
292
296
309 MomentaryButton(Canvas &DestCanvas, const Block_Image &DestLoc,
310 callback_function callback)
311 : Button(DestCanvas, DestLoc, _Img, _NUM_STATES),
312 _Img{DRAM_Canvas(DestCanvas), DRAM_Canvas(DestCanvas)},
313 _callback{callback} {}
314
320 ~MomentaryButton(void) = default;
321
323
327
333 virtual void release(void)
334 {
335 if (state() == 1)
336 {
338 _callback();
339 }
340 };
341
343};
344
345/******************************************************************************/
346/*** ***/
347/*** Class Definition: ImmediateButton ***/
348/*** ***/
349/******************************************************************************
350 * @brief A single state button that calls the callback function as soon as it is pressed
351 * @note
352 * There is no imagery associated with an immediate button. It is assumed that
353 * the desitination canvas already has appropriate imagery in place to indicate
354 * to the user where the button is located.
355 ******************************************************************************/
356class ImmediateButton : public Button
357{
358
359 // Canvases used to store the on/off button imagery. These images are copied
360 // onto _DestCanvas as needed.
361 static const uint8_t _NUM_STATES = 1;
362 const DRAM_Canvas _Img[_NUM_STATES];
363
364 // Two callbacks are store (on and off)
365 const callback_function _callback;
366
367public:
369 ImmediateButton(void) = delete;
370
374
381 ImmediateButton(Canvas &DestCanvas, const Block_Image &DestLoc, callback_function callback)
382 : Button(DestCanvas, DestLoc, _Img, _NUM_STATES), _Img{DRAM_Canvas(DestCanvas, DestLoc)}, _callback{callback} {}
383
389 ~ImmediateButton(void) = default;
390
392
396
401 bool press(FT5316::TouchLocation loc) override;
402
404};
405
406#endif
void begin(void)
Initializes the button. This method should be called before first use.
Definition Button.cpp:20
void(* callback_function)(void)
Definition Button.h:86
uint8_t state(void) const
Returns the current state of the button.
Definition Button.h:177
Button(Canvas &DestCanvas, const Block_Image &DestLoc, const DRAM_Canvas *Imgs, uint8_t maxNumStates)
Constructor for creating a Button.
Definition Button.h:69
uint16_t height(void) const
Returns the height of the button in pixels.
Definition Button.h:166
void _resetToState0(void)
Internal method for resetting the button to state 0.
Definition Button.h:190
uint16_t width(void) const
Returns the width of the button in pixels.
Definition Button.h:155
void disable(void)
Disables the button, so that it ignores button presses.
Definition Button.h:127
void enable(void)
Enables the button, makking it accept button presses.
Definition Button.h:117
virtual bool press(FT5316::TouchLocation loc)
Presses the button if loc is within the bounds of the button.
Definition Button.cpp:47
virtual void release(void)
Releases the button if it has been pressed.
Definition Button.h:110
bool isEnabled(void) const
Indicates whether the Button is enabled.
Definition Button.h:144
~Button(void)=default
Default destructor.
Definition Canvas.h:33
void BTE_CopyFrom(const Canvas &source) const
Uses BTE engine to copy entire image from source canvas to this one.
Definition Canvas.cpp:75
Definition Canvas.h:411
ImmediateButton(Canvas &DestCanvas, const Block_Image &DestLoc, callback_function callback)
ImmediateButton Constructor.
Definition Button.h:381
bool press(FT5316::TouchLocation loc) override
Presses the button if loc is within the bounds of the button.
Definition Button.cpp:125
~ImmediateButton(void)=default
Default destructor.
MomentaryButton(Canvas &DestCanvas, const Block_Image &DestLoc, callback_function callback)
Constructor for creating a Momentary button.
Definition Button.h:309
~MomentaryButton(void)=default
Default destructor.
virtual void release(void)
Releases the button if it has been pressed.
Definition Button.h:333
bool press(FT5316::TouchLocation loc) override
Presses the button if loc is within the bounds of the button.
Definition Button.cpp:109
~ToggleButton(void)=default
Default destructor.
ToggleButton(Canvas &DestCanvas, const Block_Image &DestLoc, callback_function Img0Callback, callback_function Img1Callback)
ToggleButton Constructor.
Definition Button.h:239
Structure to define sub-images. Generally associated with a ROM_Image.
Definition Common.h:52
Definition TFTM070A1.h:435