1 /* Copyright (C) 2014 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
25 #include <Lib/ScanLib.h>
33 #include "scan_loop.h"
37 // ----- Defines -----
45 // ----- Function Declarations -----
47 void cliFunc_dac ( char* args );
48 void cliFunc_dacVref( char* args );
49 void cliFunc_echo ( char* args );
53 // ----- Variables -----
55 // Buffer used to inform the macro processing module which keys have been detected as pressed
56 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
57 volatile uint8_t KeyIndex_BufferUsed;
60 // Scan Module command dictionary
61 char* scanCLIDictName = "ADC Test Module Commands";
62 CLIDictItem scanCLIDict[] = {
63 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
64 { "dac", "Set DAC output value, from 0 to 4095 (1/4096 Vref to Vref).", cliFunc_dac },
65 { "dacVref", "Set DAC Vref. 0 is 1.2V. 1 is 3.3V.", cliFunc_dacVref },
67 { "echo", "Example command, echos the arguments.", cliFunc_echo },
68 { 0, 0, 0 } // Null entry for dictionary end
73 // ----- Functions -----
76 inline void Scan_setup()
77 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
79 // Register Scan CLI dictionary
80 CLI_registerDictionary( scanCLIDict, scanCLIDictName );
82 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
84 // Register Scan CLI dictionary
85 CLI_registerDictionary( scanCLIDict, scanCLIDictName );
87 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
89 SIM_SCGC2 |= SIM_SCGC2_DAC0;
90 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
96 // Main Detection Loop
97 inline uint8_t Scan_loop()
103 // Signal KeyIndex_Buffer that it has been properly read
104 void Scan_finishedWithBuffer( uint8_t sentKeys )
109 // Signal that the keys have been properly sent over USB
110 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
115 void Scan_resetKeyboard()
120 // ----- CLI Command Functions -----
122 // XXX Just an example command showing how to parse arguments (more complex than generally needed)
123 void cliFunc_echo( char* args )
127 char* arg2Ptr = args;
129 print( NL ); // No \n by default after the command is entered
131 // Parse args until a \0 is found
134 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
135 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
137 // Stop processing args if no more are found
138 if ( *arg1Ptr == '\0' )
146 void cliFunc_dac( char* args )
148 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
149 // Parse code from argument
150 // NOTE: Only first argument is used
153 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
155 int dacOut = decToInt( arg1Ptr );
157 // Make sure the value is between 0 and 4096, otherwise ignore
158 if ( dacOut >= 0 && dacOut <= 4095 )
160 *(int16_t *) &(DAC0_DAT0L) = dacOut;
165 void cliFunc_dacVref( char* args )
167 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
168 // Parse code from argument
169 // NOTE: Only first argument is used
172 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
174 switch ( decToInt( arg1Ptr ) )
177 // XXX Doesn't seem to work...
178 DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
181 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2