/* * serstream * Implementation of input/output streams for the Arduino serial classes * * Created on: 2 Jan 2011 * Author: Andy Brown * * http://andybrown.me.uk/ws/terms-and-conditions */ #ifndef __810370EC_AD69_4ef7_91F5_B1AA16F14712 #define __810370EC_AD69_4ef7_91F5_B1AA16F14712 #include #include #include #include #include #include #include namespace std { /* * basic_serialbuf implements an unbuffered basic_streambuf as a backing buffer * for the IO classes */ template class basic_serialbuf : public basic_streambuf { public: /* * Types used here */ typedef charT char_type; typedef typename traits::int_type int_type; /* * constructor - wraps an existing Tserial class instance */ explicit basic_serialbuf(Tserial& serial_,ios_base::openmode which_ = ios_base::in | ios_base::out) : _serial(serial_) { basic_streambuf::openedFor = which_; } /* * Required to maintain the chain */ virtual ~basic_serialbuf() { } /* * Get a reference to the wrapped object */ Tserial& serial() { return _serial; } protected: /* * Get how many bytes available */ virtual int showmanyc(){ return _serial.available(); } /* * Read up to n chars */ virtual streamsize xsgetn(char_type* c, streamsize n) { streamsize i = 0; char_type data; while((data=_serial.read())!=-1 && i < n ) { c[i] = data; ++i; } return i; } /* * Write up to n chars */ virtual streamsize xsputn(const char_type* s, streamsize n){ for(streamsize i=0;i class basic_iserialstream : public basic_istream { public: /* * Types used here */ typedef charT char_type; /* * Constructor - default the serial object to #1 * Mega users can explicity initialise with one of * the others */ explicit basic_iserialstream(Tserial& serial_) : basic_ios(&sb), basic_istream(&sb), sb(serial_,ios_base::in) { } /* * Required to maintain the chain */ virtual ~basic_iserialstream() { } /* * Initialise the baud rate */ void begin(long speed_) { sb.serial().begin(speed_); } /* * The wrapped object */ private: basic_serialbuf sb; }; /* * Output stream */ template class basic_oserialstream : public basic_ostream { public: /* * Types used here */ typedef charT char_type; /* * Constructor - default the serial object to #1 * Mega users can explicity initialise with one of * the others */ explicit basic_oserialstream(Tserial& serial_) : basic_ios(&sb), basic_ostream(&sb), sb(serial_,ios_base::out) { } /* * Required to maintain the chain */ virtual ~basic_oserialstream() { } /* * Initialise the baud rate */ void begin(long speed_) { sb.serial().begin(speed_); } /* * The wrapped object */ private: basic_serialbuf sb; }; /* * Input/output stream */ template class basic_ioserialstream : public basic_iostream { public: /* * Types used here */ typedef charT char_type; /* * Constructor - default the serial object to #1 * Mega users can explicity initialise with one of * the others */ explicit basic_ioserialstream(Tserial& serial_) : basic_ios(&sb), basic_iostream(&sb), sb(serial_,ios_base::in | ios_base::out) { } /* * Required to maintain the chain */ virtual ~basic_ioserialstream(){ } /* * Initialise the baud rate */ void begin(long speed_) { sb.serial().begin(speed_); } /* * The wrapped object */ private: basic_serialbuf sb; }; } #endif