Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

print.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <stdio.h>
  25. #include <avr/io.h>
  26. #include <avr/pgmspace.h>
  27. #include "print.h"
  28. #define sendchar(c) do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0)
  29. int8_t (*print_sendchar_func)(uint8_t) = NULL;
  30. bool print_enable = false;
  31. /* print string stored in data memory(SRAM)
  32. * print_P("hello world");
  33. * This consumes precious SRAM memory space for string.
  34. */
  35. void print_S(const char *s)
  36. {
  37. uint8_t c;
  38. while (1) {
  39. c = *s++;
  40. if (!c) break;
  41. if (c == '\n') sendchar('\r');
  42. sendchar(c);
  43. }
  44. }
  45. /* print string stored in program memory(FLASH)
  46. * print_P(PSTR("hello world");
  47. * This consumes relatively abundant FLASH memory area not SRAM.
  48. */
  49. void print_P(const char *s)
  50. {
  51. uint8_t c;
  52. while (1) {
  53. c = pgm_read_byte(s++);
  54. if (!c) break;
  55. if (c == '\n') sendchar('\r');
  56. sendchar(c);
  57. }
  58. }
  59. static inline
  60. void print_hex4(uint8_t data)
  61. {
  62. sendchar(data + ((data < 10) ? '0' : 'A' - 10));
  63. }
  64. void print_hex8(uint8_t data)
  65. {
  66. print_hex4(data>>4);
  67. print_hex4(data&0x0F);
  68. }
  69. void print_hex16(uint16_t data)
  70. {
  71. print_hex8(data>>8);
  72. print_hex8(data);
  73. }
  74. void print_hex32(uint32_t data)
  75. {
  76. print_hex16(data>>16);
  77. print_hex16(data);
  78. }
  79. void print_dec8(uint8_t data)
  80. {
  81. if (data/100) sendchar('0' + (data/100));
  82. if (data/100 || data%100/10) sendchar('0' + (data%100/10));
  83. sendchar('0' + (data%10));
  84. }
  85. void print_dec16(uint16_t data)
  86. {
  87. // TODO
  88. }
  89. void print_dec32(uint32_t data)
  90. {
  91. // TODO
  92. }
  93. void print_bin(uint8_t data)
  94. {
  95. for (int i = 7; i >= 0; i--) {
  96. sendchar((data & (1<<i)) ? '1' : '0');
  97. }
  98. }
  99. void print_bin16(uint16_t data)
  100. {
  101. print_bin8(data>>8);
  102. print_bin8(data);
  103. }
  104. void print_bin32(uint32_t data)
  105. {
  106. print_bin8(data>>24);
  107. print_bin8(data>>16);
  108. print_bin8(data>>8);
  109. print_bin8(data);
  110. }
  111. void print_bin_reverse8(uint8_t data)
  112. {
  113. for (int i = 0; i < 8; i++) {
  114. sendchar((data & (1<<i)) ? '1' : '0');
  115. }
  116. }
  117. void print_bin_reverse16(uint16_t data)
  118. {
  119. print_bin_reverse8(data);
  120. print_bin_reverse8(data>>8);
  121. }
  122. void print_bin_reverse32(uint32_t data)
  123. {
  124. print_bin_reverse8(data);
  125. print_bin_reverse8(data>>8);
  126. print_bin_reverse8(data>>16);
  127. print_bin_reverse8(data>>24);
  128. }