Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2011,2012 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "util.h"
  23. #include "debug.h"
  24. #include "host.h"
  25. #include "led.h"
  26. #include "m0110.h"
  27. #include "matrix.h"
  28. #define CAPS 0x39
  29. #define CAPS_BREAK (CAPS | 0x80)
  30. #define ROW(key) ((key)>>3&0x0F)
  31. #define COL(key) ((key)&0x07)
  32. static bool is_modified = false;
  33. // matrix state buffer(1:on, 0:off)
  34. static uint8_t *matrix;
  35. static uint8_t _matrix0[MATRIX_ROWS];
  36. static void register_key(uint8_t key);
  37. void matrix_init(void)
  38. {
  39. m0110_init();
  40. // initialize matrix state: all keys off
  41. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  42. matrix = _matrix0;
  43. // LED flash
  44. DDRD |= (1<<6); PORTD |= (1<<6);
  45. _delay_ms(500);
  46. DDRD |= (1<<6); PORTD &= ~(1<<6);
  47. return;
  48. }
  49. uint8_t matrix_scan(void)
  50. {
  51. uint8_t key;
  52. is_modified = false;
  53. key = m0110_recv_key();
  54. if (key == M0110_NULL) {
  55. return 0;
  56. } else if (key == M0110_ERROR) {
  57. return 0;
  58. } else {
  59. is_modified = true;
  60. register_key(key);
  61. }
  62. if (debug_enable) {
  63. print("["); phex(key); print("]\n");
  64. }
  65. return 1;
  66. }
  67. inline
  68. uint8_t matrix_get_row(uint8_t row)
  69. {
  70. return matrix[row];
  71. }
  72. inline
  73. static void register_key(uint8_t key)
  74. {
  75. if (key&0x80) {
  76. matrix[ROW(key)] &= ~(1<<COL(key));
  77. } else {
  78. matrix[ROW(key)] |= (1<<COL(key));
  79. }
  80. }