» Advertenties

Zo 20 Mei 2012, 07:51

C code -
  1. //*****************************************************************************
  2. //
  3. // File Name : rprintf.h
  4. // Title : Support fot rpintf function
  5. // Author : Smoerijf.be (Orinal by Pascal Stang - edit by smoerijf)
  6. // Target MCU : Atmel AVR Series
  7. //
  8. //*****************************************************************************
  9. //@{
  10.  
  11. #ifndef RPRINTF_H
  12. #define RPRINTF_H
  13.  
  14. // needed for use of PSTR below
  15. #include <avr/pgmspace.h>
  16.  
  17. // configuration
  18. // defining RPRINTF_SIMPLE will compile a smaller, simpler, and faster printf() function
  19. // defining RPRINTF_COMPLEX will compile a larger, more capable, and slower printf() function
  20. #ifndef RPRINTF_COMPLEX
  21.   #define RPRINTF_SIMPLE
  22. #endif
  23.  
  24. // Define RPRINTF_FLOAT to enable the floating-point printf function: rprintfFloat()
  25. // (adds +4600bytes or 2.2Kwords of code)
  26.  
  27. // defines/constants
  28. #define STRING_IN_RAM   0
  29. #define STRING_IN_ROM   1
  30.  
  31. // make a putchar for those that are used to using it
  32. //#define putchar(c)   rprintfChar(c);
  33.  
  34. // functions
  35.  
  36. //! Initializes the rprintf library for an output stream.
  37. /// You must call this initializer once before using any other rprintf function.
  38. /// The argument must be a character stream output function.
  39. void rprintfInit(void (*putchar_func)(unsigned char c));
  40.  
  41. //! prints a single character to the current output device
  42. void rprintfChar(unsigned char c);
  43.  
  44. //! prints a null-terminated string stored in RAM
  45. void rprintfStr(char str[]);
  46.  
  47. //! Prints a section of a string stored in RAM.
  48. /// Begins printing at position indicated by <start>,
  49. /// and prints number of characters indicated by <len>.
  50. void rprintfStrLen(char str[], unsigned int start, unsigned int len);
  51.  
  52. //! prints a string stored in program rom
  53. /// \note This function does not actually store your string in
  54. /// program rom, but merely reads it assuming you stored it properly.
  55. void rprintfProgStr(const prog_char str[]);
  56.  
  57. //! Using the function rprintfProgStrM(...) automatically causes
  58. /// your string to be stored in ROM, thereby not wasting precious RAM.
  59. /// Example usage:
  60. /// \code
  61. /// rprintfProgStrM("Hello, this string is stored in program rom");
  62. /// \endcode
  63. #define rprintfProgStrM(string)       (rprintfProgStr(PSTR(string)))
  64.  
  65. //! Prints a carriage-return and line-feed.
  66. /// Useful when printing to serial ports/terminals.
  67. void rprintfCRLF(void);
  68.  
  69. // Prints the number contained in "data" in hex format
  70. // u04,u08,u16,and u32 functions handle 4,8,16,or 32 bits respectively
  71. void rprintfu04(unsigned char data);   ///< Print 4-bit hex number. Outputs a single hex character.
  72. void rprintfu08(unsigned char data);   ///< Print 8-bit hex number. Outputs two hex characters.
  73. void rprintfu16(unsigned short data);   ///< Print 16-bit hex number. Outputs four hex characters.
  74. void rprintfu32(unsigned long data);   ///< Print 32-bit hex number. Outputs eight hex characters.
  75.  
  76. //! A flexible integer-number printing routine.
  77. /// Print the number "n" in the given "base", using exactly "numDigits".
  78. ///   Print +/- if signed flag "isSigned" is TRUE.
  79. ///   The character specified in "padchar" will be used to pad extra characters.
  80. ///
  81. ///   Examples:
  82. /// \code
  83. /// uartPrintfNum(10, 6, TRUE, ' ', 1234); --> " +1234"
  84. ///   uartPrintfNum(10, 6, FALSE, '0', 1234); --> "001234"
  85. ///   uartPrintfNum(16, 6, FALSE, '.', 0x5AA5); --> "..5AA5"
  86. /// \endcode
  87. void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n);
  88.  
  89. #ifdef RPRINTF_FLOAT
  90.   //! floating-point print routine
  91.   void rprintfFloat(char numDigits, double x);
  92. #endif
  93.  
  94. // NOTE: Below you'll see the function prototypes of rprintf1RamRom and
  95. // rprintf2RamRom. rprintf1RamRom and rprintf2RamRom are both reduced versions
  96. // of the regular C printf() command. However, they are modified to be able
  97. // to read their text/format strings from RAM or ROM in the Atmel microprocessors.
  98. // Unless you really intend to, do not use the "RamRom" versions of the functions
  99. // directly. Instead use the #defined function versions:
  100. //
  101. // printfx("text/format",args) ...to keep your text/format string stored in RAM
  102. //     - or -
  103. // printfxROM("text/format",args) ...to keep your text/format string stored in ROM
  104. //
  105. // where x is either 1 or 2 for the simple or more powerful version of printf()
  106. //
  107. // Since there is much more ROM than RAM available in the Atmel microprocessors,
  108. // and nearly all text/format strings are constant (never change in the course
  109. // of the program), you should try to use the ROM printf version exclusively.
  110. // This will ensure you leave as much RAM as possible for program variables and
  111. // data.
  112.  
  113. //! \fn int rprintf(const char *format, ...);
  114. /// A reduced substitute for the usual C printf() function.
  115. /// This function actually points to either rprintf1RamRom or rprintf2RamRom
  116. /// depending on the user's selection. Rprintf1 is a simple small fast print
  117. /// routine while rprintf2 is larger and slower but more capable. To choose
  118. /// the routine you would like to use, define either RPRINTF_SIMPLE or
  119. /// RPRINTF_COMPLEX in global.h.
  120.  
  121. #ifdef RPRINTF_SIMPLE
  122.   //! A simple printf routine.
  123.   /// Called by rprintf() - does a simple printf (supports %d, %x, %c).
  124.   /// Supports:
  125.   /// - %d - decimal
  126.   /// - %x - hex
  127.   /// - %c - character
  128.   int rprintf1RamRom(unsigned char stringInRom, const char *format, ...);
  129.   // #defines for RAM or ROM operation
  130.   #define rprintf1(format, args...)     rprintf1RamRom(STRING_IN_ROM, PSTR(format), ## args)
  131.   #define rprintf1RAM(format, args...)   rprintf1RamRom(STRING_IN_RAM, format, ## args)
  132.  
  133.   // *** Default rprintf(...) ***
  134.   // this next line determines what the the basic rprintf() defaults to:
  135.   #define rprintf(format, args...)     rprintf1RamRom(STRING_IN_ROM, PSTR(format), ## args)
  136. #endif
  137.  
  138. char rprint1(const char *format);
  139. #define rprint(format)   rprint1(PSTR(format))
  140.  
  141. #ifdef RPRINTF_COMPLEX
  142.   //! A more powerful printf routine.
  143.   /// Called by rprintf() - does a more powerful printf (supports %d, %u, %o, %x, %c, %s).
  144.   /// Supports:
  145.   /// - %d - decimal
  146.   /// - %u - unsigned decimal
  147.   /// - %o - octal
  148.   /// - %x - hex
  149.   /// - %c - character
  150.   /// - %s - strings
  151.   /// - and the width,precision,padding modifiers
  152.   /// \note This printf does not support floating point numbers.
  153.   int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...);
  154.   // #defines for RAM or ROM operation
  155.   #define rprintf2(format, args...)     rprintf2RamRom(STRING_IN_ROM, format, ## args)
  156.   #define rprintf2RAM(format, args...)   rprintf2RamRom(STRING_IN_RAM, format, ## args)
  157.  
  158.   // *** Default rprintf(...) ***
  159.   // this next line determines what the the basic rprintf() defaults to:
  160.   #define rprintf(format, args...)     rprintf2RamRom(STRING_IN_ROM, PSTR(format), ## args)
  161. #endif
  162.  
  163. #endif
  164. //@}
Laatste wijziging: Vr 10 April 2009, 11:01