Works with simple/no kinematics

This commit is contained in:
IMback 2017-09-19 16:16:54 +02:00
parent dd6f37a960
commit 471d018bfe
7 changed files with 221 additions and 245 deletions

View file

@ -4,121 +4,145 @@
#include "hpglparser.h"
String buffer = "";
unsigned int bufferPosition = 0;
HpglParser::HpglParser(Plotter* plotter): _plotter(plotter)
{
}
bool isWhitespace(const char ch)
bool HpglParser::isWhitespace(const char ch)
{
return ch == ' ' || ch == '\n';
}
void addToBuffer(char ch)
{
buffer += ch;
}
void setBuffer(String in)
{
buffer = in;
}
void purgeBuffer()
{
buffer = buffer.substring(bufferPosition);
bufferPosition = 0;
}
bool isBufferEmpty()
{
return bufferPosition<buffer.length();
}
bool isBufferFull()
{
return (buffer.length()>40 && (!buffer.indexOf("LB"))>=0);
}
bool isNumber(char ch)
{
return ch=='-' || isdigit(ch);
}
void skipWhitespaces()
{
//preskoc non-alpha
while(bufferPosition<buffer.length() && isWhitespace(buffer[bufferPosition]))
{
bufferPosition++;
}
}
bool tryReadInt(uint16_t *i)
{
skipWhitespaces();
char ch = buffer[bufferPosition];
if (isNumber(ch))
{
unsigned int p = bufferPosition;
while(bufferPosition<buffer.length() && isNumber(buffer[bufferPosition]))
{
bufferPosition++;
}
String s = buffer.substring(p,bufferPosition);
*i = s.toInt();
return true;
}
return false;
}
bool isSeparator(char ch)
bool HpglParser::isSeparator(char ch)
{
return isWhitespace(ch) || ch==',';
}
bool readSeparator()
void HpglParser::findCoordinats()
{
bool b = false;
while(bufferPosition<buffer.length() && isSeparator(buffer[bufferPosition]))
{
b = true;
bufferPosition++;
}
return b;
bool wasPAM = (cmdMode == PA_M);
if(buffer[bufferIndex] == ';') cmdMode = 0;
buffer[bufferIndex] = '\0';
if(!xSet)
{
_nextPoint.x=atoi(buffer);
if(cmdMode != 0) xSet = true;
bufferIndex = -1;
}
else
{
_nextPoint.y=atoi(buffer);
if(wasPAM) _plotter->moveto(&_nextPoint);
else
{
Point currentPos = _plotter->getCurrentPos();
_plotter->moveto(currentPos.x + _nextPoint.x, currentPos.y + _nextPoint.y);
}
xSet = false;
bufferIndex = -1;
}
}
bool tryReadPoint(Point *pt)
void HpglParser::findCommand()
{
uint16_t i;
if (!tryReadInt(&i))
return false;
(*pt).x = i;
if (!readSeparator())
return false;
if (!tryReadInt(&i))
return false;
(*pt).y = i;
return true;
switch (buffer[bufferIndex])
{
case 'I': break;
case 'N':
{
cmdMode = SKIP_M;
bufferIndex = -1;
break;
}
case 'P':
{
if(buffer[bufferIndex-1] == 'S')
{
cmdMode = SKIP_M;
bufferIndex = -1;
}
else if(buffer[bufferIndex-1] == 'B')
{
_plotter->moveto(0,0);
cmdMode = SKIP_M;
bufferIndex = -1;
}
break;
}
case 'U':
{
if(buffer[bufferIndex-1] == 'P')
{
cmdMode = PA_M;
_plotter->pu();
bufferIndex = -1;
}
break;
}
case 'D':
{
if(buffer[bufferIndex-1] == 'P')
{
cmdMode = PA_M;
_plotter->pd();
bufferIndex = -1;
}
break;
}
case 'A':
{
if(buffer[bufferIndex-1] == 'P')
{
cmdMode = PA_M;
bufferIndex = -1;
}
else cmdMode = ERROR_M;
break;
}
case 'R':
{
if(buffer[bufferIndex-1] == 'P')
{
cmdMode = PR_M;
bufferIndex = -1;
}
else cmdMode = ERROR_M;
break;
}
}
}
String readHpglCmd()
int HpglParser::add(const char ch)
{
while(bufferPosition<buffer.length() && !isalpha(buffer[bufferPosition]))
{
bufferPosition++;
}
String cmd = buffer.substring(bufferPosition, bufferPosition + 2);
bufferPosition+=2;
return cmd;
buffer[bufferIndex]=ch;
//sense
if(cmdMode == 0 && bufferIndex > 0 && !isWhitespace(ch) )
{
findCommand();
}
else if((cmdMode == PA_M || cmdMode == PR_M) && (isSeparator(ch) || ch==';') )
{
findCoordinats();
}
else if(((cmdMode == SKIP_M || cmdMode == ERROR_M) && ch==';') || (cmdMode == 0 && ch==';') )
{
xSet=false;
bufferIndex = -1;
cmdMode=0;
}
bufferIndex++;
if(bufferIndex > 64)
{
xSet=false;
bufferIndex = 0;
cmdMode = 0;
return 2;
}
return (bufferIndex == 0 && cmdMode == 0) ? 1 : 0;
}
String readStringUntil(char ch)
{
unsigned int pos=bufferPosition;
while(bufferPosition<buffer.length() && buffer[bufferPosition]!=ch)
{
bufferPosition++;
}
String result = buffer.substring(pos, bufferPosition);
bufferPosition++;
return result;
}