]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Adding iGaging support for reading values as mm, um and nm.
[kiibohd-controller.git] / main.c
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
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/MainLib.h>
26
27 // Project Includes
28 #include <macro.h>
29 #include <scan_loop.h>
30 #include <output_com.h>
31
32 #include <cli.h>
33 #include <led.h>
34 #include <print.h>
35
36
37
38 // ----- Defines -----
39
40 // Verified Keypress Defines
41 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
42
43
44
45 // ----- Macros -----
46 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
47 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
48 #endif
49
50
51
52 // ----- Function Declarations -----
53
54 void cliFunc_distRead    ( char* args );
55 void cliFunc_free        ( char* args );
56 void cliFunc_gaugeHelp   ( char* args );
57 void cliFunc_single      ( char* args );
58 void cliFunc_start       ( char* args );
59 void cliFunc_zeroForce   ( char* args );
60 void cliFunc_zeroPosition( char* args );
61
62
63
64 // ----- Variables -----
65
66 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
67 uint16_t sendKeypressCounter = 0;
68
69 // Flag generated by the timer interrupt
70 volatile uint8_t sendKeypresses = 0;
71
72
73
74 // ----- Functions -----
75
76 // Initial Pin Setup, make sure they are sane
77 inline void pinSetup(void)
78 {
79
80 // AVR
81 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
82
83         // For each pin, 0=input, 1=output
84 #if defined(__AVR_AT90USB1286__)
85         DDRA = 0x00;
86 #endif
87         DDRB = 0x00;
88         DDRC = 0x00;
89         DDRD = 0x00;
90         DDRE = 0x00;
91         DDRF = 0x00;
92
93
94         // Setting pins to either high or pull-up resistor
95 #if defined(__AVR_AT90USB1286__)
96         PORTA = 0x00;
97 #endif
98         PORTB = 0x00;
99         PORTC = 0x00;
100         PORTD = 0x00;
101         PORTE = 0x00;
102         PORTF = 0x00;
103
104 // ARM
105 #elif defined(_mk20dx128_)
106         // TODO - Should be cleared, but not that necessary due to the pin layout
107 #endif
108 }
109
110
111 inline void usbTimerSetup(void)
112 {
113 // AVR
114 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
115
116         // Setup with 16 MHz clock
117         CPU_PRESCALE( 0 );
118
119         // Setup ISR Timer for flagging a kepress send to USB
120         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
121         TCCR0A = 0x00;
122         TCCR0B = 0x03;
123         TIMSK0 = (1 << TOIE0);
124
125 // ARM
126 #elif defined(_mk20dx128_)
127         // 48 MHz clock by default
128
129         // System Clock Gating Register Disable
130         SIM_SCGC6 |= SIM_SCGC6_PIT;
131
132         // Enable Timers
133         PIT_MCR = 0x00;
134
135         // Setup ISR Timer for flagging a kepress send to USB
136         // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
137         PIT_LDVAL0 = 0x0000BB7F;
138         PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
139
140         // Insert the required vector for Timer 0
141         NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
142 #endif
143 }
144
145
146 int main(void)
147 {
148         // Configuring Pins
149         pinSetup();
150         init_errorLED();
151
152         // Setup Output Module
153         output_setup();
154
155         // Enable CLI
156         init_cli();
157
158         // Setup ISR Timer for flagging a kepress send to USB
159         usbTimerSetup();
160
161         // Main Detection Loop
162         uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
163         while ( 1 )
164         {
165                 // Setup the scanning module
166                 scan_setup();
167
168                 while ( 1 )
169                 {
170                         // Acquire Key Indices
171                         // Loop continuously until scan_loop returns 0
172                         cli();
173                         while ( scan_loop() );
174                         sei();
175
176                         // Run Macros over Key Indices and convert to USB Keys
177                         process_macros();
178
179                         // Send keypresses over USB if the ISR has signalled that it's time
180                         if ( !sendKeypresses )
181                                 continue;
182
183                         // Send USB Data
184                         usb_send();
185
186                         // Clear sendKeypresses Flag
187                         sendKeypresses = 0;
188
189                         // Indicate Error, if valid
190                         errorLED( ledTimer );
191
192                         if ( ledTimer > 0 )
193                                 ledTimer--;
194                 }
195
196                 // Loop should never get here (indicate error)
197                 ledTimer = 255;
198
199                 // HID Debug Error message
200                 erro_print("Detection loop error, this is very bad...bug report!");
201         }
202 }
203
204
205 // ----- Interrupts -----
206
207 // USB Keyboard Data Send Counter Interrupt
208 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
209 ISR( TIMER0_OVF_vect )
210 #elif defined(_mk20dx128_) // ARM
211 void pit0_isr(void)
212 #endif
213 {
214         sendKeypressCounter++;
215         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
216                 sendKeypressCounter = 0;
217                 sendKeypresses = 1;
218         }
219
220 #if defined(_mk20dx128_) // ARM
221         // Clear the interrupt flag
222         PIT_TFLG0 = 1;
223 #endif
224 }
225
226
227 // ----- CLI Command Functions -----
228
229 uint32_t readDistanceGauge()
230 {
231         // Setup distance read parameters for iGaging Distance Scale
232         //       freq = 9kHz
233         // duty_cycle = 20%
234         // high_delay = (1/freq) *       (duty_cycle/100)
235         //  low_delay = (1/freq) * ((100-duty_cycle)/100)
236         uint8_t  bits       = 21; // 21 clock pulses, for 21 bits
237         uint32_t high_delay = 22; // Clock high time per pulse
238         uint32_t  low_delay = 89; // Clock low  time per pulse
239
240         // Data
241         uint32_t distInput = 0;
242
243         // Make sure clock is low initially
244         GPIOC_PCOR |= (1<<2); // Set Clock low
245
246         // Scan each of the bits
247         for ( uint8_t bit = 0; bit < bits; bit++ )
248         {
249                 // Begin clock pulse
250                 GPIOC_PSOR |= (1<<2); // Set Clock high
251
252                 // Delay for duty cycle
253                 delayMicroseconds( high_delay );
254
255                 // End clock pulse
256                 GPIOC_PCOR |= (1<<2); // Set Clock low
257
258                 // Read Data Bit
259                 distInput |= GPIOC_PDIR & (1<<1) ? (1 << bit) : 0;
260
261                 // Delay for duty cycle
262                 delayMicroseconds( low_delay );
263         }
264
265         return distInput;
266 }
267
268 void cliFunc_distRead( char* args )
269 {
270         // Parse number from argument
271         //  NOTE: Only first argument is used
272         char* arg1Ptr;
273         char* arg2Ptr;
274         argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
275
276         // Convert the argument into an int
277         int read_count = decToInt( arg1Ptr ) + 1;
278
279         // If no argument specified, default to 1 read
280         if ( *arg1Ptr == '\0' )
281         {
282                 read_count = 2;
283         }
284
285         // Repeat reading as many times as specified in the argument
286         print( NL );
287         while ( --read_count > 0 )
288         {
289                 // Prepare to print output
290                 info_msg("Distance: ");
291
292                 // Data
293                 uint32_t distInput = readDistanceGauge();
294
295                 // Output result
296                 printInt32( distInput );
297
298                 // Convert to mm
299                 // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
300                 // 21 bits is 2560 CPI (counts per inch) (C/inch)
301                 // 1 inch is 25.4 mm
302                 // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
303                 // Or
304                 // 1 count is 1/2560 = 0.000390625... inches
305                 // 1 count is (1/2560) * 25.4 = 0.00992187500000000 mm = 9.92187500000000 um = 9921.87500000000 nm
306                 // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
307                 //    which is 2 097 152 000, and within 32 bits (4 294 967 295).
308                 // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
309                 // For each nm there are 2 097 152 000 000 positions.
310                 // And for shits:
311                 //    mm is 2 097 152                 :          0.009 921 875 000 mm : 32 bit
312                 //    um is 2 097 152 000             :          9.921 875 000     um : 32 bit (ideal acc. for 32 bit)
313                 //    nm is 2 097 152 000 000         :      9 921.875 000         nm : 64 bit
314                 //    pm is 2 097 152 000 000 000     :  9 921 875.000             pm : 64 bit (ideal acc. for 64 bit)
315
316                 // XXX Apparently shumatech was sorta wrong about the 21 bits of usage
317                 // Yes there are 21 bits, but the values only go from ~338 to ~30681 which is less than 16 bits...
318                 // This means that the conversion at NM can use 32 bits :D
319                 // It's been noted that the multiplier should be 100.6 (and that it could vary from scale to scale)
320                 uint32_t distNM = distInput * 9921;;
321                 uint32_t distUM = distNM / 1000;
322                 uint32_t distMM = distUM / 1000;
323
324                 print("  ");
325                 printInt32( distMM );
326                 print(" mm  ");
327                 printInt32( distUM );
328                 print(" um  ");
329                 printInt32( distNM );
330                 print(" nm  ");
331
332                 print( NL );
333
334                 // Only delay if still counting
335                 if ( read_count > 1 )
336                         delay( 50 );
337         }
338 }
339
340
341 void cliFunc_free( char* args )
342 {
343 }
344
345
346 void cliFunc_gaugeHelp( char* args )
347 {
348 }
349
350
351 void cliFunc_single( char* args )
352 {
353 }
354
355
356 void cliFunc_start( char* args )
357 {
358 }
359
360
361 void cliFunc_zeroForce( char* args )
362 {
363 }
364
365
366 void cliFunc_zeroPosition( char* args )
367 {
368 }
369