xprintf.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*---------------------------------------------------------------------------
  2. Extended itoa, puts and printf (C)ChaN, 2011
  3. -----------------------------------------------------------------------------*/
  4. #pragma once
  5. #include <inttypes.h>
  6. #include <avr/pgmspace.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. extern void (*xfunc_out)(uint8_t);
  11. #define xdev_out(func) xfunc_out = (void (*)(uint8_t))(func)
  12. /* This is a pointer to user defined output function. It must be initialized
  13. before using this modle.
  14. */
  15. void xputc(char chr);
  16. /* This is a stub function to forward outputs to user defined output function.
  17. All outputs from this module are output via this function.
  18. */
  19. /*-----------------------------------------------------------------------------*/
  20. void xputs(const char *string_p);
  21. /* The string placed in the ROM is forwarded to xputc() directly.
  22. */
  23. /*-----------------------------------------------------------------------------*/
  24. void xitoa(long value, char radix, char width);
  25. /* Extended itoa().
  26. value radix width output
  27. 100 10 6 " 100"
  28. 100 10 -6 "000100"
  29. 100 10 0 "100"
  30. 4294967295 10 0 "4294967295"
  31. 4294967295 -10 0 "-1"
  32. 655360 16 -8 "000A0000"
  33. 1024 16 0 "400"
  34. 0x55 2 -8 "01010101"
  35. */
  36. /*-----------------------------------------------------------------------------*/
  37. #define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__)
  38. #define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__)
  39. #define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__)
  40. void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */
  41. // void __xsprintf(char*, const char *format_p, ...); /* Put formatted string to the memory */
  42. // void __xfprintf(void(*func)(uint8_t), const char *format_p, ...); /* Send formatted string to the specified device */
  43. /* Format string is placed in the ROM. The format flags is similar to printf().
  44. %[flag][width][size]type
  45. flag
  46. A '0' means filled with '0' when output is shorter than width.
  47. ' ' is used in default. This is effective only numeral type.
  48. width
  49. Minimum width in decimal number. This is effective only numeral type.
  50. Default width is zero.
  51. size
  52. A 'l' means the argument is long(32bit). Default is short(16bit).
  53. This is effective only numeral type.
  54. type
  55. 'c' : Character, argument is the value
  56. 's' : String placed on the RAM, argument is the pointer
  57. 'S' : String placed on the ROM, argument is the pointer
  58. 'd' : Signed decimal, argument is the value
  59. 'u' : Unsigned decimal, argument is the value
  60. 'X' : Hexdecimal, argument is the value
  61. 'b' : Binary, argument is the value
  62. '%' : '%'
  63. */
  64. /*-----------------------------------------------------------------------------*/
  65. char xatoi(char **str, long *ret);
  66. /* Get value of the numeral string.
  67. str
  68. Pointer to pointer to source string
  69. "0b11001010" binary
  70. "0377" octal
  71. "0xff800" hexdecimal
  72. "1250000" decimal
  73. "-25000" decimal
  74. ret
  75. Pointer to return value
  76. */
  77. #ifdef __cplusplus
  78. }
  79. #endif