2009-01-25 00:48:56 +01:00
|
|
|
/*
|
2011-02-18 22:11:53 +01:00
|
|
|
protocol.c - the serial protocol master control unit
|
2009-01-25 00:48:56 +01:00
|
|
|
Part of Grbl
|
|
|
|
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
2011-09-14 05:57:16 +02:00
|
|
|
Copyright (c) 2011 Sungeun K. Jeon
|
2009-01-25 00:48:56 +01:00
|
|
|
|
|
|
|
Grbl is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Grbl is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
2011-02-18 22:11:53 +01:00
|
|
|
#include "protocol.h"
|
2009-01-25 00:48:56 +01:00
|
|
|
#include "gcode.h"
|
2011-05-31 22:45:38 +02:00
|
|
|
#include "serial.h"
|
2011-06-03 15:28:14 +02:00
|
|
|
#include "print.h"
|
2011-02-05 00:45:41 +01:00
|
|
|
#include "settings.h"
|
2011-02-05 00:55:37 +01:00
|
|
|
#include "config.h"
|
2009-01-25 00:48:56 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include "nuts_bolts.h"
|
2010-03-07 23:10:41 +01:00
|
|
|
#include <avr/pgmspace.h>
|
2010-03-14 23:45:31 +01:00
|
|
|
#define LINE_BUFFER_SIZE 50
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2011-12-10 19:18:24 +01:00
|
|
|
static char line[LINE_BUFFER_SIZE]; // Line to be executed. Zero-terminated.
|
|
|
|
static uint8_t char_counter; // Last character counter in line variable.
|
|
|
|
static uint8_t iscomment; // Comment/block delete flag for processor to ignore comment characters.
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2011-12-10 19:18:24 +01:00
|
|
|
static void status_message(int status_code)
|
|
|
|
{
|
2011-02-18 22:59:16 +01:00
|
|
|
if (status_code == 0) {
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("ok\r\n"));
|
2011-02-18 22:59:16 +01:00
|
|
|
} else {
|
2011-02-17 10:14:27 +01:00
|
|
|
printPgmString(PSTR("error: "));
|
2011-02-18 22:59:16 +01:00
|
|
|
switch(status_code) {
|
|
|
|
case STATUS_BAD_NUMBER_FORMAT:
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("Bad number format\r\n")); break;
|
2011-02-18 22:59:16 +01:00
|
|
|
case STATUS_EXPECTED_COMMAND_LETTER:
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("Expected command letter\r\n")); break;
|
2011-02-18 22:59:16 +01:00
|
|
|
case STATUS_UNSUPPORTED_STATEMENT:
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("Unsupported statement\r\n")); break;
|
2011-02-18 22:59:16 +01:00
|
|
|
case STATUS_FLOATING_POINT_ERROR:
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("Floating point error\r\n")); break;
|
2011-02-18 22:59:16 +01:00
|
|
|
default:
|
|
|
|
printInteger(status_code);
|
2011-02-24 16:08:06 +01:00
|
|
|
printPgmString(PSTR("\r\n"));
|
2011-02-18 22:59:16 +01:00
|
|
|
}
|
2011-02-05 00:39:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 22:19:01 +01:00
|
|
|
void protocol_init()
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
2011-12-10 19:18:24 +01:00
|
|
|
char_counter = 0; // Reset line input
|
|
|
|
iscomment = false;
|
2011-02-10 17:01:07 +01:00
|
|
|
printPgmString(PSTR("\r\nGrbl " GRBL_VERSION));
|
2010-03-07 23:10:41 +01:00
|
|
|
printPgmString(PSTR("\r\n"));
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2011-02-18 22:59:16 +01:00
|
|
|
// Executes one line of input according to protocol
|
2011-12-10 19:18:24 +01:00
|
|
|
uint8_t protocol_execute_line(char *line)
|
|
|
|
{
|
2011-02-18 22:59:16 +01:00
|
|
|
if(line[0] == '$') {
|
|
|
|
return(settings_execute_line(line)); // Delegate lines starting with '$' to the settings module
|
|
|
|
} else {
|
|
|
|
return(gc_execute_line(line)); // Everything else is gcode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 19:18:24 +01:00
|
|
|
|
|
|
|
// Process one line of incoming serial data. Remove unneeded characters and capitalize.
|
2011-02-18 22:19:01 +01:00
|
|
|
void protocol_process()
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
|
|
|
char c;
|
2011-06-03 21:50:02 +02:00
|
|
|
while((c = serial_read()) != SERIAL_NO_DATA)
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
2011-12-10 19:18:24 +01:00
|
|
|
if ((c == '\n') || (c == '\r')) { // End of line reached
|
2011-08-16 03:39:44 +02:00
|
|
|
if (char_counter > 0) {// Line is complete. Then execute!
|
2011-12-10 19:18:24 +01:00
|
|
|
line[char_counter] = 0; // Terminate string
|
2011-08-16 03:39:44 +02:00
|
|
|
status_message(protocol_execute_line(line));
|
|
|
|
} else {
|
|
|
|
// Empty or comment line. Skip block.
|
|
|
|
status_message(STATUS_OK); // Send status message for syncing purposes.
|
|
|
|
}
|
|
|
|
char_counter = 0; // Reset line buffer index
|
|
|
|
iscomment = false; // Reset comment flag
|
2011-08-16 03:14:39 +02:00
|
|
|
} else {
|
2011-08-16 03:39:44 +02:00
|
|
|
if (iscomment) {
|
|
|
|
// Throw away all comment characters
|
|
|
|
if (c == ')') {
|
|
|
|
// End of comment. Resume line.
|
|
|
|
iscomment = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (c <= ' ') {
|
|
|
|
// Throw away whitepace and control characters
|
|
|
|
} else if (c == '/') {
|
|
|
|
// Disable block delete and throw away character
|
|
|
|
// To enable block delete, uncomment following line. Will ignore until EOL.
|
|
|
|
// iscomment = true;
|
|
|
|
} else if (c == '(') {
|
|
|
|
// Enable comments flag and ignore all characters until ')' or EOL.
|
|
|
|
iscomment = true;
|
|
|
|
} else if (char_counter >= LINE_BUFFER_SIZE-1) {
|
|
|
|
// Throw away any characters beyond the end of the line buffer
|
|
|
|
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
|
|
|
|
line[char_counter++] = c-'a'+'A';
|
|
|
|
} else {
|
|
|
|
line[char_counter++] = c;
|
|
|
|
}
|
|
|
|
}
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|