Kiibohd Controller
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

matrix.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright (C) 2011 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include "matrix.h"
  22. #define REG_SET(reg) reg |= (1 << ( matrix[row][col] % 10 ) )
  23. #define PIN_SET_COL(pin) \
  24. switch ( scanMode ) { \
  25. case scanCol: \
  26. case scanCol_powrRow: \
  27. case scanDual: \
  28. REG_SET(port##pin); break; \
  29. case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  30. } \
  31. break
  32. #define PIN_SET_ROW(pin) \
  33. switch ( scanMode ) { \
  34. case scanRow: \
  35. case scanRow_powrCol: \
  36. case scanDual: \
  37. REG_SET(port##pin); break; \
  38. case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  39. } \
  40. break
  41. #define PIN_CASE(pinLetter) \
  42. case pin##pinLetter##0: \
  43. case pin##pinLetter##1: \
  44. case pin##pinLetter##2: \
  45. case pin##pinLetter##3: \
  46. case pin##pinLetter##4: \
  47. case pin##pinLetter##5: \
  48. case pin##pinLetter##6: \
  49. case pin##pinLetter##7
  50. #define PIN_TEST_COL(pin) \
  51. if ( !( pin & ( 1 << ( matrix[0][col] % 10 ) ) \
  52. detectArray[matrix[row][col]]++; \
  53. break
  54. void matrix_pinSetup( uint8_t *matrix )
  55. {
  56. // Setup the variables
  57. uint8_t portA = 0x00;
  58. uint8_t portB = 0x00;
  59. uint8_t portC = 0x00;
  60. uint8_t portD = 0x00;
  61. uint8_t portE = 0x00;
  62. uint8_t portF = 0x00;
  63. uint8_t ddrA = 0x00;
  64. uint8_t ddrB = 0x00;
  65. uint8_t ddrC = 0x00;
  66. uint8_t ddrD = 0x00;
  67. uint8_t ddrE = 0x00;
  68. uint8_t ddrF = 0x00;
  69. // Loop through all the pin assignments, for the initial pin settings
  70. int row, col;
  71. // Rows
  72. for ( row = 1; row < sizeof(matrix); row++ ) {
  73. switch ( matrix[row][col] ) {
  74. PIN_CASE(A):
  75. PIN_SET_ROW(A);
  76. PIN_CASE(B):
  77. PIN_SET_ROW(B);
  78. PIN_CASE(C):
  79. PIN_SET_ROW(C);
  80. PIN_CASE(D):
  81. PIN_SET_ROW(D);
  82. PIN_CASE(E):
  83. PIN_SET_ROW(E);
  84. PIN_CASE(F):
  85. PIN_SET_ROW(F);
  86. default:
  87. continue;
  88. }
  89. }
  90. // Columns
  91. for ( col = 1; col < sizeof(matrix[0]); row++ ) {
  92. switch ( matrix[row][col] ) {
  93. PIN_CASE(A):
  94. PIN_SET_COL(A);
  95. PIN_CASE(B):
  96. PIN_SET_COL(B);
  97. PIN_CASE(C):
  98. PIN_SET_COL(C);
  99. PIN_CASE(D):
  100. PIN_SET_COL(D);
  101. PIN_CASE(E):
  102. PIN_SET_COL(E);
  103. PIN_CASE(F):
  104. PIN_SET_COL(F);
  105. default:
  106. continue;
  107. }
  108. }
  109. // Setting the pins
  110. DDRA = ddrA;
  111. DDRB = ddrB;
  112. DDRC = ddrC;
  113. DDRD = ddrD;
  114. DDRE = ddrE;
  115. DDRF = ddrF;
  116. PORTA = portA;
  117. PORTB = portB;
  118. PORTC = portC;
  119. PORTD = portD;
  120. PORTE = portE;
  121. PORTF = portF;
  122. }
  123. // TODO Proper matrix scanning
  124. void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
  125. {
  126. // Column Scan
  127. #if scanMode == scanCol
  128. uint8_t col = 1;
  129. uint8_t row = 1;
  130. for ( ; col < sizeof(matrix[1]); col++ ) {
  131. switch ( matrix[0][col] / 10 ) {
  132. case 0: // PINA
  133. PIN_TEST_COL(PINA);
  134. case 1: // PINB
  135. PIN_TEST_COL(PINB);
  136. case 2: // PINC
  137. PIN_TEST_COL(PINC);
  138. case 3: // PIND
  139. PIN_TEST_COL(PIND);
  140. case 4: // PINE
  141. PIN_TEST_COL(PINE);
  142. case 5: // PINF
  143. PIN_TEST_COL(PINF);
  144. }
  145. }
  146. #endif
  147. // Row Scan
  148. #if scanMode == scanRow
  149. #endif
  150. // Column Scan, Power Row
  151. #if scanMode == scanCol_powrRow
  152. #endif
  153. // Row Scan, Power Column
  154. #if scanMode == scanRow_powrCol
  155. #endif
  156. // Dual Scan
  157. #if scanMode == scanDual
  158. #endif
  159. }