Webcam Tracker  V1.0
Using openCV and an Arduino Uno
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members

Parses the serial input for commands. More...

#include <CommandParser.h>

Public Types

enum  COMMANDS {
  WAITING , PAN_TILT , PAN_TILT_OFFSET , HOME ,
  ERROR
}
 

Public Member Functions

uint8_t parse ()
 Reads serial input and checks for commands. More...
 
char * getCommand ()
 Get the command buffer. More...
 

Static Public Member Functions

static CommandParserinitialize ()
 Creates a new instance of the class if there isn't one. More...
 

Private Member Functions

 CommandParser ()
 Constructor of the class.
 
uint8_t createBuffer ()
 Checks the serial port for input if the character is a line feed(LF) or carriage return(CR) the end of a command is signaled. More...
 
uint8_t checkCommand ()
 Checks the buffer for a command Checks the buffer to see if there is a known command. If there isn't one, returns a command error. More...
 

Private Attributes

char ch
 Last read character from serial port.
 
char buf [255]
 All characters read from serial port.
 
uint8_t bufIndex
 Location index off the last character placed in the buffer.
 

Static Private Attributes

static CommandParserinstance = 0
 pointer to current used instance
 

Detailed Description

Parses the serial input for commands.

Member Enumeration Documentation

◆ COMMANDS

Parser commands

Enumerator
WAITING 

Indicates parser is waiting for a command ('/n' or '/r' character)

PAN_TILT 

Indicates a Pan and Tilt command.

PAN_TILT_OFFSET 

Indicates a Pan and Tilt offset command.

HOME 

Indicates a Home command.

ERROR 

Indicates that the command is not recognized.

29  {
30  WAITING,
31  PAN_TILT,
33  HOME,
34  ERROR
35  };
@ WAITING
Indicates parser is waiting for a command ('/n' or '/r' character)
Definition: CommandParser.h:30
@ ERROR
Indicates that the command is not recognized.
Definition: CommandParser.h:34
@ PAN_TILT
Indicates a Pan and Tilt command.
Definition: CommandParser.h:31
@ PAN_TILT_OFFSET
Indicates a Pan and Tilt offset command.
Definition: CommandParser.h:32
@ HOME
Indicates a Home command.
Definition: CommandParser.h:33

Member Function Documentation

◆ createBuffer()

uint8_t CommandParser::createBuffer ( )
private

Checks the serial port for input if the character is a line feed(LF) or carriage return(CR) the end of a command is signaled.

Returns
int:
  • -1: No serial.available
  • 0: Command not ended
  • 1: Command ended
18  {
19  if (Serial.available() > 0) {
20  ch = Serial.read();
21  if (ch != '\n' && ch != '\r') {
22  buf[bufIndex] = ch;
23  bufIndex++;
24  return 0; // Add to buffer
25  }
26  else {
27  //string termination
28  buf[bufIndex] = '\0';
29  bufIndex = 0;
30  return 1; // Buffer full
31  }
32  }
33  return -1;
34 }
char buf[255]
All characters read from serial port.
Definition: CommandParser.h:21
uint8_t bufIndex
Location index off the last character placed in the buffer.
Definition: CommandParser.h:22
char ch
Last read character from serial port.
Definition: CommandParser.h:20
Here is the caller graph for this function:

◆ checkCommand()

uint8_t CommandParser::checkCommand ( )
private

Checks the buffer for a command Checks the buffer to see if there is a known command. If there isn't one, returns a command error.

Returns
uint8_t: index of a command or error
See also
COMMANDS
36  {
37 
38  if (0 == strncmp("PTO", buf, 3)) {
39  return COMMANDS::PAN_TILT_OFFSET;
40  }
41 
42  if (0 == strncmp(buf, "PT", 2)) {
43  return COMMANDS::PAN_TILT;
44  }
45 
46  if (0 == strncmp("Ho", buf, 2)) {
47  return COMMANDS::HOME;
48  }
49 
50  return COMMANDS::ERROR;
51 }
Here is the caller graph for this function:

◆ initialize()

CommandParser * CommandParser::initialize ( )
static

Creates a new instance of the class if there isn't one.

Note
only one instance can excist because of the serial port
Returns
: CommandParser: Pointer to object instance
53  {
54  if (0 == instance) {
55  instance = new CommandParser;
56  }
57  else {
58  Serial.println("ERROR: CommandParser is already initialized");
59  }
60 
61  return instance;
62 }
static CommandParser * instance
pointer to current used instance
Definition: CommandParser.h:24
CommandParser()
Constructor of the class.
Definition: CommandParser.cpp:13
Here is the call graph for this function:

◆ parse()

uint8_t CommandParser::parse ( )

Reads serial input and checks for commands.

See also
createBuffer()
checkCommand()
Returns
index of a command or error
See also
COMMANDS
64  {
65  if (1 == createBuffer()) {
66  return checkCommand();
67  }
68 
69  return 0;
70 }
uint8_t checkCommand()
Checks the buffer for a command Checks the buffer to see if there is a known command....
Definition: CommandParser.cpp:36
uint8_t createBuffer()
Checks the serial port for input if the character is a line feed(LF) or carriage return(CR) the end o...
Definition: CommandParser.cpp:18
Here is the call graph for this function:

◆ getCommand()

char * CommandParser::getCommand ( )

Get the command buffer.

Returns
Pointer to the command buffer
72  {
73  return buf;
74 }