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.

output_com.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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/OutputLib.h>
  24. // Project Includes
  25. #include <cli.h>
  26. #include <led.h>
  27. #include <print.h>
  28. #include <scan_loop.h>
  29. // USB Includes
  30. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  31. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
  32. #include <arm/uart_serial.h>
  33. #include <arm/usb_dev.h>
  34. #include <arm/usb_keyboard.h>
  35. #include <arm/usb_serial.h>
  36. #endif
  37. // Local Includes
  38. #include "output_com.h"
  39. // ----- Macros -----
  40. // Used to build a bitmap lookup table from a byte addressable array
  41. #define byteLookup( byte ) case (( byte ) * ( 8 )): bytePosition = byte; byteShift = 0; break; \
  42. case (( byte ) * ( 8 ) + ( 1 )): bytePosition = byte; byteShift = 1; break; \
  43. case (( byte ) * ( 8 ) + ( 2 )): bytePosition = byte; byteShift = 2; break; \
  44. case (( byte ) * ( 8 ) + ( 3 )): bytePosition = byte; byteShift = 3; break; \
  45. case (( byte ) * ( 8 ) + ( 4 )): bytePosition = byte; byteShift = 4; break; \
  46. case (( byte ) * ( 8 ) + ( 5 )): bytePosition = byte; byteShift = 5; break; \
  47. case (( byte ) * ( 8 ) + ( 6 )): bytePosition = byte; byteShift = 6; break; \
  48. case (( byte ) * ( 8 ) + ( 7 )): bytePosition = byte; byteShift = 7; break
  49. // ----- Function Declarations -----
  50. void cliFunc_kbdProtocol( char* args );
  51. void cliFunc_outputDebug( char* args );
  52. void cliFunc_readLEDs ( char* args );
  53. void cliFunc_readUART ( char* args );
  54. void cliFunc_sendKeys ( char* args );
  55. void cliFunc_sendUART ( char* args );
  56. void cliFunc_setKeys ( char* args );
  57. void cliFunc_setMod ( char* args );
  58. // ----- Variables -----
  59. // Output Module command dictionary
  60. CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" );
  61. CLIDict_Entry( outputDebug, "Toggle Output Debug mode." );
  62. CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
  63. CLIDict_Entry( readUART, "Read UART buffer until empty." );
  64. CLIDict_Entry( sendKeys, "Send the prepared list of USB codes and modifier byte." );
  65. CLIDict_Entry( sendUART, "Send characters over UART0." );
  66. CLIDict_Entry( setKeys, "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m." );
  67. CLIDict_Entry( setMod, "Set the modfier byte:" NL "\t\t1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI" );
  68. CLIDict_Def( outputCLIDict, "USB Module Commands" ) = {
  69. CLIDict_Item( kbdProtocol ),
  70. CLIDict_Item( outputDebug ),
  71. CLIDict_Item( readLEDs ),
  72. CLIDict_Item( readUART ),
  73. CLIDict_Item( sendKeys ),
  74. CLIDict_Item( sendUART ),
  75. CLIDict_Item( setKeys ),
  76. CLIDict_Item( setMod ),
  77. { 0, 0, 0 } // Null entry for dictionary end
  78. };
  79. // Which modifier keys are currently pressed
  80. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  81. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  82. uint8_t USBKeys_Modifiers = 0;
  83. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  84. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  85. uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS];
  86. uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
  87. // System Control and Consumer Control 1KRO containers
  88. uint8_t USBKeys_SysCtrl;
  89. uint16_t USBKeys_ConsCtrl;
  90. // The number of keys sent to the usb in the array
  91. uint8_t USBKeys_Sent = 0;
  92. uint8_t USBKeys_SentCLI = 0;
  93. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  94. volatile uint8_t USBKeys_LEDs = 0;
  95. // Protocol setting from the host.
  96. // 0 - Boot Mode
  97. // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
  98. volatile uint8_t USBKeys_Protocol = 1;
  99. // Indicate if USB should send update
  100. // OS only needs update if there has been a change in state
  101. USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
  102. // the idle configuration, how often we send the report to the
  103. // host (ms * 4) even when it hasn't changed
  104. uint8_t USBKeys_Idle_Config = 125;
  105. // count until idle timeout
  106. uint8_t USBKeys_Idle_Count = 0;
  107. // Indicates whether the Output module is fully functional
  108. // 0 - Not fully functional, 1 - Fully functional
  109. // 0 is often used to show that a USB cable is not plugged in (but has power)
  110. volatile uint8_t Output_Available = 0;
  111. // Debug control variable for Output modules
  112. // 0 - Debug disabled (default)
  113. // 1 - Debug enabled
  114. uint8_t Output_DebugMode = 0;
  115. // mA - Set by outside module if not using USB (i.e. Interconnect)
  116. // Generally set to 100 mA (low power) or 500 mA (high power)
  117. uint16_t Output_ExtCurrent_Available = 0;
  118. // mA - Set by USB module (if exists)
  119. // Initially 100 mA, but may be negotiated higher (e.g. 500 mA)
  120. uint16_t Output_USBCurrent_Available = 0;
  121. // ----- Capabilities -----
  122. // Set Boot Keyboard Protocol
  123. void Output_kbdProtocolBoot_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  124. {
  125. // Display capability name
  126. if ( stateType == 0xFF && state == 0xFF )
  127. {
  128. print("Output_kbdProtocolBoot()");
  129. return;
  130. }
  131. // Only set if necessary
  132. if ( USBKeys_Protocol == 0 )
  133. return;
  134. // TODO Analog inputs
  135. // Only set on key press
  136. if ( stateType != 0x01 )
  137. return;
  138. // Flush the key buffers
  139. Output_flushBuffers();
  140. // Set the keyboard protocol to Boot Mode
  141. USBKeys_Protocol = 0;
  142. }
  143. // Set NKRO Keyboard Protocol
  144. void Output_kbdProtocolNKRO_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  145. {
  146. // Display capability name
  147. if ( stateType == 0xFF && state == 0xFF )
  148. {
  149. print("Output_kbdProtocolNKRO()");
  150. return;
  151. }
  152. // Only set if necessary
  153. if ( USBKeys_Protocol == 1 )
  154. return;
  155. // TODO Analog inputs
  156. // Only set on key press
  157. if ( stateType != 0x01 )
  158. return;
  159. // Flush the key buffers
  160. Output_flushBuffers();
  161. // Set the keyboard protocol to NKRO Mode
  162. USBKeys_Protocol = 1;
  163. }
  164. // Sends a Consumer Control code to the USB Output buffer
  165. void Output_consCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  166. {
  167. // Display capability name
  168. if ( stateType == 0xFF && state == 0xFF )
  169. {
  170. print("Output_consCtrlSend(consCode)");
  171. return;
  172. }
  173. // Not implemented in Boot Mode
  174. if ( USBKeys_Protocol == 0 )
  175. {
  176. warn_print("Consumer Control is not implemented for Boot Mode");
  177. return;
  178. }
  179. // TODO Analog inputs
  180. // Only indicate USB has changed if either a press or release has occured
  181. if ( state == 0x01 || state == 0x03 )
  182. USBKeys_Changed |= USBKeyChangeState_Consumer;
  183. // Only send keypresses if press or hold state
  184. if ( stateType == 0x00 && state == 0x03 ) // Release state
  185. {
  186. USBKeys_ConsCtrl = 0;
  187. return;
  188. }
  189. // Set consumer control code
  190. USBKeys_ConsCtrl = *(uint16_t*)(&args[0]);
  191. }
  192. // Ignores the given key status update
  193. // Used to prevent fall-through, this is the None keyword in KLL
  194. void Output_noneSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  195. {
  196. // Display capability name
  197. if ( stateType == 0xFF && state == 0xFF )
  198. {
  199. print("Output_noneSend()");
  200. return;
  201. }
  202. // Nothing to do, because that's the point :P
  203. }
  204. // Sends a System Control code to the USB Output buffer
  205. void Output_sysCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  206. {
  207. // Display capability name
  208. if ( stateType == 0xFF && state == 0xFF )
  209. {
  210. print("Output_sysCtrlSend(sysCode)");
  211. return;
  212. }
  213. // Not implemented in Boot Mode
  214. if ( USBKeys_Protocol == 0 )
  215. {
  216. warn_print("System Control is not implemented for Boot Mode");
  217. return;
  218. }
  219. // TODO Analog inputs
  220. // Only indicate USB has changed if either a press or release has occured
  221. if ( state == 0x01 || state == 0x03 )
  222. USBKeys_Changed |= USBKeyChangeState_System;
  223. // Only send keypresses if press or hold state
  224. if ( stateType == 0x00 && state == 0x03 ) // Release state
  225. {
  226. USBKeys_SysCtrl = 0;
  227. return;
  228. }
  229. // Set system control code
  230. USBKeys_SysCtrl = args[0];
  231. }
  232. // Adds a single USB Code to the USB Output buffer
  233. // Argument #1: USB Code
  234. void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  235. {
  236. // Display capability name
  237. if ( stateType == 0xFF && state == 0xFF )
  238. {
  239. print("Output_usbCodeSend(usbCode)");
  240. return;
  241. }
  242. // Depending on which mode the keyboard is in the USB needs Press/Hold/Release events
  243. uint8_t keyPress = 0; // Default to key release, only used for NKRO
  244. switch ( USBKeys_Protocol )
  245. {
  246. case 0: // Boot Mode
  247. // TODO Analog inputs
  248. // Only indicate USB has changed if either a press or release has occured
  249. if ( state == 0x01 || state == 0x03 )
  250. USBKeys_Changed = USBKeyChangeState_MainKeys;
  251. // Only send keypresses if press or hold state
  252. if ( stateType == 0x00 && state == 0x03 ) // Release state
  253. return;
  254. break;
  255. case 1: // NKRO Mode
  256. // Only send press and release events
  257. if ( stateType == 0x00 && state == 0x02 ) // Hold state
  258. return;
  259. // Determine if setting or unsetting the bitfield (press == set)
  260. if ( stateType == 0x00 && state == 0x01 ) // Press state
  261. keyPress = 1;
  262. break;
  263. }
  264. // Get the keycode from arguments
  265. uint8_t key = args[0];
  266. // Depending on which mode the keyboard is in, USBKeys_Keys array is used differently
  267. // Boot mode - Maximum of 6 byte codes
  268. // NKRO mode - Each bit of the 26 byte corresponds to a key
  269. // Bits 0 - 45 (bytes 0 - 5) correspond to USB Codes 4 - 49 (Main)
  270. // Bits 48 - 161 (bytes 6 - 20) correspond to USB Codes 51 - 164 (Secondary)
  271. // Bits 168 - 213 (bytes 21 - 26) correspond to USB Codes 176 - 221 (Tertiary)
  272. // Bits 214 - 216 unused
  273. uint8_t bytePosition = 0;
  274. uint8_t byteShift = 0;
  275. switch ( USBKeys_Protocol )
  276. {
  277. case 0: // Boot Mode
  278. // Set the modifier bit if this key is a modifier
  279. if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
  280. {
  281. USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
  282. }
  283. // Normal USB Code
  284. else
  285. {
  286. // USB Key limit reached
  287. if ( USBKeys_Sent >= USB_BOOT_MAX_KEYS )
  288. {
  289. warn_print("USB Key limit reached");
  290. return;
  291. }
  292. // Make sure key is within the USB HID range
  293. if ( key <= 104 )
  294. {
  295. USBKeys_Keys[USBKeys_Sent++] = key;
  296. }
  297. // Invalid key
  298. else
  299. {
  300. warn_msg("USB Code above 104/0x68 in Boot Mode: ");
  301. printHex( key );
  302. print( NL );
  303. }
  304. }
  305. break;
  306. case 1: // NKRO Mode
  307. // Set the modifier bit if this key is a modifier
  308. if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
  309. {
  310. if ( keyPress )
  311. {
  312. USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
  313. }
  314. else // Release
  315. {
  316. USBKeys_Modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0
  317. }
  318. USBKeys_Changed |= USBKeyChangeState_Modifiers;
  319. break;
  320. }
  321. // First 6 bytes
  322. else if ( key >= 4 && key <= 49 )
  323. {
  324. // Lookup (otherwise division or multiple checks are needed to do alignment)
  325. // Starting at 0th position, each byte has 8 bits, starting at 4th bit
  326. uint8_t keyPos = key + (0 * 8 - 4); // Starting position in array, Ignoring 4 keys
  327. switch ( keyPos )
  328. {
  329. byteLookup( 0 );
  330. byteLookup( 1 );
  331. byteLookup( 2 );
  332. byteLookup( 3 );
  333. byteLookup( 4 );
  334. byteLookup( 5 );
  335. }
  336. USBKeys_Changed |= USBKeyChangeState_MainKeys;
  337. }
  338. // Next 14 bytes
  339. else if ( key >= 51 && key <= 155 )
  340. {
  341. // Lookup (otherwise division or multiple checks are needed to do alignment)
  342. // Starting at 6th byte position, each byte has 8 bits, starting at 51st bit
  343. uint8_t keyPos = key + (6 * 8 - 51); // Starting position in array
  344. switch ( keyPos )
  345. {
  346. byteLookup( 6 );
  347. byteLookup( 7 );
  348. byteLookup( 8 );
  349. byteLookup( 9 );
  350. byteLookup( 10 );
  351. byteLookup( 11 );
  352. byteLookup( 12 );
  353. byteLookup( 13 );
  354. byteLookup( 14 );
  355. byteLookup( 15 );
  356. byteLookup( 16 );
  357. byteLookup( 17 );
  358. byteLookup( 18 );
  359. byteLookup( 19 );
  360. }
  361. USBKeys_Changed |= USBKeyChangeState_SecondaryKeys;
  362. }
  363. // Next byte
  364. else if ( key >= 157 && key <= 164 )
  365. {
  366. // Lookup (otherwise division or multiple checks are needed to do alignment)
  367. uint8_t keyPos = key + (20 * 8 - 157); // Starting position in array, Ignoring 6 keys
  368. switch ( keyPos )
  369. {
  370. byteLookup( 20 );
  371. }
  372. USBKeys_Changed |= USBKeyChangeState_TertiaryKeys;
  373. }
  374. // Last 6 bytes
  375. else if ( key >= 176 && key <= 221 )
  376. {
  377. // Lookup (otherwise division or multiple checks are needed to do alignment)
  378. uint8_t keyPos = key + (21 * 8 - 176); // Starting position in array
  379. switch ( keyPos )
  380. {
  381. byteLookup( 21 );
  382. byteLookup( 22 );
  383. byteLookup( 23 );
  384. byteLookup( 24 );
  385. byteLookup( 25 );
  386. byteLookup( 26 );
  387. }
  388. USBKeys_Changed |= USBKeyChangeState_QuartiaryKeys;
  389. }
  390. // Received 0x00
  391. // This is a special USB Code that internally indicates a "break"
  392. // It is used to send "nothing" in order to break up sequences of USB Codes
  393. else if ( key == 0x00 )
  394. {
  395. USBKeys_Changed |= USBKeyChangeState_MainKeys;
  396. // Also flush out buffers just in case
  397. Output_flushBuffers();
  398. break;
  399. }
  400. // Invalid key
  401. else
  402. {
  403. warn_msg("USB Code not within 4-49 (0x4-0x31), 51-155 (0x33-0x9B), 157-164 (0x9D-0xA4), 176-221 (0xB0-0xDD) or 224-231 (0xE0-0xE7) NKRO Mode: ");
  404. printHex( key );
  405. print( NL );
  406. break;
  407. }
  408. // Set/Unset
  409. if ( keyPress )
  410. {
  411. USBKeys_Keys[bytePosition] |= (1 << byteShift);
  412. USBKeys_Sent++;
  413. }
  414. else // Release
  415. {
  416. USBKeys_Keys[bytePosition] &= ~(1 << byteShift);
  417. USBKeys_Sent++;
  418. }
  419. break;
  420. }
  421. }
  422. void Output_flashMode_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  423. {
  424. // Display capability name
  425. if ( stateType == 0xFF && state == 0xFF )
  426. {
  427. print("Output_flashMode()");
  428. return;
  429. }
  430. // Start flash mode
  431. Output_firmwareReload();
  432. }
  433. // ----- Functions -----
  434. // Flush Key buffers
  435. void Output_flushBuffers()
  436. {
  437. // Zero out USBKeys_Keys array
  438. for ( uint8_t c = 0; c < USB_NKRO_BITFIELD_SIZE_KEYS; c++ )
  439. USBKeys_Keys[ c ] = 0;
  440. // Zero out other key buffers
  441. USBKeys_ConsCtrl = 0;
  442. USBKeys_Modifiers = 0;
  443. USBKeys_SysCtrl = 0;
  444. }
  445. // USB Module Setup
  446. inline void Output_setup()
  447. {
  448. // Setup UART
  449. uart_serial_setup();
  450. // Initialize the USB
  451. // If a USB connection does not exist, just ignore it
  452. // All usb related functions will non-fatally fail if called
  453. // If the USB initialization is delayed, then functionality will just be delayed
  454. usb_init();
  455. // Register USB Output CLI dictionary
  456. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  457. // Flush key buffers
  458. Output_flushBuffers();
  459. }
  460. // USB Data Send
  461. inline void Output_send()
  462. {
  463. // USB status checks
  464. // Non-standard USB state manipulation, usually does nothing
  465. usb_device_check();
  466. // Boot Mode Only, unset stale keys
  467. if ( USBKeys_Protocol == 0 )
  468. for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
  469. USBKeys_Keys[c] = 0;
  470. // Send keypresses while there are pending changes
  471. while ( USBKeys_Changed )
  472. usb_keyboard_send();
  473. // Clear keys sent
  474. USBKeys_Sent = 0;
  475. // Signal Scan Module we are finished
  476. switch ( USBKeys_Protocol )
  477. {
  478. case 0: // Boot Mode
  479. // Clear modifiers only in boot mode
  480. USBKeys_Modifiers = 0;
  481. Scan_finishedWithOutput( USBKeys_Sent <= USB_BOOT_MAX_KEYS ? USBKeys_Sent : USB_BOOT_MAX_KEYS );
  482. break;
  483. case 1: // NKRO Mode
  484. Scan_finishedWithOutput( USBKeys_Sent );
  485. break;
  486. }
  487. }
  488. // Sets the device into firmware reload mode
  489. void Output_firmwareReload()
  490. {
  491. usb_device_reload();
  492. }
  493. // USB Input buffer available
  494. inline unsigned int Output_availablechar()
  495. {
  496. return usb_serial_available() + uart_serial_available();
  497. }
  498. // USB Get Character from input buffer
  499. inline int Output_getchar()
  500. {
  501. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  502. if ( usb_serial_available() > 0 )
  503. {
  504. return (int)usb_serial_getchar();
  505. }
  506. if ( uart_serial_available() > 0 )
  507. {
  508. return (int)uart_serial_getchar();
  509. }
  510. return -1;
  511. }
  512. // USB Send Character to output buffer
  513. inline int Output_putchar( char c )
  514. {
  515. // First send to UART
  516. uart_serial_putchar( c );
  517. // Then send to USB
  518. return usb_serial_putchar( c );
  519. }
  520. // USB Send String to output buffer, null terminated
  521. inline int Output_putstr( char* str )
  522. {
  523. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  524. uint16_t count = 0;
  525. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  526. uint32_t count = 0;
  527. #endif
  528. // Count characters until NULL character, then send the amount counted
  529. while ( str[count] != '\0' )
  530. count++;
  531. // First send to UART
  532. uart_serial_write( str, count );
  533. // Then send to USB
  534. return usb_serial_write( str, count );
  535. }
  536. // Soft Chip Reset
  537. inline void Output_softReset()
  538. {
  539. usb_device_software_reset();
  540. }
  541. // Update USB current (mA)
  542. // Triggers power change event
  543. void Output_update_usb_current( unsigned int current )
  544. {
  545. // Only signal if changed
  546. if ( current == Output_USBCurrent_Available )
  547. return;
  548. // Update USB current
  549. Output_USBCurrent_Available = current;
  550. unsigned int total_current = Output_current_available();
  551. info_msg("USB Available Current Changed. Total Available: ");
  552. printInt32( total_current );
  553. print(" mA" NL);
  554. // Send new total current to the Scan Modules
  555. Scan_currentChange( Output_current_available() );
  556. }
  557. // Update external current (mA)
  558. // Triggers power change event
  559. void Output_update_external_current( unsigned int current )
  560. {
  561. // Only signal if changed
  562. if ( current == Output_ExtCurrent_Available )
  563. return;
  564. // Update external current
  565. Output_ExtCurrent_Available = current;
  566. unsigned int total_current = Output_current_available();
  567. info_msg("External Available Current Changed. Total Available: ");
  568. printInt32( total_current );
  569. print(" mA" NL);
  570. // Send new total current to the Scan Modules
  571. Scan_currentChange( Output_current_available() );
  572. }
  573. // Power/Current Available
  574. unsigned int Output_current_available()
  575. {
  576. unsigned int total_current = 0;
  577. // Check for USB current source
  578. total_current += Output_USBCurrent_Available;
  579. // Check for external current source
  580. total_current += Output_ExtCurrent_Available;
  581. // XXX If the total available current is still 0
  582. // Set to 100 mA, which is generally a safe assumption at startup
  583. // before we've been able to determine actual available current
  584. if ( total_current == 0 )
  585. {
  586. total_current = 100;
  587. }
  588. return total_current;
  589. }
  590. // ----- CLI Command Functions -----
  591. void cliFunc_kbdProtocol( char* args )
  592. {
  593. print( NL );
  594. info_msg("Keyboard Protocol: ");
  595. printInt8( USBKeys_Protocol );
  596. }
  597. void cliFunc_outputDebug( char* args )
  598. {
  599. // Parse number from argument
  600. // NOTE: Only first argument is used
  601. char* arg1Ptr;
  602. char* arg2Ptr;
  603. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  604. // Default to 1 if no argument is given
  605. Output_DebugMode = 1;
  606. if ( arg1Ptr[0] != '\0' )
  607. {
  608. Output_DebugMode = (uint16_t)numToInt( arg1Ptr );
  609. }
  610. }
  611. void cliFunc_readLEDs( char* args )
  612. {
  613. print( NL );
  614. info_msg("LED State: ");
  615. printInt8( USBKeys_LEDs );
  616. }
  617. void cliFunc_readUART( char* args )
  618. {
  619. print( NL );
  620. // Read UART buffer until empty
  621. while ( uart_serial_available() > 0 )
  622. {
  623. char out[] = { (char)uart_serial_getchar(), '\0' };
  624. dPrint( out );
  625. }
  626. }
  627. void cliFunc_sendKeys( char* args )
  628. {
  629. // Copy USBKeys_KeysCLI to USBKeys_Keys
  630. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  631. {
  632. // TODO
  633. //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
  634. }
  635. USBKeys_Sent = USBKeys_SentCLI;
  636. // Set modifier byte
  637. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  638. }
  639. void cliFunc_sendUART( char* args )
  640. {
  641. // Write all args to UART
  642. uart_serial_write( args, lenStr( args ) );
  643. }
  644. void cliFunc_setKeys( char* args )
  645. {
  646. char* curArgs;
  647. char* arg1Ptr;
  648. char* arg2Ptr = args;
  649. // Parse up to USBKeys_MaxSize args (whichever is least)
  650. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
  651. {
  652. curArgs = arg2Ptr;
  653. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  654. // Stop processing args if no more are found
  655. if ( *arg1Ptr == '\0' )
  656. break;
  657. // Add the USB code to be sent
  658. // TODO
  659. //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
  660. }
  661. }
  662. void cliFunc_setMod( char* args )
  663. {
  664. // Parse number from argument
  665. // NOTE: Only first argument is used
  666. char* arg1Ptr;
  667. char* arg2Ptr;
  668. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  669. USBKeys_ModifiersCLI = numToInt( arg1Ptr );
  670. }