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
|
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"
|
|
|
|
#include "wiring_serial.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-01-25 23:33:19 +01:00
|
|
|
static char line[LINE_BUFFER_SIZE];
|
|
|
|
static uint8_t char_counter;
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2011-02-05 00:39:34 +01:00
|
|
|
void status_message(int status_code) {
|
2011-02-18 22:59:16 +01:00
|
|
|
if (status_code == 0) {
|
|
|
|
printPgmString(PSTR("ok\n\r"));
|
|
|
|
} 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:
|
|
|
|
printPgmString(PSTR("Bad number format\n\r")); break;
|
|
|
|
case STATUS_EXPECTED_COMMAND_LETTER:
|
|
|
|
printPgmString(PSTR("Expected command letter\n\r")); break;
|
|
|
|
case STATUS_UNSUPPORTED_STATEMENT:
|
|
|
|
printPgmString(PSTR("Unsupported statement\n\r")); break;
|
|
|
|
case STATUS_FLOATING_POINT_ERROR:
|
|
|
|
printPgmString(PSTR("Floating point error\n\r")); break;
|
|
|
|
default:
|
|
|
|
printInteger(status_code);
|
|
|
|
printPgmString(PSTR("\n\r"));
|
|
|
|
}
|
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-01-22 23:29:02 +01:00
|
|
|
beginSerial(BAUD_RATE);
|
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
|
|
|
|
uint8_t protocol_execute_line(char *line) {
|
|
|
|
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-02-18 22:19:01 +01:00
|
|
|
void protocol_process()
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
while((c = serialRead()) != -1)
|
|
|
|
{
|
2010-07-02 23:23:32 +02:00
|
|
|
if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute!
|
2011-02-05 00:39:34 +01:00
|
|
|
line[char_counter] = 0; // treminate string
|
2011-02-18 22:59:16 +01:00
|
|
|
status_message(protocol_execute_line(line));
|
2011-02-05 00:39:34 +01:00
|
|
|
char_counter = 0; // reset line buffer index
|
2010-03-02 08:19:21 +01:00
|
|
|
} else if (c <= ' ') { // Throw away whitepace and control characters
|
2009-02-04 14:01:24 +01:00
|
|
|
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
|
2010-03-03 13:04:51 +01:00
|
|
|
line[char_counter++] = c-'a'+'A';
|
2009-01-25 00:48:56 +01:00
|
|
|
} else {
|
2010-03-03 13:04:51 +01:00
|
|
|
line[char_counter++] = c;
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|