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

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