Kiibohd Controller
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

print.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2011 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. // Compiler Includes
  22. #include <stdarg.h>
  23. // AVR Includes
  24. #include <avr/io.h>
  25. #include <avr/pgmspace.h>
  26. // Project Includes
  27. #include "print.h"
  28. // Defines
  29. // USB HID String Output
  30. void usb_debug_putstr( char* s )
  31. {
  32. while ( *s != '\0' )
  33. usb_debug_putchar( *s++ );
  34. }
  35. // Multiple string Output
  36. void usb_debug_putstrs( char* first, ... )
  37. {
  38. // Initialize the variadic function parameter list
  39. va_list ap;
  40. // Get the first parameter
  41. va_start( ap, first );
  42. char *cur = first;
  43. // Loop through the variadic list until "\0\0\0" is found
  44. while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
  45. {
  46. // Print out the given string
  47. usb_debug_putstr( cur );
  48. // Get the next argument ready
  49. cur = va_arg( ap, char* );
  50. }
  51. va_end( ap ); // Not required, but good practice
  52. }
  53. // Print a constant string
  54. void _print(const char *s)
  55. {
  56. char c;
  57. // Acquire the character from flash, and print it, as long as it's not NULL
  58. // Also, if a newline is found, print a carrige return as well
  59. while ( ( c = pgm_read_byte(s++) ) != '\0' )
  60. {
  61. if ( c == '\n' )
  62. usb_debug_putchar('\r');
  63. usb_debug_putchar(c);
  64. }
  65. }
  66. // String Functions
  67. void int8ToStr( uint8_t in, char* out )
  68. {
  69. // Position and sign containers
  70. uint8_t pos;
  71. pos = 0;
  72. // Evaluate through digits as decimal
  73. do
  74. {
  75. out[pos++] = in % 10 + '0';
  76. }
  77. while ( (in /= 10) > 0 );
  78. // Append null
  79. out[pos] = '\0';
  80. // Reverse the string to the correct order
  81. revsStr(out);
  82. }
  83. void int16ToStr( uint16_t in, char* out )
  84. {
  85. // Position and sign containers
  86. uint16_t pos;
  87. pos = 0;
  88. // Evaluate through digits as decimal
  89. do
  90. {
  91. out[pos++] = in % 10 + '0';
  92. }
  93. while ( (in /= 10) > 0 );
  94. // Append null
  95. out[pos] = '\0';
  96. // Reverse the string to the correct order
  97. revsStr(out);
  98. }
  99. void hexToStr_op( uint16_t in, char* out, uint8_t op )
  100. {
  101. // Position container
  102. uint16_t pos = 0;
  103. // Evaluate through digits as hex
  104. do
  105. {
  106. uint16_t cur = in % 16;
  107. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  108. }
  109. while ( (in /= 16) > 0 );
  110. // Output formatting options
  111. switch ( op )
  112. {
  113. case 1: // Add 0x
  114. out[pos++] = 'x';
  115. out[pos++] = '0';
  116. break;
  117. case 2: // 8-bit padding
  118. case 4: // 16-bit padding
  119. while ( pos < op )
  120. out[pos++] = '0';
  121. break;
  122. }
  123. // Append null
  124. out[pos] = '\0';
  125. // Reverse the string to the correct order
  126. revsStr(out);
  127. }
  128. void revsStr( char* in )
  129. {
  130. // Iterators
  131. int i, j;
  132. // Temp storage
  133. char c;
  134. // Loop through the string, and reverse the order of the characters
  135. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  136. {
  137. c = in[i];
  138. in[i] = in[j];
  139. in[j] = c;
  140. }
  141. }
  142. uint16_t lenStr( char* in )
  143. {
  144. // Iterator
  145. char *pos;
  146. // Loop until null is found
  147. for ( pos = in; *pos; pos++ );
  148. // Return the difference between the pointers of in and pos (which is the string length)
  149. return (pos - in);
  150. }