summaryrefslogtreecommitdiff
path: root/12key-arduino/Keypad.h
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-10-19 20:12:24 -0500
committersanine <sanine.not@pm.me>2023-10-19 20:12:24 -0500
commita91ba5a9d7ced85277ad60f1dbf381192f5c5ed8 (patch)
tree7223cb8bf4440edf84a2c6e54e7f480dcb1be52e /12key-arduino/Keypad.h
parente55263a0a250dea2bf567df8dcbad8ca3580b10c (diff)
updatemain
Diffstat (limited to '12key-arduino/Keypad.h')
-rw-r--r--12key-arduino/Keypad.h143
1 files changed, 143 insertions, 0 deletions
diff --git a/12key-arduino/Keypad.h b/12key-arduino/Keypad.h
new file mode 100644
index 0000000..f056348
--- /dev/null
+++ b/12key-arduino/Keypad.h
@@ -0,0 +1,143 @@
+#pragma once
+
+#include <Arduino.h>
+#include "pins.h"
+
+
+#define DEFAULT_DEBOUNCE_TIME 25
+
+
+#define KEY_1 0x800
+#define KEY_2 0x400
+#define KEY_3 0x200
+#define KEY_4 0x100
+#define KEY_5 0x080
+#define KEY_6 0x040
+#define KEY_7 0x020
+#define KEY_8 0x010
+#define KEY_9 0x008
+#define KEY_STAR 0x004
+#define KEY_0 0x002
+#define KEY_HASH 0x001
+
+
+class Keypad {
+ protected:
+ unsigned int m_mask;
+ unsigned int m_debounceTime;
+ bool m_debouncing;
+ unsigned long m_debounceEnd;
+ bool m_pressed;
+
+ static Keypad buttons[12];
+
+ void startDebounce() {
+ m_debouncing = true;
+ m_debounceEnd = millis() + m_debounceTime;
+ }
+
+ public:
+ static unsigned int state;
+ static void Setup() {
+ pinMode(LED, OUTPUT);
+
+ pinMode(COL_1, INPUT_PULLUP);
+ pinMode(COL_2, INPUT_PULLUP);
+ pinMode(COL_3, INPUT_PULLUP);
+
+ pinMode(ROW_1, INPUT_PULLUP);
+ pinMode(ROW_2, INPUT_PULLUP);
+ pinMode(ROW_3, INPUT_PULLUP);
+ pinMode(ROW_4, INPUT_PULLUP);
+ }
+
+ static void Update() {
+ byte col1 = 1 - digitalRead(COL_1);
+ byte col2 = 1 - digitalRead(COL_2);
+ byte col3 = 1 - digitalRead(COL_3);
+
+ byte row1 = 1 - digitalRead(ROW_1);
+ byte row2 = 1 - digitalRead(ROW_2);
+ byte row3 = 1 - digitalRead(ROW_3);
+ byte row4 = 1 - digitalRead(ROW_4);
+
+ #define BUILD_ROW(row, c1, c2, c3) \
+ ( ((row & c1) << 2) | ((row & c2) << 1) | (row & c3) )
+
+ byte row1_buttons = BUILD_ROW(row1, col1, col2, col3);
+ byte row2_buttons = BUILD_ROW(row2, col1, col2, col3);
+ byte row3_buttons = BUILD_ROW(row3, col1, col2, col3);
+ byte row4_buttons = BUILD_ROW(row4, col1, col2, col3);
+
+ unsigned int pins = 0 |
+ (row1_buttons << 9) |
+ (row2_buttons << 6) |
+ (row3_buttons << 3) |
+ (row4_buttons << 0);
+
+ for (int i=0; i<12; i++) {
+ buttons[i].update(pins);
+ }
+ }
+
+ Keypad(unsigned int index, unsigned int debounceTime=DEFAULT_DEBOUNCE_TIME) {
+ m_mask = 1 << index;
+ m_debounceTime = debounceTime;
+ m_debouncing = false;
+ m_pressed = false;
+ }
+
+ void update(unsigned int pins) {
+ if (m_debouncing) {
+ if (millis() > m_debounceEnd) {
+ /* done debouncing, continue */
+ m_debouncing = false;
+ }
+ else {
+ /* still debouncing, ignore changes */
+ return;
+ }
+ }
+
+ if ((m_mask & pins)) {
+ /* pressed */
+ if (m_pressed) {
+ /* already marked */
+ }
+ else {
+ /* update state */
+ m_pressed = true;
+ state = state | m_mask;
+ startDebounce();
+ }
+ }
+ else {
+ /* released */
+ if (!m_pressed) {
+ /* already marked, ignore */
+ }
+ else {
+ /* update state */
+ m_pressed = false;
+ state = state & ~m_mask;
+ startDebounce();
+ }
+ }
+ }
+};
+unsigned int Keypad::state = 0;
+
+Keypad Keypad::buttons[12] = {
+ Keypad(0),
+ Keypad(1),
+ Keypad(2),
+ Keypad(3),
+ Keypad(4),
+ Keypad(5),
+ Keypad(6),
+ Keypad(7),
+ Keypad(8),
+ Keypad(9),
+ Keypad(10),
+ Keypad(11),
+};