Kiibohd Controller
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

matrix_scan.c 9.3KB

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