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.

преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "util.h"
  20. #include "matrix.h"
  21. #include "debug.h"
  22. #include "protocol/serial.h"
  23. #include "led.h"
  24. #include "host.h"
  25. /*
  26. * Matrix Array usage:
  27. *
  28. * ROW: 16(4bits)
  29. * COL: 8(3bits)
  30. *
  31. * 8bit wide
  32. * +---------+
  33. * 0|00 ... 07|
  34. * 1|08 ... 0F|
  35. * :| ... |
  36. * :| ... |
  37. * E|70 ... 77|
  38. * F|78 ... 7F|
  39. * +---------+
  40. */
  41. static uint8_t matrix[MATRIX_ROWS];
  42. #define ROW(code) ((code>>3)&0xF)
  43. #define COL(code) (code&0x07)
  44. void matrix_init(void)
  45. {
  46. DDRD |= (1<<6);
  47. PORTD |= (1<<6);
  48. //debug_enable = true;
  49. serial_init();
  50. // initialize matrix state: all keys off
  51. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  52. // wait for keyboard coming up
  53. // otherwise LED status update fails
  54. print("Reseting ");
  55. while (1) {
  56. print(".");
  57. while (serial_recv());
  58. serial_send(0x01);
  59. _delay_ms(500);
  60. if (serial_recv() == 0xFF) {
  61. _delay_ms(500);
  62. if (serial_recv() == 0x04)
  63. break;
  64. }
  65. }
  66. print(" Done\n");
  67. return;
  68. }
  69. uint8_t matrix_scan(void)
  70. {
  71. uint8_t code;
  72. code = serial_recv();
  73. if (!code) return 0;
  74. debug_hex(code); debug(" ");
  75. switch (code) {
  76. case 0xFF: // reset success: FF 04
  77. print("reset: ");
  78. _delay_ms(500);
  79. code = serial_recv();
  80. xprintf("%02X\n", code);
  81. if (code == 0x04) {
  82. // LED status
  83. led_set(host_keyboard_leds());
  84. }
  85. return 0;
  86. case 0xFE: // layout: FE <layout>
  87. print("layout: ");
  88. _delay_ms(500);
  89. xprintf("%02X\n", serial_recv());
  90. return 0;
  91. case 0x7E: // reset fail: 7E 01
  92. print("reset fail: ");
  93. _delay_ms(500);
  94. xprintf("%02X\n", serial_recv());
  95. return 0;
  96. case 0x7F:
  97. // all keys up
  98. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  99. return 0;
  100. }
  101. if (code&0x80) {
  102. // break code
  103. if (matrix_is_on(ROW(code), COL(code))) {
  104. matrix[ROW(code)] &= ~(1<<COL(code));
  105. }
  106. } else {
  107. // make code
  108. if (!matrix_is_on(ROW(code), COL(code))) {
  109. matrix[ROW(code)] |= (1<<COL(code));
  110. }
  111. }
  112. return code;
  113. }
  114. inline
  115. uint8_t matrix_get_row(uint8_t row)
  116. {
  117. return matrix[row];
  118. }