LVGLPlusPlus
C++ library on top of the lovely LVGL project.
Loading...
Searching...
No Matches
lvppBase.h
Go to the documentation of this file.
1// Copyright 2023 Robert M. Wolff (bob dot wolff 68 at gmail dot com)
2//
3// Redistribution and use in source and binary forms, with or without modification,
4// are permitted provided that the following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation and/or
11// other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its contributors
14// may be used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27//
28#ifndef _LVPPBASE_H
29#define _LVPPBASE_H
30
31#include <vector>
32#include <utility> // For std::pair
33#include <string>
34#include <functional>
35
36#ifdef LV_LVGL_H_INCLUDE_SIMPLE
37#include "lvgl.h"
38#else
39#include "lvgl/lvgl.h"
40#endif
41
47static std::string eventNames[_LV_EVENT_LAST];
48
70class lvppBase {
71public:
80 lvppBase(const char* fName, const char* oType);
81 ~lvppBase();
94 void createObj(lv_obj_t* o);
101 static const char* getEventName(lv_event_code_t code);
108 static bool isUnknownCode(lv_event_code_t code);
116 lv_obj_t* getObj(void) { return obj; };
124 lv_obj_t* getLabelObj(void) { return label; };
134 virtual void setSize(lv_coord_t width, lv_coord_t height);
145 void align(lv_align_t align, lv_coord_t x_ofs, lv_coord_t y_ofs);
156 void setFontSize(uint8_t points);
163 void setFont(const lv_font_t* pF);
169 void setBGColor(lv_color_t color);
179 void setColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction);
185 virtual void setText(const char* pText);
191 std::string getText();
202 void setTextAlign(lv_align_t align, lv_coord_t xoff, lv_coord_t yoff);
208 void setTextColor(lv_color_t newColor);
214 void setLabelJustificationAlignment(lv_text_align_t _align);
222 void setLabelColorizationEnabled(bool bEnable=true);
223
233 virtual void setAdjText(const char* pText, lv_coord_t x_ofs=-10000, lv_coord_t y_ofs=-10000);
239 void setAdjTextColor(lv_color_t newColor);
245 void setAdjBGColor(lv_color_t color);
253 void setAdjColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction);
260 void setAdjFont(const lv_font_t* pF);
266 void setAdjJustificationAlignment(lv_text_align_t _align);
271 virtual void onClicked() { };
277 virtual void internalOnClicked() { };
282 virtual void onValueChanged() { };
288 virtual void internalOnValueChanged() { };
295 void setFriendlyName(const char* pName);
301 std::string getFriendlyName() { return friendlyName; };
309 std::string getObjType() { return objType; };
315 const char* whoAmI(void);
326 void setCallbackOnClicked(std::function<void()> cbF) {
327 assert(!cbOnClicked);
328 assert(cbF);
329 cbOnClicked = cbF;
330 };
341 void setCallbackOnValueChanged(std::function<void()> cbF) {
342 assert(!cbOnValueChanged);
343 assert(cbF);
344 cbOnValueChanged = cbF;
345 };
346
353 virtual void setNewParent(lv_obj_t* pNewParent);
354protected:
355 lv_obj_t* label;
356 lv_obj_t* adjLabel;
357 std::function<void()> cbOnClicked=nullptr;
358 std::function<void()> cbOnValueChanged=nullptr;
359 lv_obj_t* obj;
360 lv_obj_t* objParent;
361 std::string objType;
362 std::string friendlyName;
363 lv_style_t style_obj;
364 void setObjType(const char* pType) { objType = pType; };
365
371 void baseEventHandler(lv_event_t* event);
377 virtual void eventHandler(lv_event_t* event) { };
384 static void lvCallback(lv_event_t* event);
389 static void initEventNames(void);
390
392private:
393 void createLabel();
394 void createAdjLabel();
395};
396
397//
398//
399// l v p p B a s e W i t h V a l u e
400//
401//
402
409public:
419 lvppBaseWithValue(const char* fName, const char* oType);
436 virtual void setValue(int16_t value, bool animate=true);
442 int16_t getValue(void) { return curValue; };
449 uint16_t getValuePercentage(void) { return 100 * (curValue-min) / (max-min); };
457 void enableValueLabel(lv_coord_t xoff, lv_coord_t yoff, lv_align_t alignment=LV_ALIGN_CENTER);
466 void setValueLabelFormat(const char* fmt);
472 void setValueLabelColor(lv_color_t newColor);
478 void setValueLabelBGColor(lv_color_t newColor);
486 void setValueLabelColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction);
492 void setValueLabelJustificationAlignment(lv_text_align_t _align);
498 void setValueLabelFont(const lv_font_t* pF);
505 void setValueRange(int16_t _min, int16_t _max) { min = _min; max = _max; };
512 virtual void setNewParent(lv_obj_t* pNewParent);
513protected:
520 virtual int16_t baseGetter() = 0;
528 virtual void baseSetter(int16_t nVal, bool animate=true) = 0;
530 int16_t curValue;
531 lv_obj_t* valueLabel;
532 std::string valueLabelFormat;
533 int16_t min, max;
534private:
535 void createValueLabel();
536};
537
548public:
554 virtual void setOptions(const char* pOpts);
560 virtual void setOptions(std::vector<std::string>& _opts);
568 virtual void setOptionsWithIDs(std::vector<std::pair<std::string, uint64_t>>& valIDs);
574 virtual void addOption(const char* pOpt);
582 virtual void addOptionWithID(const char* pOpt, uint64_t id);
588 virtual void addOption(std::string& _opt);
596 virtual void addOptionWithID(std::string& _opt, uint64_t id);
604 virtual void clearOptions();
611 virtual uint64_t getSelectedID();
612protected:
621 virtual void lvOptionSetter(const char* pOpts) = 0;
630 virtual uint16_t lvOptionGetIndex() = 0;
631 std::string oneString;
632 std::vector<std::string> options;
633 std::vector<uint64_t> idList;
634 const char* getNewlineSepOptions();
635};
636
637
638#endif
lvppBase is the root of the C++ library LVGLPlusPlus. It strives to create consistent ways to manipul...
Definition: lvppBase.h:70
virtual void onValueChanged()
Callback that derived objects can use to get notification when the value changes of an object.
Definition: lvppBase.h:282
void setFriendlyName(const char *pName)
Set the Friendly Name of the object. This is the name given in the constructor's fName parameter and ...
Definition: lvppBase.cpp:346
static bool isUnknownCode(lv_event_code_t code)
Check to see if an event code is valid or not. Utility function.
Definition: lvppBase.cpp:398
static void initEventNames(void)
One-time initialization of the event names array.
Definition: lvppBase.cpp:364
void setFont(const lv_font_t *pF)
Set the Font to be used. This is a more complete/complex version of setFontSize()....
Definition: lvppBase.cpp:97
const char * whoAmI(void)
Utility useful for dumping the friendly name and object type if needed.
Definition: lvppBase.cpp:352
std::string getText()
Get the Text from the object/label and put it in a std::string.
Definition: lvppBase.cpp:200
void align(lv_align_t align, lv_coord_t x_ofs, lv_coord_t y_ofs)
Change the x/y location of the object.
Definition: lvppBase.cpp:185
void setObjType(const char *pType)
Definition: lvppBase.h:364
lv_obj_t * getLabelObj(void)
Get the label object.
Definition: lvppBase.h:124
std::function< void()> cbOnClicked
Start without a lambda callback.
Definition: lvppBase.h:357
void setCallbackOnClicked(std::function< void()> cbF)
Uses a lambda callback for when an object is clicked.
Definition: lvppBase.h:326
lv_obj_t * objParent
Any parent object (following same principle as LVGL here)
Definition: lvppBase.h:360
virtual void onClicked()
Callback that derived objects can use to get notification when the object is clicked.
Definition: lvppBase.h:271
lv_obj_t * label
Primary label.
Definition: lvppBase.h:355
void setFontSize(uint8_t points)
Set the Font Size for the object in simple 'points'.
Definition: lvppBase.cpp:57
void setAdjFont(const lv_font_t *pF)
Set the Font to be used for the adjacent text.
Definition: lvppBase.cpp:266
std::string friendlyName
Definition: lvppBase.h:362
static bool bEventNamesInitComplete
Status of the event names table to avoid re-processing.
Definition: lvppBase.h:391
virtual void internalOnValueChanged()
Internal version of the onValueChanged() callback which is used for other derived internal objects in...
Definition: lvppBase.h:288
void setLabelJustificationAlignment(lv_text_align_t _align)
Set the Label's text alignment (left/right/center)
Definition: lvppBase.cpp:223
std::string getFriendlyName()
Get the Friendly Name of the object.
Definition: lvppBase.h:301
void setAdjBGColor(lv_color_t color)
set the background color for the adjacent text
Definition: lvppBase.cpp:251
std::string objType
Definition: lvppBase.h:361
void setCallbackOnValueChanged(std::function< void()> cbF)
Uses a lambda callback for when an object value changes.
Definition: lvppBase.h:341
lv_obj_t * obj
The LVGL object that was created for this widget.
Definition: lvppBase.h:359
void setBGColor(lv_color_t color)
Set the background color of the object.
Definition: lvppBase.cpp:105
void setAdjColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction)
Sets a Color Gradient from col1 to col2 in a vertical or horizontal direction.
Definition: lvppBase.cpp:258
std::function< void()> cbOnValueChanged
Start without a lambda callback.
Definition: lvppBase.h:358
virtual void setSize(lv_coord_t width, lv_coord_t height)
Set the Size object. Most of the time, LVGL does a great job of setting a sane size,...
Definition: lvppBase.cpp:339
virtual void setAdjText(const char *pText, lv_coord_t x_ofs=-10000, lv_coord_t y_ofs=-10000)
Set the text for the adjacent text label. This label is not enabled by default. Setting this text wil...
Definition: lvppBase.cpp:235
static const char * getEventName(lv_event_code_t code)
Get the Event Name from a given event code. Translate it to text, essentially.
Definition: lvppBase.cpp:402
virtual void setNewParent(lv_obj_t *pNewParent)
Changes the parent object on the fly. Used primarily for lvppScreen::addObject(). Sets parents of adj...
Definition: lvppBase.cpp:419
virtual void eventHandler(lv_event_t *event)
Any events not handled by baseEventHandler come here. Can be overridden by a derived class.
Definition: lvppBase.h:377
std::string getObjType()
Get the Obj Type.
Definition: lvppBase.h:309
void createObj(lv_obj_t *o)
Create the handlers and store the newly created object.
Definition: lvppBase.cpp:278
void setAdjTextColor(lv_color_t newColor)
Set the Adjacent text color.
Definition: lvppBase.cpp:245
void setTextColor(lv_color_t newColor)
Set the Text Color of the primary label object.
Definition: lvppBase.cpp:215
virtual void internalOnClicked()
Internal version of the onClicked() callback which is used for other derived internal objects in the ...
Definition: lvppBase.h:277
void setColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction)
Sets a Color Gradient from col1 to col2 in a vertical or horizontal direction. The gradient is place ...
Definition: lvppBase.cpp:116
static void lvCallback(lv_event_t *event)
Static member to handle all inbound callback events. Used to "route" event handling to the appropriat...
Definition: lvppBase.cpp:295
void setAdjJustificationAlignment(lv_text_align_t _align)
Set the adjacent Label's text alignment (left/right/center)
Definition: lvppBase.cpp:272
void setTextAlign(lv_align_t align, lv_coord_t xoff, lv_coord_t yoff)
Set the Text Alignment. This allows the object's primary label to be moved from its 'center' position...
Definition: lvppBase.cpp:209
lv_style_t style_obj
Main style object - unsure if I'm handling all of the style bit correctly.
Definition: lvppBase.h:363
lv_obj_t * adjLabel
For items that have a label 'nearby' (adjacent label)
Definition: lvppBase.h:356
~lvppBase()
Definition: lvppBase.cpp:153
virtual void setText(const char *pText)
Set the Text for the primary text label. For a button, for instance, this is the text on top of the b...
Definition: lvppBase.cpp:190
void setLabelColorizationEnabled(bool bEnable=true)
Turn on or off inline text colorization. To use colors inline, simply use #RRGGBB in the text where t...
Definition: lvppBase.cpp:229
lv_obj_t * getObj(void)
Get the Obj object.
Definition: lvppBase.h:116
void baseEventHandler(lv_event_t *event)
Main event handler loop for all events. This handler makes calls for onClicked() and onValueChanged()
Definition: lvppBase.cpp:303
Derivation from lvppBase. Those widgets which have an inherant 'value' to show are based on this slig...
Definition: lvppBase.h:408
virtual void setValue(int16_t value, bool animate=true)
Set the Value associated with the object. This is a pure virtual function and must be overridden and ...
Definition: lvppBase.cpp:452
virtual int16_t baseGetter()=0
Derived classes must implement this pure virtual by getting the current value from the widget itself....
int16_t min
Definition: lvppBase.h:533
virtual void baseSetter(int16_t nVal, bool animate=true)=0
Derived classes must implement this pure virtual which sets the current value of the widget itself....
lv_obj_t * valueLabel
The label that receives the formatted value if the label is enabled.
Definition: lvppBase.h:531
std::string valueLabelFormat
The format to use when the value changes.
Definition: lvppBase.h:532
uint16_t getValuePercentage(void)
Get the 'percentage' Value from the object. This is the percentage represented from min to max of the...
Definition: lvppBase.h:449
int16_t getValue(void)
Get the Value from the object.
Definition: lvppBase.h:442
void setValueRange(int16_t _min, int16_t _max)
Set the Value Range allowed. NOTE: This enforcement needs to happen in the derived setValue() functio...
Definition: lvppBase.h:505
void enableValueLabel(lv_coord_t xoff, lv_coord_t yoff, lv_align_t alignment=LV_ALIGN_CENTER)
In the case where a label is desired to be shown for the value, enable it and align it here.
Definition: lvppBase.cpp:467
void setValueLabelFormat(const char *fmt)
Set the Value Label Format. This 'format' allows the label to be more than simply a number....
Definition: lvppBase.cpp:481
int16_t max
The allowable range of the value.
Definition: lvppBase.h:533
void setValueLabelBGColor(lv_color_t newColor)
Set the Value Label's background Color.
Definition: lvppBase.cpp:496
void internalOnValueChanged()
Internal handler for value change. Does the formatting as well.
Definition: lvppBase.cpp:525
virtual void setNewParent(lv_obj_t *pNewParent)
Changes the parent object on the fly. Used primarily for lvppScreen::addObject(). Sets parents of adj...
Definition: lvppBase.cpp:534
int16_t curValue
The value we're keeping and handling in this class.
Definition: lvppBase.h:530
void setValueLabelFont(const lv_font_t *pF)
Set the Value Label Font.
Definition: lvppBase.cpp:472
void setValueLabelColorGradient(lv_color_t col1, lv_color_t col2, lv_grad_dir_t direction)
Sets a Color Gradient from col1 to col2 in a vertical or horizontal direction.
Definition: lvppBase.cpp:506
void setValueLabelJustificationAlignment(lv_text_align_t _align)
Set the value Label's text alignment (left/right/center)
Definition: lvppBase.cpp:516
void setValueLabelColor(lv_color_t newColor)
Set the Value Label's text Color.
Definition: lvppBase.cpp:487
Utility class to aid in consistent handling of all lv_ widgets which have a list of choices....
Definition: lvppBase.h:547
std::vector< uint64_t > idList
The vector of associated ID values (if used)
Definition: lvppBase.h:633
virtual void addOption(const char *pOpt)
Add a single option to the options list.
Definition: lvppBase.cpp:606
virtual void setOptionsWithIDs(std::vector< std::pair< std::string, uint64_t > > &valIDs)
Set the Options along with a related ID. This is intended to give the user a more meaningful item to ...
Definition: lvppBase.cpp:587
const char * getNewlineSepOptions()
Packs all the option strings into a single newline separated pointer.
Definition: lvppBase.cpp:643
virtual uint16_t lvOptionGetIndex()=0
This is the pure virtual method which must be implemented by any class that utilizes lvppOptions....
virtual void lvOptionSetter(const char *pOpts)=0
This is the pure virtual which must be implemented by any class that utilizes lvppOptions....
virtual uint64_t getSelectedID()
Get the ID associated with the currently selected item. Using this method only makes sense if the lis...
Definition: lvppBase.cpp:639
std::vector< std::string > options
The vector of options kept locally.
Definition: lvppBase.h:632
std::string oneString
Temp area for getNewlineSepOptions()
Definition: lvppBase.h:631
virtual void clearOptions()
Removes all entries from the options list. NOTE: Method does not clear the LVGL widget....
Definition: lvppBase.cpp:634
virtual void addOptionWithID(const char *pOpt, uint64_t id)
Add a single option to the options list and pair it with a meaningful ID. To be used in conjunction w...
Definition: lvppBase.cpp:620
virtual void setOptions(const char *pOpts)
Set the Options all in one shot from a const char*.
Definition: lvppBase.cpp:550