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 /*
Michael J. Spencer 2:1df0b61d3b5a 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
Michael J. Spencer 2:1df0b61d3b5a 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.
Michael J. Spencer 2:1df0b61d3b5a 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/>.
Michael J. Spencer 2:1df0b61d3b5a 6 */
Michael J. Spencer 2:1df0b61d3b5a 7
scachat 0:31e91bb0ef3c 8 #ifndef STREAMOUTPUT_H
scachat 0:31e91bb0ef3c 9 #define STREAMOUTPUT_H
scachat 0:31e91bb0ef3c 10
Bigcheese 3:f151d08d335c 11 #include <stdarg.h>
Bigcheese 3:f151d08d335c 12 #include <string.h>
Michael J. Spencer 2:1df0b61d3b5a 13 #include <stdio.h>
Michael J. Spencer 2:1df0b61d3b5a 14
Michael J. Spencer 2:1df0b61d3b5a 15 // This is a base class for all StreamOutput objects.
Michael J. Spencer 2:1df0b61d3b5a 16 // StreamOutputs are basically "things you can sent strings to". They are passed along with gcodes for example so modules can answer to those gcodes.
Michael J. Spencer 2:1df0b61d3b5a 17 // They are usually associated with a command source, but can also be a NullStreamOutput if we just want to ignore whatever is sent
Michael J. Spencer 2:1df0b61d3b5a 18
Michael J. Spencer 2:1df0b61d3b5a 19 class NullStreamOutput;
Michael J. Spencer 2:1df0b61d3b5a 20
scachat 0:31e91bb0ef3c 21 class StreamOutput {
scachat 0:31e91bb0ef3c 22 public:
Michael J. Spencer 2:1df0b61d3b5a 23 StreamOutput(){}
Michael J. Spencer 2:1df0b61d3b5a 24 virtual ~StreamOutput(){}
Michael J. Spencer 2:1df0b61d3b5a 25
Michael J. Spencer 2:1df0b61d3b5a 26 virtual int printf(const char* format, ...) __attribute__ ((format(printf, 2, 3))) {
Michael J. Spencer 2:1df0b61d3b5a 27 char b[64];
Michael J. Spencer 2:1df0b61d3b5a 28 char *buffer;
Michael J. Spencer 2:1df0b61d3b5a 29 // Make the message
Michael J. Spencer 2:1df0b61d3b5a 30 va_list args;
Michael J. Spencer 2:1df0b61d3b5a 31 va_start(args, format);
Michael J. Spencer 2:1df0b61d3b5a 32
Michael J. Spencer 2:1df0b61d3b5a 33 int size = vsnprintf(b, 64, format, args)
Michael J. Spencer 2:1df0b61d3b5a 34 + 1; // we add one to take into account space for the terminating \0
scachat 0:31e91bb0ef3c 35
Michael J. Spencer 2:1df0b61d3b5a 36 if (size < 64)
Michael J. Spencer 2:1df0b61d3b5a 37 buffer = b;
Michael J. Spencer 2:1df0b61d3b5a 38 else
Michael J. Spencer 2:1df0b61d3b5a 39 {
Michael J. Spencer 2:1df0b61d3b5a 40 buffer = new char[size];
Michael J. Spencer 2:1df0b61d3b5a 41 vsnprintf(buffer, size, format, args);
Michael J. Spencer 2:1df0b61d3b5a 42 }
Michael J. Spencer 2:1df0b61d3b5a 43 va_end(args);
scachat 0:31e91bb0ef3c 44
Michael J. Spencer 2:1df0b61d3b5a 45 puts(buffer);
scachat 0:31e91bb0ef3c 46
Michael J. Spencer 2:1df0b61d3b5a 47 if (buffer != b)
Michael J. Spencer 2:1df0b61d3b5a 48 delete[] buffer;
scachat 0:31e91bb0ef3c 49
Michael J. Spencer 2:1df0b61d3b5a 50 return size - 1;
Michael J. Spencer 2:1df0b61d3b5a 51 }
Michael J. Spencer 2:1df0b61d3b5a 52 virtual int _putc(int c) { return 1; }
Michael J. Spencer 2:1df0b61d3b5a 53 virtual int _getc(void) { return 0; }
Michael J. Spencer 2:1df0b61d3b5a 54 virtual int puts(const char* str) = 0;
Michael J. Spencer 2:1df0b61d3b5a 55
Michael J. Spencer 2:1df0b61d3b5a 56 static NullStreamOutput NullStream;
scachat 0:31e91bb0ef3c 57 };
scachat 0:31e91bb0ef3c 58
Michael J. Spencer 2:1df0b61d3b5a 59 class NullStreamOutput : public StreamOutput {
Michael J. Spencer 2:1df0b61d3b5a 60 public:
Michael J. Spencer 2:1df0b61d3b5a 61 int puts(const char* str) { return strlen(str); }
Michael J. Spencer 2:1df0b61d3b5a 62 };
scachat 0:31e91bb0ef3c 63
scachat 0:31e91bb0ef3c 64 #endif