matrix.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SimonTester
  11. *
  12. * Column pins are input with internal pull-down.
  13. * Row pins are output and strobe with high.
  14. * Key is high or 1 when it turns on.
  15. *
  16. * col: { PA5, PA4, PB6, PB3 }
  17. * row: { PA7 }
  18. */
  19. /* matrix state(1:on, 0:off) */
  20. static matrix_row_t matrix[MATRIX_ROWS];
  21. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  22. static bool debouncing = false;
  23. static uint16_t debouncing_time = 0;
  24. void matrix_init(void) {
  25. printf("matrix init\n");
  26. //debug_matrix = true;
  27. /* Column(sense) */
  28. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_PULLDOWN);
  29. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_PULLDOWN);
  30. palSetPadMode(GPIOB, 6, PAL_MODE_INPUT_PULLDOWN);
  31. palSetPadMode(GPIOB, 3, PAL_MODE_INPUT_PULLDOWN);
  32. /* Row(strobe) */
  33. palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL);
  34. palSetPad(GPIOA, 7); // Only one row, so leave it in strobe state all the time.
  35. // I did it this way so hardware hackers would have another
  36. // pin to play with.
  37. memset(matrix, 0, MATRIX_ROWS);
  38. memset(matrix_debouncing, 0, MATRIX_ROWS);
  39. /* Setup the backlight */
  40. palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL);
  41. palSetPadMode(GPIOA, 3, PAL_MODE_OUTPUT_PUSHPULL);
  42. palSetPadMode(GPIOA, 15, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL);
  44. // Set them high to turn off the LEDs
  45. palClearPad(GPIOA, 6);
  46. palSetPad(GPIOA, 3);
  47. palSetPad(GPIOA, 15);
  48. palSetPad(GPIOB, 5);
  49. }
  50. uint8_t matrix_scan(void) {
  51. matrix_row_t data = 0;
  52. // read col data: { PA5, PA4, PB6, PB3 }
  53. data = (palReadPad(GPIOA, 5) |
  54. (palReadPad(GPIOA, 4) << 1) |
  55. (palReadPad(GPIOB, 6) << 2) |
  56. (palReadPad(GPIOB, 3) << 3));
  57. if (matrix_debouncing[0] != data) {
  58. matrix_debouncing[0] = data;
  59. debouncing = true;
  60. debouncing_time = timer_read();
  61. }
  62. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  63. matrix[0] = matrix_debouncing[0];
  64. debouncing = false;
  65. }
  66. return 1;
  67. }
  68. bool matrix_is_on(uint8_t row, uint8_t col) {
  69. return (matrix[row] & (1<<col));
  70. }
  71. matrix_row_t matrix_get_row(uint8_t row) {
  72. return matrix[row];
  73. }
  74. void matrix_print(void) {
  75. printf("\nr/c 01234567\n");
  76. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  77. printf("%X0: ", row);
  78. matrix_row_t data = matrix_get_row(row);
  79. for (int col = 0; col < MATRIX_COLS; col++) {
  80. if (data & (1<<col))
  81. printf("1");
  82. else
  83. printf("0");
  84. }
  85. printf("\n");
  86. }
  87. }