add GRBL ^X and ? to serial uart handler
[clinton/Smoothieware.git] / src / modules / communication / SerialConsole.cpp
CommitLineData
df27a6a3 1/*
4cff3ded
AW
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie 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.
4 Smoothie 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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
4cff3ded
AW
6*/
7
8#include <string>
838b33b4 9#include <stdarg.h>
4cff3ded 10using std::string;
4cff3ded
AW
11#include "libs/Module.h"
12#include "libs/Kernel.h"
13#include "libs/nuts_bolts.h"
14#include "SerialConsole.h"
13e4a3f9 15#include "libs/RingBuffer.h"
423df6df 16#include "libs/SerialMessage.h"
838b33b4 17#include "libs/StreamOutput.h"
61134a65 18#include "libs/StreamOutputPool.h"
4cff3ded
AW
19
20// Serial reading module
df27a6a3 21// Treats every received line as a command and passes it ( via event call ) to the command dispatcher.
4cff3ded 22// The command dispatcher will then ask other modules if they can do something with it
838b33b4 23SerialConsole::SerialConsole( PinName rx_pin, PinName tx_pin, int baud_rate ){
da3a10b9 24 this->serial = new mbed::Serial( rx_pin, tx_pin );
838b33b4 25 this->serial->baud(baud_rate);
df27a6a3 26}
4cff3ded 27
4cff3ded
AW
28// Called when the module has just been loaded
29void SerialConsole::on_module_loaded() {
30 // We want to be called every time a new char is received
da3a10b9 31 this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq);
dcc91612 32 query_flag= false;
4cff3ded
AW
33
34 // We only call the command dispatcher in the main loop, nowhere else
35 this->register_for_event(ON_MAIN_LOOP);
dcc91612 36 this->register_for_event(ON_IDLE);
38d375e7
AW
37
38 // Add to the pack of streams kernel can call to, for example for broadcasting
314ab8f7 39 THEKERNEL->streams->append_stream(this);
4cff3ded 40}
ca5e9a57 41
4cff3ded
AW
42// Called on Serial::RxIrq interrupt, meaning we have received a char
43void SerialConsole::on_serial_char_received(){
171061cf 44 while(this->serial->readable()){
838b33b4 45 char received = this->serial->getc();
dcc91612
JM
46 if(received == '?') {
47 query_flag= true;
48 continue;
49 }
50 if(received == 26) { // ^X
51 halt_flag= true;
52 continue;
53 }
06ca20a3
BD
54 // convert CR to NL (for host OSs that don't send NL)
55 if( received == '\r' ){ received = '\n'; }
df27a6a3 56 this->buffer.push_back(received);
13e4a3f9 57 }
4cff3ded 58}
ca5e9a57 59
dcc91612
JM
60void SerialConsole::on_idle(void * argument)
61{
62 if(query_flag) {
63 query_flag= false;
64 puts(THEKERNEL->get_query_string().c_str());
65 }
66 if(halt_flag) {
67 halt_flag= false;
68 THEKERNEL->call_event(ON_HALT, nullptr);
69 }
70}
71
4cff3ded
AW
72// Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
73void SerialConsole::on_main_loop(void * argument){
13e4a3f9 74 if( this->has_char('\n') ){
13e4a3f9 75 string received;
57775cc4 76 received.reserve(20);
13e4a3f9
AW
77 while(1){
78 char c;
79 this->buffer.pop_front(c);
80 if( c == '\n' ){
df27a6a3 81 struct SerialMessage message;
b6c86164
AW
82 message.message = received;
83 message.stream = this;
314ab8f7 84 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
13e4a3f9
AW
85 return;
86 }else{
87 received += c;
88 }
89 }
90 }
4cff3ded
AW
91}
92
838b33b4 93
836222e7
MM
94int SerialConsole::puts(const char* s)
95{
25a021fb 96 return fwrite(s, strlen(s), 1, (FILE*)(*this->serial));
838b33b4
AW
97}
98
99b9ed8a
MM
99int SerialConsole::_putc(int c)
100{
101 return this->serial->putc(c);
102}
103
104int SerialConsole::_getc()
105{
106 return this->serial->getc();
107}
838b33b4 108
edac9072 109// Does the queue have a given char ?
13e4a3f9 110bool SerialConsole::has_char(char letter){
ed1517a1
MM
111 int index = this->buffer.tail;
112 while( index != this->buffer.head ){
13e4a3f9
AW
113 if( this->buffer.buffer[index] == letter ){
114 return true;
115 }
116 index = this->buffer.next_block_index(index);
117 }
118 return false;
119}