Extensions to the ‘get_knob()’ Function

Here are ellaborations on the get_knob() function described in the programming tutorial: Suppose you want to give the user the option to use a previous value and cancel the operation of setting the knob. The user would have a choice between using the value of the knob, or a default value depending on whether they press the start or the stop button.

/* function to get input from the knob. The function has two input parameters. The parameter ‘prompt’ is a string which explains the expected input to the user. The paramter ‘defaultvalue’ is an integer that is returned if the user pushes the start button instead of the stop button.*/
 
int get_knob( char prompt[], int defaultvalue) {
        /* this variable stores the value of the knob */
        int value;
        /*loop forever */
        while (1){
        /* print the prompt */
                printf(prompt);
        /* remember the knob and display it with the default*/
                value = knob();
                printf(" %d or new:%d",defaultvalue,value);
        /* refresh the screen */
                sleep(.1);
                printf("\n");
        /* if the user pushes the stop button...*/
                if ( stop_button()){
        /* debounce button, and return knob value*/
                    sleep(.3);
                    return value;
                }
                if ( start_button()){
        /* debounce button, and return default */
                    sleep(.3);
                    return defaultvalue;
                }
 
        }
}

Here is an example of using the modified version of get_knob().

        int power = 50;
        
                /* get input from knob. Default is 50 */
        power = get_knob("Power:", power);

Now, you may notice that adjusting the knob by one or two points is a difficult task and you don’t need the full 0-255 range. It might be useful to scale the output of the knob so that the range is only as large as needed. To do this, the function will take a floating point argument as a scale factor. This value will then be multiplied by the return value of knob() to produce a different range of values. If the scale factor is less than one, the output values will be smaller than the output of the knob, which ranges from 0 to 255. If the scale factor is larger than one, the output will be scaled up accordingly. This multiplication requires two type conversions, one to turn the knob value into a float so that it can be multiplied by the scalefactor, and another to convert the result to an integer value. Note how parenthesis are used to ensure the order of each typecast.

 

/* This function takes three input parameters and returns an integer value when the user pushes stop or start.  The first parameter is a string which explains to the user what value he or she is expected to give.  The second is a floating point value which will scale the output of the knob. The third is the default value. The start button is the equivalent of ‘cancel’, and the stop button is the equivalent of ‘OK’*/
 
int get_knob(char propmpt[],float scalefactor, int defaultvalue){
        int value;
                /* loop forever */
        while ( 1 ) {
                    /* display the prompt */
                printf(prompt);
                /* multiply integer knob value by floating point */
                /* scalefactor. Put the result into value */
                value = (int) ( ( float ) knob() * scalefactor);
                /* print the default value next to the new value*/
                printf( " %d %d",defaultvalue, value);
                /* make the screen loop pretty */
                sleep(.1);
                printf("\n");
                /* if the user pushes start, return the default*/
                if ( start_button()){
                            /* debounce */
                    sleep(.3);
                    return defaultvalue;
                }
                /* if the user pushes stop, return the new value*/
                if ( stop_button()){
                            /* debounce */
                    sleep(.3);
                    return value;
                }
        }
}

This function is now quite useful. When you call it, it displays the value of the knob with the prompt and a default value. If you press start, it’s the same as saying "Cancel, I’d like to accept the default". If you press stop, that’s like saying "Ok, go with the value of the knob". The default is the value you specify in the last parameter. Here’s an example that uses this function:

void main (  ) {
                /* set I to an initial value */
        int I=5;
                /* call get_knob using the specified prompt, */ 
                /* scaling the output by .5, and using the stored */
                /* value in I as the default value. */
        I = get_knob("Value 1:",.5, I);
                /* clear the screen, and display the result */
        print ( "\n%d",I);
}

Try running this a few times, to get a feel for how get_knob behaves. You can then use it to store values into your Handy Board. If you would like to easy access to this feature of IC4, copy this function into a new file. Save this file as ‘get_knob.ic’ and put it into the Handyboard folder under IC4. Then by adding a “#use get_knob.ic” to the top of any new program the get_knob() function definition will be loaded along with the rest of your code and you can call this function as if it were in the standard library.

 


Go to The Botball Home Page