Skip to content

9. Basic Text IO

ronf-ucb edited this page Jan 18, 2021 · 5 revisions

Debugging real-time code on the Huzzah is a challenge. In particular, print statements can be slow and disrupt control loops.

9.1 LED to blink

Sometimes toggling an LED can help to find code which is not entered or not exited.
#define BLINK_GPIO 13 // IO13
The LED can be turned on using
gpio_set_level(BLINK_GPIO, 1); Other GPIO lines are available, see Huzzah32 schematic to see unused GPIO lines. Remember to use a resistor in series with an LED to prevent burning out IO line.

9.2 printf

The normal C printf works. Some points to note:

  • Tasks need to have a bigger stack configMINIMAL_STACK_SIZE + 2048
  • make sure to use flush(stdout); to make sure print finishes.
  • snprintf is recommended, as buffer length is specified
  • printing a floating point number to a string is slow: almost 1000 us
  • print an int and long to a string: about 400 us
  • integer to ascii: itoa()
  • printf-stdarg.c does not handle floating point but uses much less stack space
    The log task saved almost 800 bytes of stack space by eliminating printf
    void printString(char *string)
    { int i=0;
    while (string[i] != '\0')
    // print single character, avoid printf to save on stack
    { fputc(string[i],stdout);
    i++; }
    }

9.3 Text input

It is recommended that advanced text input such as for parameters be handled by remote Python, and then numbers sent directly by WiFi (see next subsection), so that parsing is not needed on the Huzzah32. A discussion of input suggestions in `C’’ are suggested here:
https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf

  • scanf() does not work, and is not recommended due to input insecurity (buffer overflow attacks)
  • fgetc(stdin) and fputc(char,stdout) work. However, 0xFF needs to be ignored. ch = fgetc(stdin); if (ch!=0xFF) // discard idle character (non-input)
  • ESP-IDF provides console component, which includes building blocks needed to develop an interactive console over serial port. Since much interaction with the car can be remote (not connected through the USB cable), effort is better spent on a remote Python interface. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/console.html

9.4 Advanced IO

To use WiFi for debugging/data logging, use #define WIFILOG In SkeletonHuzzah32. Then Huzzah32 is a soft access point, connect and use python script to get debugging/log info. To add a debugging message, do:
snprintf(log, sizeof(log), printf args here);
log_add(log);

log_add() by itself takes about 40 us. However the snprintf command can be quite slow compared to itoa(). The Python script running on a WiFi connected laptop, print-client-udp.py, will connect through 192.168.4.2. See section below for details on using WiFi.