LcdKeypad.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * LcdKeypad.h
  3. *
  4. * Created on: 15.10.2013
  5. * Author: niklausd
  6. */
  7. #ifndef LCDKEYPAD_H_
  8. #define LCDKEYPAD_H_
  9. #include "Print.h"
  10. class Timer;
  11. class LcdKeypadAdapter;
  12. class LiquidTWI2;
  13. class LiquidCrystal;
  14. #define LCD_5x10DOTS 0x04
  15. #define LCD_5x8DOTS 0x00
  16. class LcdKeypad : public Print
  17. {
  18. public:
  19. typedef enum
  20. {
  21. LCD_DT_TWI2 = 0
  22. , LCD_DT_CRYST = 1
  23. } LcdDeviceType;
  24. typedef enum
  25. {
  26. LCDC_5x8DOTS = 0x00
  27. , LCDC_5x10DOTS = 0x04
  28. } LcdCharDots;
  29. typedef enum
  30. {
  31. MCPT_MCP23008 = 0
  32. , MCPT_MCP23017 = 1
  33. } MCPType;
  34. typedef enum
  35. {
  36. LCDBL_OFF = 0
  37. , LCDBL_RED = 1
  38. , LCDBL_GREEN = 2
  39. , LCDBL_YELLOW = 3
  40. , LCDBL_BLUE = 4
  41. , LCDBL_VIOLET = 5
  42. , LCDBL_TEAL = 6
  43. , LCDBL_WHITE = 7
  44. } LcdBacklightColor;
  45. typedef enum
  46. {
  47. NO_KEY = 0
  48. , SELECT_KEY = 1
  49. , LEFT_KEY = 2
  50. , UP_KEY = 3
  51. , DOWN_KEY = 4
  52. , RIGHT_KEY = 5
  53. } Key;
  54. /**
  55. * self selecting constructor (tries LCD_DT_TWI2 first, and then LCD_DT_CRYST if LCD_DT_TWI2 not successfully found)
  56. */
  57. LcdKeypad(MCPType mcptype = MCPT_MCP23017, uint8_t i2cAddr = 0x20, uint8_t detectDevice = 1, uint8_t backlightInverted = 0,
  58. int lcdRSPin = s_defaultLcdRSPin, int lcdEnPin = s_defaultLcdEnPin,
  59. int lcdD4Pin = s_defaultLcdD4Pin, int lcdD5Pin = s_defaultLcdD5Pin,
  60. int lcdD6Pin = s_defaultLcdD6Pin, int lcdD7Pin = s_defaultLcdD7Pin,
  61. int lcdBackLightCtrlPin = s_defaultLcdBackLightCtrlPin, bool isLcdBackLightOn = s_defaultIsLcdBackLightOn);
  62. virtual ~LcdKeypad();
  63. void setBackLightOn(bool isLcdBackLightOn);
  64. void handleButtons();
  65. Key getCurrentKey();
  66. bool isNoKey();
  67. bool isUpKey();
  68. bool isDownKey();
  69. bool isSelectKey();
  70. bool isLeftKey();
  71. bool isRightKey();
  72. void attachAdapter(LcdKeypadAdapter* adapter);
  73. LcdKeypadAdapter* adapter();
  74. public:
  75. void begin(uint8_t cols, uint8_t rows, uint8_t dotsize = LCDC_5x8DOTS);
  76. void clear();
  77. void home();
  78. void noDisplay();
  79. void display();
  80. void noBlink();
  81. void blink();
  82. void noCursor();
  83. void cursor();
  84. void scrollDisplayLeft();
  85. void scrollDisplayRight();
  86. void leftToRight();
  87. void rightToLeft();
  88. void autoscroll();
  89. void noAutoscroll();
  90. void createChar(uint8_t location, uint8_t charmap[]);
  91. void setCursor(uint8_t col, uint8_t row);
  92. void setBacklight(LcdBacklightColor lcdBacklightColor);
  93. #if defined(ARDUINO) && (ARDUINO >= 100) //scl
  94. size_t write(uint8_t value);
  95. #else
  96. void write(uint8_t value);
  97. #endif
  98. void command(uint8_t value);
  99. void setMCPType(MCPType mcptype);
  100. private:
  101. void setBackLightControl();
  102. uint8_t readButtons();
  103. private:
  104. int m_lcdBackLightCtrlPin;
  105. LcdBacklightColor m_backlightColor;
  106. Key m_currentKey;
  107. Timer* m_keyPollTimer;
  108. LcdKeypadAdapter* m_adapter;
  109. LiquidTWI2* m_liquidTwi2;
  110. LiquidCrystal* m_liquidCrystal;
  111. static const int s_defaultLcdRSPin;
  112. static const int s_defaultLcdEnPin;
  113. static const int s_defaultLcdD4Pin;
  114. static const int s_defaultLcdD5Pin;
  115. static const int s_defaultLcdD6Pin;
  116. static const int s_defaultLcdD7Pin;
  117. static const int s_defaultLcdBackLightCtrlPin;
  118. static const bool s_defaultIsLcdBackLightOn;
  119. static const int s_defaultKeyAdcPin;
  120. static const int s_defaultKeyPollTime;
  121. static const int s_rightKeyLimit;
  122. static const int s_upKeyLimit;
  123. static const int s_downKeyLimit;
  124. static const int s_leftKeyLimit;
  125. static const int s_selectKeyLimit;
  126. private: // forbidden default functions
  127. LcdKeypad& operator = (const LcdKeypad& src); // assignment operator
  128. LcdKeypad(const LcdKeypad& src); // copy constructor
  129. };
  130. //-----------------------------------------------------------------------------
  131. class LcdKeypadAdapter
  132. {
  133. public:
  134. virtual void handleKeyChanged(LcdKeypad::Key newKey) = 0;
  135. virtual ~LcdKeypadAdapter() { }
  136. protected:
  137. LcdKeypadAdapter() { }
  138. private: // forbidden default functions
  139. LcdKeypadAdapter& operator = (const LcdKeypadAdapter& src); // assignment operator
  140. LcdKeypadAdapter(const LcdKeypadAdapter& src); // copy constructor
  141. };
  142. //-----------------------------------------------------------------------------
  143. #endif /* LCDKEYPAD_H_ */