MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_Device_TX.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 
00029 #ifndef MHV_TXBUFFER_H
00030 #define MHV_TXBUFFER_H
00031 
00032 #define MHVLIB_NEED_PURE_VIRTUAL
00033 
00034 #include <inttypes.h>
00035 #include <avr/interrupt.h>
00036 #include <MHV_io.h>
00037 #include <stdio.h>
00038 #include <MHV_RingBuffer.h>
00039 #include <avr/pgmspace.h>
00040 
00041 #define MHV_TX_BUFFER_CREATE(_mhvTxName, _mhvTxElementCount) \
00042         char _mhvTxName ## Buf[_mhvTxElementCount * sizeof(MHV_TX_BUFFER) + 1]; \
00043         MHV_RingBuffer _mhvTxName(_mhvTxName ## Buf, _mhvTxElementCount * sizeof(MHV_TX_BUFFER) + 1);
00044 
00045 struct mhv_tx_buffer {
00046         const char      *data;
00047         uint16_t        length;
00048         void            (*completeFunction)(const char *);
00049         bool            progmem;
00050         bool            isString;
00051 };
00052 typedef struct mhv_tx_buffer MHV_TX_BUFFER;
00053 
00054 class MHV_Device_TX {
00055 protected:
00056         MHV_TX_BUFFER   _currentTx;
00057         MHV_RingBuffer  *_txPointers;
00058         const char              *_tx;
00059 
00060         MHV_Device_TX(MHV_RingBuffer *txPointers);
00061         virtual void runTxBuffers()=0;
00062         bool moreTX();
00063         int nextCharacter();
00064 
00065 public:
00066         bool canWrite();
00067         bool write(const char *buffer);
00068         bool write(const char *buffer, uint16_t length);
00069         bool write(const char *buffer, void (*completeFunction)(const char *));
00070         bool write(const char *buffer, uint16_t length, void (*completeFunction)(const char *));
00071         bool write_P(PGM_P buffer);
00072         bool write_P(PGM_P buffer, uint16_t length);
00073 };
00074 
00075 #endif