cmd stuff

This commit is contained in:
Wolfgang Hottgenroth 2015-02-16 14:08:45 +01:00
parent 8820ac6782
commit 929943559f
3 changed files with 112 additions and 0 deletions

View File

@ -6,9 +6,12 @@
#include "rotary.h"
#include "pwm.h"
#include "display.h"
#include "cmd.h"
CmdServer cmdServer();
void setup() {
Serial.begin(115200);
Serial.println("Teensy SMPS");
@ -16,8 +19,10 @@ void setup() {
pwmInit();
rotaryInit();
displayInit();
cmdServer.begin();
}
void loop() {
displayExec();
cmdServer.exec();
}

85
cmd.cpp Normal file
View File

@ -0,0 +1,85 @@
#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");
}
}

22
cmd.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef CMD_H_
#define CMD_H_
#include <stdint.h>
#include <WString.h>
class CmdServer {
public:
CmdServer();
void begin();
void exec();
private:
void parseCommand();
String cmd;
String params;
};
#endif /* CMD_H_ */