DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Keymatrix.cpp 3.6KB

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