]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Force curve gauge is feature complete!!
[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_stop        ( char* args );
60 void cliFunc_zeroForce   ( char* args );
61 void cliFunc_zeroPosition( char* args );
62
63 char receiveUART0Char();
64
65 void transmitUART0String( char* str );
66
67 uint32_t readDistanceGauge();
68
69
70
71 // ----- Variables -----
72
73 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
74 uint16_t sendKeypressCounter = 0;
75
76 // Flag generated by the timer interrupt
77 volatile uint8_t sendKeypresses = 0;
78
79
80
81 // ----- Functions -----
82
83 // Initial Pin Setup, make sure they are sane
84 inline void pinSetup(void)
85 {
86
87 // AVR
88 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
89
90         // For each pin, 0=input, 1=output
91 #if defined(__AVR_AT90USB1286__)
92         DDRA = 0x00;
93 #endif
94         DDRB = 0x00;
95         DDRC = 0x00;
96         DDRD = 0x00;
97         DDRE = 0x00;
98         DDRF = 0x00;
99
100
101         // Setting pins to either high or pull-up resistor
102 #if defined(__AVR_AT90USB1286__)
103         PORTA = 0x00;
104 #endif
105         PORTB = 0x00;
106         PORTC = 0x00;
107         PORTD = 0x00;
108         PORTE = 0x00;
109         PORTF = 0x00;
110
111 // ARM
112 #elif defined(_mk20dx128_)
113         // TODO - Should be cleared, but not that necessary due to the pin layout
114 #endif
115 }
116
117
118 inline void usbTimerSetup(void)
119 {
120 // AVR
121 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
122
123         // Setup with 16 MHz clock
124         CPU_PRESCALE( 0 );
125
126         // Setup ISR Timer for flagging a kepress send to USB
127         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
128         TCCR0A = 0x00;
129         TCCR0B = 0x03;
130         TIMSK0 = (1 << TOIE0);
131
132 // ARM
133 #elif defined(_mk20dx128_)
134         // 48 MHz clock by default
135
136         // System Clock Gating Register Disable
137         SIM_SCGC6 |= SIM_SCGC6_PIT;
138
139         // Enable Timers
140         PIT_MCR = 0x00;
141
142         // Setup ISR Timer for flagging a kepress send to USB
143         // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
144         PIT_LDVAL0 = 0x0000BB7F;
145         PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
146
147         // Insert the required vector for Timer 0
148         NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
149 #endif
150 }
151
152
153 int main(void)
154 {
155         // Configuring Pins
156         pinSetup();
157         init_errorLED();
158
159         // Setup Output Module
160         output_setup();
161
162         // Enable CLI
163         init_cli();
164
165         // Setup ISR Timer for flagging a kepress send to USB
166         usbTimerSetup();
167
168         // Main Detection Loop
169         uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
170         while ( 1 )
171         {
172                 // Setup the scanning module
173                 scan_setup();
174
175                 while ( 1 )
176                 {
177                         // Acquire Key Indices
178                         // Loop continuously until scan_loop returns 0
179                         cli();
180                         while ( scan_loop() );
181                         sei();
182
183                         // Run Macros over Key Indices and convert to USB Keys
184                         process_macros();
185
186                         // Send keypresses over USB if the ISR has signalled that it's time
187                         if ( !sendKeypresses )
188                                 continue;
189
190                         // Send USB Data
191                         usb_send();
192
193                         // Clear sendKeypresses Flag
194                         sendKeypresses = 0;
195
196                         // Indicate Error, if valid
197                         errorLED( ledTimer );
198
199                         if ( ledTimer > 0 )
200                                 ledTimer--;
201                 }
202
203                 // Loop should never get here (indicate error)
204                 ledTimer = 255;
205
206                 // HID Debug Error message
207                 erro_print("Detection loop error, this is very bad...bug report!");
208         }
209 }
210
211
212 // ----- Interrupts -----
213
214 // USB Keyboard Data Send Counter Interrupt
215 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
216 ISR( TIMER0_OVF_vect )
217 #elif defined(_mk20dx128_) // ARM
218 void pit0_isr(void)
219 #endif
220 {
221         sendKeypressCounter++;
222         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
223                 sendKeypressCounter = 0;
224                 sendKeypresses = 1;
225         }
226
227 #if defined(_mk20dx128_) // ARM
228         // Clear the interrupt flag
229         PIT_TFLG0 = 1;
230 #endif
231 }
232
233
234 // ----- CLI Command Functions -----
235
236 uint32_t readDistanceGauge()
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
247         // Data
248         uint32_t distInput = 0;
249
250         // Make sure clock is low initially
251         GPIOC_PCOR |= (1<<2); // Set Clock low
252
253         // Scan each of the bits
254         for ( uint8_t bit = 0; bit < bits; bit++ )
255         {
256                 // Begin clock pulse
257                 GPIOC_PSOR |= (1<<2); // Set Clock high
258
259                 // Delay for duty cycle
260                 delayMicroseconds( high_delay );
261
262                 // End clock pulse
263                 GPIOC_PCOR |= (1<<2); // Set Clock low
264
265                 // Read Data Bit
266                 distInput |= GPIOC_PDIR & (1<<1) ? (1 << bit) : 0;
267
268                 // Delay for duty cycle
269                 delayMicroseconds( low_delay );
270         }
271
272         return distInput;
273 }
274
275 void cliFunc_distRead( char* args )
276 {
277         // Parse number from argument
278         //  NOTE: Only first argument is used
279         char* arg1Ptr;
280         char* arg2Ptr;
281         argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
282
283         // Convert the argument into an int
284         int read_count = decToInt( arg1Ptr ) + 1;
285
286         // If no argument specified, default to 1 read
287         if ( *arg1Ptr == '\0' )
288         {
289                 read_count = 2;
290         }
291
292         // Repeat reading as many times as specified in the argument
293         print( NL );
294         while ( --read_count > 0 )
295         {
296                 // Prepare to print output
297                 info_msg("Distance: ");
298
299                 // Data
300                 uint32_t distInput = readDistanceGauge() - distanceOffset;
301
302                 // Output result
303                 printInt32( distInput );
304
305                 // Convert to mm
306                 // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
307                 // 21 bits is 2560 CPI (counts per inch) (C/inch)
308                 // 1 inch is 25.4 mm
309                 // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
310                 // Or
311                 // 1 count is 1/2560 = 0.000390625... inches
312                 // 1 count is (1/2560) * 25.4 = 0.00992187500000000 mm = 9.92187500000000 um = 9921.87500000000 nm
313                 // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
314                 //    which is 2 097 152 000, and within 32 bits (4 294 967 295).
315                 // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
316                 // For each nm there are 2 097 152 000 000 positions.
317                 // And for shits:
318                 //    mm is 2 097 152                 :          0.009 921 875 000 mm : 32 bit
319                 //    um is 2 097 152 000             :          9.921 875 000     um : 32 bit (ideal acc. for 32 bit)
320                 //    nm is 2 097 152 000 000         :      9 921.875 000         nm : 64 bit
321                 //    pm is 2 097 152 000 000 000     :  9 921 875.000             pm : 64 bit (ideal acc. for 64 bit)
322
323                 // XXX Apparently shumatech was sorta wrong about the 21 bits of usage
324                 // Yes there are 21 bits, but the values only go from ~338 to ~30681 which is less than 16 bits...
325                 // This means that the conversion at NM can use 32 bits :D
326                 // It's been noted that the multiplier should be 100.6 (and that it could vary from scale to scale)
327                 uint32_t distNM = distInput * 9921;;
328                 uint32_t distUM = distNM / 1000;
329                 uint32_t distMM = distUM / 1000;
330
331                 print("  ");
332                 printInt32( distMM );
333                 print(" mm  ");
334                 printInt32( distUM );
335                 print(" um  ");
336                 printInt32( distNM );
337                 print(" nm  ");
338
339                 print( NL );
340
341                 // Only delay if still counting
342                 if ( read_count > 1 )
343                         delay( 50 );
344         }
345 }
346
347
348 void cliFunc_free( char* args )
349 {
350         // Set the forceDistanceRead to 1, which will read until start has passed twice
351         forceDistanceRead = 1;
352 }
353
354
355 void cliFunc_gaugeHelp( char* args )
356 {
357         print( NL
358 "\033[1;32mForce Curve Gauge Help\033[0m" NL
359 " \033[1;33mUsage Overview\033[0m" NL
360 "  TODO" NL
361 " \033[1;33mAdditional Command Details\033[0m" NL
362 "  \033[1;35mdistRead\033[0m" NL
363 "     Reads the current value from the distance gauge." NL
364 "     If specified it will N repeated reads with a delay after each read. Useful for testing the distance gauge." NL
365 "       e.g. \033[35mdistRead 250\033[0m" NL
366 "  \033[1;35mfree\033[0m" NL
367 "     Start free scanning force/distance reads." NL
368 "     Will continue until the [start] distance point has been past twice." NL
369 "  \033[1;35mimadaComm\033[0m" NL
370 "     Sends a command to the Imada force gauge." NL
371 "       e.g. \033[35mimadaComm D\033[0m" NL
372 "     The commands supported by the gauge depends on the model. Listed below is for the DS2." NL
373 "       K  Select g  units (default)" NL
374 "       N  Select N  units" NL
375 "       O  Select oz units" NL
376 "       P  Select peak mode" NL
377 "       T  Select real time mode (default)" NL
378 "       Z  Zero out display/reading" NL
379 "       Q  Turn off power" NL
380 "       E  Read high/low set points" NL
381 "       D  Read data from force gauge" NL
382 "       E\033[35mHHHHLLLL\033[0m" NL
383 "          Set the high/low setpoints, ignore decimals" NL
384 "          \033[35mHHHH\033[0m is 4 digit high, \033[35mLLLL\033[0m is 4 digit low" NL
385 "     Responses from the above commands." NL
386 "       R  Command successful" NL
387 "       E  Error/Invalid Command" NL
388 "       E\033[35mHHHHLLLL\033[0m" NL
389 "          Current high/low setpoints" NL
390 "          \033[35mHHHH\033[0m is 4 digit high, \033[35mLLLL\033[0m is 4 digit low" NL
391 "       \033[35m[value][units][mode]\033[0m" NL
392 "          Data read response" NL
393 "          \033[35m[value]\033[0m is force currently showing on the display (peak or realtime)" NL
394 "          \033[35m[units]\033[0m is the configured force units" NL
395 "          \033[35m[mode]\033[0m  is the current mode (peak or realtime)" NL
396 "  \033[1;35mread\033[0m" NL
397 "     Read the current force/distance value." NL
398 "     If specified it will N repeated reads with a delay after each read." NL
399 "       e.g. \033[35mread 125\033[0m" NL
400 "  \033[1;35mstart\033[0m" NL
401 "     Distance marker \033[35m[start]\033[0m for the start/end of a force curve measurement." NL
402 "     While in free running mode, a special message is displayed when reaching the \033[35m[start]\033[0m point." NL
403 "       \033[35m[start]\033[0m is defined by positioning the distance sensor at the position to start and running this command." NL
404                 );
405 }
406
407
408 void cliFunc_read( char* args )
409 {
410         // Parse number from argument
411         //  NOTE: Only first argument is used
412         char* arg1Ptr;
413         char* arg2Ptr;
414         argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
415
416         // Convert the argument into an int
417         int read_count = decToInt( arg1Ptr ) + 1;
418
419         // If no argument specified, default to 1 read
420         if ( *arg1Ptr == '\0' )
421         {
422                 read_count = 2;
423         }
424
425         // Set the overall read count to read_count
426         forceDistanceReadCount = read_count;
427 }
428
429
430 void cliFunc_start( char* args )
431 {
432         // Read the current distance and set the new start/end position
433         distanceStart = readDistanceGauge();
434
435         print( NL );
436         info_msg("New start/end position: ");
437         printInt32( distanceStart - distanceOffset );
438 }
439
440
441 void cliFunc_stop( char* args )
442 {
443         // Reset the forceDistanceRead and forceDistanceReadCount
444         forceDistanceRead = 0;
445         forceDistanceReadCount = 0;
446 }
447
448
449 void cliFunc_zeroForce( char* args )
450 {
451         // Just use the imadaComm command sending the needed argument
452         char* commandArg = "Z";
453         imadaVerboseRead( commandArg );
454 }
455
456
457 void cliFunc_zeroPosition( char* args )
458 {
459         // Read the current distance and set the new offset
460         distanceOffset = readDistanceGauge();
461
462         print( NL );
463         info_msg("New distance offset: ");
464         printInt32( distanceOffset );
465 }
466