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_scan.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // Project Includes
  25. #include <print.h>
  26. // Local Includes
  27. #include "matrix_scan.h"
  28. // Matrix Configuration
  29. #include <matrix.h>
  30. // ----- Macros -----
  31. #define REG_SET(reg) reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
  32. #define PIN_SET_COL(pin) \
  33. switch ( scanMode ) { \
  34. case scanCol: \
  35. case scanCol_powrRow: \
  36. case scanDual: \
  37. REG_SET(port##pin); break; \
  38. case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  39. } \
  40. break
  41. #define PIN_SET_ROW(pin) \
  42. switch ( scanMode ) { \
  43. case scanRow: \
  44. case scanRow_powrCol: \
  45. case scanDual: \
  46. REG_SET(port##pin); break; \
  47. case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  48. } \
  49. break
  50. #define PIN_CASE(pinLetter) \
  51. case pin##pinLetter##0: \
  52. case pin##pinLetter##1: \
  53. case pin##pinLetter##2: \
  54. case pin##pinLetter##3: \
  55. case pin##pinLetter##4: \
  56. case pin##pinLetter##5: \
  57. case pin##pinLetter##6: \
  58. case pin##pinLetter##7
  59. #define PIN_TEST_COL(pin) \
  60. if ( !( pin & ( 1 << ( matrix[0*(MAX_ROW_SIZE+1)+col] % 10 ) ) ) ) \
  61. detectArray[matrix[row*(MAX_ROW_SIZE+1)+col]]++; \
  62. break
  63. // ----- Variables -----
  64. // ----- Functions -----
  65. // Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
  66. inline void matrix_pinSetup( uint8_t *matrix )
  67. {
  68. // Setup the variables
  69. uint8_t portA = 0x00;
  70. uint8_t portB = 0x00;
  71. uint8_t portC = 0x00;
  72. uint8_t portD = 0x00;
  73. uint8_t portE = 0x00;
  74. uint8_t portF = 0x00;
  75. uint8_t ddrA = 0x00;
  76. uint8_t ddrB = 0x00;
  77. uint8_t ddrC = 0x00;
  78. uint8_t ddrD = 0x00;
  79. uint8_t ddrE = 0x00;
  80. uint8_t ddrF = 0x00;
  81. // Loop through all the pin assignments, for the initial pin settings
  82. uint16_t row, col;
  83. // Rows
  84. for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
  85. {
  86. // We can't pass 2D arrays, so just point to the first element and calculate directly
  87. switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
  88. {
  89. PIN_CASE(A):
  90. PIN_SET_ROW(A);
  91. PIN_CASE(B):
  92. PIN_SET_ROW(B);
  93. PIN_CASE(C):
  94. PIN_SET_ROW(C);
  95. PIN_CASE(D):
  96. PIN_SET_ROW(D);
  97. PIN_CASE(E):
  98. PIN_SET_ROW(E);
  99. PIN_CASE(F):
  100. PIN_SET_ROW(F);
  101. default:
  102. continue;
  103. }
  104. }
  105. // Columns
  106. for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
  107. {
  108. // We can't pass 2D arrays, so just point to the first element and calculate directly
  109. switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
  110. {
  111. PIN_CASE(A):
  112. PIN_SET_COL(A);
  113. PIN_CASE(B):
  114. PIN_SET_COL(B);
  115. PIN_CASE(C):
  116. PIN_SET_COL(C);
  117. PIN_CASE(D):
  118. PIN_SET_COL(D);
  119. PIN_CASE(E):
  120. PIN_SET_COL(E);
  121. PIN_CASE(F):
  122. PIN_SET_COL(F);
  123. default:
  124. continue;
  125. }
  126. }
  127. // Pin Status
  128. char tmpStr[6];
  129. info_print("Initial Matrix Pin Setup");
  130. info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
  131. print(" ");
  132. hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  133. hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  134. hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  135. hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  136. hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  137. hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  138. print("\n");
  139. info_print("portA portB portC portD portE portF");
  140. print(" ");
  141. hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  142. hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  143. hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  144. hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  145. hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  146. hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  147. print("\n");
  148. // Setting the pins
  149. #if defined(__AVR_AT90USB1286__)
  150. DDRA = ddrA;
  151. #endif
  152. DDRB = ddrB;
  153. DDRC = ddrC;
  154. DDRD = ddrD;
  155. DDRE = ddrE;
  156. DDRF = ddrF;
  157. #if defined(__AVR_AT90USB1286__)
  158. PORTA = portA;
  159. #endif
  160. PORTB = portB;
  161. PORTC = portC;
  162. PORTD = portD;
  163. PORTE = portE;
  164. PORTF = portF;
  165. }
  166. // TODO Proper matrix scanning
  167. inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
  168. {
  169. // Column Scan
  170. #if scanMode == scanCol
  171. uint16_t col = 1;
  172. uint16_t row = 1;
  173. for ( ; col < (MAX_ROW_SIZE+1) + 1; col++ )
  174. {
  175. switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
  176. {
  177. #if defined(__AVR_AT90USB1286__)
  178. case 0: // PINA
  179. PIN_TEST_COL(PINA);
  180. #endif
  181. case 1: // PINB
  182. PIN_TEST_COL(PINB);
  183. case 2: // PINC
  184. PIN_TEST_COL(PINC);
  185. case 3: // PIND
  186. PIN_TEST_COL(PIND);
  187. case 4: // PINE
  188. PIN_TEST_COL(PINE);
  189. case 5: // PINF
  190. PIN_TEST_COL(PINF);
  191. }
  192. }
  193. #endif
  194. // Row Scan
  195. #if scanMode == scanRow
  196. #endif
  197. // Column Scan, Power Row
  198. #if scanMode == scanCol_powrRow
  199. #endif
  200. // Row Scan, Power Column
  201. #if scanMode == scanRow_powrCol
  202. #endif
  203. // Dual Scan
  204. #if scanMode == scanDual
  205. #endif
  206. }