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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (C) 2011-2014 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_) || defined(_mk20dx256_) // 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_) || defined(_mk20dx256_) // 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 printInt32( uint32_t in )
  96. {
  97. // Max number of characters is 10 + 1 for null
  98. char tmpStr[11];
  99. // Convert number
  100. int32ToStr( in, tmpStr );
  101. // Print number
  102. dPrintStr( tmpStr );
  103. }
  104. void printHex_op( uint16_t in, uint8_t op )
  105. {
  106. // With an op of 1, the max number of characters is 6 + 1 for null
  107. // e.g. "0xFFFF\0"
  108. // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
  109. char tmpStr[7];
  110. // Convert number
  111. hexToStr_op( in, tmpStr, op );
  112. // Print number
  113. dPrintStr( tmpStr );
  114. }
  115. // String Functions
  116. void int8ToStr( uint8_t in, char* out )
  117. {
  118. // Position and sign containers
  119. uint8_t pos;
  120. pos = 0;
  121. // Evaluate through digits as decimal
  122. do
  123. {
  124. out[pos++] = in % 10 + '0';
  125. }
  126. while ( (in /= 10) > 0 );
  127. // Append null
  128. out[pos] = '\0';
  129. // Reverse the string to the correct order
  130. revsStr(out);
  131. }
  132. void int16ToStr( uint16_t in, char* out )
  133. {
  134. // Position and sign containers
  135. uint16_t pos;
  136. pos = 0;
  137. // Evaluate through digits as decimal
  138. do
  139. {
  140. out[pos++] = in % 10 + '0';
  141. }
  142. while ( (in /= 10) > 0 );
  143. // Append null
  144. out[pos] = '\0';
  145. // Reverse the string to the correct order
  146. revsStr(out);
  147. }
  148. void int32ToStr( uint32_t in, char* out )
  149. {
  150. // Position and sign containers
  151. uint32_t pos;
  152. pos = 0;
  153. // Evaluate through digits as decimal
  154. do
  155. {
  156. out[pos++] = in % 10 + '0';
  157. }
  158. while ( (in /= 10) > 0 );
  159. // Append null
  160. out[pos] = '\0';
  161. // Reverse the string to the correct order
  162. revsStr(out);
  163. }
  164. void hexToStr_op( uint16_t in, char* out, uint8_t op )
  165. {
  166. // Position container
  167. uint16_t pos = 0;
  168. // Evaluate through digits as hex
  169. do
  170. {
  171. uint16_t cur = in % 16;
  172. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  173. }
  174. while ( (in /= 16) > 0 );
  175. // Output formatting options
  176. switch ( op )
  177. {
  178. case 1: // Add 0x
  179. out[pos++] = 'x';
  180. out[pos++] = '0';
  181. break;
  182. case 2: // 8-bit padding
  183. case 4: // 16-bit padding
  184. while ( pos < op )
  185. out[pos++] = '0';
  186. break;
  187. }
  188. // Append null
  189. out[pos] = '\0';
  190. // Reverse the string to the correct order
  191. revsStr(out);
  192. }
  193. void revsStr( char* in )
  194. {
  195. // Iterators
  196. int i, j;
  197. // Temp storage
  198. char c;
  199. // Loop through the string, and reverse the order of the characters
  200. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  201. {
  202. c = in[i];
  203. in[i] = in[j];
  204. in[j] = c;
  205. }
  206. }
  207. uint16_t lenStr( char* in )
  208. {
  209. // Iterator
  210. char *pos;
  211. // Loop until null is found
  212. for ( pos = in; *pos; pos++ );
  213. // Return the difference between the pointers of in and pos (which is the string length)
  214. return (pos - in);
  215. }
  216. int16_t eqStr( char* str1, char* str2 )
  217. {
  218. // Scan each string for NULLs and whether they are the same
  219. while( *str1 != '\0' && *str1++ == *str2++ );
  220. // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
  221. // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
  222. return *--str1 == *--str2 ? -1 : *++str1;
  223. }
  224. int decToInt( char* in )
  225. {
  226. // Pointers to the LSD (Least Significant Digit) and MSD
  227. char* lsd = in;
  228. char* msd = in;
  229. int total = 0;
  230. int sign = 1; // Default to positive
  231. // Scan the string once to determine the length
  232. while ( *lsd != '\0' )
  233. {
  234. // Check for positive/negative
  235. switch ( *lsd++ )
  236. {
  237. // Fall through is intentional, only do something on negative, ignore the rest
  238. // Update the MSD to remove leading spaces and signs
  239. case '-': sign = -1;
  240. case '+':
  241. case ' ':
  242. msd = lsd;
  243. break;
  244. }
  245. }
  246. // Rescan the string from the LSD to MSD to convert it to a decimal number
  247. for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
  248. total += ( (*--lsd) - '0' ) * digit;
  249. // Propagate sign and return
  250. return total * sign;
  251. }