removed most mbed.h includes
[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 using std::string;
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12 #include "libs/nuts_bolts.h"
13 #include "SerialConsole.h"
14 #include "libs/RingBuffer.h"
15 #include "libs/SerialMessage.h"
16
17 // Serial reading module
18 // Treats every received line as a command and passes it ( via event call ) to the command dispatcher.
19 // The command dispatcher will then ask other modules if they can do something with it
20 SerialConsole::SerialConsole( PinName rx_pin, PinName tx_pin, int baud_rate ) : Serial( rx_pin, tx_pin ){ //We extend Serial, it is convenient
21 this->baud(baud_rate);
22 }
23
24 // Called when the module has just been loaded
25 void SerialConsole::on_module_loaded() {
26 // We want to be called every time a new char is received
27 this->attach(this, &SerialConsole::on_serial_char_received, Serial::RxIrq);
28
29 // We only call the command dispatcher in the main loop, nowhere else
30 this->register_for_event(ON_MAIN_LOOP);
31 }
32
33 // Called on Serial::RxIrq interrupt, meaning we have received a char
34 void SerialConsole::on_serial_char_received(){
35 if(this->readable()){
36 char received = this->getc();
37 //On newline, we have received a line, else concatenate in buffer
38 if( received == '\r' ){ return; }
39 this->buffer.push_back(received);
40 }
41 }
42
43 // Call event when newline received, for other modules to read the line
44 inline void SerialConsole::line_received(){}
45
46 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
47 void SerialConsole::on_main_loop(void * argument){
48 if( this->has_char('\n') ){
49 int index = 0;
50 string received;
51 while(1){
52 char c;
53 this->buffer.pop_front(c);
54 if( c == '\n' ){
55 struct SerialMessage message;
56 message.message = received;
57 message.stream = this;
58 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
59 return;
60 }else{
61 received += c;
62 }
63 }
64 }
65 }
66
67 bool SerialConsole::has_char(char letter){
68 int index = this->buffer.head;
69 while( index != this->buffer.tail ){
70 if( this->buffer.buffer[index] == letter ){
71 return true;
72 }
73 index = this->buffer.next_block_index(index);
74 }
75 return false;
76 }