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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Copyright (C) 2011-2014 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/MainLib.h>
  24. // Project Includes
  25. #include <macro.h>
  26. #include <scan_loop.h>
  27. #include <output_com.h>
  28. #include <cli.h>
  29. #include <led.h>
  30. #include <print.h>
  31. // ----- Defines -----
  32. // Verified Keypress Defines
  33. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  34. // ----- Macros -----
  35. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  36. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  37. #endif
  38. // ----- Function Declarations -----
  39. void cliFunc_distRead ( char* args );
  40. void cliFunc_free ( char* args );
  41. void cliFunc_gaugeHelp ( char* args );
  42. void cliFunc_single ( char* args );
  43. void cliFunc_start ( char* args );
  44. void cliFunc_zeroForce ( char* args );
  45. void cliFunc_zeroPosition( char* args );
  46. // ----- Variables -----
  47. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  48. uint16_t sendKeypressCounter = 0;
  49. // Flag generated by the timer interrupt
  50. volatile uint8_t sendKeypresses = 0;
  51. // ----- Functions -----
  52. // Initial Pin Setup, make sure they are sane
  53. inline void pinSetup(void)
  54. {
  55. // AVR
  56. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  57. // For each pin, 0=input, 1=output
  58. #if defined(__AVR_AT90USB1286__)
  59. DDRA = 0x00;
  60. #endif
  61. DDRB = 0x00;
  62. DDRC = 0x00;
  63. DDRD = 0x00;
  64. DDRE = 0x00;
  65. DDRF = 0x00;
  66. // Setting pins to either high or pull-up resistor
  67. #if defined(__AVR_AT90USB1286__)
  68. PORTA = 0x00;
  69. #endif
  70. PORTB = 0x00;
  71. PORTC = 0x00;
  72. PORTD = 0x00;
  73. PORTE = 0x00;
  74. PORTF = 0x00;
  75. // ARM
  76. #elif defined(_mk20dx128_)
  77. // TODO - Should be cleared, but not that necessary due to the pin layout
  78. #endif
  79. }
  80. inline void usbTimerSetup(void)
  81. {
  82. // AVR
  83. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  84. // Setup with 16 MHz clock
  85. CPU_PRESCALE( 0 );
  86. // Setup ISR Timer for flagging a kepress send to USB
  87. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  88. TCCR0A = 0x00;
  89. TCCR0B = 0x03;
  90. TIMSK0 = (1 << TOIE0);
  91. // ARM
  92. #elif defined(_mk20dx128_)
  93. // 48 MHz clock by default
  94. // System Clock Gating Register Disable
  95. SIM_SCGC6 |= SIM_SCGC6_PIT;
  96. // Enable Timers
  97. PIT_MCR = 0x00;
  98. // Setup ISR Timer for flagging a kepress send to USB
  99. // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
  100. PIT_LDVAL0 = 0x0000BB7F;
  101. PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
  102. // Insert the required vector for Timer 0
  103. NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
  104. #endif
  105. }
  106. int main(void)
  107. {
  108. // Configuring Pins
  109. pinSetup();
  110. init_errorLED();
  111. // Setup Output Module
  112. output_setup();
  113. // Enable CLI
  114. init_cli();
  115. // Setup ISR Timer for flagging a kepress send to USB
  116. usbTimerSetup();
  117. // Main Detection Loop
  118. uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
  119. while ( 1 )
  120. {
  121. // Setup the scanning module
  122. scan_setup();
  123. while ( 1 )
  124. {
  125. // Acquire Key Indices
  126. // Loop continuously until scan_loop returns 0
  127. cli();
  128. while ( scan_loop() );
  129. sei();
  130. // Run Macros over Key Indices and convert to USB Keys
  131. process_macros();
  132. // Send keypresses over USB if the ISR has signalled that it's time
  133. if ( !sendKeypresses )
  134. continue;
  135. // Send USB Data
  136. usb_send();
  137. // Clear sendKeypresses Flag
  138. sendKeypresses = 0;
  139. // Indicate Error, if valid
  140. errorLED( ledTimer );
  141. if ( ledTimer > 0 )
  142. ledTimer--;
  143. }
  144. // Loop should never get here (indicate error)
  145. ledTimer = 255;
  146. // HID Debug Error message
  147. erro_print("Detection loop error, this is very bad...bug report!");
  148. }
  149. }
  150. // ----- Interrupts -----
  151. // USB Keyboard Data Send Counter Interrupt
  152. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  153. ISR( TIMER0_OVF_vect )
  154. #elif defined(_mk20dx128_) // ARM
  155. void pit0_isr(void)
  156. #endif
  157. {
  158. sendKeypressCounter++;
  159. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  160. sendKeypressCounter = 0;
  161. sendKeypresses = 1;
  162. }
  163. #if defined(_mk20dx128_) // ARM
  164. // Clear the interrupt flag
  165. PIT_TFLG0 = 1;
  166. #endif
  167. }
  168. // ----- CLI Command Functions -----
  169. void cliFunc_distRead( char* args )
  170. {
  171. // Prepare to print output
  172. print( NL );
  173. info_msg("Distance: ");
  174. // Data
  175. uint32_t distInput = 0;
  176. // Setup distance read parameters for iGaging Distance Scale
  177. // freq = 9kHz
  178. // duty_cycle = 20%
  179. // high_delay = (1/freq) * (duty_cycle/100)
  180. // low_delay = (1/freq) * ((100-duty_cycle)/100)
  181. uint8_t bits = 21; // 21 clock pulses, for 21 bits
  182. //uint32_t high_delay = 22; // Clock high time per pulse
  183. //uint32_t low_delay = 89; // Clock low time per pulse
  184. uint32_t high_delay = 40; // Clock high time per pulse
  185. uint32_t low_delay = 60; // Clock low time per pulse
  186. // Make sure clock is low initially
  187. GPIOC_PCOR |= (1<<2); // Set Clock low
  188. /*
  189. while(1)
  190. {
  191. */
  192. // Scan each of the bits
  193. for ( uint8_t bit = bits; bit > 0; bit-- )
  194. {
  195. // Begin clock pulse
  196. GPIOC_PSOR |= (1<<2); // Set Clock high
  197. // Delay for duty cycle
  198. delayMicroseconds( high_delay );
  199. // End clock pulse
  200. GPIOC_PCOR |= (1<<2); // Set Clock low
  201. // Read Data Bit
  202. //distInput |= GPIOD_PDIR & (1<<6) ? (1 << (bit - 1)) : 0;
  203. //if ( GPIOD_PDIR )
  204. if ( GPIOD_PDIR & (1<<6) )
  205. {
  206. print("1");
  207. }
  208. else
  209. {
  210. print("0");
  211. }
  212. // Delay for duty cycle
  213. delayMicroseconds( low_delay );
  214. }
  215. print(" ");
  216. // Output result
  217. printInt32( distInput );
  218. // Convert to mm
  219. // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
  220. // 21 bits is 2560 CPI (counts per inch) (C/inch)
  221. // 1 inch is 25.4 mm
  222. // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
  223. // Or
  224. // 1 count is 1/2560 = 0.000390625... inches
  225. // 1 count is (1/2560) * 25.4 = 0.0000153789370078740 mm = 0.0153789370078740 um = 15.3789370078740 nm
  226. // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
  227. // which is 2 097 152 000, and within 32 bits (4 294 967 295).
  228. // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
  229. // For each nm there are 2 097 152 000 000 positions.
  230. // And for shits:
  231. // pm is 2 097 152 : 0.000 015 378 937 007 874 0 mm : 32 bit
  232. // pm is 2 097 152 000 : 0.015 378 937 007 874 0 um : 32 bit (ideal acc. for 32 bit)
  233. // pm is 2 097 152 000 000 : 15.378 937 007 874 0 nm : 64 bit
  234. // pm is 2 097 152 000 000 000 : 15 378.937 007 874 0 pm : 64 bit
  235. // fm is 2 097 152 000 000 000 000 : 15 378 937.007 874 0 fm : 64 bit (ideal acc. for 64 bit)
  236. //uint64_t distNM = distInput * 15;
  237. //uint64_t distPM = distInput * 15378;
  238. uint64_t distFM = distInput * 15378937;
  239. // Calculate um and mm
  240. //uint32_t distNM = distInput * 15; // XXX
  241. //uint32_t distUM = distNM / 1000;
  242. //uint32_t distMM = distNM / 1000000;
  243. uint32_t distNM = distFM * 1000000;
  244. uint32_t distUM = distNM / 1000;
  245. uint32_t distMM = distUM / 1000;
  246. print(" ");
  247. printInt32( distMM );
  248. print(" mm ");
  249. printInt32( distUM );
  250. print(" um ");
  251. printInt32( distNM );
  252. print(" nm");
  253. /*
  254. //Wait
  255. print(NL);
  256. delay( 7 );
  257. distInput = 0;
  258. }
  259. */
  260. }
  261. void cliFunc_free( char* args )
  262. {
  263. }
  264. void cliFunc_gaugeHelp( char* args )
  265. {
  266. }
  267. void cliFunc_single( char* args )
  268. {
  269. }
  270. void cliFunc_start( char* args )
  271. {
  272. }
  273. void cliFunc_zeroForce( char* args )
  274. {
  275. }
  276. void cliFunc_zeroPosition( char* args )
  277. {
  278. }