@@ -6,35 +6,43 @@ namespace {
66 void upBtnEventHandler (lv_obj_t * obj, lv_event_t event) {
77 auto * widget = static_cast <Counter*>(obj->user_data );
88 if (event == LV_EVENT_SHORT_CLICKED || event == LV_EVENT_LONG_PRESSED_REPEAT) {
9- widget->Increment ();
9+ widget->UpBtnPressed ();
1010 }
1111 }
1212
1313 void downBtnEventHandler (lv_obj_t * obj, lv_event_t event) {
1414 auto * widget = static_cast <Counter*>(obj->user_data );
1515 if (event == LV_EVENT_SHORT_CLICKED || event == LV_EVENT_LONG_PRESSED_REPEAT) {
16- widget->Decrement ();
16+ widget->DownBtnPressed ();
1717 }
1818 }
1919}
2020
21- Counter::Counter (int min, int max) : min {min}, max {max} {
21+ Counter::Counter (int min, int max, lv_font_t & font ) : min {min}, max {max}, font {font } {
2222}
2323
24- void Counter::Increment () {
24+ void Counter::UpBtnPressed () {
2525 value++;
2626 if (value > max) {
2727 value = min;
2828 }
2929 UpdateLabel ();
30+
31+ if (ValueChangedHandler != nullptr ) {
32+ ValueChangedHandler (userData);
33+ }
3034};
3135
32- void Counter::Decrement () {
36+ void Counter::DownBtnPressed () {
3337 value--;
3438 if (value < min) {
3539 value = max;
3640 }
3741 UpdateLabel ();
42+
43+ if (ValueChangedHandler != nullptr ) {
44+ ValueChangedHandler (userData);
45+ }
3846};
3947
4048void Counter::SetValue (int newValue) {
@@ -58,7 +66,28 @@ void Counter::ShowControls() {
5866}
5967
6068void Counter::UpdateLabel () {
61- lv_label_set_text_fmt (number, " %.2i" , value);
69+ if (twelveHourMode) {
70+ if (value == 0 ) {
71+ lv_label_set_text_static (number, " 12" );
72+ } else if (value <= 12 ) {
73+ lv_label_set_text_fmt (number, " %.2i" , value);
74+ } else {
75+ lv_label_set_text_fmt (number, " %.2i" , value - 12 );
76+ }
77+ } else {
78+ lv_label_set_text_fmt (number, " %.2i" , value);
79+ }
80+ }
81+
82+ // Value is kept between 0 and 23, but the displayed value is converted to 12-hour.
83+ // Make sure to set the max and min values to 0 and 23. Otherwise behaviour is undefined
84+ void Counter::EnableTwelveHourMode () {
85+ twelveHourMode = true ;
86+ }
87+
88+ void Counter::SetValueChangedEventCallback (void * userData, void (*handler)(void * userData)) {
89+ this ->userData = userData;
90+ this ->ValueChangedHandler = handler;
6291}
6392
6493void Counter::Create () {
@@ -68,7 +97,7 @@ void Counter::Create() {
6897 lv_obj_set_style_local_bg_color (counterContainer, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, bgColor);
6998
7099 number = lv_label_create (counterContainer, nullptr );
71- lv_obj_set_style_local_text_font (number, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76 );
100+ lv_obj_set_style_local_text_font (number, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &font );
72101 lv_obj_align (number, nullptr , LV_ALIGN_CENTER, 0 , 0 );
73102 lv_obj_set_auto_realign (number, true );
74103 lv_label_set_text_static (number, " 00" );
0 commit comments