]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Scan/ADCTest/scan_loop.c
Move matrix information to a cli command
[kiibohd-controller.git] / Scan / ADCTest / scan_loop.c
index 93c70c086f373c0c0dc0a0b624826732843c1bb6..d994933c0da6f22359c98e2e8eb28be1b5b60823 100644 (file)
@@ -65,11 +65,17 @@ volatile uint8_t KeyIndex_BufferUsed;
 
 
 // Scan Module command dictionary
-char*       scanCLIDictName = "ADC Test Module Commands";
-CLIDictItem scanCLIDict[] = {
-       { "adc",     "Read the specified number of values from the ADC.", cliFunc_adc },
-       { "adcInit", "Intialize/calibrate ADC. Arg 1 specifies the pin.", cliFunc_adcInit },
-#if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
+char scanCLIDictName[] = "ADC Test Module Commands";
+const CLIDictItem scanCLIDict[] = {
+#if defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
+       { "adc",     "Read the specified number of values from the ADC at the given pin: <pin> [# of reads]"
+                 NL "\t\t See \033[35mLib/pin_map.teensy3\033[0m for ADC0 channel number.", cliFunc_adc },
+       { "adcInit", "Intialize/calibrate ADC: <ADC Resolution> <Vref> <Hardware averaging samples>"
+                 NL "\t\tADC Resolution -> 8, 10, 12, 16 (bit)"
+                 NL "\t\t          Vref -> 0 (1.2 V), 1 (External)"
+                 NL "\t\tHw Avg Samples -> 0 (disabled), 4, 8, 16, 32", cliFunc_adcInit },
+#endif
+#if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
        { "dac",     "Set DAC output value, from 0 to 4095 (1/4096 Vref to Vref).", cliFunc_dac },
        { "dacVref", "Set DAC Vref. 0 is 1.2V. 1 is 3.3V.", cliFunc_dacVref },
 #endif
@@ -88,7 +94,7 @@ inline void Scan_setup()
        // Register Scan CLI dictionary
        CLI_registerDictionary( scanCLIDict, scanCLIDictName );
 }
-#elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
+#elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
 {
        // Register Scan CLI dictionary
        CLI_registerDictionary( scanCLIDict, scanCLIDictName );
@@ -97,7 +103,7 @@ inline void Scan_setup()
        VREF_TRM = 0x60;
        VREF_SC  = 0xE1; // Enable 1.2V Vref
 
-#if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
+#if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
        // DAC Setup
        SIM_SCGC2 |= SIM_SCGC2_DAC0;
        DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
@@ -140,11 +146,11 @@ void cliFunc_echo( char* args )
        char* arg1Ptr;
        char* arg2Ptr = args;
 
-       print( NL ); // No \n by default after the command is entered
-
        // Parse args until a \0 is found
        while ( 1 )
        {
+               print( NL ); // No \r\n by default after the command is entered
+
                curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
                CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
 
@@ -161,7 +167,7 @@ void cliFunc_adc( char* args )
 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
 {
 }
-#elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
+#elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
 {
        // Parse code from argument
        //  NOTE: Only first argument is used
@@ -170,14 +176,19 @@ void cliFunc_adc( char* args )
        CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
        // Set the ADC Channel
-       uint8_t channel = decToInt( arg1Ptr );
+       uint8_t channel = numToInt( arg1Ptr );
        __disable_irq();
        ADC0_SC1A = channel;
        __enable_irq();
 
        // Number of ADC samples to display
        CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
-       int displayedADC = decToInt( arg1Ptr );
+
+       int displayedADC = 1; // Default to 1 read
+       if ( arg1Ptr ) // If there is an argument, use that instead
+       {
+               displayedADC = numToInt( arg1Ptr );
+       }
 
        // Poll ADC until it gets a value, making sure to serve interrupts on each attempt
        while ( displayedADC > 0 )
@@ -188,8 +199,15 @@ void cliFunc_adc( char* args )
                if ( (ADC0_SC1A & ADC_SC1_COCO) )
                {
                        int result = ADC0_RA;
+                       print( NL );
                        printInt32( result );
                        displayedADC--;
+
+                       // Prepare for another read
+                       if ( displayedADC > 0 )
+                       {
+                               ADC0_SC1A = channel;
+                       }
                }
 
                __enable_irq();
@@ -202,7 +220,7 @@ void cliFunc_adcInit( char* args )
 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
 {
 }
-#elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
+#elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
 {
        // Parse code from argument
        //  NOTE: Only first argument is used
@@ -214,7 +232,7 @@ void cliFunc_adcInit( char* args )
        ADC0_SC3 = 0;
 
        // Select bit resolution
-       int bitResolution = decToInt( arg1Ptr );
+       int bitResolution = numToInt( arg1Ptr );
        switch ( bitResolution )
        {
        case 8: // 8-bit
@@ -242,7 +260,7 @@ void cliFunc_adcInit( char* args )
 
        // Select Vref
        CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
-       int vRef = decToInt( arg1Ptr );
+       int vRef = numToInt( arg1Ptr );
        switch ( vRef )
        {
        case 0: // 1.2V internal Vref
@@ -258,7 +276,7 @@ void cliFunc_adcInit( char* args )
 
        // Hardware averaging (and start calibration)
        CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
-       int hardwareAvg = decToInt( arg1Ptr );
+       int hardwareAvg = numToInt( arg1Ptr );
        switch ( hardwareAvg )
        {
        case 0:  // No hardware averaging
@@ -297,9 +315,9 @@ void cliFunc_adcInit( char* args )
        sum = (sum / 2) | 0x8000;
        ADC0_PG = sum;
 
-       info_msg("Calibration ADC0_PG (Plus-Side Gain Register) set to: ");
-       printInt16( sum );
        print( NL );
+       info_msg("Calibration ADC0_PG (Plus-Side Gain Register)  set to: ");
+       printInt16( sum );
 
        // ADC Minus-Side Gain Register
        // XXX I don't think this is necessary when doing single-ended (as opposed to differential) -HaaTa
@@ -308,23 +326,23 @@ void cliFunc_adcInit( char* args )
        sum = (sum / 2) | 0x8000;
        ADC0_MG = sum;
 
+       print( NL );
        info_msg("Calibration ADC0_MG (Minus-Side Gain Register) set to: ");
        printInt16( sum );
-       print( NL );
        __enable_irq(); // Re-enable interrupts
 }
 #endif
 
 void cliFunc_dac( char* args )
 {
-#if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
+#if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
        // Parse code from argument
        //  NOTE: Only first argument is used
        char* arg1Ptr;
        char* arg2Ptr;
        CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
-       int dacOut = decToInt( arg1Ptr );
+       int dacOut = numToInt( arg1Ptr );
 
        // Make sure the value is between 0 and 4096, otherwise ignore
        if ( dacOut >= 0 && dacOut <= 4095 )
@@ -336,14 +354,14 @@ void cliFunc_dac( char* args )
 
 void cliFunc_dacVref( char* args )
 {
-#if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
+#if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
        // Parse code from argument
        //  NOTE: Only first argument is used
        char* arg1Ptr;
        char* arg2Ptr;
        CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
-       switch ( decToInt( arg1Ptr ) )
+       switch ( numToInt( arg1Ptr ) )
        {
        case 0:
                DAC0_C0 = DAC_C0_DACEN; // 1.2V Vref is DACREF_1