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

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