Merge branch 'edge' into button
[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
19 // Serial reading module
20 // Treats every received line as a command and passes it ( via event call ) to the command dispatcher.
21 // The command dispatcher will then ask other modules if they can do something with it
22 SerialConsole::SerialConsole( PinName rx_pin, PinName tx_pin, int baud_rate ){
23 this->serial = new mbed::Serial( rx_pin, tx_pin );
24 this->serial->baud(baud_rate);
25 }
26
27 // Called when the module has just been loaded
28 void SerialConsole::on_module_loaded() {
29 // We want to be called every time a new char is received
30 this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq);
31
32 // We only call the command dispatcher in the main loop, nowhere else
33 this->register_for_event(ON_MAIN_LOOP);
34
35 // Add to the pack of streams kernel can call to, for example for broadcasting
36 this->kernel->streams->append_stream(this);
37 }
38
39 // Called on Serial::RxIrq interrupt, meaning we have received a char
40 void SerialConsole::on_serial_char_received(){
41 while(this->serial->readable()){
42 char received = this->serial->getc();
43 // convert CR to NL (for host OSs that don't send NL)
44 if( received == '\r' ){ received = '\n'; }
45 this->buffer.push_back(received);
46 }
47 }
48
49 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
50 void SerialConsole::on_main_loop(void * argument){
51 if( this->has_char('\n') ){
52 string received;
53 received.reserve(20);
54 while(1){
55 char c;
56 this->buffer.pop_front(c);
57 if( c == '\n' ){
58 struct SerialMessage message;
59 message.message = received;
60 message.stream = this;
61 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
62 return;
63 }else{
64 received += c;
65 }
66 }
67 }
68 }
69
70
71 int SerialConsole::puts(const char* s)
72 {
73 return fwrite(s, strlen(s), 1, this->serial->_file);
74 }
75
76 int SerialConsole::_putc(int c)
77 {
78 return this->serial->putc(c);
79 }
80
81 int SerialConsole::_getc()
82 {
83 return this->serial->getc();
84 }
85
86 bool SerialConsole::has_char(char letter){
87 int index = this->buffer.head;
88 while( index != this->buffer.tail ){
89 if( this->buffer.buffer[index] == letter ){
90 return true;
91 }
92 index = this->buffer.next_block_index(index);
93 }
94 return false;
95 }