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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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_keys.h"
  26. #include "layouts.h"
  27. //#include "usb_keyboard.h"
  28. // TEMP INCLUDES
  29. #include "usb_keyboard_debug.h"
  30. #include <print.h>
  31. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  32. // Number of keys
  33. #define KEYBOARD_SIZE 63
  34. #define KEYPAD_SIZE 16
  35. // Debouncing Defines
  36. #define SAMPLE_THRESHOLD 100
  37. #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
  38. // Verified Keypress Defines
  39. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  40. // Drive Pin Defines
  41. #define DRIVE_reg_1 PORTD
  42. #define DRIVE_reg_2 PORTD
  43. #define DRIVE_reg_3 PORTD
  44. #define DRIVE_reg_4 PORTD
  45. #define DRIVE_reg_5 PORTD
  46. #define DRIVE_reg_6 PORTD
  47. #define DRIVE_reg_7 PORTE
  48. #define DRIVE_reg_8 PORTE
  49. #define DRIVE_reg_9 PORTE
  50. #define DRIVE_reg_10 <blank>
  51. #define DRIVE_reg_11 <blank>
  52. #define DRIVE_reg_12 <blank>
  53. #define DRIVE_pin_1 2
  54. #define DRIVE_pin_2 3
  55. #define DRIVE_pin_3 4
  56. #define DRIVE_pin_4 5
  57. #define DRIVE_pin_5 6
  58. #define DRIVE_pin_6 7
  59. #define DRIVE_pin_7 0
  60. #define DRIVE_pin_8 1
  61. #define DRIVE_pin_9 6
  62. #define DRIVE_pin_10 <blank>
  63. #define DRIVE_pin_11 <blank>
  64. #define DRIVE_pin_12 <blank>
  65. // Detect Pin/Group Defines
  66. #define DETECT_group_1 1
  67. #define DETECT_group_2 2
  68. #define DETECT_group_3 3
  69. #define DETECT_group_4 4
  70. #define DETECT_group_5 5
  71. #define DETECT_group_6 6
  72. #define DETECT_group_7 7
  73. #define DETECT_group_8 8
  74. #define DETECT_group_9 9
  75. #define DETECT_group_10 <blank>
  76. #define DETECT_group_11 <blank>
  77. #define DETECT_group_12 <blank>
  78. #define DETECT_group_size_1 7
  79. #define DETECT_group_size_2 7
  80. #define DETECT_group_size_3 6
  81. #define DETECT_group_size_4 8
  82. #define DETECT_group_size_5 7
  83. #define DETECT_group_size_6 7
  84. #define DETECT_group_size_7 8
  85. #define DETECT_group_size_8 8
  86. #define DETECT_group_size_9 4
  87. #define DETECT_group_size_10 <blank>
  88. #define DETECT_group_size_11 <blank>
  89. #define DETECT_group_size_12 <blank>
  90. // Switch Codes
  91. #define DETECT_group_array_1 {55,22,6 ,40,43,27,11}
  92. #define DETECT_group_array_2 {56,23,7 ,41,58,26,10}
  93. #define DETECT_group_array_3 {57,24,8 ,42,25,9}
  94. #define DETECT_group_array_4 {54,21,5 ,39,44,28,12,59}
  95. #define DETECT_group_array_5 {53,20,4 ,38,45,29,13}
  96. #define DETECT_group_array_6 {52,19,3 ,37,46,30,14}
  97. #define DETECT_group_array_7 {51,18,2 ,36,61,31,15,63}
  98. #define DETECT_group_array_8 {50,17,1 ,35,47,32,16,62}
  99. #define DETECT_group_array_9 {48,49,33,34} // 49/60 are the same line
  100. #define DETECT_group_array_10 <blank>
  101. #define DETECT_group_array_11 <blank>
  102. #define DETECT_group_array_12 <blank>
  103. // Drive Macros (Generally don't need to be changed), except for maybe DRIVE_DETECT
  104. #define DRIVE_DETECT(reg,pin,group) \
  105. reg &= ~(1 << pin); \
  106. detection(group); \
  107. reg |= (1 << pin);
  108. #define DD_CASE(number) \
  109. case number:\
  110. DRIVE_DETECT(DRIVE_reg_##number, DRIVE_pin_##number, DETECT_group_##number)
  111. #define DD_CASE_ORD(number) \
  112. DD_CASE(number) \
  113. break;
  114. #define DD_CASE_END(number,var) \
  115. DD_CASE(number) \
  116. var = -1; \
  117. break;
  118. // Updates the current detection sample and last sample bit
  119. // Detection Macros (Probably don't need to be changed, but depending the matrix, may have to be)
  120. // Determine if key is either normal or a modifier
  121. #define DET_GROUP_CHECK(index,test) \
  122. if ( test ) { \
  123. keyDetectArray[groupArray[index]]++; \
  124. }
  125. // XXX - Detection Groups
  126. // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
  127. // Inverse logic applies for the PINs
  128. // Used for 1 detection group (Special group)
  129. #define DET_GROUP_1 \
  130. DET_GROUP_CHECK(0,!( PINB & (1 << 7) )) \
  131. DET_GROUP_CHECK(1,!( PINC & (1 << 0) )) \
  132. DET_GROUP_CHECK(2,!( PIND & (1 << 0) )) \
  133. DET_GROUP_CHECK(3,!( PIND & (1 << 1) )) \
  134. // Used for 4 detection groups (Skips J1 P9)
  135. #define DET_GROUP_2 \
  136. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  137. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  138. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  139. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  140. DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
  141. DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
  142. DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
  143. // Used for 1 detection group (Skips J1 P6 and J1 P9)
  144. #define DET_GROUP_3 \
  145. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  146. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  147. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  148. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  149. DET_GROUP_CHECK(4,!( PINB & (1 << 4) )) \
  150. DET_GROUP_CHECK(5,!( PINB & (1 << 5) )) \
  151. // Used for 3 detection groups (No skips, except special group 1)
  152. #define DET_GROUP_4 \
  153. DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
  154. DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
  155. DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
  156. DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
  157. DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
  158. DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
  159. DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
  160. DET_GROUP_CHECK(7,!( PINB & (1 << 6) )) \
  161. // Combines the DET_GROUP_Xs above for the given groupArray
  162. #define DET_GROUP(group,det_group) \
  163. case group: \
  164. { \
  165. uint8_t groupArray[DETECT_group_size_##group] = DETECT_group_array_##group; \
  166. _delay_us(1); \
  167. DET_GROUP_##det_group \
  168. } \
  169. break;
  170. // Loop over all of the sampled keys of the given array
  171. // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
  172. // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
  173. #define DEBOUNCE_ASSESS(table,size) \
  174. for ( uint8_t key = 1; key < size + 1; key++ ) {\
  175. table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
  176. } \
  177. // Keypad detection
  178. // Each switch has it's own detection line, inverse logic
  179. #define KEYPAD_DETECT(test,switch_code) \
  180. if ( !(test) ) { \
  181. keypadDetectArray[switch_code]++; \
  182. } \
  183. // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
  184. // Other Bits: Pressed state sample counter
  185. uint8_t keyDetectArray[KEYBOARD_SIZE + 1];
  186. uint8_t keypadDetectArray[KEYPAD_SIZE + 1];
  187. // Interrupt Variables
  188. uint16_t sendKeypressCounter = 0;
  189. volatile uint8_t sendKeypresses = 0;
  190. void detection( int group )
  191. {
  192. // XXX Modify for different detection groups <-> groupArray mappings
  193. switch ( group ) {
  194. DET_GROUP(1,2)
  195. DET_GROUP(2,2)
  196. DET_GROUP(3,3)
  197. DET_GROUP(4,4)
  198. DET_GROUP(5,2)
  199. DET_GROUP(6,2)
  200. DET_GROUP(7,4)
  201. DET_GROUP(8,4)
  202. DET_GROUP(9,1)
  203. }
  204. }
  205. // XXX This part is configurable
  206. inline void pinSetup(void)
  207. {
  208. // For each pin, 0=input, 1=output
  209. DDRA = 0x00;
  210. DDRB = 0x00;
  211. DDRC = 0x00;
  212. DDRD = 0xFC;
  213. DDRE = 0x43;
  214. DDRF = 0x00;
  215. // Setting pins to either high or pull-up resistor
  216. PORTA = 0xFF;
  217. PORTB = 0xFF;
  218. PORTC = 0x01;
  219. PORTD = 0xFF;
  220. PORTE = 0xC3;
  221. PORTF = 0xFF;
  222. }
  223. // Given a sampling array, and the current number of detected keypress
  224. // Add as many keypresses from the sampling array to the USB key send array as possible.
  225. void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
  226. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
  227. if ( keys[key] & (1 << 7) ) {
  228. pint8( key );
  229. print(" ");
  230. uint8_t modFound = 0;
  231. // Determine if the key is a modifier
  232. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  233. // Modifier found
  234. if ( modifiers[mod] == key ) {
  235. keyboard_modifier_keys |= map[key];
  236. modFound = 1;
  237. break;
  238. }
  239. }
  240. if ( modFound )
  241. continue;
  242. // Too many keys
  243. if ( *validKeys == 6 )
  244. break;
  245. keyboard_keys[(*validKeys)++] = map[key];
  246. }
  247. }
  248. }
  249. int main( void )
  250. {
  251. // Setup with 16 MHz clock
  252. CPU_PRESCALE( 0 );
  253. // Configuring Pins
  254. pinSetup();
  255. // Initialize the USB, and then wait for the host to set configuration.
  256. // If the Teensy is powered without a PC connected to the USB port,
  257. // this will wait forever.
  258. usb_init();
  259. while ( !usb_configured() ) /* wait */ ;
  260. // Wait an extra second for the PC's operating system to load drivers
  261. // and do whatever it does to actually be ready for input
  262. _delay_ms(1000);
  263. // Setup ISR Timer for flagging a kepress send to USB
  264. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  265. TCCR0A = 0x00;
  266. TCCR0B = 0x03;
  267. TIMSK0 = (1 << TOIE0);
  268. // Main Detection Loop
  269. int8_t group = 1;
  270. uint8_t count = 0;
  271. for ( ;;group++ ) {
  272. // XXX Change number of ORDs if number of lines (RowsxColumns) differ
  273. // Determine which keys are being pressed
  274. switch ( group ) {
  275. DD_CASE_ORD(1)
  276. DD_CASE_ORD(2)
  277. DD_CASE_ORD(3)
  278. DD_CASE_ORD(4)
  279. DD_CASE_ORD(5)
  280. DD_CASE_ORD(6)
  281. DD_CASE_ORD(7)
  282. DD_CASE_ORD(8)
  283. DD_CASE_END(9,group)
  284. }
  285. // Check all Keyboard keys first
  286. if ( group != -1 )
  287. continue;
  288. // Check Keypad keys
  289. KEYPAD_DETECT(PINA & (1 << 0),11)
  290. KEYPAD_DETECT(PINA & (1 << 1),3)
  291. KEYPAD_DETECT(PINA & (1 << 2),7)
  292. KEYPAD_DETECT(PINA & (1 << 3),4)
  293. KEYPAD_DETECT(PINA & (1 << 4),15)
  294. KEYPAD_DETECT(PINA & (1 << 5),6)
  295. KEYPAD_DETECT(PINA & (1 << 6),2)
  296. KEYPAD_DETECT(PINA & (1 << 7),10)
  297. KEYPAD_DETECT(PINF & (1 << 0),8)
  298. KEYPAD_DETECT(PINF & (1 << 1),12)
  299. KEYPAD_DETECT(PINF & (1 << 2),16)
  300. KEYPAD_DETECT(PINF & (1 << 3),13)
  301. KEYPAD_DETECT(PINF & (1 << 4),1)
  302. KEYPAD_DETECT(PINF & (1 << 5),5)
  303. KEYPAD_DETECT(PINF & (1 << 6),9)
  304. KEYPAD_DETECT(PINF & (1 << 7),14)
  305. // Check count to see if the sample threshold may have been reached, otherwise collect more data
  306. count++;
  307. if ( count < MAX_SAMPLES )
  308. continue;
  309. // Reset Sample Counter
  310. count = 0;
  311. // Assess debouncing sample table
  312. DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
  313. DEBOUNCE_ASSESS(keypadDetectArray,KEYPAD_SIZE)
  314. // Send keypresses over USB if the ISR has signalled that it's time
  315. if ( !sendKeypresses )
  316. continue;
  317. // Detect Valid Keypresses - TODO
  318. uint8_t validKeys = 0;
  319. // Map selection
  320. if ( keyDetectArray[34] & (1 << 7) ) { // CapsLock FN Modifier
  321. keyPressDetection( keyDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_modifierMask, MODIFIERS_KEYBOARD, colemakMap );
  322. keyPressDetection( keypadDetectArray, &validKeys, KEYPAD_SIZE, keypad_modifierMask, MODIFIERS_KEYPAD, keypadDefaultMap );
  323. }
  324. else {
  325. keyPressDetection( keyDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_modifierMask, MODIFIERS_KEYBOARD, defaultMap );
  326. keyPressDetection( keypadDetectArray, &validKeys, KEYPAD_SIZE, keypad_modifierMask, MODIFIERS_KEYPAD, keypadDefaultMap );
  327. }
  328. print(":\n");
  329. // TODO undo potentially old keys
  330. for ( uint8_t c = validKeys; c < 6; c++ )
  331. keyboard_keys[c] = 0;
  332. // Send keypresses
  333. usb_keyboard_send();
  334. // Clear sendKeypresses Flag
  335. sendKeypresses = 0;
  336. // Clear modifiers
  337. keyboard_modifier_keys = 0;
  338. }
  339. return 0;
  340. }
  341. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  342. ISR( TIMER0_OVF_vect )
  343. {
  344. sendKeypressCounter++;
  345. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  346. sendKeypressCounter = 0;
  347. sendKeypresses = 1;
  348. }
  349. }