]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Initial work for iGaging distance gauge.
[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 void cliFunc_distRead( char* args )
230 {
231         // Prepare to print output
232         print( NL );
233         info_msg("Distance: ");
234
235         // Data
236         uint32_t distInput = 0;
237
238         // Setup distance read parameters for iGaging Distance Scale
239         //       freq = 9kHz
240         // duty_cycle = 20%
241         // high_delay = (1/freq) *       (duty_cycle/100)
242         //  low_delay = (1/freq) * ((100-duty_cycle)/100)
243         uint8_t  bits       = 21; // 21 clock pulses, for 21 bits
244         //uint32_t high_delay = 22; // Clock high time per pulse
245         //uint32_t  low_delay = 89; // Clock low  time per pulse
246         uint32_t high_delay = 40; // Clock high time per pulse
247         uint32_t  low_delay = 60; // Clock low  time per pulse
248
249         // Make sure clock is low initially
250         GPIOC_PCOR |= (1<<2); // Set Clock low
251 /*
252 while(1)
253 {
254 */
255         // Scan each of the bits
256         for ( uint8_t bit = bits; bit > 0; bit-- )
257         {
258                 // Begin clock pulse
259                 GPIOC_PSOR |= (1<<2); // Set Clock high
260
261                 // Delay for duty cycle
262                 delayMicroseconds( high_delay );
263
264                 // End clock pulse
265                 GPIOC_PCOR |= (1<<2); // Set Clock low
266
267                 // Read Data Bit
268                 //distInput |= GPIOD_PDIR & (1<<6) ? (1 << (bit - 1)) : 0;
269                 //if ( GPIOD_PDIR )
270                 if ( GPIOD_PDIR & (1<<6) )
271                 {
272                         print("1");
273                 }
274                 else
275                 {
276                         print("0");
277                 }
278
279                 // Delay for duty cycle
280                 delayMicroseconds( low_delay );
281         }
282         print("  ");
283
284         // Output result
285         printInt32( distInput );
286
287         // Convert to mm
288         // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
289         // 21 bits is 2560 CPI (counts per inch) (C/inch)
290         // 1 inch is 25.4 mm
291         // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
292         // Or
293         // 1 count is 1/2560 = 0.000390625... inches
294         // 1 count is (1/2560) * 25.4 = 0.0000153789370078740 mm = 0.0153789370078740 um = 15.3789370078740 nm
295         // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
296         //    which is 2 097 152 000, and within 32 bits (4 294 967 295).
297         // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
298         // For each nm there are 2 097 152 000 000 positions.
299         // And for shits:
300         //    pm is 2 097 152                 :          0.000 015 378 937 007 874 0 mm : 32 bit
301         //    pm is 2 097 152 000             :          0.015 378 937 007 874 0     um : 32 bit (ideal acc. for 32 bit)
302         //    pm is 2 097 152 000 000         :         15.378 937 007 874 0         nm : 64 bit
303         //    pm is 2 097 152 000 000 000     :     15 378.937 007 874 0             pm : 64 bit
304         //    fm is 2 097 152 000 000 000 000 : 15 378 937.007 874 0                 fm : 64 bit (ideal acc. for 64 bit)
305         //uint64_t distNM = distInput * 15;
306         //uint64_t distPM = distInput * 15378;
307         uint64_t distFM = distInput * 15378937;
308
309         // Calculate um and mm
310         //uint32_t distNM = distInput * 15; // XXX
311         //uint32_t distUM = distNM / 1000;
312         //uint32_t distMM = distNM / 1000000;
313         uint32_t distNM = distFM * 1000000;
314         uint32_t distUM = distNM / 1000;
315         uint32_t distMM = distUM / 1000;
316
317         print("  ");
318         printInt32( distMM );
319         print(" mm  ");
320         printInt32( distUM );
321         print(" um  ");
322         printInt32( distNM );
323         print(" nm");
324
325 /*
326 //Wait
327 print(NL);
328 delay( 7 );
329 distInput = 0;
330 }
331 */
332 }
333
334
335 void cliFunc_free( char* args )
336 {
337 }
338
339
340 void cliFunc_gaugeHelp( char* args )
341 {
342 }
343
344
345 void cliFunc_single( char* args )
346 {
347 }
348
349
350 void cliFunc_start( char* args )
351 {
352 }
353
354
355 void cliFunc_zeroForce( char* args )
356 {
357 }
358
359
360 void cliFunc_zeroPosition( char* args )
361 {
362 }
363