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.

main.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Copyright (C) 2011 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include <avr/io.h>
  22. #include <avr/pgmspace.h>
  23. #include <avr/interrupt.h>
  24. #include <util/delay.h>
  25. //#include "usb_keyboard.h"
  26. // TEMP INCLUDES
  27. #include "usb_keyboard_debug.h"
  28. #include <print.h>
  29. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  30. // Sleep defined in milliseconds
  31. // Number of keys
  32. #define KEYBOARD_SIZE 63
  33. #define KEYPAD_SIZE 16
  34. // Debouncing Defines
  35. #define SAMPLE_THRESHOLD 100
  36. #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
  37. // Verified Keypress Defines
  38. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  39. // Drive Pin Defines
  40. #define DRIVE_reg_1 PORTD
  41. #define DRIVE_reg_2 PORTD
  42. #define DRIVE_reg_3 PORTD
  43. #define DRIVE_reg_4 PORTD
  44. #define DRIVE_reg_5 PORTD
  45. #define DRIVE_reg_6 PORTD
  46. #define DRIVE_reg_7 PORTE
  47. #define DRIVE_reg_8 PORTE
  48. #define DRIVE_reg_9 PORTE
  49. #define DRIVE_reg_10 <blank>
  50. #define DRIVE_reg_11 <blank>
  51. #define DRIVE_reg_12 <blank>
  52. #define DRIVE_pin_1 2
  53. #define DRIVE_pin_2 3
  54. #define DRIVE_pin_3 4
  55. #define DRIVE_pin_4 5
  56. #define DRIVE_pin_5 6
  57. #define DRIVE_pin_6 7
  58. #define DRIVE_pin_7 0
  59. #define DRIVE_pin_8 1
  60. #define DRIVE_pin_9 6
  61. #define DRIVE_pin_10 <blank>
  62. #define DRIVE_pin_11 <blank>
  63. #define DRIVE_pin_12 <blank>
  64. // Detect Pin/Group Defines
  65. #define DETECT_group_1 1
  66. #define DETECT_group_2 2
  67. #define DETECT_group_3 3
  68. #define DETECT_group_4 4
  69. #define DETECT_group_5 5
  70. #define DETECT_group_6 6
  71. #define DETECT_group_7 7
  72. #define DETECT_group_8 8
  73. #define DETECT_group_9 9
  74. #define DETECT_group_10 <blank>
  75. #define DETECT_group_11 <blank>
  76. #define DETECT_group_12 <blank>
  77. #define DETECT_group_size_1 7
  78. #define DETECT_group_size_2 7
  79. #define DETECT_group_size_3 6
  80. #define DETECT_group_size_4 8
  81. #define DETECT_group_size_5 7
  82. #define DETECT_group_size_6 7
  83. #define DETECT_group_size_7 8
  84. #define DETECT_group_size_8 8
  85. #define DETECT_group_size_9 4
  86. #define DETECT_group_size_10 <blank>
  87. #define DETECT_group_size_11 <blank>
  88. #define DETECT_group_size_12 <blank>
  89. /*
  90. #define DETECT_group_array_1 {{KEY_ESC,KEY_CTRL,KEY_CAPS_LOCK,KEY_SHIFT},{0,1,0,1}}
  91. #define DETECT_group_array_2 {{KEY_BACKSPACE,KEY_UP,KEY_DOWN,KEY_A,KEY_INSERT,KEY_ALT,KEY_Z,KEY_RIGHT},{0,0,0,0,0,1,0,0}}
  92. #define DETECT_group_array_3 {{KEY_TILDE,KEY_DELETE,KEY_LEFT,KEY_SPACE,KEY_X,KEY_S,KEY_TAB,KEY_1},{0,0,0,0,0,0,0,0}}
  93. #define DETECT_group_array_4 {{KEY_SLASH,KEY_RIGHT_BRACE,KEY_ENTER,KEY_D,KEY_2,KEY_Q,KEY_C},{0,0,0,0,0,0,0}}
  94. #define DETECT_group_array_5 {{KEY_EQUAL,KEY_LEFT_BRACE,KEY_QUOTE,KEY_F,KEY_3,KEY_W,KEY_V},{0,0,0,0,0,0,0}}
  95. #define DETECT_group_array_6 {{KEY_MINUS,KEY_P,KEY_SEMICOLON,KEY_G,KEY_4,KEY_E,KEY_B,KEY_BACKSLASH},{0,0,0,0,0,0,0,0}}
  96. #define DETECT_group_array_7 {{KEY_8,KEY_U,KEY_K,KEY_7,KEY_Y,KEY_COMMA},{0,0,0,0,0,0}}
  97. #define DETECT_group_array_8 {{KEY_9,KEY_I,KEY_PERIOD,KEY_J,KEY_6,KEY_T,KEY_M},{0,0,0,0,0,0,0}}
  98. #define DETECT_group_array_9 {{KEY_0,KEY_O,KEY_L,KEY_H,KEY_5,KEY_R,KEY_N},{0,0,0,0,0,0,0}}
  99. */
  100. // Switch Codes
  101. #define DETECT_group_array_1 {55,22,6 ,40,43,27,11}
  102. #define DETECT_group_array_2 {56,23,7 ,41,58,26,10}
  103. #define DETECT_group_array_3 {57,24,8 ,42,25,9}
  104. #define DETECT_group_array_4 {54,21,5 ,39,44,28,12,59}
  105. #define DETECT_group_array_5 {53,20,4 ,38,45,29,13}
  106. #define DETECT_group_array_6 {52,19,3 ,37,46,30,14}
  107. #define DETECT_group_array_7 {51,18,2 ,36,61,31,15,63}
  108. #define DETECT_group_array_8 {50,17,1 ,35,47,32,16,62}
  109. #define DETECT_group_array_9 {48,49,33,34} // 49/60 are the same line
  110. #define DETECT_group_array_10 <blank>
  111. #define DETECT_group_array_11 <blank>
  112. #define DETECT_group_array_12 <blank>
  113. // Drive Macros (Generally don't need to be changed), except for maybe DRIVE_DETECT
  114. #define DRIVE_DETECT(reg,pin,group) \
  115. reg &= ~(1 << pin); \
  116. detection(group); \
  117. reg |= (1 << pin);
  118. #define DD_CASE(number) \
  119. case number:\
  120. DRIVE_DETECT(DRIVE_reg_##number, DRIVE_pin_##number, DETECT_group_##number)
  121. #define DD_CASE_ORD(number) \
  122. DD_CASE(number) \
  123. break;
  124. #define DD_CASE_END(number,var) \
  125. DD_CASE(number) \
  126. var = -1; \
  127. break;
  128. // Updates the current detection sample and last sample bit
  129. // Detection Macros (Probably don't need to be changed, but depending the matrix, may have to be)
  130. // Determine if key is either normal or a modifier
  131. #define DET_GROUP_CHECK(index,test) \
  132. if ( test ) { \
  133. keyDetectArray[groupArray[index]]++; \
  134. }
  135. // XXX - Detection Groups
  136. // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
  137. // Inverse logic applies for the PINs
  138. // Used for 1 detection group (Special group)
  139. #define DET_GROUP_1 \
  140. DET_GROUP_CHECK(0,!( PINB & (1 << 7) )) \
  141. DET_GROUP_CHECK(1,!( PINC & (1 << 0) )) \
  142. DET_GROUP_CHECK(2,!( PIND & (1 << 0) )) \
  143. DET_GROUP_CHECK(3,!( PIND & (1 << 1) )) \
  144. // Used for 4 detection groups (Skips J1 P9)
  145. #define DET_GROUP_2 \
  146. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  147. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  148. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  149. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  150. DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
  151. DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
  152. DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
  153. // Used for 1 detection group (Skips J1 P6 and J1 P9)
  154. #define DET_GROUP_3 \
  155. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  156. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  157. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  158. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  159. DET_GROUP_CHECK(4,!( PINB & (1 << 4) )) \
  160. DET_GROUP_CHECK(5,!( PINB & (1 << 5) )) \
  161. // Used for 3 detection groups (No skips, except special group 1)
  162. #define DET_GROUP_4 \
  163. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  164. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  165. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  166. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  167. DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
  168. DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
  169. DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
  170. DET_GROUP_CHECK(7,!( PINB & (1 << 6) )) \
  171. // Combines the DET_GROUP_Xs above for the given groupArray
  172. #define DET_GROUP(group,det_group) \
  173. case group: \
  174. { \
  175. uint8_t groupArray[DETECT_group_size_##group] = DETECT_group_array_##group; \
  176. _delay_us(1); \
  177. DET_GROUP_##det_group \
  178. } \
  179. break;
  180. // Loop over all of the sampled keys of the given array
  181. // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
  182. // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
  183. #define DEBOUNCE_ASSESS(table,size) \
  184. for ( uint8_t key = 1; key < size + 1; key++ ) {\
  185. table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
  186. } \
  187. // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
  188. // Other Bits: Pressed state sample counter
  189. uint8_t keyDetectArray[KEYBOARD_SIZE + 1];
  190. uint8_t keypadDetectArray[KEYPAD_SIZE + 1];
  191. uint16_t sendKeypressCounter = 0;
  192. volatile uint8_t sendKeypresses = 0;
  193. static const uint8_t defaultMap[] = { 0,
  194. KEY_INSERT,
  195. KEY_1,
  196. KEY_2,
  197. KEY_3,
  198. KEY_4,
  199. KEY_5,
  200. KEY_6,
  201. KEY_7,
  202. KEY_8,
  203. KEY_9,
  204. KEY_0,
  205. KEY_MINUS,
  206. KEY_EQUAL,
  207. KEY_BACKSLASH,
  208. KEY_ALT,
  209. KEY_TAB,
  210. KEY_Q,
  211. KEY_W,
  212. KEY_E,
  213. KEY_R,
  214. KEY_T,
  215. KEY_Y,
  216. KEY_U,
  217. KEY_I,
  218. KEY_O,
  219. KEY_P,
  220. KEY_LEFT_BRACE,
  221. KEY_RIGHT_BRACE,
  222. KEY_DELETE,
  223. KEY_UP,
  224. KEY_CTRL,
  225. KEY_CAPS_LOCK,
  226. KEY_A,
  227. KEY_S,
  228. KEY_D,
  229. KEY_F,
  230. KEY_G,
  231. KEY_H,
  232. KEY_J,
  233. KEY_K,
  234. KEY_L,
  235. KEY_SEMICOLON,
  236. KEY_QUOTE,
  237. KEY_ENTER,
  238. KEY_DOWN,
  239. KEY_ESC,
  240. KEY_LEFT_SHIFT,
  241. KEY_Z,
  242. KEY_X,
  243. KEY_C,
  244. KEY_V,
  245. KEY_B,
  246. KEY_N,
  247. KEY_M,
  248. KEY_COMMA,
  249. KEY_PERIOD,
  250. KEY_SLASH,
  251. KEY_RIGHT_SHIFT,
  252. KEY_LEFT,
  253. KEY_RIGHT,
  254. KEY_SPACE };
  255. // Scan Code Decoder (for debug)
  256. void printDecodeScancode( int code )
  257. {
  258. static const char* decodeArray[] = { "", "", "", "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Enter", "Esc", "Backspace", "Tab", "Space", "-_", "=+", "[{", "]}", "\\", "#", ";:", "'\"", "`~", ",<", ".>", "/?", "Caps Lock", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Print Screen", "Scroll Lock", "Pause", "Insert", "Home", "Page Up", "Delete", "End", "Page Down", "Right", "Left", "Down", "Up", "Num Lock", "K1", "K2", "K3", "K4", "K5", "K6", "K7", "K8", "K9", "K0", "K." };
  259. print_P( decodeArray[ defaultMap[code] ] );
  260. }
  261. void detection( int group )
  262. {
  263. // XXX Modify for different detection groups <-> groupArray mappings
  264. switch ( group ) {
  265. DET_GROUP(1,2)
  266. DET_GROUP(2,2)
  267. DET_GROUP(3,3)
  268. DET_GROUP(4,4)
  269. DET_GROUP(5,2)
  270. DET_GROUP(6,2)
  271. DET_GROUP(7,4)
  272. DET_GROUP(8,4)
  273. DET_GROUP(9,1)
  274. }
  275. }
  276. // XXX This part is configurable
  277. inline void pinSetup(void)
  278. {
  279. // For each pin, 0=input, 1=output
  280. DDRA = 0x00;
  281. DDRB = 0x00;
  282. DDRC = 0x00;
  283. DDRD = 0xFC;
  284. DDRE = 0x43;
  285. DDRF = 0x00;
  286. // Setting pins to either high or pull-up resistor
  287. PORTA = 0xFF;
  288. PORTB = 0xFF;
  289. PORTC = 0x01;
  290. PORTD = 0xFF;
  291. PORTE = 0xC3;
  292. PORTF = 0xFF;
  293. }
  294. int main( void )
  295. {
  296. // Setup with 16 MHz clock
  297. CPU_PRESCALE( 0 );
  298. // Configuring Pins
  299. pinSetup();
  300. // Initialize the USB, and then wait for the host to set configuration.
  301. // If the Teensy is powered without a PC connected to the USB port,
  302. // this will wait forever.
  303. usb_init();
  304. while ( !usb_configured() ) /* wait */ ;
  305. // Wait an extra second for the PC's operating system to load drivers
  306. // and do whatever it does to actually be ready for input
  307. _delay_ms(1000);
  308. // Setup ISR Timer for flagging a kepress send to USB
  309. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  310. //
  311. TCCR0A = 0x00;
  312. TCCR0B = 0x03;
  313. TIMSK0 = (1 << TOIE0);
  314. // Main Detection Loop
  315. int8_t group = 1;
  316. uint8_t count = 0;
  317. for ( ;;group++ ) {
  318. // XXX Change number of ORDs if number of lines (RowsxColumns) differ
  319. // Determine which keys are being pressed
  320. switch ( group ) {
  321. DD_CASE_ORD(1)
  322. DD_CASE_ORD(2)
  323. DD_CASE_ORD(3)
  324. DD_CASE_ORD(4)
  325. DD_CASE_ORD(5)
  326. DD_CASE_ORD(6)
  327. DD_CASE_ORD(7)
  328. DD_CASE_ORD(8)
  329. DD_CASE_END(9,group)
  330. }
  331. // Check all Keyboard keys first
  332. if ( group != -1 )
  333. continue;
  334. // Check Keypad keys
  335. // TODO
  336. // Check count to see if the sample threshold may have been reached, otherwise collect more data
  337. count++;
  338. if ( count < MAX_SAMPLES )
  339. continue;
  340. // Reset Sample Counter
  341. count = 0;
  342. // Assess debouncing sample table
  343. DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
  344. //DEBOUNCE_ASSESS(keypadDetectArray,KEYPAD_SIZE)
  345. // Send keypresses over USB if the ISR has signalled that it's time
  346. if ( !sendKeypresses )
  347. continue;
  348. // Detect Valid Keypresses - TODO
  349. uint8_t validKeys = 0;
  350. for ( uint8_t key = 0; key < KEYBOARD_SIZE + 1; key++ ) {
  351. //phex(keyDetectArray[key]);
  352. //print ("|");
  353. if ( keyDetectArray[key] & (1 << 7) ) {
  354. //print("0x");
  355. //phex( key );
  356. pint8( key );
  357. print(" ");
  358. // Too many keys
  359. if ( validKeys == 6 )
  360. break;
  361. keyboard_keys[validKeys++] = defaultMap[key];
  362. }
  363. }
  364. print(":\n");
  365. // TODO undo potentially old keys
  366. for ( uint8_t c = validKeys; c < 6; c++ )
  367. keyboard_keys[c] = 0;
  368. // Debugging Output
  369. //phex(PINA);
  370. //phex(PINF);
  371. //print("\n");
  372. // Print out the current keys pressed
  373. /*
  374. if ( keyDetectCount > 0 ) {
  375. print("Switch: ");
  376. for ( int c = 0; c < keyDetectCount; c++ ) {
  377. print("0x");
  378. phex( keyDetectArray[c] );
  379. print("|");
  380. //printDecodeScancode( keyDetectArray[c] );
  381. print(" ");
  382. }
  383. print("\n");
  384. }
  385. if ( modifiers ) {
  386. print("Modifiers: ");
  387. phex( modifiers );
  388. print("\n");
  389. }
  390. */
  391. // After going through each of the key groups, send the detected keys and modifiers
  392. // Currently limited to the USB spec (6 keys + modifiers)
  393. // Making sure to pass zeros when there are no keys being pressed
  394. /*
  395. for ( int c = 0; c < 6 && c < keyDetectCount; c++ )
  396. keyboard_keys[c] = c < keyDetectCount ? keyDetectArray[c] : 0;
  397. // Modifiers
  398. keyboard_modifier_keys = modifiers;
  399. */
  400. // Send keypresses
  401. usb_keyboard_send();
  402. // Clear sendKeypresses Flag
  403. sendKeypresses = 0;
  404. }
  405. // usb_keyboard_press(KEY_B, KEY_SHIFT);
  406. return 0;
  407. }
  408. ISR( TIMER0_OVF_vect )
  409. {
  410. sendKeypressCounter++;
  411. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  412. sendKeypressCounter = 0;
  413. sendKeypresses = 1;
  414. }
  415. }