TeensyPwm/cmd.cpp
2015-02-16 14:08:45 +01:00

86 lines
1.8 KiB
C++

#include <Arduino.h>
#include "cmd.h"
#include "pwm.h"
CmdServer::CmdServer() :
cmd(""), params("")
{
}
void CmdServer::begin() {
}
void CmdServer::exec() {
static uint8_t state = 0;
bool done = false;
char ch;
while ((ch = Serial.read()) != -1) {
switch (state) {
case 0:
if ((ch != ' ') && (ch != '\n')) {
cmd += ch;
} else if (ch == '\n') {
done = true;
} else {
state = 1;
}
break;
case 1:
if (ch != '\n') {
params += ch;
} else {
done = true;
state = 0;
}
break;
}
if (done) {
parseCommand();
cmd = "";
params = "";
}
}
}
void CmdServer::parseCommand() {
int space = params.indexOf(' ');
String p1 = "";
char pb1[128];
if (space != -1) {
params.toCharArray(pb1, 128, space+1);
}
if (cmd.equalsIgnoreCase("help")) {
Serial.println("Help: show, u, p, i, d");
} else if (cmd.equalsIgnoreCase("show")) {
Serial.print("U_Des = "); Serial.println(getUDes());
Serial.print("U_Cur = "); Serial.println(getUCur());
Serial.print("Dutycycle = "); Serial.print(getDutycycle()); Serial.println("%");
Serial.print("p = "); Serial.println(getP);
Serial.print("i = "); Serial.println(getI);
Serial.print("d = "); Serial.println(getD);
} else if (cmd.equalsIgnoreCase("u") && (space != -1)) {
float v = atof(pb1);
setUDes(v);
} else if (cmd.equalsIgnoreCase("p") && (space != -1)) {
float v = atof(pb1);
setP(v);
} else if (cmd.equalsIgnoreCase("i") && (space != -1)) {
float v = atof(pb1);
setI(v);
} else if (cmd.equalsIgnoreCase("d") && (space != -1)) {
float v = atof(pb1);
setD(v);
} else {}
m_client.println("command not found");
}
}