PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Public Types | Public Member Functions | Static Public Attributes | List of all members
PropWare::Scanner Class Reference

Interface for all classes capable of scanning. More...

#include <PropWare/hmi/input/scanner.h>

+ Collaboration diagram for PropWare::Scanner:

Public Types

enum  ErrorCode {
  NO_ERROR = 0,
  BEG_ERROR,
  BAD_INPUT = BEG_ERROR,
  END_ERROR = BAD_INPUT
}
 

Public Member Functions

 Scanner (ScanCapable &scanCapable, const Printer *printer=NULL)
 Construct a Scanner instance and control whether or not received characters are echoed back via the *printer argument. More...
 
char get_char ()
 
ErrorCode gets (char string[], int32_t length, const char delimiter=DEFAULT_DELIMITER)
 Read words from the bus until the delimiter is received. More...
 
ErrorCode get_token (char string[], int32_t length, const char *delimiters=WHITESPACE_CHARS)
 Read characters from the bus until the any one of the given delimiters is received. More...
 
template<typename T >
Scanneroperator>> (T &c)
 Extract formatted input. More...
 
const ErrorCode get (char &c)
 Extract formatted input. More...
 
const ErrorCode get (uint8_t &x)
 
template<size_t N>
const ErrorCode get (char(&buffer)[N])
 
const ErrorCode get (uint16_t &x)
 
const ErrorCode get (int16_t &x)
 
const ErrorCode get (uint32_t &x)
 
const ErrorCode get (int32_t &x)
 
const ErrorCode get (float &f)
 
void input_prompt (const char prompt[], const char failureResponse[], char userInput[], const size_t bufferLength, const Comparator< char > &comparator)
 Prompt the user for input and store the value only if it is sanitized. More...
 
template<typename T >
void input_prompt (const char prompt[], const char failureResponse[], T *userInput, const Comparator< T > &comparator)
 Prompt the user for input and store the value only if it is sanitized. More...
 

Static Public Attributes

static const char DEFAULT_DELIMITER = '\n'
 
static const char WHITESPACE_CHARS [] = "\r\n\t "
 Set of all standard whitespace.
 

Detailed Description

Interface for all classes capable of scanning.

Examples
BufferedUART_Demo.cpp, Eeprom_Demo.cpp, and FullDuplexSerial_Demo.cpp.

Definition at line 43 of file scanner.h.

Member Enumeration Documentation

◆ ErrorCode

Enumerator
NO_ERROR 

No error

BEG_ERROR 

First Scanner error

BAD_INPUT 

Scanner Error 0

END_ERROR 

Last Scanner error code

Definition at line 45 of file scanner.h.

Constructor & Destructor Documentation

◆ Scanner()

PropWare::Scanner::Scanner ( ScanCapable scanCapable,
const Printer printer = NULL 
)

Construct a Scanner instance and control whether or not received characters are echoed back via the *printer argument.

Parameters
*scanCapableObject capable of scanning for characters
*printerIf non-null, scanned characters will be echoed out this printer

Definition at line 64 of file scanner.h.

Member Function Documentation

◆ get() [1/7]

const ErrorCode PropWare::Scanner::get ( char c)

Extract formatted input.

Parameters
[in]&cObject where the value that the extracted characters represent is stored.
Returns
Error code if the input can not be converted to the desired format, 0 otherwise

Definition at line 186 of file scanner.h.

+ Here is the caller graph for this function:

◆ get() [2/7]

const ErrorCode PropWare::Scanner::get ( float f)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 281 of file scanner.h.

◆ get() [3/7]

const ErrorCode PropWare::Scanner::get ( int16_t &  x)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 238 of file scanner.h.

◆ get() [4/7]

const ErrorCode PropWare::Scanner::get ( int32_t &  x)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 268 of file scanner.h.

◆ get() [5/7]

const ErrorCode PropWare::Scanner::get ( uint16_t &  x)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 222 of file scanner.h.

◆ get() [6/7]

const ErrorCode PropWare::Scanner::get ( uint32_t &  x)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 255 of file scanner.h.

◆ get() [7/7]

const ErrorCode PropWare::Scanner::get ( uint8_t &  x)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 201 of file scanner.h.

◆ get_char()

char PropWare::Scanner::get_char ( )
See also
PropWare::ScanCapable::get_char

Definition at line 72 of file scanner.h.

+ Here is the call graph for this function:

◆ get_token()

ErrorCode PropWare::Scanner::get_token ( char  string[],
int32_t  length,
const char delimiters = WHITESPACE_CHARS 
)

Read characters from the bus until the any one of the given delimiters is received.

If found, the delimiter will be replaced with a null-terminator. If the buffer is filled before a delimiter is found, no null-terminator will be inserted

Parameters
[in]string[]Output buffer which should store the data
[in]delimitersSet of characters (null-terminated), any of which will be considered as the stop point for the reader
Returns
Zero upon success, error code otherwise

Definition at line 136 of file scanner.h.

+ Here is the call graph for this function:

◆ gets()

ErrorCode PropWare::Scanner::gets ( char  string[],
int32_t  length,
const char  delimiter = DEFAULT_DELIMITER 
)

Read words from the bus until the delimiter is received.

If found, the delimiter will be replaced with a null-terminator. If the buffer is filled before a delimiter is found, no null-terminator will be inserted

Parameters
[in]string[]Output buffer which should store the data
[in]lengthMaximum number of characters to read
[in]delimiterCharacter which should be considered as the stop point for the reader
Returns
Zero upon success, error code otherwise

Definition at line 91 of file scanner.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ input_prompt() [1/2]

void PropWare::Scanner::input_prompt ( const char  prompt[],
const char  failureResponse[],
char  userInput[],
const size_t  bufferLength,
const Comparator< char > &  comparator 
)

Prompt the user for input and store the value only if it is sanitized.

For safe input of strings only, use this method. For example, to request a yes/no answer, use the YES_NO_COMP shared instance:

char answer[32]; // A nice big buffer in case the user enters something bad
pwIn.input_prompt("Do you like the Parallax Propeller?\n>>> ", "Please enter yes or no (y/n)\n", answer, 32,
pwOut << "You said " << answer << "!\n";
Parameters
[in]prompt[]User prompt which will be displayed before each attempt to read the serial bus
[in]failureResponse[]Message to be displayed after each incorrect input
[out]userInput[]Buffer that can be used for storing the user's input
[in]bufferLengthSize (in bytes) of the userInput[] buffer
[in]comparatorDetermines whether or not the received input was valid
Examples
Scanner_Demo.cpp, and StringBuilder_Demo.cpp.

Definition at line 311 of file scanner.h.

+ Here is the call graph for this function:

◆ input_prompt() [2/2]

template<typename T >
void PropWare::Scanner::input_prompt ( const char  prompt[],
const char  failureResponse[],
T *  userInput,
const Comparator< T > &  comparator 
)

Prompt the user for input and store the value only if it is sanitized.

For safe input of any value other than strings, use this method. For example, to request any non-negative number, use the NON_NEGATIVE_COMP shared instance:

int number;
pwIn.input_prompt("Enter a non-negative number:\n>>> ",
"That is either not a number, or it is negative.\n", &number, NON_NEGATIVE_COMP);
pwOut << "You entered " << number << "\n";
Parameters
[in]prompt[]User prompt which will be displayed before each attempt to read the serial bus
[in]failureResponse[]Message to be displayed after each incorrect input
[out]*userInputResulting value will be stored at this address
[in]comparatorDetermines whether or not the received input was valid

Definition at line 344 of file scanner.h.

+ Here is the call graph for this function:

◆ operator>>()

template<typename T >
Scanner& PropWare::Scanner::operator>> ( T &  c)

Extract formatted input.

Parameters
[in]&cObject where the value that the extracted characters represent is stored.
Returns
The Scanner object (*this)

Definition at line 174 of file scanner.h.

+ Here is the call graph for this function:

The documentation for this class was generated from the following files:
NON_NEGATIVE_COMP
const PropWare::NonNegativeIntegerComparator NON_NEGATIVE_COMP
Global instance for shared use by PropWare applications.
Definition: comparator.cpp:28
YES_NO_COMP
const PropWare::YesNoComparator YES_NO_COMP
Global instance for shared use by PropWare applications.
Definition: comparator.cpp:29
PropWare::Scanner::input_prompt
void input_prompt(const char prompt[], const char failureResponse[], char userInput[], const size_t bufferLength, const Comparator< char > &comparator)
Prompt the user for input and store the value only if it is sanitized.
Definition: scanner.h:311
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...