I am trying to learn class libraries with a fairly simple class and function to concatenate serial data and report the compiled parsed data through another function. My problem is that I can't seem to get it to compile without errors. The errors range from missing types to missing functions. They all seem to be related to the Strings deceleration in the class for the return result. This program is using multiple ultrasonic range finders and streams the data to a host. It is programed to have a control command set to manipulate different settings within the routines. The issues I am having are related to the parsing of the input commands.
Could you please give me some pointers as to what my misunderstanding is.
Thank you
Using the Ardunio 1.5.1.r2 IDE
Error report
In file included from SysComs.cpp:18:
SysComs.h:8: error: 'String' does not name a type
SysComs.h:13: error: 'String' does not name a type
SysComs.cpp: In constructor 'SysComs::SysComs()':
SysComs.cpp:26: error: '_result' was not declared in this scope
SysComs.cpp: In member function 'bool SysComs::encode(char)':
SysComs.cpp:35: error: '_result' was not declared in this scope
SysComs.cpp:36: error: 'millis' was not declared in this scope
SysComs.cpp:48: error: '_result' was not declared in this scope
SysComs.cpp:53: error: '_result' was not declared in this scope
SysComs.cpp: At global scope:
SysComs.cpp:60: error: 'String' does not name a type
SysComs.cpp: In member function 'unsigned int SysComs::length()':
SysComs.cpp:65: error: '_result' was not declared in this scope
SysComs.cpp: In member function 'long unsigned int SysComs::last_time()':
SysComs.cpp:69: error: 'millis' was not declared in this scope
SysComs.h
#ifndef SysComs_h
#define SysComs_h
class SysComs {
public:
SysComs();
bool encode(char c);
String result() ;
unsigned int length() ;
unsigned long last_time() ;
private:
String _result;
unsigned int _count;
bool _valid;
bool _end;
unsigned long _last_time;
unsigned long _in_time;
};
#endif
SysComs.cpp
/*
SysComs - Library for parsing serial input byte by byte without significantly delaying the main program
Data structure command must be preceded by " $ " Results will strip leading $
bool <->char SysComs.encode(char) returns true at end of command <-> parse data input
string <- SysComs.result() returns string result value
int <- SysComs.length() returns length of string result
float <- SysComs.last_time() returns time last command received
ie:
if (SysComs.encode(Serial.read())) { // True if a complete command data structure found
String CommandString = SysCom.result(); // Parse data to systems command routine
int Command_Length = SysCom.length(); // Length of command ( without $ )
} else { // not a valid command fulfilled
float Last_Command = SysComs.last_time(); // Time lapsed since last command received in mS
}
*/
#include "SysComs.h"
SysComs::SysComs() {
_count = 0;
_valid = false;
_last_time = 0;
_in_time = 0;
_end = false;
_result = '\0';
}
bool SysComs::encode(char c) {
_end = false;
switch(c) {
case '
main.h
#ifndef main_h
#define main_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif
#define POWER_RELAY 12
#define ESTOP 0
#define POWER_READ 1
#endif
main.cpp
#include <String.h>
#include <Arduino.h>
#include "main.h"
#include "NewPing.cpp"
#include "SysComs.cpp"
SysComs SC;
void SetPower(boolean _state);
void SetSleep(boolean _state);
void ResetTimers() ;
void SonarScan();
void echoCheck() ;
bool ComCheck() ;
void Command(String d);
void StreamData() ;
void StreamStatus();
void setup() {
}
void loop() {
ComCheck(); // Check for commands
}
bool ComCheck() {
unsigned long td = millis(); // Store time for error correction
while(Serial.available()) { // While serial buffer full
if (SC.encode(Serial.read())) { // True if a complete command data structure found
Command(SC.result()); // Parse data to systems command routien
ResetTimers(); // Reset interval timers
return true; // Com check reported new data which delayed normal time cycles
}
}
pto += millis() - td; // Add read delay to time off set for echo accuracy
return false; // No new commands received
}
void Command(String d) {
d.toUpperCase(); // Convert data to upper case
if (d == "SLEEP") {
SetSleep(true);
}
if (d == "WAKE") {
if (PWR) {
SetSleep(false);
} else {
Serial.println("$SRERR,*7");
}
}
if (d == "POWERDOWN") {
SetPower(false);
}
if (d == "POWERUP") {
SetPower(true);
delay (100);
}
if (d == "STATUS") {
StreamStatus();
}
StreamStatus();
}
: // Command begin
_count = 0;
_valid = true;
_result = '\0';
_in_time = millis();
return _end;
case '\r': // Command end
_end = true;
_valid = false;
_last_time = millis();
case '\n': // Command end
_end = true;
_valid = false;
_last_time = millis();
}
if (_valid) {
_result += c; // Append data to result
_count++;; // increment length
}
if (_count > 25) { // Data too long
_count = 0; // Clear results
_result = '\0';
_end = false;
_valid = false;
}
return _end;
}
String SysComs::result() {
if (_end) return _result;
}
unsigned int SysComs::length() {
return _result.length();
}
unsigned long SysComs::last_time() {
return (millis() - _last_time);
}
main.h
§DISCOURSE_HOISTED_CODE_3§
main.cpp
§DISCOURSE_HOISTED_CODE_4§