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

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