Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Smoothie by
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->baud(baud_rate); 00025 } 00026 00027 // Called when the module has just been loaded 00028 void SerialConsole::on_module_loaded() { 00029 // We want to be called every time a new char is received 00030 this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq); 00031 00032 // We only call the command dispatcher in the main loop, nowhere else 00033 this->register_for_event(ON_MAIN_LOOP); 00034 00035 // Add to the pack of streams kernel can call to, for example for broadcasting 00036 THEKERNEL->streams->append_stream(this); 00037 } 00038 00039 // Called on Serial::RxIrq interrupt, meaning we have received a char 00040 void SerialConsole::on_serial_char_received(){ 00041 while(this->serial->readable()){ 00042 char received = this->serial->getc(); 00043 // convert CR to NL (for host OSs that don't send NL) 00044 if( received == '\r' ){ received = '\n'; } 00045 this->buffer.push_back(received); 00046 } 00047 } 00048 00049 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data 00050 void SerialConsole::on_main_loop(void * argument){ 00051 if( this->has_char('\n') ){ 00052 string received; 00053 received.reserve(20); 00054 while(1){ 00055 char c; 00056 this->buffer.pop_front(c); 00057 if( c == '\n' ){ 00058 struct SerialMessage message; 00059 message.message = received; 00060 message.stream = this; 00061 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); 00062 return; 00063 }else{ 00064 received += c; 00065 } 00066 } 00067 } 00068 } 00069 00070 00071 int SerialConsole::puts(const char* s) 00072 { 00073 return fwrite(s, strlen(s), 1, (FILE*)(*this->serial)); 00074 } 00075 00076 int SerialConsole::_putc(int c) 00077 { 00078 return this->serial->putc(c); 00079 } 00080 00081 int SerialConsole::_getc() 00082 { 00083 return this->serial->getc(); 00084 } 00085 00086 // Does the queue have a given char ? 00087 bool SerialConsole::has_char(char letter){ 00088 int index = this->buffer.tail; 00089 while( index != this->buffer.head ){ 00090 if( this->buffer.buffer[index] == letter ){ 00091 return true; 00092 } 00093 index = this->buffer.next_block_index(index); 00094 } 00095 return false; 00096 }
Generated on Tue Jul 12 2022 20:09:02 by
1.7.2
