Keyboard firmwares for Atmel AVR and Cortex-M
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

print.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #define sendchar(c) do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0)
  28. int8_t (*print_sendchar_func)(uint8_t) = 0;
  29. bool print_enable = true;
  30. /* print string stored in data memory(SRAM)
  31. * print_P("hello world");
  32. * This consumes precious SRAM memory space for string.
  33. */
  34. void print_S(const char *s)
  35. {
  36. uint8_t c;
  37. while (1) {
  38. c = *s++;
  39. if (!c) break;
  40. if (c == '\n') sendchar('\r');
  41. sendchar(c);
  42. }
  43. }
  44. /* print string stored in program memory(FLASH)
  45. * print_P(PSTR("hello world");
  46. * This consumes relatively abundant FLASH memory area not SRAM.
  47. */
  48. void print_P(const char *s)
  49. {
  50. uint8_t c;
  51. while (1) {
  52. c = pgm_read_byte(s++);
  53. if (!c) break;
  54. if (c == '\n') sendchar('\r');
  55. sendchar(c);
  56. }
  57. }
  58. void print_CRLF(void)
  59. {
  60. sendchar('\r'); sendchar('\n');
  61. }
  62. #define SIGNED 0x80
  63. #define BIN 2
  64. #define OCT 8
  65. #define DEC 10
  66. #define HEX 16
  67. static inline
  68. char itoc(uint8_t i)
  69. {
  70. return (i < 10 ? '0' + i : 'A' + i - 10);
  71. }
  72. static inline
  73. void print_int(uint16_t data, uint8_t base)
  74. {
  75. char buf[7] = {'\0'};
  76. char *p = &buf[6];
  77. if ((base & SIGNED) && (data & 0x8000)) {
  78. data = -data;
  79. buf[0] = '-';
  80. }
  81. base &= ~SIGNED;
  82. uint16_t n;
  83. do {
  84. n = data;
  85. data /= base;
  86. *(--p) = itoc(n - data*base);
  87. } while (data);
  88. if (buf[0]) *(--p) = buf[0];
  89. print_S(p);
  90. }
  91. void print_dec(uint16_t data)
  92. {
  93. print_int(data, DEC);
  94. }
  95. void print_decs(int16_t data)
  96. {
  97. print_int(data, DEC|SIGNED);
  98. }
  99. static inline
  100. void print_hex4(uint8_t data)
  101. {
  102. sendchar(data + ((data < 10) ? '0' : 'A' - 10));
  103. }
  104. void print_hex8(uint8_t data)
  105. {
  106. print_hex4(data>>4);
  107. print_hex4(data&0x0F);
  108. }
  109. void print_hex16(uint16_t data)
  110. {
  111. print_hex8(data>>8);
  112. print_hex8(data);
  113. }
  114. void print_hex32(uint32_t data)
  115. {
  116. print_hex16(data>>16);
  117. print_hex16(data);
  118. }
  119. void print_bin8(uint8_t data)
  120. {
  121. for (int i = 7; i >= 0; i--) {
  122. sendchar((data & (1<<i)) ? '1' : '0');
  123. }
  124. }
  125. void print_bin16(uint16_t data)
  126. {
  127. print_bin8(data>>8);
  128. print_bin8(data);
  129. }
  130. void print_bin32(uint32_t data)
  131. {
  132. print_bin8(data>>24);
  133. print_bin8(data>>16);
  134. print_bin8(data>>8);
  135. print_bin8(data);
  136. }
  137. void print_bin_reverse8(uint8_t data)
  138. {
  139. for (int i = 0; i < 8; i++) {
  140. sendchar((data & (1<<i)) ? '1' : '0');
  141. }
  142. }
  143. void print_bin_reverse16(uint16_t data)
  144. {
  145. print_bin_reverse8(data);
  146. print_bin_reverse8(data>>8);
  147. }
  148. void print_bin_reverse32(uint32_t data)
  149. {
  150. print_bin_reverse8(data);
  151. print_bin_reverse8(data>>8);
  152. print_bin_reverse8(data>>16);
  153. print_bin_reverse8(data>>24);
  154. }