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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // Multiple string Output
  28. void printstrs( char* first, ... )
  29. {
  30. // Initialize the variadic function parameter list
  31. va_list ap;
  32. // Get the first parameter
  33. va_start( ap, first );
  34. char *cur = first;
  35. // Loop through the variadic list until "\0\0\0" is found
  36. while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
  37. {
  38. // Print out the given string
  39. Output_putstr( cur );
  40. // Get the next argument ready
  41. cur = va_arg( ap, char* );
  42. }
  43. va_end( ap ); // Not required, but good practice
  44. }
  45. // Print a constant string
  46. void _print( const char* s )
  47. {
  48. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  49. // Pull string out of flash
  50. char c;
  51. while ( ( c = pgm_read_byte( s++ ) ) != '\0' )
  52. {
  53. Output_putchar( c );
  54. }
  55. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  56. Output_putstr( (char*)s );
  57. #endif
  58. }
  59. // Number Printing Functions
  60. void printInt8( uint8_t in )
  61. {
  62. // Max number of characters is 3 + 1 for null
  63. char tmpStr[4];
  64. // Convert number
  65. int8ToStr( in, tmpStr );
  66. // Print number
  67. dPrintStr( tmpStr );
  68. }
  69. void printInt16( uint16_t in )
  70. {
  71. // Max number of characters is 5 + 1 for null
  72. char tmpStr[6];
  73. // Convert number
  74. int16ToStr( in, tmpStr );
  75. // Print number
  76. dPrintStr( tmpStr );
  77. }
  78. void printInt32( uint32_t in )
  79. {
  80. // Max number of characters is 10 + 1 for null
  81. char tmpStr[11];
  82. // Convert number
  83. int32ToStr( in, tmpStr );
  84. // Print number
  85. dPrintStr( tmpStr );
  86. }
  87. void printHex_op( uint16_t in, uint8_t op )
  88. {
  89. // With an op of 1, the max number of characters is 6 + 1 for null
  90. // e.g. "0xFFFF\0"
  91. // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
  92. char tmpStr[7];
  93. // Convert number
  94. hexToStr_op( in, tmpStr, op );
  95. // Print number
  96. dPrintStr( tmpStr );
  97. }
  98. // String Functions
  99. void int8ToStr( uint8_t in, char* out )
  100. {
  101. // Position and sign containers
  102. uint8_t pos;
  103. pos = 0;
  104. // Evaluate through digits as decimal
  105. do
  106. {
  107. out[pos++] = in % 10 + '0';
  108. }
  109. while ( (in /= 10) > 0 );
  110. // Append null
  111. out[pos] = '\0';
  112. // Reverse the string to the correct order
  113. revsStr(out);
  114. }
  115. void int16ToStr( uint16_t in, char* out )
  116. {
  117. // Position and sign containers
  118. uint16_t pos;
  119. pos = 0;
  120. // Evaluate through digits as decimal
  121. do
  122. {
  123. out[pos++] = in % 10 + '0';
  124. }
  125. while ( (in /= 10) > 0 );
  126. // Append null
  127. out[pos] = '\0';
  128. // Reverse the string to the correct order
  129. revsStr(out);
  130. }
  131. void int32ToStr( uint32_t in, char* out )
  132. {
  133. // Position and sign containers
  134. uint32_t pos;
  135. pos = 0;
  136. // Evaluate through digits as decimal
  137. do
  138. {
  139. out[pos++] = in % 10 + '0';
  140. }
  141. while ( (in /= 10) > 0 );
  142. // Append null
  143. out[pos] = '\0';
  144. // Reverse the string to the correct order
  145. revsStr(out);
  146. }
  147. void hexToStr_op( uint16_t in, char* out, uint8_t op )
  148. {
  149. // Position container
  150. uint16_t pos = 0;
  151. // Evaluate through digits as hex
  152. do
  153. {
  154. uint16_t cur = in % 16;
  155. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  156. }
  157. while ( (in /= 16) > 0 );
  158. // Output formatting options
  159. switch ( op )
  160. {
  161. case 1: // Add 0x
  162. out[pos++] = 'x';
  163. out[pos++] = '0';
  164. break;
  165. case 2: // 8-bit padding
  166. case 4: // 16-bit padding
  167. while ( pos < op )
  168. out[pos++] = '0';
  169. break;
  170. }
  171. // Append null
  172. out[pos] = '\0';
  173. // Reverse the string to the correct order
  174. revsStr(out);
  175. }
  176. void revsStr( char* in )
  177. {
  178. // Iterators
  179. int i, j;
  180. // Temp storage
  181. char c;
  182. // Loop through the string, and reverse the order of the characters
  183. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  184. {
  185. c = in[i];
  186. in[i] = in[j];
  187. in[j] = c;
  188. }
  189. }
  190. uint16_t lenStr( char* in )
  191. {
  192. // Iterator
  193. char *pos;
  194. // Loop until null is found
  195. for ( pos = in; *pos; pos++ );
  196. // Return the difference between the pointers of in and pos (which is the string length)
  197. return (pos - in);
  198. }
  199. int16_t eqStr( char* str1, char* str2 )
  200. {
  201. // Scan each string for NULLs and whether they are the same
  202. while( *str1 != '\0' && *str1++ == *str2++ );
  203. // If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
  204. // If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
  205. return *--str1 == *--str2 ? -1 : *++str1;
  206. }
  207. int decToInt( char* in )
  208. {
  209. // Pointers to the LSD (Least Significant Digit) and MSD
  210. char* lsd = in;
  211. char* msd = in;
  212. int total = 0;
  213. int sign = 1; // Default to positive
  214. // Scan the string once to determine the length
  215. while ( *lsd != '\0' )
  216. {
  217. // Check for positive/negative
  218. switch ( *lsd++ )
  219. {
  220. // Fall through is intentional, only do something on negative, ignore the rest
  221. // Update the MSD to remove leading spaces and signs
  222. case '-': sign = -1;
  223. case '+':
  224. case ' ':
  225. msd = lsd;
  226. break;
  227. }
  228. }
  229. // Rescan the string from the LSD to MSD to convert it to a decimal number
  230. for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
  231. total += ( (*--lsd) - '0' ) * digit;
  232. // Propagate sign and return
  233. return total * sign;
  234. }