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 
39 #pragma once
40 
41 #include <LiquidCrystal.h>
42 
43 #include "LiquidMenu_config.h"
44 
45 #include "serial_debugging.h"
46 
47 #ifndef LiquidCrystal_h
48 #warning "LiquidCrystal library is required!"
49 #endif
50 
51 const char VERSION[] = "1.1";
52 
54 
57 enum class DataType : uint8_t {
58  NOT_USED = 0,
59  BOOL = 1, BOOLEAN = 1,
60  INT8_T = 8,
61  UINT8_T = 9, BYTE = 9,
62  INT16_T = 16,
63  UINT16_T = 17,
64  INT32_T = 32,
65  UINT32_T = 33,
66  FLOAT = 50, DOUBLE = 50,
67  CHAR = 60,
68  CHAR_PTR = 61,
69  CONST_CHAR_PTR = 62,
70  GLYPH = 70,
71 };
72 
74 /*
75 Used to store and set the relative or absolute position of the focus indicator.
76 */
77 enum class Position : uint8_t {
78  RIGHT = 1, NORMAL = 1,
79  LEFT = 2,
80  CUSTOM = 3,
81 };
82 
84 
88 
94 DataType recognizeType(bool variable);
95 
100 DataType recognizeType(char variable);
101 
106 DataType recognizeType(char* variable);
107 
112 DataType recognizeType(const char* variable);
113 
118 DataType recognizeType(int8_t variable);
119 
124 DataType recognizeType(uint8_t variable);
125 
130 DataType recognizeType(int16_t variable);
131 
136 DataType recognizeType(uint16_t variable);
137 
142 DataType recognizeType(int32_t variable);
143 
148 DataType recognizeType(uint32_t variable);
149 
154 DataType recognizeType(float variable);
155 
160 DataType recognizeType(double variable);
161 
163 
164 
166 
171 void print_me(uint16_t address);
172 
173 
175 
182 class LiquidLine {
183  friend class LiquidScreen;
184 
185 public:
188 
190 
196  LiquidLine(uint8_t column, uint8_t row)
197  : _row(row), _column(column), _focusRow(row - 1),
198  _focusColumn(column - 1), _focusPosition(Position::NORMAL),
199  _variableCount(0), _focusable(false) {
200  for (uint8_t i = 0; i < MAX_VARIABLES; i++) {
201  _variable[i] = nullptr;
202  _variableType[i] = DataType::NOT_USED;
203  }
204  for (uint8_t f = 0; f < MAX_FUNCTIONS; f++) {
205  _function[f] = 0;
206  }
207  }
208 
210 
215  template <typename A>
216  LiquidLine(uint8_t column, uint8_t row, A &variableA)
217  : LiquidLine(column, row) {
218  add_variable(variableA);
219  //_variable[0] = (void*)&variableA;
220  //_variableType[0] = recognizeType(variableA);
221  }
222 
224 
230  template <typename A, typename B>
231  LiquidLine(uint8_t column, uint8_t row,
232  A &variableA, B &variableB)
233  : LiquidLine(column, row, variableA) {
234  add_variable(variableB);
235  //_variable[1] = (void*)&variableB;
236  //_variableType[1] = recognizeType(variableB);
237  }
238 
240 
247  template <typename A, typename B, typename C>
248  LiquidLine(uint8_t column, uint8_t row,
249  A &variableA, B &variableB, C &variableC)
250  : LiquidLine(column, row, variableA, variableB) {
251  add_variable(variableC);
252  //_variable[2] = (void*)&variableC;
253  //_variableType[2] = recognizeType(variableC);
254  }
255 
257 
265  template <typename A, typename B, typename C, typename D>
266  LiquidLine(uint8_t column, uint8_t row,
267  A &variableA, B &variableB, C &variableC, D &variableD)
268  : LiquidLine(column, row, variableA, variableB, variableC) {
269  add_variable(variableD);
270  //_variable[3] = (void*)&variableD;
271  //_variableType[3] = recognizeType(variableD);
272  }
273 
275 
276 
279 
281 
292  template <typename T>
293  bool add_variable(T &variable) {
294  print_me((uint16_t)this);
295  if (_variableCount < MAX_VARIABLES) {
296  _variable[_variableCount] = (void*)&variable;
297  _variableType[_variableCount] = recognizeType(variable);
298  DEBUG(F("Added variable '")); DEBUG(variable); DEBUGLN(F("'"));
299  _variableCount++;
300  return true;
301  }
302  DEBUG(F("Adding variable ")); DEBUG(variable);
303  DEBUGLN(F(" failed, edit LiquidMenu_config.h to allow for more variables"));
304  return false;
305  }
306 
308 
326  bool attach_function(uint8_t number, void (*function)(void));
327 
329 
345  bool set_focusPosition(Position position,
346  uint8_t column = 0, uint8_t row = 0);
347 
349 
358  bool set_asGlyph(uint8_t number);
360 
361 private:
363 
371  void print(LiquidCrystal *p_liquidCrystal, bool isFocused);
372 
374 
381  void print_variable(LiquidCrystal *p_liquidCrystal, uint8_t number);
382 
384 
392  bool call_function(uint8_t number) const;
393 
394  uint8_t _row, _column, _focusRow, _focusColumn;
395  Position _focusPosition;
396  uint8_t _variableCount;
397  void (*_function[MAX_FUNCTIONS])(void);
398  const void *_variable[MAX_VARIABLES];
399  DataType _variableType[MAX_VARIABLES];
400  bool _focusable;
401 };
402 
403 
405 
414  friend class LiquidMenu;
415 
416 public:
417 
420 
422 
425  LiquidScreen();
426 
428 
431  LiquidScreen(LiquidLine &liquidLine);
432 
434 
438  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2);
439 
441 
446  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
447  LiquidLine &liquidLine3);
448 
450 
456  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
457  LiquidLine &liquidLine3, LiquidLine &liquidLine4);
458 
460 
463 
465 
476  bool add_line(LiquidLine &liquidLine);
477 
479 
493  bool set_focusPosition(Position position);
494 
496 
507  void hide(bool hide);
509 
510 private:
512 
518  void print(LiquidCrystal *p_liquidCrystal) const;
519 
521 
527  void switch_focus(bool forward = true);
528 
530 
540  bool call_function(uint8_t number) const;
541 
542  LiquidLine *_p_liquidLine[MAX_LINES];
543  uint8_t _lineCount;
544  uint8_t _focus;
545  bool _hidden;
546 };
547 
548 
550 
559 class LiquidMenu {
560  friend class LiquidSystem;
561 
562 public:
563 
566 
568 
575  LiquidMenu(LiquidCrystal &liquidCrystal, uint8_t startingScreen = 1);
576 
578 
584  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen,
585  uint8_t startingScreen = 1);
586 
588 
595  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
596  LiquidScreen &liquidScreen2, uint8_t startingScreen = 1);
597 
599 
607  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
608  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
609  uint8_t startingScreen = 1);
610 
612 
621  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
622  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
623  LiquidScreen &liquidScreen4, uint8_t startingScreen = 1);
624 
626 
629 
631 
642  bool add_screen(LiquidScreen &liquidScreen);
643 
645  void next_screen();
646 
648 
651  void operator++();
652 
654 
657  void operator++(int);
658 
660  void previous_screen();
661 
663 
666  void operator--();
667 
669 
672  void operator--(int);
673 
675 
679  bool change_screen(LiquidScreen &p_liquidScreen);
680 
682 
687  bool change_screen(uint8_t number);
688 
690 
694  bool operator=(LiquidScreen &p_liquidScreen);
695 
697 
702  bool operator=(uint8_t number);
703 
705 
711  void switch_focus(bool forward = true);
712 
714 
728  bool set_focusPosition(Position position);
729 
731 
744  bool set_focusSymbol(Position position, uint8_t symbol[8]);
745 
747 
758  bool call_function(uint8_t number) const;
759 
761 
764  void update() const;
765 
767 
773  void softUpdate() const;
774 
776 
777 private:
778  LiquidCrystal *_p_liquidCrystal;
779  LiquidScreen *_p_liquidScreen[MAX_SCREENS];
780  uint8_t _screenCount;
781  uint8_t _currentScreen;
782 };
783 
784 
786 
797 public:
798 
801 
803 
808  LiquidSystem(uint8_t startingMenu = 1);
809 
811 
816  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
817  uint8_t startingMenu = 1);
818 
820 
826  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
827  LiquidMenu &liquidMenu3, uint8_t startingMenu = 1);
828 
830 
837  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
838  LiquidMenu &liquidMenu3, LiquidMenu &liquidMenu4,
839  uint8_t startingMenu = 1);
840 
842 
845 
847 
858  bool add_menu(LiquidMenu &liquidMenu);
859 
861 
865  bool change_menu(LiquidMenu &p_liquidMenu);
866 
868  void next_screen();
869 
871 
874  void operator++();
875 
877 
880  void operator++(int);
881 
883  void previous_screen();
884 
886 
889  void operator--();
890 
892 
895  void operator--(int);
896 
898 
902  bool change_screen(LiquidScreen &p_liquidScreen);
903 
905 
910  bool change_screen(uint8_t number);
911 
913 
917  bool operator=(LiquidScreen &p_liquidScreen);
918 
920 
925  bool operator=(uint8_t number);
926 
928 
934  void switch_focus(bool forward = true);
935 
937 
951  bool set_focusPosition(Position position);
952 
954 
967  bool set_focusSymbol(Position position, uint8_t symbol[8]);
968 
970 
981  bool call_function(uint8_t number) const;
982 
984 
987  void update() const;
988 
990 
996  void softUpdate() const;
997 
999 
1000 private:
1001  LiquidMenu *_p_liquidMenu[MAX_MENUS];
1002  uint8_t _menuCount;
1003  uint8_t _currentMenu;
1004 };
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC)
Constructor for three variables/constants.
Definition: LiquidMenu.h:248
void operator--()
Switches to the previous screen.
Definition: LiquidSystem.cpp:97
void switch_focus(bool forward=true)
Switches the focus.
Definition: LiquidMenu.cpp:160
bool operator=(LiquidScreen &p_liquidScreen)
Switches to the specified screen.
Definition: LiquidSystem.cpp:117
Represents a collection of menus forming a menu system.
Definition: LiquidMenu.h:796
const char VERSION[]
The version of the library.
Definition: LiquidMenu.h:51
void softUpdate() const
Prints the current screen to the display (without clearing).
Definition: LiquidSystem.cpp:144
Definition: symbols.h:3
Represents the individual lines printed on the display.
Definition: LiquidMenu.h:182
void switch_focus(bool forward=true)
Switches the focus.
Definition: LiquidSystem.cpp:121
bool add_variable(T &variable)
Adds a variable to the line.
Definition: LiquidMenu.h:293
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 numer of available variables per line.
Definition: LiquidMenu_config.h:12
void previous_screen()
Switches to the previous screen.
Definition: LiquidSystem.cpp:93
bool set_focusPosition(Position position)
Sets the focus position for the whole menu at once.
Definition: LiquidMenu.cpp:166
bool set_focusPosition(Position position)
Sets the focus position for the whole menu at once.
Definition: LiquidSystem.cpp:125
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB)
Constructor for two variables/constants.
Definition: LiquidMenu.h:231
Represents a screen shown on the display.
Definition: LiquidMenu.h:413
bool set_focusSymbol(Position position, uint8_t symbol[8])
Changes the focus indicator&#39;s symbol.
Definition: LiquidSystem.cpp:129
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:109
LiquidLine(uint8_t column, uint8_t row)
The main constructor.
Definition: LiquidMenu.h:196
Represents a collection of screens forming a menu.
Definition: LiquidMenu.h:559
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC, D &variableD)
Constructor for four variables/constants.
Definition: LiquidMenu.h:266
Position
Position enum.
Definition: LiquidMenu.h:77
bool call_function(uint8_t number) const
Calls an attached function specified by the number.
Definition: LiquidMenu.cpp:210
DataType recognizeType(bool variable)
Definition: recognizeType.cpp:3
void update() const
Prints the current screen to the display.
Definition: LiquidSystem.cpp:140
void next_screen()
Switches to the next screen.
Definition: LiquidSystem.cpp:81
DataType
Data type enum.
Definition: LiquidMenu.h:57
const uint8_t MAX_MENUS
Configures the number of available manus per menus system.
Definition: LiquidMenu_config.h:24
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:216
void print_me(uint16_t address)
Prints the number passed to it in a specific way.
Definition: LiquidLine.cpp:29
bool call_function(uint8_t number) const
Calls an attached function specified by the number.
Definition: LiquidSystem.cpp:133
void operator++()
Switches to the next screen.
Definition: LiquidSystem.cpp:85
bool set_focusPosition(Position position)
Sets the focus position for the whole screen at once.
Definition: LiquidScreen.cpp:66