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