MHVLib  20111227
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 #define MHV_DEBUG(__dbg_tx, __dbg_format, __dbg_args...) \
00046 do {\
00047         __dbg_tx.debug(__FILE__, __LINE__, __FUNCTION__, PSTR(__dbg_format), ## __dbg_args); \
00048 } while (0)
00049 
00050 struct mhv_tx_buffer {
00051         const char      *data;
00052         uint16_t        length;
00053         void            (*completeFunction)(const char *);
00054         bool            progmem;
00055         bool            isString;
00056 };
00057 typedef struct mhv_tx_buffer MHV_TX_BUFFER;
00058 
00059 class MHV_Device_TX {
00060 protected:
00061         MHV_TX_BUFFER   _currentTx;
00062         MHV_RingBuffer  &_txPointers;
00063         const char              *_tx;
00064 
00065         MHV_Device_TX(MHV_RingBuffer &txPointers);
00066         virtual void runTxBuffers()=0;
00067         bool moreTX();
00068         int nextCharacter();
00069 
00070 public:
00071         bool canWrite();
00072         bool write(const char *buffer);
00073         bool write(const char *buffer, uint16_t length);
00074         bool write(const char *buffer, void (*completeFunction)(const char *));
00075         bool write(const char *buffer, uint16_t length, void (*completeFunction)(const char *));
00076         bool write_P(PGM_P buffer);
00077         bool write_P(PGM_P buffer, uint16_t length);
00078         bool  debug(const char *file, int line, const char *function,
00079                         PGM_P format, ...);
00080         bool printf(PGM_P format, ...);
00081         bool write(uint8_t value);
00082         bool write(uint16_t value);
00083         bool write(uint32_t value);
00084         bool write(int8_t value);
00085         bool write(int16_t value);
00086         bool write(int32_t value);
00087 };
00088 
00089 
00090 
00091 #endif