grbl/protocol.c

89 wiersze
2.7 KiB
C
Czysty Zwykły widok Historia

2009-01-24 23:48:56 +00:00
/*
2011-02-18 21:11:53 +00:00
protocol.c - the serial protocol master control unit
2009-01-24 23:48:56 +00:00
Part of Grbl
Copyright (c) 2009-2011 Simen Svale Skogsrud
2009-01-24 23:48:56 +00: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 21:11:53 +00:00
#include "protocol.h"
2009-01-24 23:48:56 +00:00
#include "gcode.h"
#include "wiring_serial.h"
2011-02-04 23:45:41 +00:00
#include "settings.h"
#include "config.h"
2009-01-24 23:48:56 +00:00
#include <math.h>
#include "nuts_bolts.h"
2010-03-07 22:10:41 +00:00
#include <avr/pgmspace.h>
#define LINE_BUFFER_SIZE 50
2009-01-24 23:48:56 +00:00
2011-01-25 22:33:19 +00:00
static char line[LINE_BUFFER_SIZE];
static uint8_t char_counter;
2009-01-24 23:48:56 +00:00
static void status_message(int status_code) {
if (status_code == 0) {
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("ok\r\n"));
} else {
2011-02-17 09:14:27 +00:00
printPgmString(PSTR("error: "));
switch(status_code) {
case STATUS_BAD_NUMBER_FORMAT:
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("Bad number format\r\n")); break;
case STATUS_EXPECTED_COMMAND_LETTER:
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("Expected command letter\r\n")); break;
case STATUS_UNSUPPORTED_STATEMENT:
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("Unsupported statement\r\n")); break;
case STATUS_FLOATING_POINT_ERROR:
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("Floating point error\r\n")); break;
default:
printInteger(status_code);
2011-02-24 15:08:06 +00:00
printPgmString(PSTR("\r\n"));
}
}
}
void protocol_init()
2009-01-24 23:48:56 +00:00
{
beginSerial(BAUD_RATE);
printPgmString(PSTR("\r\nGrbl " GRBL_VERSION));
2010-03-07 22:10:41 +00:00
printPgmString(PSTR("\r\n"));
2009-01-24 23:48:56 +00: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
}
}
void protocol_process()
2009-01-24 23:48:56 +00:00
{
char c;
while((c = serialRead()) != -1)
{
if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute!
line[char_counter] = 0; // treminate string
status_message(protocol_execute_line(line));
char_counter = 0; // reset line buffer index
} else if (c <= ' ') { // Throw away whitepace and control characters
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
2010-03-03 12:04:51 +00:00
line[char_counter++] = c-'a'+'A';
2009-01-24 23:48:56 +00:00
} else {
2010-03-03 12:04:51 +00:00
line[char_counter++] = c;
2009-01-24 23:48:56 +00:00
}
}
}