IoAbstraction
KeyboardManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef _KEYBOARD_MANGER_H_
7 #define _KEYBOARD_MANGER_H_
8 
9 #include "IoAbstraction.h"
10 
21 public:
27  virtual void keyPressed(char key, bool held)=0;
32  virtual void keyReleased(char key)=0;
33 };
34 
44 private:
45  uint8_t rows;
46  uint8_t cols;
47  const char *pgmLayout;
48  uint8_t *pins;
49 public:
57  KeyboardLayout(uint8_t rows, uint8_t cols, const char* pgmLayout) {
58  this->rows = rows;
59  this->cols = cols;
60  this->pgmLayout = pgmLayout;
61  this->pins = new uint8_t[rows + cols];
62  }
63 
64  ~KeyboardLayout() {
65  delete pins;
66  }
67 
68  int numColumns() { return cols; }
69 
70  int numRows() { return rows; }
71 
72  void setColPin(int col, int pin) {
73  if(col < cols) pins[rows + col] = pin;
74  }
75 
76  void setRowPin(int row, int pin) {
77  if(row < rows) pins[row] = pin;
78  }
79 
80  int getRowPin(int row) {
81  return pins[row];
82  }
83 
84  int getColPin(int col) {
85  return pins[rows + col];
86  }
87 
88  char keyFor(uint8_t row, uint8_t col) {
89  if(row < rows && col < cols) return pgm_read_byte_near(&pgmLayout[(row * cols)+col]);
90  else return 0;
91  }
92 };
93 
97 enum KeyMode: uint8_t {
98  KEYMODE_NOT_PRESSED,
99  KEYMODE_DEBOUNCE,
100  KEYMODE_DEBOUNCE1,
101  KEYMODE_DEBOUNCE2,
102  KEYMODE_PRESSED,
103  KEYMODE_REPEATING
104 };
105 
106 #define KEYBOARD_TASK_MILLIS 50
107 
118 class MatrixKeyboardManager : public BaseEvent {
119 private:
120  static MatrixKeyboardManager* INSTANCE;
121  KeyboardListener* listener;
122  KeyboardLayout* layout;
123  IoAbstractionRef ioRef;
124  uint8_t repeatTicks = 10;
125  uint8_t repeatStartTicks = 30;
126  char currentKey;
127  volatile KeyMode keyMode;
128  uint8_t counter;
129  bool interruptMode;
130 public:
132  void initialise(IoAbstractionRef ref, KeyboardLayout* layout, KeyboardListener* listener, bool interruptMode = false);
133  void setRepeatKeyMillis(int startAfterMillis, int repeatMillis);
134 
135  uint32_t timeOfNextCheck() override;
136  void exec() override;
137 
138  friend void rawKeyboardInterrupt();
139 private:
140  void setToOutput(int i);
141  void enableAllOutputsForInterrupt();
142 
143  void doDebounce(char time);
144 };
145 
146 #define MAKE_KEYBOARD_LAYOUT_3X4(varName) const char KEYBOARD_STD_3X4_KEYS[] PROGMEM = "123456789*0#"; KeyboardLayout varName(4, 3, KEYBOARD_STD_3X4_KEYS);
147 #define MAKE_KEYBOARD_LAYOUT_4X4(varName) const char KEYBOARD_STD_4X4_KEYS[] PROGMEM = "123A456B789C*0#D"; KeyboardLayout varName(4, 4, KEYBOARD_STD_4X4_KEYS);
148 
149 #endif // _KEYBOARD_MANGER_H_
Using basic IoFacilities allows one to abstract away the use of IoExpanders, such that the switching ...
KeyMode
Definition: KeyboardManager.h:97
Definition: BasicIoAbstraction.h:38
Definition: KeyboardManager.h:43
KeyboardLayout(uint8_t rows, uint8_t cols, const char *pgmLayout)
Definition: KeyboardManager.h:57
Definition: KeyboardManager.h:20
virtual void keyReleased(char key)=0
virtual void keyPressed(char key, bool held)=0
Definition: KeyboardManager.h:118