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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /* Copyright (C) 2014-2016 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. // Compiler Includes
  23. #include <Lib/ScanLib.h>
  24. // Project Includes
  25. #include <cli.h>
  26. #include <kll_defs.h>
  27. #include <led.h>
  28. #include <print.h>
  29. #include <macro.h>
  30. #include <Lib/delay.h>
  31. // Local Includes
  32. #include "matrix_scan.h"
  33. // Matrix Configuration
  34. #include <matrix.h>
  35. // ----- Defines -----
  36. #if ( DebounceThrottleDiv_define > 0 )
  37. nat_ptr_t Matrix_divCounter = 0;
  38. #endif
  39. #if StrobeDelay_define > 0 && !defined( STROBE_DELAY )
  40. #define STROBE_DELAY StrobeDelay_define
  41. #endif
  42. // ----- Function Declarations -----
  43. // CLI Functions
  44. void cliFunc_matrixDebug( char* args );
  45. void cliFunc_matrixInfo( char* args );
  46. void cliFunc_matrixState( char* args );
  47. // ----- Variables -----
  48. // Scan Module command dictionary
  49. CLIDict_Entry( matrixDebug, "Enables matrix debug mode, prints out each scan code." NL "\t\tIf argument \033[35mT\033[0m is given, prints out each scan code state transition." );
  50. CLIDict_Entry( matrixInfo, "Print info about the configured matrix." );
  51. CLIDict_Entry( matrixState, "Prints out the current scan table N times." NL "\t\t \033[1mO\033[0m - Off, \033[1;33mP\033[0m - Press, \033[1;32mH\033[0m - Hold, \033[1;35mR\033[0m - Release, \033[1;31mI\033[0m - Invalid" );
  52. CLIDict_Def( matrixCLIDict, "Matrix Module Commands" ) = {
  53. CLIDict_Item( matrixDebug ),
  54. CLIDict_Item( matrixInfo ),
  55. CLIDict_Item( matrixState ),
  56. { 0, 0, 0 } // Null entry for dictionary end
  57. };
  58. // Debounce Array
  59. KeyState Matrix_scanArray[ Matrix_colsNum * Matrix_rowsNum ];
  60. // Ghost Arrays
  61. #ifdef GHOSTING_MATRIX
  62. KeyGhost Matrix_ghostArray[ Matrix_colsNum * Matrix_rowsNum ];
  63. uint8_t col_use[Matrix_colsNum], row_use[Matrix_rowsNum]; // used count
  64. uint8_t col_ghost[Matrix_colsNum], row_ghost[Matrix_rowsNum]; // marked as having ghost if 1
  65. uint8_t col_ghost_old[Matrix_colsNum], row_ghost_old[Matrix_rowsNum]; // old ghost state
  66. #endif
  67. // Matrix debug flag - If set to 1, for each keypress the scan code is displayed in hex
  68. // If set to 2, for each key state change, the scan code is displayed along with the state
  69. uint8_t matrixDebugMode = 0;
  70. // Matrix State Table Debug Counter - If non-zero display state table after every matrix scan
  71. uint16_t matrixDebugStateCounter = 0;
  72. // Matrix Scan Counters
  73. uint16_t matrixMaxScans = 0;
  74. uint16_t matrixCurScans = 0;
  75. uint16_t matrixPrevScans = 0;
  76. // System Timer used for delaying debounce decisions
  77. extern volatile uint32_t systick_millis_count;
  78. // ----- Functions -----
  79. // Pin action (Strobe, Sense, Strobe Setup, Sense Setup)
  80. // NOTE: This function is highly dependent upon the organization of the register map
  81. // Only guaranteed to work with Freescale MK20 series uCs
  82. uint8_t Matrix_pin( GPIO_Pin gpio, Type type )
  83. {
  84. // Register width is defined as size of a pointer
  85. unsigned int gpio_offset = gpio.port * 0x40 / sizeof(unsigned int*);
  86. unsigned int port_offset = gpio.port * 0x1000 / sizeof(unsigned int*) + gpio.pin;
  87. // Assumes 0x40 between GPIO Port registers and 0x1000 between PORT pin registers
  88. // See Lib/mk20dx.h
  89. volatile unsigned int *GPIO_PDDR = (unsigned int*)(&GPIOA_PDDR) + gpio_offset;
  90. #ifndef GHOSTING_MATRIX
  91. volatile unsigned int *GPIO_PSOR = (unsigned int*)(&GPIOA_PSOR) + gpio_offset;
  92. #endif
  93. volatile unsigned int *GPIO_PCOR = (unsigned int*)(&GPIOA_PCOR) + gpio_offset;
  94. volatile unsigned int *GPIO_PDIR = (unsigned int*)(&GPIOA_PDIR) + gpio_offset;
  95. volatile unsigned int *PORT_PCR = (unsigned int*)(&PORTA_PCR0) + port_offset;
  96. // Operation depends on Type
  97. switch ( type )
  98. {
  99. case Type_StrobeOn:
  100. #ifdef GHOSTING_MATRIX
  101. *GPIO_PCOR |= (1 << gpio.pin);
  102. *GPIO_PDDR |= (1 << gpio.pin); // output, low
  103. #else
  104. *GPIO_PSOR |= (1 << gpio.pin);
  105. #endif
  106. break;
  107. case Type_StrobeOff:
  108. #ifdef GHOSTING_MATRIX
  109. // Ghosting martix needs to put not used (off) strobes in high impedance state
  110. *GPIO_PDDR &= ~(1 << gpio.pin); // input, high Z state
  111. #endif
  112. *GPIO_PCOR |= (1 << gpio.pin);
  113. break;
  114. case Type_StrobeSetup:
  115. #ifdef GHOSTING_MATRIX
  116. *GPIO_PDDR &= ~(1 << gpio.pin); // input, high Z state
  117. *GPIO_PCOR |= (1 << gpio.pin);
  118. #else
  119. // Set as output pin
  120. *GPIO_PDDR |= (1 << gpio.pin);
  121. #endif
  122. // Configure pin with slow slew, high drive strength and GPIO mux
  123. *PORT_PCR = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  124. // Enabling open-drain if specified
  125. switch ( Matrix_type )
  126. {
  127. case Config_Opendrain:
  128. *PORT_PCR |= PORT_PCR_ODE;
  129. break;
  130. // Do nothing otherwise
  131. default:
  132. break;
  133. }
  134. break;
  135. case Type_Sense:
  136. #ifdef GHOSTING_MATRIX // inverted
  137. return *GPIO_PDIR & (1 << gpio.pin) ? 0 : 1;
  138. #else
  139. return *GPIO_PDIR & (1 << gpio.pin) ? 1 : 0;
  140. #endif
  141. case Type_SenseSetup:
  142. // Set as input pin
  143. *GPIO_PDDR &= ~(1 << gpio.pin);
  144. // Configure pin with passive filter and GPIO mux
  145. *PORT_PCR = PORT_PCR_PFE | PORT_PCR_MUX(1);
  146. // Pull resistor config
  147. switch ( Matrix_type )
  148. {
  149. case Config_Pullup:
  150. *PORT_PCR |= PORT_PCR_PE | PORT_PCR_PS;
  151. break;
  152. case Config_Pulldown:
  153. *PORT_PCR |= PORT_PCR_PE;
  154. break;
  155. // Do nothing otherwise
  156. default:
  157. break;
  158. }
  159. break;
  160. }
  161. return 0;
  162. }
  163. // Setup GPIO pins for matrix scanning
  164. void Matrix_setup()
  165. {
  166. // Register Matrix CLI dictionary
  167. CLI_registerDictionary( matrixCLIDict, matrixCLIDictName );
  168. // Setup Strobe Pins
  169. for ( uint8_t pin = 0; pin < Matrix_colsNum; pin++ )
  170. {
  171. Matrix_pin( Matrix_cols[ pin ], Type_StrobeSetup );
  172. #ifdef GHOSTING_MATRIX
  173. col_use[pin] = 0;
  174. col_ghost[pin] = 0;
  175. col_ghost_old[pin] = 0;
  176. #endif
  177. }
  178. // Setup Sense Pins
  179. for ( uint8_t pin = 0; pin < Matrix_rowsNum; pin++ )
  180. {
  181. Matrix_pin( Matrix_rows[ pin ], Type_SenseSetup );
  182. #ifdef GHOSTING_MATRIX
  183. row_use[pin] = 0;
  184. row_ghost[pin] = 0;
  185. row_ghost_old[pin] = 0;
  186. #endif
  187. }
  188. // Clear out Debounce Array
  189. for ( uint8_t item = 0; item < Matrix_maxKeys; item++ )
  190. {
  191. Matrix_scanArray[ item ].prevState = KeyState_Off;
  192. Matrix_scanArray[ item ].curState = KeyState_Off;
  193. Matrix_scanArray[ item ].activeCount = 0;
  194. Matrix_scanArray[ item ].inactiveCount = DebounceDivThreshold_define; // Start at 'off' steady state
  195. Matrix_scanArray[ item ].prevDecisionTime = 0;
  196. #ifdef GHOSTING_MATRIX
  197. Matrix_ghostArray[ item ].prev = KeyState_Off;
  198. Matrix_ghostArray[ item ].cur = KeyState_Off;
  199. Matrix_ghostArray[ item ].saved = KeyState_Off;
  200. #endif
  201. }
  202. // Clear scan stats counters
  203. matrixMaxScans = 0;
  204. matrixPrevScans = 0;
  205. }
  206. void Matrix_keyPositionDebug( KeyPosition pos )
  207. {
  208. // Depending on the state, use a different flag + color
  209. switch ( pos )
  210. {
  211. case KeyState_Off:
  212. print("\033[1mO\033[0m");
  213. break;
  214. case KeyState_Press:
  215. print("\033[1;33mP\033[0m");
  216. break;
  217. case KeyState_Hold:
  218. print("\033[1;32mH\033[0m");
  219. break;
  220. case KeyState_Release:
  221. print("\033[1;35mR\033[0m");
  222. break;
  223. case KeyState_Invalid:
  224. default:
  225. print("\033[1;31mI\033[0m");
  226. break;
  227. }
  228. }
  229. // Scan the matrix for keypresses
  230. // NOTE: scanNum should be reset to 0 after a USB send (to reset all the counters)
  231. void Matrix_scan( uint16_t scanNum )
  232. {
  233. #if ( DebounceThrottleDiv_define > 0 )
  234. // Scan-rate throttling
  235. // By scanning using a divider, the scan rate slowed down
  236. // DebounceThrottleDiv_define == 1 means -> /2 or half scan rate
  237. // This helps with bouncy switches on fast uCs
  238. if ( !( Matrix_divCounter++ & (1 << ( DebounceThrottleDiv_define - 1 )) ) )
  239. return;
  240. #endif
  241. // Increment stats counters
  242. if ( scanNum > matrixMaxScans ) matrixMaxScans = scanNum;
  243. if ( scanNum == 0 )
  244. {
  245. matrixPrevScans = matrixCurScans;
  246. matrixCurScans = 0;
  247. }
  248. else
  249. {
  250. matrixCurScans++;
  251. }
  252. // Read systick for event scheduling
  253. uint8_t currentTime = (uint8_t)systick_millis_count;
  254. // For each strobe, scan each of the sense pins
  255. for ( uint8_t strobe = 0; strobe < Matrix_colsNum; strobe++ )
  256. {
  257. #ifdef STROBE_DELAY
  258. uint32_t start = micros();
  259. while ((micros() - start) < STROBE_DELAY);
  260. #endif
  261. // Strobe Pin
  262. Matrix_pin( Matrix_cols[ strobe ], Type_StrobeOn );
  263. #ifdef STROBE_DELAY
  264. start = micros();
  265. while ((micros() - start) < STROBE_DELAY);
  266. #endif
  267. // Scan each of the sense pins
  268. for ( uint8_t sense = 0; sense < Matrix_rowsNum; sense++ )
  269. {
  270. // Key position
  271. uint8_t key = Matrix_colsNum * sense + strobe;
  272. KeyState *state = &Matrix_scanArray[ key ];
  273. // If first scan, reset state
  274. if ( scanNum == 0 )
  275. {
  276. // Set previous state, and reset current state
  277. state->prevState = state->curState;
  278. state->curState = KeyState_Invalid;
  279. }
  280. // Signal Detected
  281. // Increment count and right shift opposing count
  282. // This means there is a maximum of scan 13 cycles on a perfect off to on transition
  283. // (coming from a steady state 0xFFFF off scans)
  284. // Somewhat longer with switch bounciness
  285. // The advantage of this is that the count is ongoing and never needs to be reset
  286. // State still needs to be kept track of to deal with what to send to the Macro module
  287. if ( Matrix_pin( Matrix_rows[ sense ], Type_Sense ) )
  288. {
  289. // Only update if not going to wrap around
  290. if ( state->activeCount < DebounceDivThreshold_define ) state->activeCount += 1;
  291. state->inactiveCount >>= 1;
  292. }
  293. // Signal Not Detected
  294. else
  295. {
  296. // Only update if not going to wrap around
  297. if ( state->inactiveCount < DebounceDivThreshold_define ) state->inactiveCount += 1;
  298. state->activeCount >>= 1;
  299. }
  300. // Check for state change if it hasn't been set
  301. // But only if enough time has passed since last state change
  302. // Only check if the minimum number of scans has been met
  303. // the current state is invalid
  304. // and either active or inactive count is over the debounce threshold
  305. if ( state->curState == KeyState_Invalid )
  306. {
  307. // Determine time since last decision
  308. uint8_t lastTransition = currentTime - state->prevDecisionTime;
  309. // Attempt state transition
  310. switch ( state->prevState )
  311. {
  312. case KeyState_Press:
  313. case KeyState_Hold:
  314. if ( state->activeCount > state->inactiveCount )
  315. {
  316. state->curState = KeyState_Hold;
  317. }
  318. else
  319. {
  320. // If not enough time has passed since Hold
  321. // Keep previous state
  322. if ( lastTransition < MinDebounceTime_define )
  323. {
  324. //warn_print("FAST Release stopped");
  325. state->curState = state->prevState;
  326. continue;
  327. }
  328. state->curState = KeyState_Release;
  329. }
  330. break;
  331. case KeyState_Release:
  332. case KeyState_Off:
  333. if ( state->activeCount > state->inactiveCount )
  334. {
  335. // If not enough time has passed since Hold
  336. // Keep previous state
  337. if ( lastTransition < MinDebounceTime_define )
  338. {
  339. //warn_print("FAST Press stopped");
  340. state->curState = state->prevState;
  341. continue;
  342. }
  343. state->curState = KeyState_Press;
  344. }
  345. else
  346. {
  347. state->curState = KeyState_Off;
  348. }
  349. break;
  350. case KeyState_Invalid:
  351. default:
  352. erro_print("Matrix scan bug!! Report me!");
  353. break;
  354. }
  355. // Update decision time
  356. state->prevDecisionTime = currentTime;
  357. // Send keystate to macro module
  358. #ifndef GHOSTING_MATRIX
  359. Macro_keyState( key, state->curState );
  360. #endif
  361. // Matrix Debug, only if there is a state change
  362. if ( matrixDebugMode && state->curState != state->prevState )
  363. {
  364. // Basic debug output
  365. if ( matrixDebugMode == 1 && state->curState == KeyState_Press )
  366. {
  367. printHex( key );
  368. print(" ");
  369. }
  370. // State transition debug output
  371. else if ( matrixDebugMode == 2 )
  372. {
  373. printHex( key );
  374. Matrix_keyPositionDebug( state->curState );
  375. print(" ");
  376. }
  377. }
  378. }
  379. }
  380. // Unstrobe Pin
  381. Matrix_pin( Matrix_cols[ strobe ], Type_StrobeOff );
  382. }
  383. // Matrix ghosting check and elimination
  384. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  385. #ifdef GHOSTING_MATRIX
  386. // strobe = column, sense = row
  387. // Count (rows) use for columns
  388. for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
  389. {
  390. uint8_t used = 0;
  391. for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
  392. {
  393. uint8_t key = Matrix_colsNum * row + col;
  394. KeyState *state = &Matrix_scanArray[ key ];
  395. if ( keyOn(state->curState) )
  396. used++;
  397. }
  398. col_use[col] = used;
  399. col_ghost_old[col] = col_ghost[col];
  400. col_ghost[col] = 0; // clear
  401. }
  402. // Count (columns) use for rows
  403. for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
  404. {
  405. uint8_t used = 0;
  406. for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
  407. {
  408. uint8_t key = Matrix_colsNum * row + col;
  409. KeyState *state = &Matrix_scanArray[ key ];
  410. if ( keyOn(state->curState) )
  411. used++;
  412. }
  413. row_use[row] = used;
  414. row_ghost_old[row] = row_ghost[row];
  415. row_ghost[row] = 0; // clear
  416. }
  417. // Check if matrix has ghost
  418. // Happens when key is pressed and some other key is pressed in same row and another in same column
  419. for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
  420. {
  421. for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
  422. {
  423. uint8_t key = Matrix_colsNum * row + col;
  424. KeyState *state = &Matrix_scanArray[ key ];
  425. if ( keyOn(state->curState) && col_use[col] >= 2 && row_use[row] >= 2 )
  426. {
  427. // mark col and row as having ghost
  428. col_ghost[col] = 1;
  429. row_ghost[row] = 1;
  430. }
  431. }
  432. }
  433. // Send keys
  434. for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
  435. {
  436. for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
  437. {
  438. uint8_t key = Matrix_colsNum * row + col;
  439. KeyState *state = &Matrix_scanArray[ key ];
  440. KeyGhost *st = &Matrix_ghostArray[ key ];
  441. // col or row is ghosting (crossed)
  442. uint8_t ghost = (col_ghost[col] > 0 || row_ghost[row] > 0) ? 1 : 0;
  443. uint8_t ghost_old = (col_ghost_old[col] > 0 || row_ghost_old[row] > 0) ? 1 : 0;
  444. ghost = ghost || ghost_old ? 1 : 0;
  445. st->prev = st->cur; // previous
  446. // save state if no ghost or outside ghosted area
  447. if ( ghost == 0 )
  448. st->saved = state->curState; // save state if no ghost
  449. // final
  450. // use saved state if ghosting, or current if not
  451. st->cur = ghost > 0 ? st->saved : state->curState;
  452. // Send keystate to macro module
  453. KeyPosition k = !st->cur
  454. ? (!st->prev ? KeyState_Off : KeyState_Release)
  455. : ( st->prev ? KeyState_Hold : KeyState_Press);
  456. Macro_keyState( key, k );
  457. }
  458. }
  459. #endif
  460. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  461. // State Table Output Debug
  462. if ( matrixDebugStateCounter > 0 )
  463. {
  464. // Decrement counter
  465. matrixDebugStateCounter--;
  466. // Output stats on number of scans being done per USB send
  467. print( NL );
  468. info_msg("Max scans: ");
  469. printHex( matrixMaxScans );
  470. print( NL );
  471. info_msg("Previous scans: ");
  472. printHex( matrixPrevScans );
  473. print( NL );
  474. // Output current scan number
  475. info_msg("Scan Number: ");
  476. printHex( scanNum );
  477. print( NL );
  478. // Display the state info for each key
  479. print("<key>:<previous state><current state> <active count> <inactive count>");
  480. for ( uint8_t key = 0; key < Matrix_maxKeys; key++ )
  481. {
  482. // Every 4 keys, put a newline
  483. if ( key % 4 == 0 )
  484. print( NL );
  485. print("\033[1m0x");
  486. printHex_op( key, 2 );
  487. print("\033[0m");
  488. print(":");
  489. Matrix_keyPositionDebug( Matrix_scanArray[ key ].prevState );
  490. Matrix_keyPositionDebug( Matrix_scanArray[ key ].curState );
  491. print(" 0x");
  492. printHex_op( Matrix_scanArray[ key ].activeCount, 4 );
  493. print(" 0x");
  494. printHex_op( Matrix_scanArray[ key ].inactiveCount, 4 );
  495. print(" ");
  496. }
  497. print( NL );
  498. }
  499. }
  500. // Called by parent scan module whenever the available current changes
  501. // current - mA
  502. void Matrix_currentChange( unsigned int current )
  503. {
  504. // TODO - Any potential power savings?
  505. }
  506. // ----- CLI Command Functions -----
  507. void cliFunc_matrixInfo( char* args )
  508. {
  509. print( NL );
  510. info_msg("Columns: ");
  511. printHex( Matrix_colsNum );
  512. print( NL );
  513. info_msg("Rows: ");
  514. printHex( Matrix_rowsNum );
  515. print( NL );
  516. info_msg("Max Keys: ");
  517. printHex( Matrix_maxKeys );
  518. }
  519. void cliFunc_matrixDebug( char* args )
  520. {
  521. // Parse number from argument
  522. // NOTE: Only first argument is used
  523. char* arg1Ptr;
  524. char* arg2Ptr;
  525. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  526. // Set the matrix debug flag depending on the argument
  527. // If no argument, set to scan code only
  528. // If set to T, set to state transition
  529. switch ( arg1Ptr[0] )
  530. {
  531. // T as argument
  532. case 'T':
  533. case 't':
  534. matrixDebugMode = matrixDebugMode != 2 ? 2 : 0;
  535. break;
  536. // No argument
  537. case '\0':
  538. matrixDebugMode = matrixDebugMode != 1 ? 1 : 0;
  539. break;
  540. // Invalid argument
  541. default:
  542. return;
  543. }
  544. print( NL );
  545. info_msg("Matrix Debug Mode: ");
  546. printInt8( matrixDebugMode );
  547. }
  548. void cliFunc_matrixState( char* args )
  549. {
  550. // Parse number from argument
  551. // NOTE: Only first argument is used
  552. char* arg1Ptr;
  553. char* arg2Ptr;
  554. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  555. // Default to 1 if no argument is given
  556. matrixDebugStateCounter = 1;
  557. if ( arg1Ptr[0] != '\0' )
  558. {
  559. matrixDebugStateCounter = (uint16_t)numToInt( arg1Ptr );
  560. }
  561. }