Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xprintf.h 3.3KB

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