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.

print.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright 2012 Jun Wako <[email protected]> */
  2. /* Very basic print functions, intended to be used with usb_debug_only.c
  3. * http://www.pjrc.com/teensy/
  4. * Copyright (c) 2008 PJRC.COM, LLC
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include <avr/io.h>
  25. #include <avr/pgmspace.h>
  26. #include "print.h"
  27. #ifndef NO_PRINT
  28. #define sendchar(c) do { if (print_sendchar_func) (print_sendchar_func)(c); } while (0)
  29. static int8_t (*print_sendchar_func)(uint8_t) = 0;
  30. void print_set_sendchar(int8_t (*sendchar_func)(uint8_t))
  31. {
  32. print_sendchar_func = sendchar_func;
  33. }
  34. /* print string stored in data memory(SRAM)
  35. * print_P("hello world");
  36. * This consumes precious SRAM memory space for string.
  37. */
  38. void print_S(const char *s)
  39. {
  40. uint8_t c;
  41. while (1) {
  42. c = *s++;
  43. if (!c) break;
  44. if (c == '\n') sendchar('\r');
  45. sendchar(c);
  46. }
  47. }
  48. /* print string stored in program memory(FLASH)
  49. * print_P(PSTR("hello world");
  50. * This consumes relatively abundant FLASH memory area not SRAM.
  51. */
  52. void print_P(const char *s)
  53. {
  54. uint8_t c;
  55. while (1) {
  56. c = pgm_read_byte(s++);
  57. if (!c) break;
  58. if (c == '\n') sendchar('\r');
  59. sendchar(c);
  60. }
  61. }
  62. void print_CRLF(void)
  63. {
  64. sendchar('\r'); sendchar('\n');
  65. }
  66. #define SIGNED 0x80
  67. #define BIN 2
  68. #define OCT 8
  69. #define DEC 10
  70. #define HEX 16
  71. static inline
  72. char itoc(uint8_t i)
  73. {
  74. return (i < 10 ? '0' + i : 'A' + i - 10);
  75. }
  76. static inline
  77. void print_int(uint16_t data, uint8_t base)
  78. {
  79. char buf[7] = {'\0'};
  80. char *p = &buf[6];
  81. if ((base & SIGNED) && (data & 0x8000)) {
  82. data = -data;
  83. buf[0] = '-';
  84. }
  85. base &= ~SIGNED;
  86. uint16_t n;
  87. do {
  88. n = data;
  89. data /= base;
  90. *(--p) = itoc(n - data*base);
  91. } while (data);
  92. if (buf[0]) *(--p) = buf[0];
  93. print_S(p);
  94. }
  95. void print_dec(uint16_t data)
  96. {
  97. print_int(data, DEC);
  98. }
  99. void print_decs(int16_t data)
  100. {
  101. print_int(data, DEC|SIGNED);
  102. }
  103. void print_hex4(uint8_t data)
  104. {
  105. sendchar(data + ((data < 10) ? '0' : 'A' - 10));
  106. }
  107. void print_hex8(uint8_t data)
  108. {
  109. print_hex4(data>>4);
  110. print_hex4(data&0x0F);
  111. }
  112. void print_hex16(uint16_t data)
  113. {
  114. print_hex8(data>>8);
  115. print_hex8(data);
  116. }
  117. void print_hex32(uint32_t data)
  118. {
  119. print_hex16(data>>16);
  120. print_hex16(data);
  121. }
  122. void print_bin4(uint8_t data)
  123. {
  124. for (int i = 4; i >= 0; i--) {
  125. sendchar((data & (1<<i)) ? '1' : '0');
  126. }
  127. }
  128. void print_bin8(uint8_t data)
  129. {
  130. for (int i = 7; i >= 0; i--) {
  131. sendchar((data & (1<<i)) ? '1' : '0');
  132. }
  133. }
  134. void print_bin16(uint16_t data)
  135. {
  136. print_bin8(data>>8);
  137. print_bin8(data);
  138. }
  139. void print_bin32(uint32_t data)
  140. {
  141. print_bin8(data>>24);
  142. print_bin8(data>>16);
  143. print_bin8(data>>8);
  144. print_bin8(data);
  145. }
  146. void print_bin_reverse8(uint8_t data)
  147. {
  148. for (int i = 0; i < 8; i++) {
  149. sendchar((data & (1<<i)) ? '1' : '0');
  150. }
  151. }
  152. void print_bin_reverse16(uint16_t data)
  153. {
  154. print_bin_reverse8(data);
  155. print_bin_reverse8(data>>8);
  156. }
  157. void print_bin_reverse32(uint32_t data)
  158. {
  159. print_bin_reverse8(data);
  160. print_bin_reverse8(data>>8);
  161. print_bin_reverse8(data>>16);
  162. print_bin_reverse8(data>>24);
  163. }
  164. #endif