Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // ----- Includes -----
  22. // Local Includes
  23. #include "matrix.h"
  24. // ----- Macros -----
  25. #define REG_SET(reg) reg |= (1 << ( matrix[row][col] % 10 ) )
  26. #define PIN_SET_COL(pin) \
  27. switch ( scanMode ) { \
  28. case scanCol: \
  29. case scanCol_powrRow: \
  30. case scanDual: \
  31. REG_SET(port##pin); break; \
  32. case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  33. } \
  34. break
  35. #define PIN_SET_ROW(pin) \
  36. switch ( scanMode ) { \
  37. case scanRow: \
  38. case scanRow_powrCol: \
  39. case scanDual: \
  40. REG_SET(port##pin); break; \
  41. case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  42. } \
  43. break
  44. #define PIN_CASE(pinLetter) \
  45. case pin##pinLetter##0: \
  46. case pin##pinLetter##1: \
  47. case pin##pinLetter##2: \
  48. case pin##pinLetter##3: \
  49. case pin##pinLetter##4: \
  50. case pin##pinLetter##5: \
  51. case pin##pinLetter##6: \
  52. case pin##pinLetter##7
  53. #define PIN_TEST_COL(pin) \
  54. if ( !( pin & ( 1 << ( matrix[0][col] % 10 ) ) \
  55. detectArray[matrix[row][col]]++; \
  56. break
  57. // ----- Variables -----
  58. uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
  59. // ----- Functions -----
  60. void matrix_pinSetup( uint8_t *matrix )
  61. {
  62. // Setup the variables
  63. uint8_t portA = 0x00;
  64. uint8_t portB = 0x00;
  65. uint8_t portC = 0x00;
  66. uint8_t portD = 0x00;
  67. uint8_t portE = 0x00;
  68. uint8_t portF = 0x00;
  69. uint8_t ddrA = 0x00;
  70. uint8_t ddrB = 0x00;
  71. uint8_t ddrC = 0x00;
  72. uint8_t ddrD = 0x00;
  73. uint8_t ddrE = 0x00;
  74. uint8_t ddrF = 0x00;
  75. // Loop through all the pin assignments, for the initial pin settings
  76. int row, col;
  77. // Rows
  78. for ( row = 1; row < sizeof(matrix); row++ ) {
  79. switch ( matrix[row][col] ) {
  80. PIN_CASE(A):
  81. PIN_SET_ROW(A);
  82. PIN_CASE(B):
  83. PIN_SET_ROW(B);
  84. PIN_CASE(C):
  85. PIN_SET_ROW(C);
  86. PIN_CASE(D):
  87. PIN_SET_ROW(D);
  88. PIN_CASE(E):
  89. PIN_SET_ROW(E);
  90. PIN_CASE(F):
  91. PIN_SET_ROW(F);
  92. default:
  93. continue;
  94. }
  95. }
  96. // Columns
  97. for ( col = 1; col < sizeof(matrix[0]); row++ ) {
  98. switch ( matrix[row][col] ) {
  99. PIN_CASE(A):
  100. PIN_SET_COL(A);
  101. PIN_CASE(B):
  102. PIN_SET_COL(B);
  103. PIN_CASE(C):
  104. PIN_SET_COL(C);
  105. PIN_CASE(D):
  106. PIN_SET_COL(D);
  107. PIN_CASE(E):
  108. PIN_SET_COL(E);
  109. PIN_CASE(F):
  110. PIN_SET_COL(F);
  111. default:
  112. continue;
  113. }
  114. }
  115. // Setting the pins
  116. DDRA = ddrA;
  117. DDRB = ddrB;
  118. DDRC = ddrC;
  119. DDRD = ddrD;
  120. DDRE = ddrE;
  121. DDRF = ddrF;
  122. PORTA = portA;
  123. PORTB = portB;
  124. PORTC = portC;
  125. PORTD = portD;
  126. PORTE = portE;
  127. PORTF = portF;
  128. }
  129. // TODO Proper matrix scanning
  130. void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
  131. {
  132. // Column Scan
  133. #if scanMode == scanCol
  134. uint8_t col = 1;
  135. uint8_t row = 1;
  136. for ( ; col < sizeof(matrix[1]); col++ ) {
  137. switch ( matrix[0][col] / 10 ) {
  138. case 0: // PINA
  139. PIN_TEST_COL(PINA);
  140. case 1: // PINB
  141. PIN_TEST_COL(PINB);
  142. case 2: // PINC
  143. PIN_TEST_COL(PINC);
  144. case 3: // PIND
  145. PIN_TEST_COL(PIND);
  146. case 4: // PINE
  147. PIN_TEST_COL(PINE);
  148. case 5: // PINF
  149. PIN_TEST_COL(PINF);
  150. }
  151. }
  152. #endif
  153. // Row Scan
  154. #if scanMode == scanRow
  155. #endif
  156. // Column Scan, Power Row
  157. #if scanMode == scanCol_powrRow
  158. #endif
  159. // Row Scan, Power Column
  160. #if scanMode == scanRow_powrCol
  161. #endif
  162. // Dual Scan
  163. #if scanMode == scanDual
  164. #endif
  165. }