00001 /*! \file rprintf.h \brief printf routine and associated routines. */ 00002 //**************************************************************************** 00003 // 00004 // File Name : 'rprintf.h' 00005 // Title : printf routine and associated routines 00006 // Author : Pascal Stang - Copyright (C) 2000-2002 00007 // Created : 2000.12.26 00008 // Revised : 2003.5.1 00009 // Version : 1.0 00010 // Target MCU : Atmel AVR series and other targets 00011 // Editor Tabs : 4 00012 // 00013 // NOTE: This code is currently below version 1.0, and therefore is considered 00014 // to be lacking in some functionality or documentation, or may not be fully 00015 // tested. Nonetheless, you can expect most functions to work. 00016 // 00017 // This code is distributed under the GNU Public License 00018 // which can be found at http://www.gnu.org/licenses/gpl.txt 00019 // 00020 //**************************************************************************** 00021 00022 #ifndef RPRINTF_H 00023 #define RPRINTF_H 00024 00025 // needed for use of PSTR below 00026 #include <avr/pgmspace.h> 00027 00028 // include configuration 00029 #include "rprintfconf.h" 00030 00031 // defines/constants 00032 #define STRING_IN_RAM 0 00033 #define STRING_IN_ROM 1 00034 00035 // make a putchar for those that are used to using it 00036 #define putchar(c) rprintfChar(c); 00037 00038 // functions 00039 00040 //! initializes the rprintf library for an output stream 00041 // you must call this initializer once before using any other rprintf function 00042 // the argument must be a single-character stream output function 00043 void rprintfInit(void (*putchar_func)(unsigned char c)); 00044 00045 //! prints a single character to the current output device 00046 void rprintfChar(unsigned char c); 00047 00048 //! prints a null-terminated string stored in RAM 00049 void rprintfStr(char str[]); 00050 00051 //! prints a section of a string stored in RAM 00052 // begins printing at position indicated by <start> 00053 // prints number of characters indicated by <len> 00054 void rprintfStrLen(char str[], unsigned char start, unsigned char len); 00055 00056 //! prints a string stored in program rom 00057 // NOTE: this function does not actually store your string in 00058 // program rom, but merely reads it assuming you stored it properly. 00059 void rprintfProgStr(char str[]); 00060 // Using the function rprintfProgStrM(...) automatically causes 00061 // your string to be stored in ROM, thereby not wasting precious RAM 00062 // Example usage: 00063 // rprintfProgStrM("Hello, this string is stored in program rom"); 00064 #define rprintfProgStrM(string) (rprintfProgStr(PSTR(string))) 00065 00066 //! prints a carriage return and line feed 00067 // useful when printing to serial ports/terminals 00068 void rprintfCRLF(void); 00069 00070 // prints the number contained in "data" in hex format 00071 // u04,u08,u16,and u32 functions handle 4,8,16,or 32 bits respectively 00072 void rprintfu04(unsigned char data); ///< print 4-bit hex number 00073 void rprintfu08(unsigned char data); ///< print 8-bit hex number 00074 void rprintfu16(unsigned short data); ///< print 16-bit hex number 00075 void rprintfu32(unsigned long data); ///< print 32-bit hex number 00076 00077 //! a flexible integer number printing routine 00078 void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n); 00079 00080 #ifdef RPRINTF_FLOAT 00081 //! floating-point print routine 00082 void rprintfFloat(char numDigits, double x); 00083 #endif 00084 00085 // NOTE: Below you'll see the function prototypes of rprintf1RamRom and 00086 // rprintf2RamRom. rprintf1RamRom and rprintf2RamRom are both reduced versions 00087 // of the regular C printf() command. However, they are modified to be able 00088 // to read their text/format strings from RAM or ROM in the Atmel microprocessors. 00089 // Unless you really intend to, do not use the "RamRom" versions of the functions 00090 // directly. Instead use the #defined function versions: 00091 // 00092 // printfx("text/format",args) ...to keep your text/format string stored in RAM 00093 // - or - 00094 // printfxROM("text/format",args) ...to keep your text/format string stored in ROM 00095 // 00096 // where x is either 1 or 2 for the simple or more powerful version of printf() 00097 // 00098 // Since there is much more ROM than RAM available in the Atmel microprocessors, 00099 // and nearly all text/format strings are constant (never change in the course 00100 // of the program), you should try to use the ROM printf version exclusively. 00101 // This will ensure you leave as much RAM as possible for program variables and 00102 // data. 00103 00104 #ifdef RPRINTF_SIMPLE 00105 // a simple printf routine 00106 int rprintf1RamRom(unsigned char stringInRom, const char *format, ...); 00107 // #defines for RAM or ROM operation 00108 #define rprintf1(format, args...) rprintf1RamRom(STRING_IN_ROM, PSTR(format), ## args) 00109 #define rprintf1RAM(format, args...) rprintf1RamRom(STRING_IN_RAM, format, ## args) 00110 00111 // *** Default rprintf(...) *** 00112 // this next line determines what the the basic rprintf() defaults to: 00113 #define rprintf(format, args...) rprintf1RamRom(STRING_IN_ROM, PSTR(format), ## args) 00114 #endif 00115 00116 #ifdef RPRINTF_COMPLEX 00117 // a more powerful printf routine 00118 int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...); 00119 // #defines for RAM or ROM operation 00120 #define rprintf2(format, args...) rprintf2RamRom(STRING_IN_ROM, format, ## args) 00121 #define rprintf2RAM(format, args...) rprintf2RamRom(STRING_IN_RAM, format, ## args) 00122 00123 // *** Default rprintf(...) *** 00124 // this next line determines what the the basic rprintf() defaults to: 00125 #define rprintf(format, args...) rprintf2RamRom(STRING_IN_ROM, PSTR(format), ## args) 00126 #endif 00127 00128 #endif