3
 
 * Implementation of input/output streams for the Arduino serial classes
 
5
 
 *  Created on: 2 Jan 2011
 
8
 
 *  http://andybrown.me.uk/ws/terms-and-conditions
 
11
 
#ifndef __810370EC_AD69_4ef7_91F5_B1AA16F14712
 
12
 
#define __810370EC_AD69_4ef7_91F5_B1AA16F14712
 
14
 
#include <basic_definitions>
 
16
 
//#include <stl_config.h>
 
22
 
#include <HardwareSerial.h>
 
28
 
 * basic_serialbuf implements an unbuffered basic_streambuf as a backing buffer
 
32
 
        template <class charT, class traits, class Tserial>
 
33
 
                class basic_serialbuf : public basic_streambuf<charT,traits>
 
41
 
                typedef charT char_type;
 
42
 
                typedef typename traits::int_type int_type;
 
45
 
         * constructor - wraps an existing Tserial class instance
 
48
 
                explicit basic_serialbuf(Tserial& serial_,ios_base::openmode which_ = ios_base::in | ios_base::out)
 
51
 
                        basic_streambuf<charT,traits>::openedFor = which_;
 
55
 
         * Required to maintain the chain
 
58
 
                virtual ~basic_serialbuf() { }
 
61
 
         * Get a reference to the wrapped object
 
64
 
                Tserial& serial() { return _serial; }
 
69
 
         * Get how many bytes available
 
72
 
                virtual int showmanyc(){
 
73
 
                        return _serial.available();
 
80
 
                virtual streamsize xsgetn(char_type* c, streamsize n) {
 
85
 
                        while((data=_serial.read())!=-1 && i < n ) {
 
96
 
                virtual streamsize xsputn(const char_type* s, streamsize n){
 
98
 
                        //_serial.print("[PUT ");
 
100
 
                        //_serial.print("] ");
 
101
 
                        for(streamsize i=0;i<n;i++)
 
113
 
         * write a single char
 
116
 
                virtual int_type overflow (int_type c = traits::eof()) {
 
117
 
                        if(!traits::eq_int_type(c,traits::eof()))
 
119
 
                                //_serial.print("[OF]");
 
120
 
                                if ( (char_type)c == '\n' )
 
122
 
                                _serial.print((char_type)c);
 
124
 
                        return traits::not_eof(c);
 
129
 
         * peek at a char where possible
 
132
 
                virtual int_type underflow(){
 
133
 
                        if(_serial.available())
 
134
 
                                return _serial.peek();
 
136
 
                                return traits::eof();
 
140
 
         * Read a char where possible
 
143
 
                virtual int_type uflow(){
 
144
 
                        if(_serial.available())
 
145
 
                                return _serial.read();
 
147
 
                                return traits::eof();
 
151
 
         * Our wrapped arduino class
 
162
 
        template <class charT, class traits, class Tserial> class basic_iserialstream
 
163
 
                : public basic_istream<charT,traits>
 
171
 
                typedef charT char_type;
 
174
 
         * Constructor - default the serial object to #1
 
175
 
         * Mega users can explicity initialise with one of
 
179
 
                explicit basic_iserialstream(Tserial& serial_)
 
180
 
                        : basic_ios<charT, traits>(&sb), basic_istream<charT,traits>(&sb), sb(serial_,ios_base::in)
 
185
 
         * Required to maintain the chain
 
188
 
                virtual ~basic_iserialstream() {  }
 
191
 
         * Initialise the baud rate
 
194
 
                void begin(long speed_) {
 
195
 
                        sb.serial().begin(speed_);
 
196
 
                        sb.serial().println(__FUNCTION__);
 
204
 
                basic_serialbuf<charT,traits,Tserial> sb;
 
212
 
        template <class charT, class traits, class Tserial> class basic_oserialstream
 
213
 
                : public basic_ostream<charT,traits>
 
221
 
                typedef charT char_type;
 
224
 
                 * Constructor - default the serial object to #1
 
225
 
                 * Mega users can explicity initialise with one of
 
229
 
                explicit basic_oserialstream(Tserial& serial_)
 
230
 
                        : basic_ios<charT, traits>(&sb), basic_ostream<charT,traits>(&sb), sb(serial_,ios_base::out)
 
235
 
                 * Required to maintain the chain
 
238
 
                virtual ~basic_oserialstream() {  }
 
241
 
                 * Initialise the baud rate
 
244
 
                void begin(long speed_) {
 
245
 
                        sb.serial().begin(speed_);
 
253
 
                basic_serialbuf<charT,traits,Tserial> sb;
 
258
 
 * Input/output stream
 
261
 
        template <class charT, class traits, class Tserial> class basic_ioserialstream
 
262
 
                 : public basic_iostream<charT,traits>
 
270
 
                typedef charT char_type;
 
273
 
                 * Constructor - default the serial object to #1
 
274
 
                 * Mega users can explicity initialise with one of
 
278
 
                explicit basic_ioserialstream(Tserial& serial_)
 
279
 
                        : basic_ios<charT, traits>(&sb), basic_iostream<charT,traits>(&sb), sb(serial_,ios_base::in | ios_base::out)
 
284
 
                 * Required to maintain the chain
 
287
 
                virtual ~basic_ioserialstream(){  }
 
290
 
                 * Initialise the baud rate
 
293
 
                void begin(long speed_) {
 
294
 
                        sb.serial().begin(speed_);
 
302
 
                basic_serialbuf<charT, traits, Tserial> sb;