Get Values From Terminal

Now that we know how to get variable addresses, let’s try passing them a function that can do something useful with them.  In this activity, you’ll get to experiment with the scan function to receive a value you type into SimpleIDE Terminal. 

 

Test Code

The scan function allows you to type values into the SimpleIDE Terminal. It can then convert the characters you type into the value those numbers represent.  For example, after displaying “Enter reps: “ using a print function call, your code can then make a scan function call to load the number you typed into a variable. 

If you haven't already installed the latest USB driver, SimpleIDE, or Learn folder, go to Propeller C - Set up SimpleIDE and Propeller C - Start Simple.

  • Click SimpleIDE’s New Project button.
  • Set the folder to …Documents\SimpleIDE\Learn\Examples\C Intro\Basics\
  • Set the File name to Scan to Int.
  • Copy the code below into SimpleIDE.
  • Set the power switch to position 1 if it isn't already (if applicable for your board).
  • Click the Run with Terminal button.
  • Click to the right of “Enter reps: “ in the SimpleIDE Terminal; then, type a value and press Enter. 
  • Use SimpleIDE Terminal to verify that the Propeller chip counts up to the value you typed.
     
/*
Scan to Int.c
*/

#include "simpletools.h"                         // Include simpletools library

int reps;                                        // Declare variable named reps

int main()                                       // Main function
{
  print("Enter reps: ");                         // User prompt to enter reps
  scan("%d\n", &reps);                           // Scan reps user types
  print("\nCounting to %d\n", reps);             // Display value scanned

  for(int n = 1; n <= reps; n++)                 // Count to value scanned
  {
    print("i = %d\n", n);                        // Display each step
  }

  print("\nDone!");                              // Display when done
}

 

How it Works

After including the simpletools library, the program declares an int variable named reps that will be used to limit how high the Propeller counts. 

#include "simpletools.h"                         // Include simpletools library

int reps;                                        // Declare variable named reps

The main function starts by printing the “enter reps” message. 

int main()                                       // Main function
{
  print("Enter reps: ");                         // User prompt to enter reps

The scan function takes keyboard input from the SimpleIDE Terminal and uses it to set one or more values in one or more variables.  This scan function has a formatting string with %d, which tells it to store the decimal integer conversion of the characters you type.  The second argument in the function call is &reps, which tells the scan function the address of the variable where you want the result.  That’s the way the scan function is designed – instead of a variable value, it needs a variable address to do its job.

  scan("%d\n", &reps);                           // Scan reps user types

This print call helps verify that the scan function did its job by displaying the value reps stores.

  print("\nCounting to %d\n", reps);             // Display value scanned

The rest of the program counts from 1 to reps, in a manner similar to the Counting Loops activity.

  for(int n = 1; n <= reps; n++)                 // Count to value scanned
  {
    print("i = %d\n", n);                        // Display each step
  }

  print("\nDone!");                              // Display when done
}

 


Did You Know?

After scan has stored the value at a variable’s address, like &reps, the reps variable stores the value.  So, your code can do things like print("reps = %d\n", reps) to display the value of reps, and for(int n = 1; n <= reps; n++), which uses the value of reps to limit how high the for… loop counts.

The scan function is part of the simpletext library, which the simpletools library includes automatically.  It is similar to the C language stdio library’s scanf.  We use simpletext with the propeller because it takes less memory with code that uses floating point variables.  The print function is also part of simpletext, and similar to printf from the stdio library.  

Forgetting to use & in the scan function is a very common programming error. 

scan is kind of like the inverse of print.  For example, in print("%d\n", reps), the %d would make it display the decimal character representation of the value reps stores, and \n would make it display a newline.  In scan("%d\n", &reps), the %d that tells the Propeller to receive a decimal character representation of a value from SimpleIDE terminal, convert it to an int value, and store it in the &reps memory location.  The one important difference is that print would need the value of reps, but scan uses &reps – the address of the reps variable. 

The scan function’s formatting flags have the same meanings as their print equivalents:

  • %d – Decimal integer
  • %f – floating point value
  • %c – character
  • %s – string

You will see more on %c and %s as you progress through Propeller C activities.

There are important differences between scan and print:

  • The non-format flag characters in the formatting string don’t matter
  • Values input with %d terminate with the first non-numeric character.
  • The scan function terminates with \n regardless of whether you add it to the format string.

For example, scan("%d%d", &startVal, &endVal) will take two values that have to be separated by at least one non-numeric character, and the scan call itself gets completed by \n.  You could input 123, followed by a space, then 127, then press the Enter key.  But, you could also type abcdef123ghijklm127opq followed by the Enter key, and the results would be the same: startVal would store 123 and endVal would store 127.


 

Try This

Here, a single scan function call scan("%d %d\n", &startVal, &endVal) captures two values.  Then, a single print call print("\nCounting from %d to %d.\n", startVal, endVal) displays those two values, and then the for… loop counts from the start value to the end value.

  • Click SimpleIDE’s New Project button.
  • Set the folder to …Documents\SimpleIDE\My Projects\
  • Set the File name to Scan to Int 2 Values.
  • Copy the code below into SimpleIDE.
  • Click the Run with Terminal button.

  • Click the Terminal window below “Then, press Enter.”, and type two values separated by a space.  Then press the Enter key.   IMPORTANT: Make sure the first value you type is smaller than the second value.
  • Verify that SimpleIDE Terminal displays “Counting from…to…” with the values you entered, and then counts from the first value to the second value. 
  • Re-run the program, and try typing abcdefg123hijklm127opqr followed by the Enter key.  Did it still work?

 

Your Turn

Since scan can be kind of tricky with the scan function’s rule set, let’s try functions from the simpletext library with a simpler set of rules for getting values from the SimpleIDE Terminal. The getDec function does not require pointers, and terminates with a press of the Enter key (meaning that instead of adding a space between each number as you have been doing, you would press enter instead).  So, you could replace the scan code block from the Try This section with code that uses getDec, and end up with the same output in your Terminal Window.

  • Make sure your Try This code works.
  • Replace this code block:
  print("Type values for startVal and \n");
  print("endVal, separated by a space. \n");
  print("Then, press Enter.\n");

  scan("%d%d", &startVal, &endVal); 

... with this:

  print("Type values for startVal and \n");
  print("endVal.  Press Enter after each one. \n");

  startVal = getDec();
  endVal = getDec();
  • Test and verify that the program still works the same as it did before modifying.

Like print and scan, the getDec function is part of the simpletext library, which the simpletools library includes.  You can view a full list of the simpletext library’s functions in SimpleIDE’s Help -> Simple Library Reference.  For displaying data in the SimpleIDE Terminal, look for functions that start with put, and for acquiring data from the terminal, look for functions that begin with get.

  • Try replacing:

print("\nCounting from %d to %d.\n", startVal, endVal)

...with these calls to simpletext functions:

putStr("\nCounting from ");
putDec(startVal);
putStr(" to ");
putDec(endVal);
putStr(".\n");
  • Again, test and verify that the program still works the same.

Although print and scan provide some convenience with fewer lines of code, there is also a penalty in terms of program size.  Replacing all print and scan calls with equivalent sets of get and put calls, can often significantly reduce the program size. 

  • Reopen the unmodified Scan To Int project.
  • Use the Build Project button to compile the code then check the total code size in the SimpleIDE window’s bottom-left corner.
  • Replace all print and scan functions with put and get functions. 
  • Use the Build Project button again, and check how much memory you saved.