Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

print.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // String Functions
  77. void int8ToStr( uint8_t in, char* out )
  78. {
  79. // Position and sign containers
  80. uint8_t pos;
  81. pos = 0;
  82. // Evaluate through digits as decimal
  83. do
  84. {
  85. out[pos++] = in % 10 + '0';
  86. }
  87. while ( (in /= 10) > 0 );
  88. // Append null
  89. out[pos] = '\0';
  90. // Reverse the string to the correct order
  91. revsStr(out);
  92. }
  93. void int16ToStr( uint16_t in, char* out )
  94. {
  95. // Position and sign containers
  96. uint16_t pos;
  97. pos = 0;
  98. // Evaluate through digits as decimal
  99. do
  100. {
  101. out[pos++] = in % 10 + '0';
  102. }
  103. while ( (in /= 10) > 0 );
  104. // Append null
  105. out[pos] = '\0';
  106. // Reverse the string to the correct order
  107. revsStr(out);
  108. }
  109. void hexToStr_op( uint16_t in, char* out, uint8_t op )
  110. {
  111. // Position container
  112. uint16_t pos = 0;
  113. // Evaluate through digits as hex
  114. do
  115. {
  116. uint16_t cur = in % 16;
  117. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  118. }
  119. while ( (in /= 16) > 0 );
  120. // Output formatting options
  121. switch ( op )
  122. {
  123. case 1: // Add 0x
  124. out[pos++] = 'x';
  125. out[pos++] = '0';
  126. break;
  127. case 2: // 8-bit padding
  128. case 4: // 16-bit padding
  129. while ( pos < op )
  130. out[pos++] = '0';
  131. break;
  132. }
  133. // Append null
  134. out[pos] = '\0';
  135. // Reverse the string to the correct order
  136. revsStr(out);
  137. }
  138. void revsStr( char* in )
  139. {
  140. // Iterators
  141. int i, j;
  142. // Temp storage
  143. char c;
  144. // Loop through the string, and reverse the order of the characters
  145. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  146. {
  147. c = in[i];
  148. in[i] = in[j];
  149. in[j] = c;
  150. }
  151. }
  152. uint16_t lenStr( char* in )
  153. {
  154. // Iterator
  155. char *pos;
  156. // Loop until null is found
  157. for ( pos = in; *pos; pos++ );
  158. // Return the difference between the pointers of in and pos (which is the string length)
  159. return (pos - in);
  160. }