smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialConsole.cpp Source File

SerialConsole.cpp

00001 /*  
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
00006 */
00007 
00008 #include <string>
00009 #include <stdarg.h>
00010 using std::string;
00011 #include "libs/Module.h"
00012 #include "libs/Kernel.h"
00013 #include "libs/nuts_bolts.h"
00014 #include "SerialConsole.h"
00015 #include "libs/RingBuffer.h"
00016 #include "libs/SerialMessage.h"
00017 #include "libs/StreamOutput.h"
00018 
00019 // Serial reading module
00020 // Treats every received line as a command and passes it ( via event call ) to the command dispatcher. 
00021 // The command dispatcher will then ask other modules if they can do something with it
00022 SerialConsole::SerialConsole( PinName rx_pin, PinName tx_pin, int baud_rate ){
00023     this->serial = new mbed::Serial( rx_pin, tx_pin );
00024     this->serial->printf("T1\n");
00025     this->serial->baud(baud_rate);
00026 }  
00027 
00028 // Called when the module has just been loaded
00029 void SerialConsole::on_module_loaded() {
00030     // We want to be called every time a new char is received
00031     this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq);
00032 
00033     // We only call the command dispatcher in the main loop, nowhere else
00034     this->register_for_event(ON_MAIN_LOOP);
00035 }
00036         
00037 // Called on Serial::RxIrq interrupt, meaning we have received a char
00038 void SerialConsole::on_serial_char_received(){
00039     while(this->serial->readable()){
00040         char received = this->serial->getc();
00041         // convert CR to NL (for host OSs that don't send NL)
00042         if( received == '\r' ){ received = '\n'; }
00043         this->buffer.push_back(received); 
00044     }
00045 }
00046         
00047 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
00048 void SerialConsole::on_main_loop(void * argument){
00049     if( this->has_char('\n') ){
00050         int index = 0;
00051         string received;
00052         while(1){
00053            char c;
00054            this->buffer.pop_front(c);
00055            if( c == '\n' ){
00056                 struct SerialMessage message; 
00057                 message.message = received;
00058                 message.stream = this;
00059                 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); 
00060                 return;
00061             }else{
00062                 received += c;
00063             }
00064         }
00065     }
00066 }
00067 
00068 
00069 int SerialConsole::printf(const char* format, ...){
00070     va_list args;
00071     int result=0; 
00072     va_start (args, format);
00073     this->serial->printf( format, args);
00074     va_end (args);
00075     return result;
00076 }
00077 
00078 
00079 bool SerialConsole::has_char(char letter){
00080     int index = this->buffer.head;
00081     while( index != this->buffer.tail ){
00082         if( this->buffer.buffer[index] == letter ){
00083             return true;
00084         }
00085         index = this->buffer.next_block_index(index);
00086     }
00087     return false;
00088 }