Kiibohd Controller
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

print.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2011-2013 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <stdarg.h>
  24. // Project Includes
  25. #include "print.h"
  26. // ----- Functions -----
  27. // USB HID String Output
  28. void usb_debug_putstr( char* s )
  29. {
  30. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  31. while ( *s != '\0' )
  32. usb_debug_putchar( *s++ );
  33. #elif defined(_mk20dx128_) // ARM
  34. // Count characters until NULL character, then send the amount counted
  35. uint32_t count = 0;
  36. while ( s[count] != '\0' )
  37. count++;
  38. usb_serial_write( s, count );
  39. #endif
  40. }
  41. // Multiple string Output
  42. void usb_debug_putstrs( char* first, ... )
  43. {
  44. // Initialize the variadic function parameter list
  45. va_list ap;
  46. // Get the first parameter
  47. va_start( ap, first );
  48. char *cur = first;
  49. // Loop through the variadic list until "\0\0\0" is found
  50. while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
  51. {
  52. // Print out the given string
  53. usb_debug_putstr( cur );
  54. // Get the next argument ready
  55. cur = va_arg( ap, char* );
  56. }
  57. va_end( ap ); // Not required, but good practice
  58. }
  59. // Print a constant string
  60. void _print(const char *s)
  61. {
  62. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  63. char c;
  64. // Acquire the character from flash, and print it, as long as it's not NULL
  65. // Also, if a newline is found, print a carrige return as well
  66. while ( ( c = pgm_read_byte(s++) ) != '\0' )
  67. {
  68. if ( c == '\n' )
  69. usb_debug_putchar('\r');
  70. usb_debug_putchar(c);
  71. }
  72. #elif defined(_mk20dx128_) // ARM
  73. usb_debug_putstr( (char*)s );
  74. #endif
  75. }
  76. // Number Printing Functions
  77. void printInt8( uint8_t in )
  78. {
  79. // Max number of characters is 3 + 1 for null
  80. char tmpStr[4];
  81. // Convert number
  82. int8ToStr( in, tmpStr );
  83. // Print number
  84. dPrintStr( tmpStr );
  85. }
  86. void printInt16( uint16_t in )
  87. {
  88. // Max number of characters is 5 + 1 for null
  89. char tmpStr[6];
  90. // Convert number
  91. int16ToStr( in, tmpStr );
  92. // Print number
  93. dPrintStr( tmpStr );
  94. }
  95. void printHex_op( uint16_t in, uint8_t op )
  96. {
  97. // With an op of 1, the max number of characters is 6 + 1 for null
  98. // e.g. "0xFFFF\0"
  99. // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
  100. char tmpStr[7];
  101. // Convert number
  102. hexToStr_op( in, tmpStr, op );
  103. // Print number
  104. dPrintStr( tmpStr );
  105. }
  106. // String Functions
  107. void int8ToStr( uint8_t in, char* out )
  108. {
  109. // Position and sign containers
  110. uint8_t pos;
  111. pos = 0;
  112. // Evaluate through digits as decimal
  113. do
  114. {
  115. out[pos++] = in % 10 + '0';
  116. }
  117. while ( (in /= 10) > 0 );
  118. // Append null
  119. out[pos] = '\0';
  120. // Reverse the string to the correct order
  121. revsStr(out);
  122. }
  123. void int16ToStr( uint16_t in, char* out )
  124. {
  125. // Position and sign containers
  126. uint16_t pos;
  127. pos = 0;
  128. // Evaluate through digits as decimal
  129. do
  130. {
  131. out[pos++] = in % 10 + '0';
  132. }
  133. while ( (in /= 10) > 0 );
  134. // Append null
  135. out[pos] = '\0';
  136. // Reverse the string to the correct order
  137. revsStr(out);
  138. }
  139. void hexToStr_op( uint16_t in, char* out, uint8_t op )
  140. {
  141. // Position container
  142. uint16_t pos = 0;
  143. // Evaluate through digits as hex
  144. do
  145. {
  146. uint16_t cur = in % 16;
  147. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  148. }
  149. while ( (in /= 16) > 0 );
  150. // Output formatting options
  151. switch ( op )
  152. {
  153. case 1: // Add 0x
  154. out[pos++] = 'x';
  155. out[pos++] = '0';
  156. break;
  157. case 2: // 8-bit padding
  158. case 4: // 16-bit padding
  159. while ( pos < op )
  160. out[pos++] = '0';
  161. break;
  162. }
  163. // Append null
  164. out[pos] = '\0';
  165. // Reverse the string to the correct order
  166. revsStr(out);
  167. }
  168. void revsStr( char* in )
  169. {
  170. // Iterators
  171. int i, j;
  172. // Temp storage
  173. char c;
  174. // Loop through the string, and reverse the order of the characters
  175. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  176. {
  177. c = in[i];
  178. in[i] = in[j];
  179. in[j] = c;
  180. }
  181. }
  182. uint16_t lenStr( char* in )
  183. {
  184. // Iterator
  185. char *pos;
  186. // Loop until null is found
  187. for ( pos = in; *pos; pos++ );
  188. // Return the difference between the pointers of in and pos (which is the string length)
  189. return (pos - in);
  190. }