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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <util/delay.h>
  25. // Project Includes
  26. #include <print.h>
  27. // Local Includes
  28. #include "matrix_scan.h"
  29. // Matrix Configuration
  30. #include <matrix.h>
  31. // ----- Macros -----
  32. // -- pinSetup Macros --
  33. #define REG_SET(reg) reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
  34. #define PIN_SET_COL(pin,scan) \
  35. switch ( scan ) { \
  36. case scanCol: \
  37. case scanRow_powrCol: \
  38. case scanDual: \
  39. REG_SET(port##pin); break; \
  40. case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  41. } \
  42. break
  43. #define PIN_SET_ROW(pin,scan) \
  44. switch ( scan ) { \
  45. case scanRow: \
  46. case scanCol_powrRow: \
  47. case scanDual: \
  48. REG_SET(port##pin); break; \
  49. case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
  50. } \
  51. break
  52. #define PIN_CASE(pinLetter) \
  53. case pin##pinLetter##0: \
  54. case pin##pinLetter##1: \
  55. case pin##pinLetter##2: \
  56. case pin##pinLetter##3: \
  57. case pin##pinLetter##4: \
  58. case pin##pinLetter##5: \
  59. case pin##pinLetter##6: \
  60. case pin##pinLetter##7
  61. // -- Column Scan Macros --
  62. #define PIN_TEST_COL(pin) \
  63. scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
  64. if ( scanCode && !( pin & ( 1 << ( matrix[0*(MAX_ROW_SIZE+1)+col] % 10 ) ) ) ) \
  65. { \
  66. warn_print("YAY!"); \
  67. detectArray[scanCode]++; \
  68. } \
  69. break
  70. // -- Row Scan Macros --
  71. #define PIN_TEST_ROW(pin) \
  72. scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
  73. if ( scanCode && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) ) \
  74. detectArray[scanCode]++; \
  75. break
  76. // -- Scan Dual Macros --
  77. #define PIN_DUALTEST_ROW(pin) \
  78. scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
  79. if ( scanCode \
  80. && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) \
  81. && detectArray[scanCode] & 0x01 ) \
  82. { \
  83. detectArray[scanCode]++; \
  84. } \
  85. else \
  86. { \
  87. if ( detectArray[scanCode] & 0x01 ) \
  88. detectArray[scanCode]--; \
  89. } \
  90. break
  91. // ----- Variables -----
  92. // ----- Functions -----
  93. // Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
  94. void matrix_pinSetup( uint8_t *matrix, uint8_t scanType )
  95. {
  96. // Setup the variables
  97. uint8_t portA = 0x00;
  98. uint8_t portB = 0x00;
  99. uint8_t portC = 0x00;
  100. uint8_t portD = 0x00;
  101. uint8_t portE = 0x00;
  102. uint8_t portF = 0x00;
  103. uint8_t ddrA = 0x00;
  104. uint8_t ddrB = 0x00;
  105. uint8_t ddrC = 0x00;
  106. uint8_t ddrD = 0x00;
  107. uint8_t ddrE = 0x00;
  108. uint8_t ddrF = 0x00;
  109. // Loop through all the pin assignments, for the initial pin settings
  110. uint16_t row, col;
  111. // Rows
  112. for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
  113. {
  114. // We can't pass 2D arrays, so just point to the first element and calculate directly
  115. switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
  116. {
  117. PIN_CASE(A):
  118. PIN_SET_ROW(A, scanType);
  119. PIN_CASE(B):
  120. PIN_SET_ROW(B, scanType);
  121. PIN_CASE(C):
  122. PIN_SET_ROW(C, scanType);
  123. PIN_CASE(D):
  124. PIN_SET_ROW(D, scanType);
  125. PIN_CASE(E):
  126. PIN_SET_ROW(E, scanType);
  127. PIN_CASE(F):
  128. PIN_SET_ROW(F, scanType);
  129. default:
  130. continue;
  131. }
  132. }
  133. // Columns
  134. for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
  135. {
  136. // We can't pass 2D arrays, so just point to the first element and calculate directly
  137. switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
  138. {
  139. PIN_CASE(A):
  140. PIN_SET_COL(A, scanType);
  141. PIN_CASE(B):
  142. PIN_SET_COL(B, scanType);
  143. PIN_CASE(C):
  144. PIN_SET_COL(C, scanType);
  145. PIN_CASE(D):
  146. PIN_SET_COL(D, scanType);
  147. PIN_CASE(E):
  148. PIN_SET_COL(E, scanType);
  149. PIN_CASE(F):
  150. PIN_SET_COL(F, scanType);
  151. default:
  152. continue;
  153. }
  154. }
  155. // Pin Status
  156. if ( scanType == scanMode )
  157. {
  158. char tmpStr[6];
  159. info_print("Initial Matrix Pin Setup");
  160. info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
  161. print(" ");
  162. hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  163. hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  164. hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  165. hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  166. hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  167. hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  168. print("\n");
  169. info_print("portA portB portC portD portE portF");
  170. print(" ");
  171. hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  172. hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  173. hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  174. hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  175. hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  176. hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
  177. print("\n");
  178. int8ToStr( scanType, tmpStr );
  179. }
  180. // Setting the pins
  181. #if defined(__AVR_AT90USB1286__)
  182. DDRA = ddrA;
  183. #endif
  184. DDRB = ddrB;
  185. DDRC = ddrC;
  186. DDRD = ddrD;
  187. DDRE = ddrE;
  188. DDRF = ddrF;
  189. #if defined(__AVR_AT90USB1286__)
  190. PORTA = portA;
  191. #endif
  192. PORTB = portB;
  193. PORTC = portC;
  194. PORTD = portD;
  195. PORTE = portE;
  196. PORTF = portF;
  197. }
  198. // Scans the given matrix determined by the scanMode method
  199. inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
  200. {
  201. // Loop variables for all modes
  202. uint16_t col = 1;
  203. uint16_t row = 1;
  204. uint16_t scanCode = 0;
  205. // Column Scan and Column Scan, Power Row
  206. #if scanMode == scanCol || scanMode == scanCol_powrRow
  207. for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
  208. {
  209. // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
  210. // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
  211. switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
  212. REG_SET(port##pin); break; \
  213. {
  214. #if defined(__AVR_AT90USB1286__)
  215. case 0: // PINA
  216. PIN_TEST_COL(PINA);
  217. #endif
  218. case 1: // PINB
  219. PIN_TEST_COL(PINB);
  220. case 2: // PINC
  221. PIN_TEST_COL(PINC);
  222. case 3: // PIND
  223. PIN_TEST_COL(PIND);
  224. case 4: // PINE
  225. PIN_TEST_COL(PINE);
  226. case 5: // PINF
  227. PIN_TEST_COL(PINF);
  228. }
  229. }
  230. #endif // scanMode
  231. // Row Scan and Row Scan, Power Row
  232. #if scanMode == scanRow || scanMode == scanRow_powrCol
  233. for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
  234. {
  235. // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
  236. // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
  237. switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
  238. {
  239. #if defined(__AVR_AT90USB1286__)
  240. case 0: // PINA
  241. PIN_TEST_ROW(PINA);
  242. #endif
  243. case 1: // PINB
  244. PIN_TEST_ROW(PINB);
  245. case 2: // PINC
  246. PIN_TEST_ROW(PINC);
  247. case 3: // PIND
  248. PIN_TEST_ROW(PIND);
  249. case 4: // PINE
  250. PIN_TEST_ROW(PINE);
  251. case 5: // PINF
  252. PIN_TEST_ROW(PINF);
  253. }
  254. }
  255. #endif // scanMode
  256. // Dual Scan
  257. #if scanMode == scanDual
  258. // First do a scan of all of the columns, marking each one
  259. matrix_pinSetup( matrix, scanCol_powrRow );
  260. _delay_us( 1 );
  261. for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
  262. {
  263. // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
  264. // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
  265. switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
  266. {
  267. #if defined(__AVR_AT90USB1286__)
  268. case 0: // PINA
  269. PIN_TEST_COL(PINA);
  270. #endif
  271. case 1: // PINB
  272. PIN_TEST_COL(PINB);
  273. case 2: // PINC
  274. PIN_TEST_COL(PINC);
  275. case 3: // PIND
  276. PIN_TEST_COL(PIND);
  277. case 4: // PINE
  278. PIN_TEST_COL(PINE);
  279. case 5: // PINF
  280. PIN_TEST_COL(PINF);
  281. }
  282. }
  283. // Next, do a scan of all of the rows, clearing any "vague" keys (only detected on row, but not column, or vice-versa)
  284. // And marking any keys that are detected on the row and column
  285. matrix_pinSetup( matrix, scanRow_powrCol );
  286. _delay_us( 1 );
  287. col = 1;
  288. row = 1;
  289. for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
  290. {
  291. // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
  292. // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
  293. switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
  294. {
  295. #if defined(__AVR_AT90USB1286__)
  296. case 0: // PINA
  297. PIN_DUALTEST_ROW(PINA);
  298. #endif
  299. case 1: // PINB
  300. PIN_DUALTEST_ROW(PINB);
  301. case 2: // PINC
  302. PIN_DUALTEST_ROW(PINC);
  303. case 3: // PIND
  304. PIN_DUALTEST_ROW(PIND);
  305. case 4: // PINE
  306. PIN_DUALTEST_ROW(PINE);
  307. case 5: // PINF
  308. PIN_DUALTEST_ROW(PINF);
  309. }
  310. }
  311. #endif
  312. }