Print and Scan Function Review

The wifi library examples make liberal use of wifi_print and wifi_scan.  They behave similarly to print and scan, so let's have a quick review on how those work.  The scan function can be used to pick characters, values, and strings from characters you type in the terminal and place them in variables.  Conversely, the print function can be used to display the values of characters, values and strings in the terminal.

Let's say that you are expecting to type something like io=pin26state1 to control the Propeller Activity Board's P26 and P27 LEDs. The goal is for your program to load the number (26 or 27) into a variable named pin, and another number (1 or 0) into a variable named state.  You could use this to go capture the two values with the %d formatting flag and put them in the variables. 

scan("%d%d", &pin, &state);

Don't forget to use the & with the variables.  The scan function needs variable addresses, not variable values.

Then, if you want to print the variable values to the terminal, you can use the print function:

print("pin = %d, state = %d", pin, state);

Unlike the scan function, the print function requires variable values in most cases, so the pin and state variables do not have the & character.

Let's try this in a program that controls LEDs.

#include "simpletools.h"

int pin, state;

int main()
  set_direction(26, 1);        // 1 -> output
  set_direction(27, 1);

  print("Type a 26/27 1/0 variations on:\r");
  print("Then, press Enter\r\r");

    scan("%d%d", &pin, &state);
    print("pin = %d, state = %d\r\r", pin, state);
    set_output(pin, state);

There are lots more examples of using print and scan with format flags in Propeller C - Start Simple.