smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

Committer:
scachat
Date:
Tue Jul 31 21:11:18 2012 +0000
Revision:
0:31e91bb0ef3c
smoothie port to mbed online compiler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scachat 0:31e91bb0ef3c 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.
scachat 0:31e91bb0ef3c 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
scachat 0:31e91bb0ef3c 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->printf("T1\n");
scachat 0:31e91bb0ef3c 25 this->serial->baud(baud_rate);
scachat 0:31e91bb0ef3c 26 }
scachat 0:31e91bb0ef3c 27
scachat 0:31e91bb0ef3c 28 // Called when the module has just been loaded
scachat 0:31e91bb0ef3c 29 void SerialConsole::on_module_loaded() {
scachat 0:31e91bb0ef3c 30 // We want to be called every time a new char is received
scachat 0:31e91bb0ef3c 31 this->serial->attach(this, &SerialConsole::on_serial_char_received, mbed::Serial::RxIrq);
scachat 0:31e91bb0ef3c 32
scachat 0:31e91bb0ef3c 33 // We only call the command dispatcher in the main loop, nowhere else
scachat 0:31e91bb0ef3c 34 this->register_for_event(ON_MAIN_LOOP);
scachat 0:31e91bb0ef3c 35 }
scachat 0:31e91bb0ef3c 36
scachat 0:31e91bb0ef3c 37 // Called on Serial::RxIrq interrupt, meaning we have received a char
scachat 0:31e91bb0ef3c 38 void SerialConsole::on_serial_char_received(){
scachat 0:31e91bb0ef3c 39 while(this->serial->readable()){
scachat 0:31e91bb0ef3c 40 char received = this->serial->getc();
scachat 0:31e91bb0ef3c 41 // convert CR to NL (for host OSs that don't send NL)
scachat 0:31e91bb0ef3c 42 if( received == '\r' ){ received = '\n'; }
scachat 0:31e91bb0ef3c 43 this->buffer.push_back(received);
scachat 0:31e91bb0ef3c 44 }
scachat 0:31e91bb0ef3c 45 }
scachat 0:31e91bb0ef3c 46
scachat 0:31e91bb0ef3c 47 // Actual event calling must happen in the main loop because if it happens in the interrupt we will loose data
scachat 0:31e91bb0ef3c 48 void SerialConsole::on_main_loop(void * argument){
scachat 0:31e91bb0ef3c 49 if( this->has_char('\n') ){
scachat 0:31e91bb0ef3c 50 int index = 0;
scachat 0:31e91bb0ef3c 51 string received;
scachat 0:31e91bb0ef3c 52 while(1){
scachat 0:31e91bb0ef3c 53 char c;
scachat 0:31e91bb0ef3c 54 this->buffer.pop_front(c);
scachat 0:31e91bb0ef3c 55 if( c == '\n' ){
scachat 0:31e91bb0ef3c 56 struct SerialMessage message;
scachat 0:31e91bb0ef3c 57 message.message = received;
scachat 0:31e91bb0ef3c 58 message.stream = this;
scachat 0:31e91bb0ef3c 59 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
scachat 0:31e91bb0ef3c 60 return;
scachat 0:31e91bb0ef3c 61 }else{
scachat 0:31e91bb0ef3c 62 received += c;
scachat 0:31e91bb0ef3c 63 }
scachat 0:31e91bb0ef3c 64 }
scachat 0:31e91bb0ef3c 65 }
scachat 0:31e91bb0ef3c 66 }
scachat 0:31e91bb0ef3c 67
scachat 0:31e91bb0ef3c 68
scachat 0:31e91bb0ef3c 69 int SerialConsole::printf(const char* format, ...){
scachat 0:31e91bb0ef3c 70 va_list args;
scachat 0:31e91bb0ef3c 71 int result=0;
scachat 0:31e91bb0ef3c 72 va_start (args, format);
scachat 0:31e91bb0ef3c 73 this->serial->printf( format, args);
scachat 0:31e91bb0ef3c 74 va_end (args);
scachat 0:31e91bb0ef3c 75 return result;
scachat 0:31e91bb0ef3c 76 }
scachat 0:31e91bb0ef3c 77
scachat 0:31e91bb0ef3c 78
scachat 0:31e91bb0ef3c 79 bool SerialConsole::has_char(char letter){
scachat 0:31e91bb0ef3c 80 int index = this->buffer.head;
scachat 0:31e91bb0ef3c 81 while( index != this->buffer.tail ){
scachat 0:31e91bb0ef3c 82 if( this->buffer.buffer[index] == letter ){
scachat 0:31e91bb0ef3c 83 return true;
scachat 0:31e91bb0ef3c 84 }
scachat 0:31e91bb0ef3c 85 index = this->buffer.next_block_index(index);
scachat 0:31e91bb0ef3c 86 }
scachat 0:31e91bb0ef3c 87 return false;
scachat 0:31e91bb0ef3c 88 }