2
0

matrix.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include "hal.h"
  5. #include "timer.h"
  6. #include "wait.h"
  7. #include "printf.h"
  8. #include "backlight.h"
  9. #include "matrix.h"
  10. /* Clueboard 1%
  11. *
  12. * Single switch connected to A1 and GND.
  13. *
  14. * col: { GND }
  15. * row: { PA1 }
  16. */
  17. /* matrix state(1:on, 0:off) */
  18. static matrix_row_t matrix[MATRIX_ROWS];
  19. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  20. static bool debouncing = false;
  21. static uint16_t debouncing_time = 0;
  22. void matrix_init(void) {
  23. printf("matrix init\n");
  24. /* Column(sense) */
  25. palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_PULLUP);
  26. memset(matrix, 0, MATRIX_ROWS);
  27. memset(matrix_debouncing, 0, MATRIX_ROWS);
  28. }
  29. uint8_t matrix_scan(void) {
  30. matrix_row_t data = 0;
  31. // read switch data: { PA1 }
  32. data = (palReadPad(GPIOA, 1));
  33. if (matrix_debouncing[0] != data) {
  34. matrix_debouncing[0] = data;
  35. debouncing = true;
  36. debouncing_time = timer_read();
  37. }
  38. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  39. matrix[0] = matrix_debouncing[0];
  40. debouncing = false;
  41. }
  42. return 1;
  43. }
  44. bool matrix_is_on(uint8_t row, uint8_t col) {
  45. return (matrix[row] & (1<<col));
  46. }
  47. matrix_row_t matrix_get_row(uint8_t row) {
  48. return matrix[row];
  49. }
  50. void matrix_print(void) {
  51. printf("\nr/c 01234567\n");
  52. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  53. printf("%X0: ", row);
  54. matrix_row_t data = matrix_get_row(row);
  55. for (int col = 0; col < MATRIX_COLS; col++) {
  56. if (data & (1<<col))
  57. printf("1");
  58. else
  59. printf("0");
  60. }
  61. printf("\n");
  62. }
  63. }