Skip to content

Commit

Permalink
Strip line numbers and checksums from gcode for parsing.
Browse files Browse the repository at this point in the history
BEWARE that this probably will result in errors since the TFT will likely be thinking all of these commands are coming from its own system. In order for this to work properly, I'll probably need to add logic to make some parsing contingent upon where the gcode originated from.
  • Loading branch information
Steve Blanding committed Feb 14, 2020
1 parent 7d40a86 commit bf164af
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions TFT/src/User/API/interfaceCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

QUEUE infoCmd; //
QUEUE infoCacheCmd; // Only when heatHasWaiting() is false the cmd in this cache will move to infoCmd queue.
char gcode[CMD_MAX_CHAR]; //temporary buffer so we can work with pure gcode during parsing

static u8 cmd_index=0;

// Is there a code character in the current gcode command.
static bool cmd_seen(char code)
{
for(cmd_index = 0; infoCmd.queue[infoCmd.index_r].gcode[cmd_index] != 0 && cmd_index < CMD_MAX_CHAR; cmd_index++)
for(cmd_index = 0; gcode[cmd_index] != 0 && cmd_index < CMD_MAX_CHAR; cmd_index++)
{
if(infoCmd.queue[infoCmd.index_r].gcode[cmd_index] == code)
if(gcode[cmd_index] == code)
{
cmd_index += 1;
return true;
Expand All @@ -24,13 +25,13 @@ static bool cmd_seen(char code)
// Get the int after 'code', Call after cmd_seen('code').
static u32 cmd_value(void)
{
return (strtol(&infoCmd.queue[infoCmd.index_r].gcode[cmd_index], NULL, 10));
return (strtol(&gcode[cmd_index], NULL, 10));
}

// Get the float after 'code', Call after cmd_seen('code').
static float cmd_float(void)
{
return (strtod(&infoCmd.queue[infoCmd.index_r].gcode[cmd_index], NULL));
return (strtod(&gcode[cmd_index], NULL));
}

// Store gcode cmd to infoCmd queue, this cmd will be sent by UART in sendQueueCmd(),
Expand Down Expand Up @@ -152,34 +153,36 @@ void sendQueueCmd(void)
bool avoid_terminal = false;
u16 cmd=0;

// look for M117 commands even when they are couched behind N commands (line numbers)
if (cmd_seen('M'))
// copy the gcode into our local buffer, stripping out any linenumbers or checksums
if (infoCmd.queue[infoCmd.index_r].gcode[0] == 'N')
{
if (cmd_value() == 117)
// this code starts with a line number (and likely ends with a checksum) so we need to strip it out
int i = 1;
// find the first space
while (i < CMD_MAX_CHAR && infoCmd.queue[infoCmd.index_r].gcode[i] != ' ') i++;
// then find the next non space
while (i < CMD_MAX_CHAR && infoCmd.queue[infoCmd.index_r].gcode[i] == ' ') i++;
// copy the rest
strncpy(gcode, &infoCmd.queue[infoCmd.index_r].gcode[i], CMD_MAX_CHAR);
// and strip out anything after the last '*', thereby removing the checksum
for (i = CMD_MAX_CHAR; i > 0; i--)
{
char message[CMD_MAX_CHAR];
strncpy(message, &infoCmd.queue[infoCmd.index_r].gcode[cmd_index + 4], CMD_MAX_CHAR);
// strip out any checksum that might be in the string
for (int i = 0; i < CMD_MAX_CHAR && message[i] !=0 ; i++)
{
if (message[i] == '*')
{
message[i] = 0;
break;
}
}
statusScreen_setMsg((u8 *)"M117", (u8 *)&message);
if (infoMenu.menu[infoMenu.cur] != menuStatus)
{
popupReminder((u8 *)"M117", (u8 *)&message);
}
if (gcode[i] == '*')
{
gcode[i] = 0;
break;
}
}
}

switch(infoCmd.queue[infoCmd.index_r].gcode[0])
else
{
strncpy(gcode, infoCmd.queue[infoCmd.index_r].gcode, CMD_MAX_CHAR);
}

switch(gcode[0])
{
case 'M':
cmd=strtol(&infoCmd.queue[infoCmd.index_r].gcode[1],NULL,10);
cmd=strtol(&gcode[1],NULL,10);
switch(cmd)
{
case 0:
Expand Down Expand Up @@ -225,7 +228,7 @@ void sendQueueCmd(void)
{
TOOL i = heatGetCurrentToolNozzle();
if(cmd_seen('T')) i = (TOOL)(cmd_value() + NOZZLE0);
infoCmd.queue[infoCmd.index_r].gcode[3]='4';
gcode[3]='4';
heatSetIsWaiting(i,true);
}
case 104: //M104
Expand All @@ -240,7 +243,7 @@ void sendQueueCmd(void)
{
char buf[12];
sprintf(buf, "S%d\n", heatGetTargetTemp(i));
strcat(infoCmd.queue[infoCmd.index_r].gcode,(const char*)buf);
strcat(gcode,(const char*)buf);
heatSetSendWaiting(i, false);
}
break;
Expand All @@ -265,7 +268,7 @@ void sendQueueCmd(void)
{
char buf[12];
sprintf(buf, "S%d\n", fanGetSpeed(i));
strcat(infoCmd.queue[infoCmd.index_r].gcode,(const char*)buf);
strcat(gcode,(const char*)buf);
fanSetSendWaiting(i, false);
}
break;
Expand All @@ -284,17 +287,17 @@ void sendQueueCmd(void)
positionSetUpdateWaiting(false);
#endif
break;
#if 0

case 117: //M117
statusScreen_setMsg((u8 *)"M117", (u8 *)&infoCmd.queue[infoCmd.index_r].gcode[5]);
statusScreen_setMsg((u8 *)"M117", (u8 *)&gcode[5]);
if (infoMenu.menu[infoMenu.cur] != menuStatus)
{
popupReminder((u8 *)"M117", (u8 *)&infoCmd.queue[infoCmd.index_r].gcode[5]);
popupReminder((u8 *)"M117", (u8 *)&gcode[5]);
}
break;
#endif

case 190: //M190
infoCmd.queue[infoCmd.index_r].gcode[2]='4';
gcode[2]='4';
heatSetIsWaiting(BED,true);
case 140: //M140
if(cmd_seen('S'))
Expand All @@ -305,7 +308,7 @@ void sendQueueCmd(void)
{
char buf[12];
sprintf(buf, "S%d\n", heatGetTargetTemp(BED));
strcat(infoCmd.queue[infoCmd.index_r].gcode,(const char*)buf);
strcat(gcode,(const char*)buf);
heatSetSendWaiting(BED, false);
}
break;
Expand All @@ -319,7 +322,7 @@ void sendQueueCmd(void)
{
char buf[12];
sprintf(buf, "S%d\n", speedGetPercent(0));
strcat(infoCmd.queue[infoCmd.index_r].gcode,(const char*)buf);
strcat(gcode,(const char*)buf);
speedSetSendWaiting(0, false);
}
break;
Expand All @@ -332,15 +335,15 @@ void sendQueueCmd(void)
{
char buf[12];
sprintf(buf, "S%d\n", speedGetPercent(1));
strcat(infoCmd.queue[infoCmd.index_r].gcode,(const char*)buf);
strcat(gcode,(const char*)buf);
speedSetSendWaiting(1, false);
}
break;
}
break;

case 'G':
cmd=strtol(&infoCmd.queue[infoCmd.index_r].gcode[1],NULL,10);
cmd=strtol(&gcode[1],NULL,10);
switch(cmd)
{
case 0: //G0
Expand Down Expand Up @@ -397,7 +400,7 @@ void sendQueueCmd(void)
break;

case 'T':
cmd=strtol(&infoCmd.queue[infoCmd.index_r].gcode[1], NULL, 10);
cmd=strtol(&gcode[1], NULL, 10);
heatSetCurrentToolNozzle((TOOL)(cmd + NOZZLE0));
break;
}
Expand Down

0 comments on commit bf164af

Please sign in to comment.