matrix.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2018 Charlie Waters
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <hal.h>
  21. #include "timer.h"
  22. #include "wait.h"
  23. #include "print.h"
  24. #include "matrix.h"
  25. #include "annepro2.h"
  26. pin_t row_list[MATRIX_ROWS] = MATRIX_ROW_PINS;
  27. pin_t col_list[MATRIX_COLS] = MATRIX_COL_PINS;
  28. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  29. bool matrix_has_changed = false;
  30. // cache of input ports for columns
  31. static uint16_t port_cache[4];
  32. // scan each row
  33. for (int row = 0; row < MATRIX_ROWS; row++) {
  34. palClearLine(row_list[row]);
  35. __NOP();
  36. __NOP();
  37. __NOP();
  38. __NOP();
  39. // read i/o ports
  40. port_cache[0] = palReadPort(IOPORTA);
  41. port_cache[1] = palReadPort(IOPORTB);
  42. port_cache[2] = palReadPort(IOPORTC);
  43. port_cache[3] = palReadPort(IOPORTD);
  44. palSetLine(row_list[row]);
  45. // get columns from ports
  46. matrix_row_t data = 0;
  47. for (int col = 0; col < MATRIX_COLS; ++col) {
  48. pin_t line = col_list[col];
  49. uint16_t port = port_cache[HT32_PAL_IDX(PAL_PORT(line))];
  50. data |= (((port & (1 << PAL_PAD(line))) ? 0 : 1) << col);
  51. }
  52. if (current_matrix[row] != data) {
  53. current_matrix[row] = data;
  54. matrix_has_changed = true;
  55. }
  56. }
  57. return matrix_has_changed;
  58. }