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 #include <inttypes.h>
43 #include <avr/pgmspace.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 
47 #include "LiquidMenu_config.h"
48 
49 #include "serial_debugging.h"
50 
51 #ifndef LiquidCrystal_h
52 #warning "LiquidCrystal library is required!"
53 #endif
54 
55 const char VERSION[] = "1.1";
56 
58 
61 enum class DataType : uint8_t {
62  NOT_USED = 0,
63  BOOL = 1, BOOLEAN = 1,
64  INT8_T = 8,
65  UINT8_T = 9, BYTE = 9,
66  INT16_T = 16,
67  UINT16_T = 17,
68  INT32_T = 32,
69  UINT32_T = 33,
70  FLOAT = 50, DOUBLE = 50,
71  CHAR = 60,
72  CHAR_PTR = 61,
73  CONST_CHAR_PTR = 62,
74  PROG_CONST_CHAR_PTR = 65,
75  GLYPH = 70,
76 };
77 
79 /*
80 Used to store and set the relative or absolute position of the focus indicator.
81 */
82 enum class Position : uint8_t {
83  RIGHT = 1, NORMAL = 1,
84  LEFT = 2,
85  CUSTOM = 3,
86 };
87 
89 
93 
99 DataType recognizeType(bool variable);
100 
105 DataType recognizeType(char variable);
106 
111 DataType recognizeType(char* variable);
112 
117 DataType recognizeType(const char* variable);
118 
123 DataType recognizeType(int8_t variable);
124 
129 DataType recognizeType(uint8_t variable);
130 
135 DataType recognizeType(int16_t variable);
136 
141 DataType recognizeType(uint16_t variable);
142 
147 DataType recognizeType(int32_t variable);
148 
153 DataType recognizeType(uint32_t variable);
154 
159 DataType recognizeType(float variable);
160 
165 DataType recognizeType(double variable);
167 
168 
170 
175 void print_me(uintptr_t address);
176 
177 
179 
186 class LiquidLine {
187  friend class LiquidScreen;
188 
189 public:
192 
194 
200  LiquidLine(uint8_t column, uint8_t row)
201  : _row(row), _column(column), _focusRow(row - 1),
202  _focusColumn(column - 1), _focusPosition(Position::NORMAL),
203  _variableCount(0), _focusable(false) {
204  for (uint8_t i = 0; i < MAX_VARIABLES; i++) {
205  _variable[i] = nullptr;
206  _variableType[i] = DataType::NOT_USED;
207  }
208  for (uint8_t f = 0; f < MAX_FUNCTIONS; f++) {
209  _function[f] = 0;
210  }
211  }
212 
214 
219  template <typename A>
220  LiquidLine(uint8_t column, uint8_t row, A &variableA)
221  : LiquidLine(column, row) {
222  add_variable(variableA);
223  //_variable[0] = (void*)&variableA;
224  //_variableType[0] = recognizeType(variableA);
225  }
226 
228 
234  template <typename A, typename B>
235  LiquidLine(uint8_t column, uint8_t row,
236  A &variableA, B &variableB)
237  : LiquidLine(column, row, variableA) {
238  add_variable(variableB);
239  //_variable[1] = (void*)&variableB;
240  //_variableType[1] = recognizeType(variableB);
241  }
242 
244 
251  template <typename A, typename B, typename C>
252  LiquidLine(uint8_t column, uint8_t row,
253  A &variableA, B &variableB, C &variableC)
254  : LiquidLine(column, row, variableA, variableB) {
255  add_variable(variableC);
256  //_variable[2] = (void*)&variableC;
257  //_variableType[2] = recognizeType(variableC);
258  }
259 
261 
269  template <typename A, typename B, typename C, typename D>
270  LiquidLine(uint8_t column, uint8_t row,
271  A &variableA, B &variableB, C &variableC, D &variableD)
272  : LiquidLine(column, row, variableA, variableB, variableC) {
273  add_variable(variableD);
274  //_variable[3] = (void*)&variableD;
275  //_variableType[3] = recognizeType(variableD);
276  }
277 
279 
280 
283 
285 
296  template <typename T>
297  bool add_variable(T &variable) {
298  print_me((uintptr_t)this);
299  if (_variableCount < MAX_VARIABLES) {
300  _variable[_variableCount] = (void*)&variable;
301  _variableType[_variableCount] = recognizeType(variable);
302  DEBUG(F("Added variable '")); DEBUG(variable); DEBUGLN(F("'"));
303  _variableCount++;
304  return true;
305  }
306  DEBUG(F("Adding variable ")); DEBUG(variable);
307  DEBUGLN(F(" failed, edit LiquidMenu_config.h to allow for more variables"));
308  return false;
309  }
310 
312 
330  bool attach_function(uint8_t number, void (*function)(void));
331 
333 
349  bool set_focusPosition(Position position,
350  uint8_t column = 0, uint8_t row = 0);
351 
353 
362  bool set_asGlyph(uint8_t number);
363 
365 
373  bool set_asProgmem(uint8_t number);
375 
376 private:
378 
386  void print(LiquidCrystal *p_liquidCrystal, bool isFocused);
387 
389 
396  void print_variable(LiquidCrystal *p_liquidCrystal, uint8_t number);
397 
399 
407  bool call_function(uint8_t number) const;
408 
409  uint8_t _row, _column, _focusRow, _focusColumn;
410  Position _focusPosition;
411  uint8_t _variableCount;
412  void (*_function[MAX_FUNCTIONS])(void);
413  const void *_variable[MAX_VARIABLES];
414  DataType _variableType[MAX_VARIABLES];
415  bool _focusable;
416 };
417 
418 
420 
429  friend class LiquidMenu;
430 
431 public:
432 
435 
437 
440  LiquidScreen();
441 
443 
446  explicit LiquidScreen(LiquidLine &liquidLine);
447 
449 
453  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2);
454 
456 
461  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
462  LiquidLine &liquidLine3);
463 
465 
471  LiquidScreen(LiquidLine &liquidLine1, LiquidLine &liquidLine2,
472  LiquidLine &liquidLine3, LiquidLine &liquidLine4);
473 
475 
478 
480 
491  bool add_line(LiquidLine &liquidLine);
492 
494 
508  bool set_focusPosition(Position position);
509 
511 
522  void hide(bool hide);
524 
525 private:
527 
533  void print(LiquidCrystal *p_liquidCrystal) const;
534 
536 
542  void switch_focus(bool forward = true);
543 
545 
555  bool call_function(uint8_t number) const;
556 
557  LiquidLine *_p_liquidLine[MAX_LINES];
558  uint8_t _lineCount;
559  uint8_t _focus;
560  bool _hidden;
561 };
562 
563 
565 
574 class LiquidMenu {
575  friend class LiquidSystem;
576 
577 public:
578 
581 
583 
590  LiquidMenu(LiquidCrystal &liquidCrystal, uint8_t startingScreen = 1);
591 
593 
599  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen,
600  uint8_t startingScreen = 1);
601 
603 
610  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
611  LiquidScreen &liquidScreen2, uint8_t startingScreen = 1);
612 
614 
622  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
623  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
624  uint8_t startingScreen = 1);
625 
627 
636  LiquidMenu(LiquidCrystal &liquidCrystal, LiquidScreen &liquidScreen1,
637  LiquidScreen &liquidScreen2, LiquidScreen &liquidScreen3,
638  LiquidScreen &liquidScreen4, uint8_t startingScreen = 1);
639 
641 
644 
646 
657  bool add_screen(LiquidScreen &liquidScreen);
658 
660  void next_screen();
661 
663 
666  void operator++();
667 
669 
672  void operator++(int);
673 
675  void previous_screen();
676 
678 
681  void operator--();
682 
684 
687  void operator--(int);
688 
690 
694  bool change_screen(LiquidScreen &p_liquidScreen);
695 
697 
702  bool change_screen(uint8_t number);
703 
705 
709  bool operator=(LiquidScreen &p_liquidScreen);
710 
712 
717  bool operator=(uint8_t number);
718 
720 
726  void switch_focus(bool forward = true);
727 
729 
743  bool set_focusPosition(Position position);
744 
746 
759  bool set_focusSymbol(Position position, uint8_t symbol[8]);
760 
762 
773  bool call_function(uint8_t number) const;
774 
776 
779  void update() const;
780 
782 
788  void softUpdate() const;
789 
791 
792 private:
793  LiquidCrystal *_p_liquidCrystal;
794  LiquidScreen *_p_liquidScreen[MAX_SCREENS];
795  uint8_t _screenCount;
796  uint8_t _currentScreen;
797 };
798 
799 
801 
812 public:
813 
816 
818 
823  explicit LiquidSystem(uint8_t startingMenu = 1);
824 
826 
831  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
832  uint8_t startingMenu = 1);
833 
835 
841  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
842  LiquidMenu &liquidMenu3, uint8_t startingMenu = 1);
843 
845 
852  LiquidSystem(LiquidMenu &liquidMenu1, LiquidMenu &liquidMenu2,
853  LiquidMenu &liquidMenu3, LiquidMenu &liquidMenu4,
854  uint8_t startingMenu = 1);
855 
857 
860 
862 
873  bool add_menu(LiquidMenu &liquidMenu);
874 
876 
880  bool change_menu(LiquidMenu &p_liquidMenu);
881 
883  void next_screen();
884 
886 
889  void operator++();
890 
892 
895  void operator++(int);
896 
898  void previous_screen();
899 
901 
904  void operator--();
905 
907 
910  void operator--(int);
911 
913 
917  bool change_screen(LiquidScreen &p_liquidScreen);
918 
920 
925  bool change_screen(uint8_t number);
926 
928 
932  bool operator=(LiquidScreen &p_liquidScreen);
933 
935 
940  bool operator=(uint8_t number);
941 
943 
949  void switch_focus(bool forward = true);
950 
952 
966  bool set_focusPosition(Position position);
967 
969 
982  bool set_focusSymbol(Position position, uint8_t symbol[8]);
983 
985 
996  bool call_function(uint8_t number) const;
997 
999 
1002  void update() const;
1003 
1005 
1011  void softUpdate() const;
1012 
1014 
1015 private:
1016  LiquidMenu *_p_liquidMenu[MAX_MENUS];
1017  uint8_t _menuCount;
1018  uint8_t _currentMenu;
1019 };
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC)
Constructor for three variables/constants.
Definition: LiquidMenu.h:252
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:811
const char VERSION[]
The version of the library.
Definition: LiquidMenu.h:55
Definition: symbols.h:3
Represents the individual lines printed on the display.
Definition: LiquidMenu.h:186
void update() const
Prints the current screen to the display.
Definition: LiquidSystem.cpp:140
void softUpdate() const
Prints the current screen to the display (without clearing).
Definition: LiquidSystem.cpp:144
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:297
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:235
Represents a screen shown on the display.
Definition: LiquidMenu.h:428
bool set_focusSymbol(Position position, uint8_t symbol[8])
Changes the focus indicator&#39;s symbol.
Definition: LiquidSystem.cpp:129
bool call_function(uint8_t number) const
Calls an attached function specified by the number.
Definition: LiquidSystem.cpp:133
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:200
Represents a collection of screens forming a menu.
Definition: LiquidMenu.h:574
LiquidLine(uint8_t column, uint8_t row, A &variableA, B &variableB, C &variableC, D &variableD)
Constructor for four variables/constants.
Definition: LiquidMenu.h:270
Position
Position enum.
Definition: LiquidMenu.h:82
DataType recognizeType(bool variable)
Definition: recognizeType.cpp:3
void next_screen()
Switches to the next screen.
Definition: LiquidSystem.cpp:81
DataType
Data type enum.
Definition: LiquidMenu.h:61
void print_me(uintptr_t address)
Prints the number passed to it in a specific way.
Definition: LiquidLine.cpp:30
const uint8_t MAX_MENUS
Configures the number of available manus 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:209
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:220
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