Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

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