Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Committer:
Bigcheese
Date:
Sun Mar 02 06:33:08 2014 +0000
Revision:
3:f151d08d335c
Parent:
2:1df0b61d3b5a
Bunch of stuff. Need to locally merge in updated USB changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael J. Spencer 2:1df0b61d3b5a 1 /*
scachat 0:31e91bb0ef3c 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
scachat 0:31e91bb0ef3c 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.
scachat 0:31e91bb0ef3c 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.
Michael J. Spencer 2:1df0b61d3b5a 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
scachat 0:31e91bb0ef3c 6 */
scachat 0:31e91bb0ef3c 7
scachat 0:31e91bb0ef3c 8 #include <string>
scachat 0:31e91bb0ef3c 9 #include <stdarg.h>
scachat 0:31e91bb0ef3c 10 using std::string;
scachat 0:31e91bb0ef3c 11 #include "libs/Module.h"
scachat 0:31e91bb0ef3c 12 #include "libs/Kernel.h"
scachat 0:31e91bb0ef3c 13 #include "libs/nuts_bolts.h"
scachat 0:31e91bb0ef3c 14 #include "SerialConsole.h"
scachat 0:31e91bb0ef3c 15 #include "libs/RingBuffer.h"
scachat 0:31e91bb0ef3c 16 #include "libs/SerialMessage.h"
scachat 0:31e91bb0ef3c 17 #include "libs/StreamOutput.h"
scachat 0:31e91bb0ef3c 18
scachat 0:31e91bb0ef3c 19 // Serial reading module
Michael J. Spencer 2:1df0b61d3b5a 20 // Treats every received line as a command and passes it ( via event call ) to the command dispatcher.
scachat 0:31e91bb0ef3c 21 // The command dispatcher will then ask other modules if they can do something with it
scachat 0:31e91bb0ef3c 22 SerialConsole::SerialConsole( PinName rx_pin, PinName tx_pin, int baud_rate ){
scachat 0:31e91bb0ef3c 23 this->serial = new mbed::Serial( rx_pin, tx_pin );
scachat 0:31e91bb0ef3c 24 this->serial->baud(baud_rate);
Michael J. Spencer 2:1df0b61d3b5a 25 }
scachat 0:31e91bb0ef3c 26
scachat 0:31e91bb0ef3c 27 // Called when the module has just been loaded
scachat 0:31e91bb0ef3c 28 void SerialConsole::on_module_loaded() {
scachat 0:31e91bb0ef3c 29 // We want to be called every time a new char is received
scachat 0:31e91bb0ef3c 30 this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq);
scachat 0:31e91bb0ef3c 31
scachat 0:31e91bb0ef3c 32 // We only call the command dispatcher in the main loop, nowhere else
scachat 0:31e91bb0ef3c 33 this->register_for_event(ON_MAIN_LOOP);
Michael J. Spencer 2:1df0b61d3b5a 34
Michael J. Spencer 2:1df0b61d3b5a 35 // Add to the pack of streams kernel can call to, for example for broadcasting
Michael J. Spencer 2:1df0b61d3b5a 36 THEKERNEL->streams->append_stream(this);
scachat 0:31e91bb0ef3c 37 }
Michael J. Spencer 2:1df0b61d3b5a 38
scachat 0:31e91bb0ef3c 39 // Called on Serial::RxIrq interrupt, meaning we have received a char
scachat 0:31e91bb0ef3c 40 void SerialConsole::on_serial_char_received(){
scachat 0:31e91bb0ef3c 41 while(this->serial->readable()){
scachat 0:31e91bb0ef3c 42 char received = this->serial->getc();
scachat 0:31e91bb0ef3c 43 // convert CR to NL (for host OSs that don't send NL)
scachat 0:31e91bb0ef3c 44 if( received == '\r' ){ received = '\n'; }
Michael J. Spencer 2:1df0b61d3b5a 45 this->buffer.push_back(received);
scachat 0:31e91bb0ef3c 46 }
scachat 0:31e91bb0ef3c 47 }
Michael J. Spencer 2:1df0b61d3b5a 48
scachat 0:31e91bb0ef3c 49 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
scachat 0:31e91bb0ef3c 50 void SerialConsole::on_main_loop(void * argument){
scachat 0:31e91bb0ef3c 51 if( this->has_char('\n') ){
scachat 0:31e91bb0ef3c 52 string received;
Michael J. Spencer 2:1df0b61d3b5a 53 received.reserve(20);
scachat 0:31e91bb0ef3c 54 while(1){
scachat 0:31e91bb0ef3c 55 char c;
scachat 0:31e91bb0ef3c 56 this->buffer.pop_front(c);
scachat 0:31e91bb0ef3c 57 if( c == '\n' ){
Michael J. Spencer 2:1df0b61d3b5a 58 struct SerialMessage message;
scachat 0:31e91bb0ef3c 59 message.message = received;
scachat 0:31e91bb0ef3c 60 message.stream = this;
Michael J. Spencer 2:1df0b61d3b5a 61 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
scachat 0:31e91bb0ef3c 62 return;
scachat 0:31e91bb0ef3c 63 }else{
scachat 0:31e91bb0ef3c 64 received += c;
scachat 0:31e91bb0ef3c 65 }
scachat 0:31e91bb0ef3c 66 }
scachat 0:31e91bb0ef3c 67 }
scachat 0:31e91bb0ef3c 68 }
scachat 0:31e91bb0ef3c 69
scachat 0:31e91bb0ef3c 70
Michael J. Spencer 2:1df0b61d3b5a 71 int SerialConsole::puts(const char* s)
Michael J. Spencer 2:1df0b61d3b5a 72 {
Michael J. Spencer 2:1df0b61d3b5a 73 return fwrite(s, strlen(s), 1, (FILE*)(*this->serial));
Michael J. Spencer 2:1df0b61d3b5a 74 }
Michael J. Spencer 2:1df0b61d3b5a 75
Michael J. Spencer 2:1df0b61d3b5a 76 int SerialConsole::_putc(int c)
Michael J. Spencer 2:1df0b61d3b5a 77 {
Michael J. Spencer 2:1df0b61d3b5a 78 return this->serial->putc(c);
scachat 0:31e91bb0ef3c 79 }
scachat 0:31e91bb0ef3c 80
Michael J. Spencer 2:1df0b61d3b5a 81 int SerialConsole::_getc()
Michael J. Spencer 2:1df0b61d3b5a 82 {
Michael J. Spencer 2:1df0b61d3b5a 83 return this->serial->getc();
Michael J. Spencer 2:1df0b61d3b5a 84 }
scachat 0:31e91bb0ef3c 85
Michael J. Spencer 2:1df0b61d3b5a 86 // Does the queue have a given char ?
scachat 0:31e91bb0ef3c 87 bool SerialConsole::has_char(char letter){
Michael J. Spencer 2:1df0b61d3b5a 88 int index = this->buffer.tail;
Michael J. Spencer 2:1df0b61d3b5a 89 while( index != this->buffer.head ){
scachat 0:31e91bb0ef3c 90 if( this->buffer.buffer[index] == letter ){
scachat 0:31e91bb0ef3c 91 return true;
scachat 0:31e91bb0ef3c 92 }
scachat 0:31e91bb0ef3c 93 index = this->buffer.next_block_index(index);
scachat 0:31e91bb0ef3c 94 }
scachat 0:31e91bb0ef3c 95 return false;
scachat 0:31e91bb0ef3c 96 }