]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/ADCTest/scan_loop.c
ADCTest DAC support for Teensy 3.1
[kiibohd-controller.git] / Scan / ADCTest / scan_loop.c
1 /* Copyright (C) 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/ScanLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <led.h>
30 #include <print.h>
31
32 // Local Includes
33 #include "scan_loop.h"
34
35
36
37 // ----- Defines -----
38
39
40
41 // ----- Macros -----
42
43
44
45 // ----- Function Declarations -----
46
47 void cliFunc_dac    ( char* args );
48 void cliFunc_dacVref( char* args );
49 void cliFunc_echo   ( char* args );
50
51
52
53 // ----- Variables -----
54
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;
58
59
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 },
66 #endif
67         { "echo",    "Example command, echos the arguments.", cliFunc_echo },
68         { 0, 0, 0 } // Null entry for dictionary end
69 };
70
71
72
73 // ----- Functions -----
74
75 // Setup
76 inline void Scan_setup()
77 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
78 {
79         // Register Scan CLI dictionary
80         CLI_registerDictionary( scanCLIDict, scanCLIDictName );
81 }
82 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
83 {
84         // Register Scan CLI dictionary
85         CLI_registerDictionary( scanCLIDict, scanCLIDictName );
86
87 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
88         // DAC Setup
89         SIM_SCGC2 |= SIM_SCGC2_DAC0;
90         DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
91 #endif
92 }
93 #endif
94
95
96 // Main Detection Loop
97 inline uint8_t Scan_loop()
98 {
99         return 0;
100 }
101
102
103 // Signal KeyIndex_Buffer that it has been properly read
104 void Scan_finishedWithBuffer( uint8_t sentKeys )
105 {
106 }
107
108
109 // Signal that the keys have been properly sent over USB
110 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
111 {
112 }
113
114 // Reset Keyboard
115 void Scan_resetKeyboard()
116 {
117 }
118
119
120 // ----- CLI Command Functions -----
121
122 // XXX Just an example command showing how to parse arguments (more complex than generally needed)
123 void cliFunc_echo( char* args )
124 {
125         char* curArgs;
126         char* arg1Ptr;
127         char* arg2Ptr = args;
128
129         print( NL ); // No \n by default after the command is entered
130
131         // Parse args until a \0 is found
132         while ( 1 )
133         {
134                 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
135                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
136
137                 // Stop processing args if no more are found
138                 if ( *arg1Ptr == '\0' )
139                         break;
140
141                 // Print out the arg
142                 dPrint( arg1Ptr );
143         }
144 }
145
146 void cliFunc_dac( char* args )
147 {
148 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
149         // Parse code from argument
150         //  NOTE: Only first argument is used
151         char* arg1Ptr;
152         char* arg2Ptr;
153         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
154
155         int dacOut = decToInt( arg1Ptr );
156
157         // Make sure the value is between 0 and 4096, otherwise ignore
158         if ( dacOut >= 0 && dacOut <= 4095 )
159         {
160                 *(int16_t *) &(DAC0_DAT0L) = dacOut;
161         }
162 #endif
163 }
164
165 void cliFunc_dacVref( char* args )
166 {
167 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
168         // Parse code from argument
169         //  NOTE: Only first argument is used
170         char* arg1Ptr;
171         char* arg2Ptr;
172         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
173
174         switch ( decToInt( arg1Ptr ) )
175         {
176         case 0:
177                 // XXX Doesn't seem to work...
178                 DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
179                 break;
180         case 1:
181                 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
182                 break;
183         }
184 #endif
185 }
186