DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Keymatrix.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <Arduino.h>
  2. #include "Keymatrix.h"
  3. #ifdef FUNCTION_UI
  4. //#define DEBUG_PRINT_MATRIX_STATE
  5. #ifdef DEBUG_PRINT_MATRIX_STATE
  6. #include "DebugLog.h"
  7. #endif // DEBUG_PRINT_MATRIX_STATE
  8. Keymatrix::Event::Event(EventType _type, int _row, int _col) {
  9. type = _type;
  10. row = _row;
  11. col = _col;
  12. }
  13. Keymatrix::Event::EventType Keymatrix::Event::getType(void) {
  14. return type;
  15. }
  16. int Keymatrix::Event::getRow(void) {
  17. return row;
  18. }
  19. int Keymatrix::Event::getCol(void) {
  20. return col;
  21. }
  22. // -1 is *, -2 is #, or digits 0-9
  23. int Keymatrix::Event::getNum(void) {
  24. if (row == 3) {
  25. switch (col) {
  26. case 0:
  27. return -1; // *
  28. case 2:
  29. return -2; // #
  30. default:
  31. return 0;
  32. }
  33. } else {
  34. return (row * 3) + col + 1;
  35. }
  36. }
  37. Keymatrix::Keymatrix(int _rows, int _cols) {
  38. debounce = default_debounce;
  39. last_scan_time = 0;
  40. rows = _rows;
  41. cols = _cols;
  42. pins = new int[rows + cols];
  43. lastPressed = new bool[rows * cols];
  44. lastState = new bool[rows * cols];
  45. for (int i = 0; i < (rows * cols); i++) {
  46. lastPressed[i] = false;
  47. lastState[i] = false;
  48. }
  49. }
  50. Keymatrix::~Keymatrix(void) {
  51. delete pins;
  52. delete lastPressed;
  53. delete lastState;
  54. }
  55. // first rows, then cols
  56. void Keymatrix::setPins(int _pins[]) {
  57. for (int i = 0; i < (rows + cols); i++) {
  58. pins[i] = _pins[i];
  59. // rows as outputs, cols as inputs
  60. if (i < rows) {
  61. //pinMode(pins[i], OUTPUT);
  62. pinMode(pins[i], INPUT);
  63. } else {
  64. pinMode(pins[i], INPUT_PULLUP);
  65. }
  66. }
  67. }
  68. void Keymatrix::setDebounce(unsigned long ms) {
  69. debounce = ms;
  70. }
  71. void Keymatrix::scan(void) {
  72. // only continue when enough time has passed
  73. unsigned long current_time = millis();
  74. if (current_time < (last_scan_time + debounce)) {
  75. return;
  76. }
  77. last_scan_time = current_time;
  78. // disable all rows
  79. for (int r = 0; r < rows; r++) {
  80. //digitalWrite(pins[r], HIGH);
  81. pinMode(pins[r], INPUT);
  82. }
  83. int buttons = rows * cols;
  84. bool pressed[buttons];
  85. // go through all rows
  86. for (int r = 0; r < rows; r++) {
  87. // enable current row
  88. pinMode(pins[r], OUTPUT);
  89. digitalWrite(pins[r], LOW);
  90. // read out all columns
  91. for (int c = 0; c < cols; c++) {
  92. int v = digitalRead(pins[rows + c]);
  93. pressed[(r * cols) + c] = (v == LOW);
  94. }
  95. // disable current row
  96. //digitalWrite(pins[r], HIGH);
  97. pinMode(pins[r], INPUT);
  98. }
  99. #ifdef DEBUG_PRINT_MATRIX_STATE
  100. for (int i = 0; i < buttons; i++) {
  101. debug.print(pressed[i] ? "1" : "0");
  102. if (i < (buttons - 1)) {
  103. debug.print(" ");
  104. } else {
  105. debug.println();
  106. }
  107. }
  108. #endif // DEBUG_PRINT_MATRIX_STATE
  109. for (int i = 0; i < buttons; i++) {
  110. // debounce - compare to previous state
  111. if ((lastPressed[i] == pressed[i]) && (pressed[i] != lastState[i])) {
  112. lastState[i] = pressed[i];
  113. int c = i % cols;
  114. int r = i / cols;
  115. Event::EventType et = (pressed[i]) ? Event::button_down : Event::button_up;
  116. events.push(new Event(et, r, c));
  117. }
  118. // save current state for next time
  119. lastPressed[i] = pressed[i];
  120. }
  121. }
  122. bool Keymatrix::hasEvent(void) {
  123. return !events.isEmpty();
  124. }
  125. Keymatrix::Event Keymatrix::getEvent(void) {
  126. if (hasEvent()) {
  127. Event *e = events.shift();
  128. Event e_copy = *e;
  129. delete e;
  130. return e_copy;
  131. } else {
  132. return Keymatrix::Event(Event::no_event, -1, -1);
  133. }
  134. }
  135. #endif // FUNCTION_UI