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.6KB

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