LiquidMenu  1.2.0
Menu creation Arduino library for LCDs, wraps LiquidCrystal.
LiquidMenu.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2016 Vasil Kalchev
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
41 #pragma once
42 
43 #include <LiquidCrystal.h>
44 #include <inttypes.h>
45 #include <avr/pgmspace.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include "LiquidMenu_config.h"
50 #include "LiquidMenu_debug.h"
51 
52 #ifndef LiquidCrystal_h
53  #warning "LiquidMenu: LiquidCrystal library is required!"
54 #endif
55 
56 #if LIQUIDMENU_DEBUG
57  #warning "LiquidMenu: Debugging messages are enabled."
58 #endif
59 
60 const char LIQUIDMENU_VERSION[] = "1.2";
61 
63 
66 enum class DataType : uint8_t {
67  NOT_USED = 0,
68  BOOL = 1, BOOLEAN = 1,
69  INT8_T = 8,
70  UINT8_T = 9, BYTE = 9,
71  INT16_T = 16,
72  UINT16_T = 17,
73  INT32_T = 32,
74  UINT32_T = 33,
75  FLOAT = 50, DOUBLE = 50,
76  CHAR = 60,
77  CHAR_PTR = 61,
78  CONST_CHAR_PTR = 62,
79  PROG_CONST_CHAR_PTR = 65,
80  GLYPH = 70,
81 };
82 
84 /*
85 Used to store and set the relative or absolute position of the focus indicator.
86 */
87 enum class Position : uint8_t {
88  RIGHT = 1, NORMAL = 1,
89  LEFT = 2,
90  CUSTOM = 3,
91 };
92 
94 
98 
104 DataType recognizeType(bool variable);
105 
110 DataType recognizeType(char variable);
111 
116 DataType recognizeType(char* variable);
117 
122 DataType recognizeType(const char* variable);
123 
128 DataType recognizeType(int8_t variable);
129 
134 DataType recognizeType(uint8_t variable);
135 
140 DataType recognizeType(int16_t variable);
141 
146 DataType recognizeType(uint16_t variable);
147 
152 DataType recognizeType(int32_t variable);
153 
158 DataType recognizeType(uint32_t variable);
159 
164 DataType recognizeType(float variable);
165 
170 DataType recognizeType(double variable);
172 
173 
175 
180 void print_me(uintptr_t address);
181 
182 
184 
191 class LiquidLine {
192  friend class LiquidScreen;
193 
194 public:
197 
199 
205  LiquidLine(uint8_t column, uint8_t row)
206  : _row(row), _column(column), _focusRow(row - 1),
207  _focusColumn(column - 1), _focusPosition(Position::NORMAL),
208  _variableCount(0), _focusable(false) {
209  for (uint8_t i = 0; i < MAX_VARIABLES; i++) {
210  _variable[i] = nullptr;
211  _variableType[i] = DataType::NOT_USED;
212  }
213  for (uint8_t f = 0; f < MAX_FUNCTIONS; f++) {
214  _function[f] = 0;
215  }
216  }
217 
219 
224  template <typename A>
225  LiquidLine(uint8_t column, uint8_t row, A &variableA)
226  : LiquidLine(column, row) {
227  add_variable(variableA);
228  }
229 
231 
237  template <typename A, typename B>
238  LiquidLine(uint8_t column, uint8_t row,
239  A &variableA, B &variableB)
240  : LiquidLine(column, row, variableA) {
241  add_variable(variableB);
242  }
243 
245 
252  template <typename A, typename B, typename C>
253  LiquidLine(uint8_t column, uint8_t row,
254  A &variableA, B &variableB, C &variableC)
255  : LiquidLine(column, row, variableA, variableB) {
256  add_variable(variableC);
257  }
258 
260 
268  template <typename A, typename B, typename C, typename D>
269  LiquidLine(uint8_t column, uint8_t row,
270  A &variableA, B &variableB, C &variableC, D &variableD)
271  : LiquidLine(column, row, variableA, variableB, variableC) {
272  add_variable(variableD);
273  }
274 
276 
277 
280 
282 
293  template <typename T>
294  bool add_variable(T &variable) {
295  print_me((uintptr_t)this);
296  if (_variableCount < MAX_VARIABLES) {
297  _variable[_variableCount] = (void*)&variable;
298  _variableType[_variableCount] = recognizeType(variable);
299  DEBUG(F("Added variable '")); DEBUG(variable); DEBUGLN(F("'"));
300  _variableCount++;
301  return true;
302  }
303  DEBUG(F("Adding variable ")); DEBUG(variable);
304  DEBUGLN(F(" failed, edit LiquidMenu_config.h to allow for more variables"));
305  return false;
306  }
307 
309 
327  bool attach_function(uint8_t number, void (*function)(void));
328 
330 
346  bool set_focusPosition(Position position,
347  uint8_t column = 0, uint8_t row = 0);
348 
350 
359  bool set_asGlyph(uint8_t number);
360 
362 
370  bool set_asProgmem(uint8_t number);
372 
373 private:
375 
383  void print(LiquidCrystal *p_liquidCrystal, bool isFocused);
384 
386 
393  void print_variable(LiquidCrystal *p_liquidCrystal, uint8_t number);
394 
396 
404  bool call_function(uint8_t number) const;
405 
406  uint8_t _row, _column, _focusRow, _focusColumn;
407  Position _focusPosition;
408  uint8_t _variableCount;
409  void (*_function[MAX_FUNCTIONS])(void);
410  const void *_variable[MAX_VARIABLES];
411  DataType _variableType[MAX_VARIABLES];
412  bool _focusable;
413 };
414 
415 
417 
426  friend class LiquidMenu;
427 
428 public:
429 
432 
434 
437  LiquidScreen();
438 
440 
443  explicit LiquidScreen(LiquidLine &liquidLine);
444 
446 
450  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2);
451 
453 
458  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
459  LiquidLine &liquidLine3);
460 
462 
468  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
469  LiquidLine &liquidLine3, LiquidLine &liquidLine4);
470 
472 
475 
477 
488  bool add_line(LiquidLine &liquidLine);
489 
491 
505  bool set_focusPosition(Position position);
506 
508 
519  void hide(bool hide);
521 
522 private:
524 
530  void print(LiquidCrystal *p_liquidCrystal) const;
531 
533 
539  void switch_focus(bool forward = true);
540 
542 
552  bool call_function(uint8_t number) const;
553 
554  LiquidLine *_p_liquidLine[MAX_LINES];
555  uint8_t _lineCount;
556  uint8_t _focus;
557  bool _hidden;
558 };
559 
560 
562 
571 class LiquidMenu {
572  friend class LiquidSystem;
573 
574 public:
575 
578 
580 
587  LiquidMenu(LiquidCrystal &liquidCrystal, uint8_t startingScreen = 1);
588 
590 
596  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen,
597  uint8_t startingScreen = 1);
598 
600 
607  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
608  LiquidScreen &liquidScreen2, uint8_t startingScreen = 1);
609 
611 
619  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
620  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
621  uint8_t startingScreen = 1);
622 
624 
633  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
634  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
635  LiquidScreen &liquidScreen4, uint8_t startingScreen = 1);
636 
638 
641 
643 
654  bool add_screen(LiquidScreen &liquidScreen);
655 
657  void next_screen();
658 
660 
663  void operator++();
664 
666 
669  void operator++(int);
670 
672  void previous_screen();
673 
675 
678  void operator--();
679 
681 
684  void operator--(int);
685 
687 
691  bool change_screen(LiquidScreen &p_liquidScreen);
692 
694 
699  bool change_screen(uint8_t number);
700 
702 
706  bool operator=(LiquidScreen &p_liquidScreen);
707 
709 
714  bool operator=(uint8_t number);
715 
717 
723  void switch_focus(bool forward = true);
724 
726 
740  bool set_focusPosition(Position position);
741 
743 
756  bool set_focusSymbol(Position position, uint8_t symbol[8]);
757 
759 
770  bool call_function(uint8_t number) const;
771 
773 
776  void update() const;
777 
779 
785  void softUpdate() const;
786 
788 
789 private:
790  LiquidCrystal *_p_liquidCrystal;
791  LiquidScreen *_p_liquidScreen[MAX_SCREENS];
792  uint8_t _screenCount;
793  uint8_t _currentScreen;
794 };
795 
796 
798 
809 public:
810 
813 
815 
820  explicit LiquidSystem(uint8_t startingMenu = 1);
821 
823 
828  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
829  uint8_t startingMenu = 1);
830 
832 
838  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
839  LiquidMenu &liquidMenu3, uint8_t startingMenu = 1);
840 
842 
849  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
850  LiquidMenu &liquidMenu3, LiquidMenu &liquidMenu4,
851  uint8_t startingMenu = 1);
852 
854 
857 
859 
870  bool add_menu(LiquidMenu &liquidMenu);
871 
873 
877  bool change_menu(LiquidMenu &p_liquidMenu);
878 
880  void next_screen();
881 
883 
886  void operator++();
887 
889 
892  void operator++(int);
893 
895  void previous_screen();
896 
898 
901  void operator--();
902 
904 
907  void operator--(int);
908 
910 
914  bool change_screen(LiquidScreen &p_liquidScreen);
915 
917 
922  bool change_screen(uint8_t number);
923 
925 
929  bool operator=(LiquidScreen &p_liquidScreen);
930 
932 
937  bool operator=(uint8_t number);
938 
940 
946  void switch_focus(bool forward = true);
947 
949 
963  bool set_focusPosition(Position position);
964 
966 
979  bool set_focusSymbol(Position position, uint8_t symbol[8]);
980 
982 
993  bool call_function(uint8_t number) const;
994 
996 
999  void update() const;
1000 
1002 
1008  void softUpdate() const;
1009 
1011 
1012 private:
1013  LiquidMenu *_p_liquidMenu[MAX_MENUS];
1014  uint8_t _menuCount;
1015  uint8_t _currentMenu;
1016 };
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC)
Constructor for three variables/constants.
Definition: LiquidMenu.h:253
void operator--()
Switches to the previous screen.
Definition: LiquidSystem.cpp:102
void switch_focus(bool forward=true)
Switches the focus.
Definition: LiquidMenu.cpp:165
bool operator=(LiquidScreen &p_liquidScreen)
Switches to the specified screen.
Definition: LiquidSystem.cpp:122
Represents a collection of menus forming a menu system.
Definition: LiquidMenu.h:808
Represents the individual lines printed on the display.
Definition: LiquidMenu.h:191
void update() const
Prints the current screen to the display.
Definition: LiquidSystem.cpp:145
void softUpdate() const
Prints the current screen to the display (without clearing).
Definition: LiquidSystem.cpp:149
void switch_focus(bool forward=true)
Switches the focus.
Definition: LiquidSystem.cpp:126
bool add_variable(T &variable)
Adds a variable to the line.
Definition: LiquidMenu.h:294
const uint8_t MAX_SCREENS
Configures the number of available screens per menu.
Definition: LiquidMenu_config.h:21
const uint8_t MAX_VARIABLES
Configures the number of available variables per line.
Definition: LiquidMenu_config.h:12
void previous_screen()
Switches to the previous screen.
Definition: LiquidSystem.cpp:98
bool set_focusPosition(Position position)
Sets the focus position for the whole menu at once.
Definition: LiquidMenu.cpp:171
bool set_focusPosition(Position position)
Sets the focus position for the whole menu at once.
Definition: LiquidSystem.cpp:130
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB)
Constructor for two variables/constants.
Definition: LiquidMenu.h:238
Represents a screen shown on the display.
Definition: LiquidMenu.h:425
#define DEBUG(x)
Normal print.
Definition: LiquidMenu_debug.h:23
bool set_focusSymbol(Position position, uint8_t symbol[8])
Changes the focus indicator&#39;s symbol.
Definition: LiquidSystem.cpp:134
bool call_function(uint8_t number) const
Calls an attached function specified by the number.
Definition: LiquidSystem.cpp:138
const uint8_t MAX_LINES
Configures the number of available lines per screen.
Definition: LiquidMenu_config.h:18
bool change_screen(LiquidScreen &p_liquidScreen)
Switches to the specified screen.
Definition: LiquidSystem.cpp:114
LiquidLine(uint8_t column, uint8_t row)
The main constructor.
Definition: LiquidMenu.h:205
const char LIQUIDMENU_VERSION[]
The version of the library.
Definition: LiquidMenu.h:60
Represents a collection of screens forming a menu.
Definition: LiquidMenu.h:571
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC, D &variableD)
Constructor for four variables/constants.
Definition: LiquidMenu.h:269
Position
Position enum.
Definition: LiquidMenu.h:87
DataType recognizeType(bool variable)
Definition: recognizeType.cpp:9
void next_screen()
Switches to the next screen.
Definition: LiquidSystem.cpp:86
DataType
Data type enum.
Definition: LiquidMenu.h:66
void print_me(uintptr_t address)
Prints the number passed to it in a specific way.
Definition: LiquidLine.cpp:32
const uint8_t MAX_MENUS
Configures the number of available menus per menus system.
Definition: LiquidMenu_config.h:24
bool call_function(uint8_t number) const
Calls an attached function specified by the number.
Definition: LiquidMenu.cpp:214
const uint8_t MAX_FUNCTIONS
Configures the number of available functions per line.
Definition: LiquidMenu_config.h:15
LiquidLine(uint8_t column, uint8_t row, A &variableA)
Constructor for one variable/constant.
Definition: LiquidMenu.h:225
#define DEBUGLN(x)
Print with newline.
Definition: LiquidMenu_debug.h:25
void operator++()
Switches to the next screen.
Definition: LiquidSystem.cpp:90
bool set_focusPosition(Position position)
Sets the focus position for the whole screen at once.
Definition: LiquidScreen.cpp:71