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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. void cliFunc_usbInitTime( char* args );
  61. // ----- Variables -----
  62. // Output Module command dictionary
  63. CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" );
  64. CLIDict_Entry( outputDebug, "Toggle Output Debug mode." );
  65. CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
  66. CLIDict_Entry( sendKeys, "Send the prepared list of USB codes and modifier byte." );
  67. CLIDict_Entry( setKeys, "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m." );
  68. 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" );
  69. CLIDict_Entry( usbInitTime, "Displays the time in ms from usb_init() till the last setup call." );
  70. CLIDict_Def( outputCLIDict, "USB Module Commands" ) = {
  71. CLIDict_Item( kbdProtocol ),
  72. CLIDict_Item( outputDebug ),
  73. CLIDict_Item( readLEDs ),
  74. CLIDict_Item( sendKeys ),
  75. CLIDict_Item( setKeys ),
  76. CLIDict_Item( setMod ),
  77. CLIDict_Item( usbInitTime ),
  78. { 0, 0, 0 } // Null entry for dictionary end
  79. };
  80. // Which modifier keys are currently pressed
  81. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  82. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  83. uint8_t USBKeys_Modifiers = 0;
  84. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  85. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  86. uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS];
  87. uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
  88. // System Control and Consumer Control 1KRO containers
  89. uint8_t USBKeys_SysCtrl;
  90. uint16_t USBKeys_ConsCtrl;
  91. // The number of keys sent to the usb in the array
  92. uint8_t USBKeys_Sent = 0;
  93. uint8_t USBKeys_SentCLI = 0;
  94. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  95. volatile uint8_t USBKeys_LEDs = 0;
  96. // Currently pressed mouse buttons, bitmask, 0 represents no buttons pressed
  97. volatile uint16_t USBMouse_Buttons = 0;
  98. // Relative mouse axis movement, stores pending movement
  99. volatile uint16_t USBMouse_Relative_x = 0;
  100. volatile uint16_t USBMouse_Relative_y = 0;
  101. // Protocol setting from the host.
  102. // 0 - Boot Mode
  103. // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
  104. volatile uint8_t USBKeys_Protocol = USBProtocol_define;
  105. // Indicate if USB should send update
  106. // OS only needs update if there has been a change in state
  107. USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
  108. // Indicate if USB should send update
  109. USBMouseChangeState USBMouse_Changed = 0;
  110. // the idle configuration, how often we send the report to the
  111. // host (ms * 4) even when it hasn't changed
  112. // 0 - Disables
  113. uint8_t USBKeys_Idle_Config = 0;
  114. // Count until idle timeout
  115. uint32_t USBKeys_Idle_Expiry = 0;
  116. uint8_t USBKeys_Idle_Count = 0;
  117. // Indicates whether the Output module is fully functional
  118. // 0 - Not fully functional, 1 - Fully functional
  119. // 0 is often used to show that a USB cable is not plugged in (but has power)
  120. volatile uint8_t Output_Available = 0;
  121. // Debug control variable for Output modules
  122. // 0 - Debug disabled (default)
  123. // 1 - Debug enabled
  124. uint8_t Output_DebugMode = 0;
  125. // mA - Set by outside module if not using USB (i.e. Interconnect)
  126. // Generally set to 100 mA (low power) or 500 mA (high power)
  127. uint16_t Output_ExtCurrent_Available = 0;
  128. // mA - Set by USB module (if exists)
  129. // Initially 100 mA, but may be negotiated higher (e.g. 500 mA)
  130. uint16_t Output_USBCurrent_Available = 0;
  131. // USB Init Time (ms) - usb_init()
  132. volatile uint32_t USBInit_TimeStart;
  133. volatile uint32_t USBInit_TimeEnd;
  134. volatile uint16_t USBInit_Ticks;
  135. // ----- Capabilities -----
  136. // Set Boot Keyboard Protocol
  137. void Output_kbdProtocolBoot_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  138. {
  139. #if enableKeyboard_define == 1
  140. // Display capability name
  141. if ( stateType == 0xFF && state == 0xFF )
  142. {
  143. print("Output_kbdProtocolBoot()");
  144. return;
  145. }
  146. // Only set if necessary
  147. if ( USBKeys_Protocol == 0 )
  148. return;
  149. // TODO Analog inputs
  150. // Only set on key press
  151. if ( stateType != 0x01 )
  152. return;
  153. // Flush the key buffers
  154. Output_flushBuffers();
  155. // Set the keyboard protocol to Boot Mode
  156. USBKeys_Protocol = 0;
  157. #endif
  158. }
  159. // Set NKRO Keyboard Protocol
  160. void Output_kbdProtocolNKRO_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  161. {
  162. #if enableKeyboard_define == 1
  163. // Display capability name
  164. if ( stateType == 0xFF && state == 0xFF )
  165. {
  166. print("Output_kbdProtocolNKRO()");
  167. return;
  168. }
  169. // Only set if necessary
  170. if ( USBKeys_Protocol == 1 )
  171. return;
  172. // TODO Analog inputs
  173. // Only set on key press
  174. if ( stateType != 0x01 )
  175. return;
  176. // Flush the key buffers
  177. Output_flushBuffers();
  178. // Set the keyboard protocol to NKRO Mode
  179. USBKeys_Protocol = 1;
  180. #endif
  181. }
  182. // Toggle Keyboard Protocol
  183. void Output_toggleKbdProtocol_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  184. {
  185. #if enableKeyboard_define == 1
  186. // Display capability name
  187. if ( stateType == 0xFF && state == 0xFF )
  188. {
  189. print("Output_toggleKbdProtocol()");
  190. return;
  191. }
  192. // Only toggle protocol if release state
  193. if ( stateType == 0x00 && state == 0x03 )
  194. {
  195. // Flush the key buffers
  196. Output_flushBuffers();
  197. // Toggle the keyboard protocol Mode
  198. USBKeys_Protocol = !USBKeys_Protocol;
  199. }
  200. #endif
  201. }
  202. // Sends a Consumer Control code to the USB Output buffer
  203. void Output_consCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  204. {
  205. #if enableKeyboard_define == 1
  206. // Display capability name
  207. if ( stateType == 0xFF && state == 0xFF )
  208. {
  209. print("Output_consCtrlSend(consCode)");
  210. return;
  211. }
  212. // TODO Analog inputs
  213. // Only indicate USB has changed if either a press or release has occured
  214. if ( state == 0x01 || state == 0x03 )
  215. USBKeys_Changed |= USBKeyChangeState_Consumer;
  216. // Only send keypresses if press or hold state
  217. if ( stateType == 0x00 && state == 0x03 ) // Release state
  218. {
  219. USBKeys_ConsCtrl = 0;
  220. return;
  221. }
  222. // Set consumer control code
  223. USBKeys_ConsCtrl = *(uint16_t*)(&args[0]);
  224. #endif
  225. }
  226. // Ignores the given key status update
  227. // Used to prevent fall-through, this is the None keyword in KLL
  228. void Output_noneSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  229. {
  230. // Display capability name
  231. if ( stateType == 0xFF && state == 0xFF )
  232. {
  233. print("Output_noneSend()");
  234. return;
  235. }
  236. // Nothing to do, because that's the point :P
  237. }
  238. // Sends a System Control code to the USB Output buffer
  239. void Output_sysCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  240. {
  241. #if enableKeyboard_define == 1
  242. // Display capability name
  243. if ( stateType == 0xFF && state == 0xFF )
  244. {
  245. print("Output_sysCtrlSend(sysCode)");
  246. return;
  247. }
  248. // TODO Analog inputs
  249. // Only indicate USB has changed if either a press or release has occured
  250. if ( state == 0x01 || state == 0x03 )
  251. USBKeys_Changed |= USBKeyChangeState_System;
  252. // Only send keypresses if press or hold state
  253. if ( stateType == 0x00 && state == 0x03 ) // Release state
  254. {
  255. USBKeys_SysCtrl = 0;
  256. return;
  257. }
  258. // Set system control code
  259. USBKeys_SysCtrl = args[0];
  260. #endif
  261. }
  262. // Adds a single USB Code to the USB Output buffer
  263. // Argument #1: USB Code
  264. void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  265. {
  266. #if enableKeyboard_define == 1
  267. // Display capability name
  268. if ( stateType == 0xFF && state == 0xFF )
  269. {
  270. print("Output_usbCodeSend(usbCode)");
  271. return;
  272. }
  273. // Depending on which mode the keyboard is in the USB needs Press/Hold/Release events
  274. uint8_t keyPress = 0; // Default to key release, only used for NKRO
  275. switch ( USBKeys_Protocol )
  276. {
  277. case 0: // Boot Mode
  278. // TODO Analog inputs
  279. // Only indicate USB has changed if either a press or release has occured
  280. if ( state == 0x01 || state == 0x03 )
  281. USBKeys_Changed = USBKeyChangeState_MainKeys;
  282. // Only send keypresses if press or hold state
  283. if ( stateType == 0x00 && state == 0x03 ) // Release state
  284. return;
  285. break;
  286. case 1: // NKRO Mode
  287. // Only send press and release events
  288. if ( stateType == 0x00 && state == 0x02 ) // Hold state
  289. return;
  290. // Determine if setting or unsetting the bitfield (press == set)
  291. if ( stateType == 0x00 && state == 0x01 ) // Press state
  292. keyPress = 1;
  293. break;
  294. }
  295. // Get the keycode from arguments
  296. uint8_t key = args[0];
  297. // Depending on which mode the keyboard is in, USBKeys_Keys array is used differently
  298. // Boot mode - Maximum of 6 byte codes
  299. // NKRO mode - Each bit of the 26 byte corresponds to a key
  300. // Bits 0 - 45 (bytes 0 - 5) correspond to USB Codes 4 - 49 (Main)
  301. // Bits 48 - 161 (bytes 6 - 20) correspond to USB Codes 51 - 164 (Secondary)
  302. // Bits 168 - 213 (bytes 21 - 26) correspond to USB Codes 176 - 221 (Tertiary)
  303. // Bits 214 - 216 unused
  304. uint8_t bytePosition = 0;
  305. uint8_t byteShift = 0;
  306. switch ( USBKeys_Protocol )
  307. {
  308. case 0: // Boot 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. USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
  313. }
  314. // Normal USB Code
  315. else
  316. {
  317. // USB Key limit reached
  318. if ( USBKeys_Sent >= USB_BOOT_MAX_KEYS )
  319. {
  320. warn_print("USB Key limit reached");
  321. return;
  322. }
  323. // Make sure key is within the USB HID range
  324. if ( key <= 104 )
  325. {
  326. USBKeys_Keys[USBKeys_Sent++] = key;
  327. }
  328. // Invalid key
  329. else
  330. {
  331. warn_msg("USB Code above 104/0x68 in Boot Mode: ");
  332. printHex( key );
  333. print( NL );
  334. }
  335. }
  336. break;
  337. case 1: // NKRO Mode
  338. // Set the modifier bit if this key is a modifier
  339. if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
  340. {
  341. if ( keyPress )
  342. {
  343. USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
  344. }
  345. else // Release
  346. {
  347. USBKeys_Modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0
  348. }
  349. USBKeys_Changed |= USBKeyChangeState_Modifiers;
  350. break;
  351. }
  352. // First 6 bytes
  353. else if ( key >= 4 && key <= 49 )
  354. {
  355. // Lookup (otherwise division or multiple checks are needed to do alignment)
  356. // Starting at 0th position, each byte has 8 bits, starting at 4th bit
  357. uint8_t keyPos = key + (0 * 8 - 4); // Starting position in array, Ignoring 4 keys
  358. switch ( keyPos )
  359. {
  360. byteLookup( 0 );
  361. byteLookup( 1 );
  362. byteLookup( 2 );
  363. byteLookup( 3 );
  364. byteLookup( 4 );
  365. byteLookup( 5 );
  366. }
  367. USBKeys_Changed |= USBKeyChangeState_MainKeys;
  368. }
  369. // Next 14 bytes
  370. else if ( key >= 51 && key <= 155 )
  371. {
  372. // Lookup (otherwise division or multiple checks are needed to do alignment)
  373. // Starting at 6th byte position, each byte has 8 bits, starting at 51st bit
  374. uint8_t keyPos = key + (6 * 8 - 51); // Starting position in array
  375. switch ( keyPos )
  376. {
  377. byteLookup( 6 );
  378. byteLookup( 7 );
  379. byteLookup( 8 );
  380. byteLookup( 9 );
  381. byteLookup( 10 );
  382. byteLookup( 11 );
  383. byteLookup( 12 );
  384. byteLookup( 13 );
  385. byteLookup( 14 );
  386. byteLookup( 15 );
  387. byteLookup( 16 );
  388. byteLookup( 17 );
  389. byteLookup( 18 );
  390. byteLookup( 19 );
  391. }
  392. USBKeys_Changed |= USBKeyChangeState_SecondaryKeys;
  393. }
  394. // Next byte
  395. else if ( key >= 157 && key <= 164 )
  396. {
  397. // Lookup (otherwise division or multiple checks are needed to do alignment)
  398. uint8_t keyPos = key + (20 * 8 - 157); // Starting position in array, Ignoring 6 keys
  399. switch ( keyPos )
  400. {
  401. byteLookup( 20 );
  402. }
  403. USBKeys_Changed |= USBKeyChangeState_TertiaryKeys;
  404. }
  405. // Last 6 bytes
  406. else if ( key >= 176 && key <= 221 )
  407. {
  408. // Lookup (otherwise division or multiple checks are needed to do alignment)
  409. uint8_t keyPos = key + (21 * 8 - 176); // Starting position in array
  410. switch ( keyPos )
  411. {
  412. byteLookup( 21 );
  413. byteLookup( 22 );
  414. byteLookup( 23 );
  415. byteLookup( 24 );
  416. byteLookup( 25 );
  417. byteLookup( 26 );
  418. }
  419. USBKeys_Changed |= USBKeyChangeState_QuartiaryKeys;
  420. }
  421. // Received 0x00
  422. // This is a special USB Code that internally indicates a "break"
  423. // It is used to send "nothing" in order to break up sequences of USB Codes
  424. else if ( key == 0x00 )
  425. {
  426. USBKeys_Changed |= USBKeyChangeState_MainKeys;
  427. // Also flush out buffers just in case
  428. Output_flushBuffers();
  429. break;
  430. }
  431. // Invalid key
  432. else
  433. {
  434. 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: ");
  435. printHex( key );
  436. print( NL );
  437. break;
  438. }
  439. // Set/Unset
  440. if ( keyPress )
  441. {
  442. USBKeys_Keys[bytePosition] |= (1 << byteShift);
  443. USBKeys_Sent++;
  444. }
  445. else // Release
  446. {
  447. USBKeys_Keys[bytePosition] &= ~(1 << byteShift);
  448. USBKeys_Sent++;
  449. }
  450. break;
  451. }
  452. #endif
  453. }
  454. void Output_flashMode_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  455. {
  456. // Display capability name
  457. if ( stateType == 0xFF && state == 0xFF )
  458. {
  459. print("Output_flashMode()");
  460. return;
  461. }
  462. // Start flash mode
  463. Output_firmwareReload();
  464. }
  465. #if enableMouse_define == 1
  466. // Sends a mouse command over the USB Output buffer
  467. // XXX This function *will* be changing in the future
  468. // If you use it, be prepared that your .kll files will break in the future (post KLL 0.5)
  469. // Argument #1: USB Mouse Button (16 bit)
  470. // Argument #2: USB X Axis (16 bit) relative
  471. // Argument #3: USB Y Axis (16 bit) relative
  472. void Output_usbMouse_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  473. {
  474. // Display capability name
  475. if ( stateType == 0xFF && state == 0xFF )
  476. {
  477. print("Output_usbMouse(mouseButton,relX,relY)");
  478. return;
  479. }
  480. // Determine which mouse button was sent
  481. // The USB spec defines up to a max of 0xFFFF buttons
  482. // The usual are:
  483. // 1 - Button 1 - (Primary)
  484. // 2 - Button 2 - (Secondary)
  485. // 3 - Button 3 - (Tertiary)
  486. uint16_t mouse_button = *(uint16_t*)(&args[0]);
  487. // X/Y Relative Axis
  488. uint16_t mouse_x = *(uint16_t*)(&args[2]);
  489. uint16_t mouse_y = *(uint16_t*)(&args[4]);
  490. // Adjust for bit shift
  491. uint16_t mouse_button_shift = mouse_button - 1;
  492. // Only send mouse button if in press or hold state
  493. if ( stateType == 0x00 && state == 0x03 ) // Release state
  494. {
  495. // Release
  496. if ( mouse_button )
  497. USBMouse_Buttons &= ~(1 << mouse_button_shift);
  498. }
  499. else
  500. {
  501. // Press or hold
  502. if ( mouse_button )
  503. USBMouse_Buttons |= (1 << mouse_button_shift);
  504. if ( mouse_x )
  505. USBMouse_Relative_x = mouse_x;
  506. if ( mouse_y )
  507. USBMouse_Relative_y = mouse_y;
  508. }
  509. // Trigger updates
  510. if ( mouse_button )
  511. USBMouse_Changed |= USBMouseChangeState_Buttons;
  512. if ( mouse_x || mouse_y )
  513. USBMouse_Changed |= USBMouseChangeState_Relative;
  514. }
  515. #endif
  516. // ----- Functions -----
  517. // Flush Key buffers
  518. void Output_flushBuffers()
  519. {
  520. // Zero out USBKeys_Keys array
  521. for ( uint8_t c = 0; c < USB_NKRO_BITFIELD_SIZE_KEYS; c++ )
  522. USBKeys_Keys[ c ] = 0;
  523. // Zero out other key buffers
  524. USBKeys_ConsCtrl = 0;
  525. USBKeys_Modifiers = 0;
  526. USBKeys_SysCtrl = 0;
  527. }
  528. // USB Module Setup
  529. inline void Output_setup()
  530. {
  531. // Initialize the USB
  532. // If a USB connection does not exist, just ignore it
  533. // All usb related functions will non-fatally fail if called
  534. // If the USB initialization is delayed, then functionality will just be delayed
  535. usb_init();
  536. // Register USB Output CLI dictionary
  537. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  538. // Flush key buffers
  539. Output_flushBuffers();
  540. }
  541. // USB Data Send
  542. inline void Output_send()
  543. {
  544. // USB status checks
  545. // Non-standard USB state manipulation, usually does nothing
  546. usb_device_check();
  547. // XXX - Behaves oddly on Mac OSX, might help with corrupted packets specific to OSX? -HaaTa
  548. /*
  549. // Check if idle count has been exceed, this forces usb_keyboard_send and usb_mouse_send to update
  550. // TODO Add joystick as well (may be endpoint specific, currently not kept track of)
  551. if ( usb_configuration && USBKeys_Idle_Config && (
  552. USBKeys_Idle_Expiry < systick_millis_count ||
  553. USBKeys_Idle_Expiry + USBKeys_Idle_Config * 4 >= systick_millis_count ) )
  554. {
  555. USBKeys_Changed = USBKeyChangeState_All;
  556. USBMouse_Changed = USBMouseChangeState_All;
  557. }
  558. */
  559. #if enableMouse_define == 1
  560. // Process mouse actions
  561. while ( USBMouse_Changed )
  562. usb_mouse_send();
  563. #endif
  564. #if enableKeyboard_define == 1
  565. // Boot Mode Only, unset stale keys
  566. if ( USBKeys_Protocol == 0 )
  567. for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
  568. USBKeys_Keys[c] = 0;
  569. // Send keypresses while there are pending changes
  570. while ( USBKeys_Changed )
  571. usb_keyboard_send();
  572. // Clear keys sent
  573. USBKeys_Sent = 0;
  574. // Signal Scan Module we are finished
  575. switch ( USBKeys_Protocol )
  576. {
  577. case 0: // Boot Mode
  578. // Clear modifiers only in boot mode
  579. USBKeys_Modifiers = 0;
  580. Scan_finishedWithOutput( USBKeys_Sent <= USB_BOOT_MAX_KEYS ? USBKeys_Sent : USB_BOOT_MAX_KEYS );
  581. break;
  582. case 1: // NKRO Mode
  583. Scan_finishedWithOutput( USBKeys_Sent );
  584. break;
  585. }
  586. #endif
  587. }
  588. // Sets the device into firmware reload mode
  589. inline void Output_firmwareReload()
  590. {
  591. usb_device_reload();
  592. }
  593. // USB Input buffer available
  594. inline unsigned int Output_availablechar()
  595. {
  596. #if enableVirtualSerialPort_define == 1
  597. return usb_serial_available();
  598. #else
  599. return 0;
  600. #endif
  601. }
  602. // USB Get Character from input buffer
  603. inline int Output_getchar()
  604. {
  605. #if enableVirtualSerialPort_define == 1
  606. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  607. return (int)usb_serial_getchar();
  608. #else
  609. return 0;
  610. #endif
  611. }
  612. // USB Send Character to output buffer
  613. inline int Output_putchar( char c )
  614. {
  615. #if enableVirtualSerialPort_define == 1
  616. return usb_serial_putchar( c );
  617. #else
  618. return 0;
  619. #endif
  620. }
  621. // USB Send String to output buffer, null terminated
  622. inline int Output_putstr( char* str )
  623. {
  624. #if enableVirtualSerialPort_define == 1
  625. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  626. uint16_t count = 0;
  627. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  628. uint32_t count = 0;
  629. #endif
  630. // Count characters until NULL character, then send the amount counted
  631. while ( str[count] != '\0' )
  632. count++;
  633. return usb_serial_write( str, count );
  634. #else
  635. return 0;
  636. #endif
  637. }
  638. // Soft Chip Reset
  639. inline void Output_softReset()
  640. {
  641. usb_device_software_reset();
  642. }
  643. // USB RawIO buffer available
  644. inline unsigned int Output_rawio_availablechar()
  645. {
  646. #if enableRawIO_define == 1
  647. return usb_rawio_available();
  648. #else
  649. return 0;
  650. #endif
  651. }
  652. // USB RawIO get buffer
  653. // XXX Must be a 64 byte buffer
  654. inline int Output_rawio_getbuffer( char* buffer )
  655. {
  656. #if enableRawIO_define == 1
  657. // No timeout, fail immediately
  658. return usb_rawio_rx( (void*)buffer, 0 );
  659. #else
  660. return 0;
  661. #endif
  662. }
  663. // USB RawIO send buffer
  664. // XXX Must be a 64 byte buffer
  665. inline int Output_rawio_sendbuffer( char* buffer )
  666. {
  667. #if enableRawIO_define == 1
  668. // No timeout, fail immediately
  669. return usb_rawio_tx( (void*)buffer, 0 );
  670. #else
  671. return 0;
  672. #endif
  673. }
  674. // Update USB current (mA)
  675. // Triggers power change event
  676. void Output_update_usb_current( unsigned int current )
  677. {
  678. // Only signal if changed
  679. if ( current == Output_USBCurrent_Available )
  680. return;
  681. // Update USB current
  682. Output_USBCurrent_Available = current;
  683. /* XXX Affects sleep states due to USB messages
  684. unsigned int total_current = Output_current_available();
  685. info_msg("USB Available Current Changed. Total Available: ");
  686. printInt32( total_current );
  687. print(" mA" NL);
  688. */
  689. // Send new total current to the Scan Modules
  690. Scan_currentChange( Output_current_available() );
  691. }
  692. // Update external current (mA)
  693. // Triggers power change event
  694. void Output_update_external_current( unsigned int current )
  695. {
  696. // Only signal if changed
  697. if ( current == Output_ExtCurrent_Available )
  698. return;
  699. // Update external current
  700. Output_ExtCurrent_Available = current;
  701. unsigned int total_current = Output_current_available();
  702. info_msg("External Available Current Changed. Total Available: ");
  703. printInt32( total_current );
  704. print(" mA" NL);
  705. // Send new total current to the Scan Modules
  706. Scan_currentChange( Output_current_available() );
  707. }
  708. // Power/Current Available
  709. unsigned int Output_current_available()
  710. {
  711. unsigned int total_current = 0;
  712. // Check for USB current source
  713. total_current += Output_USBCurrent_Available;
  714. // Check for external current source
  715. total_current += Output_ExtCurrent_Available;
  716. // XXX If the total available current is still 0
  717. // Set to 100 mA, which is generally a safe assumption at startup
  718. // before we've been able to determine actual available current
  719. if ( total_current == 0 )
  720. {
  721. total_current = 100;
  722. }
  723. return total_current;
  724. }
  725. // ----- CLI Command Functions -----
  726. void cliFunc_kbdProtocol( char* args )
  727. {
  728. print( NL );
  729. // Parse number from argument
  730. // NOTE: Only first argument is used
  731. char* arg1Ptr;
  732. char* arg2Ptr;
  733. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  734. if ( arg1Ptr[0] != '\0' )
  735. {
  736. uint8_t mode = (uint8_t)numToInt( arg1Ptr );
  737. // Do nothing if the argument was wrong
  738. if ( mode == 0 || mode == 1 )
  739. {
  740. USBKeys_Protocol = mode;
  741. info_msg("Setting Keyboard Protocol to: ");
  742. printInt8( USBKeys_Protocol );
  743. }
  744. }
  745. else
  746. {
  747. info_msg("Keyboard Protocol: ");
  748. printInt8( USBKeys_Protocol );
  749. }
  750. }
  751. void cliFunc_outputDebug( char* args )
  752. {
  753. // Parse number from argument
  754. // NOTE: Only first argument is used
  755. char* arg1Ptr;
  756. char* arg2Ptr;
  757. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  758. // Default to 1 if no argument is given
  759. Output_DebugMode = 1;
  760. if ( arg1Ptr[0] != '\0' )
  761. {
  762. Output_DebugMode = (uint16_t)numToInt( arg1Ptr );
  763. }
  764. }
  765. void cliFunc_readLEDs( char* args )
  766. {
  767. print( NL );
  768. info_msg("LED State: ");
  769. printInt8( USBKeys_LEDs );
  770. }
  771. void cliFunc_sendKeys( char* args )
  772. {
  773. // Copy USBKeys_KeysCLI to USBKeys_Keys
  774. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  775. {
  776. // TODO
  777. //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
  778. }
  779. USBKeys_Sent = USBKeys_SentCLI;
  780. // Set modifier byte
  781. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  782. }
  783. void cliFunc_setKeys( char* args )
  784. {
  785. char* curArgs;
  786. char* arg1Ptr;
  787. char* arg2Ptr = args;
  788. // Parse up to USBKeys_MaxSize args (whichever is least)
  789. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
  790. {
  791. curArgs = arg2Ptr;
  792. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  793. // Stop processing args if no more are found
  794. if ( *arg1Ptr == '\0' )
  795. break;
  796. // Add the USB code to be sent
  797. // TODO
  798. //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
  799. }
  800. }
  801. void cliFunc_setMod( char* args )
  802. {
  803. // Parse number from argument
  804. // NOTE: Only first argument is used
  805. char* arg1Ptr;
  806. char* arg2Ptr;
  807. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  808. USBKeys_ModifiersCLI = numToInt( arg1Ptr );
  809. }
  810. void cliFunc_usbInitTime( char* args )
  811. {
  812. // Calculate overall USB initialization time
  813. // XXX A protocol analyzer will be more accurate, however, this is built-in and easier to collect data
  814. print(NL);
  815. info_msg("USB Init Time: ");
  816. printInt32( USBInit_TimeEnd - USBInit_TimeStart );
  817. print(" ms - ");
  818. printInt16( USBInit_Ticks );
  819. print(" ticks");
  820. }