]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/print/print.c
Formalizing code module structure and inheritance (Large Commit)
[kiibohd-controller.git] / Debug / print / print.c
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
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <stdarg.h>
26
27 // Project Includes
28 #include "print.h"
29
30
31
32 // ----- Functions -----
33
34 // USB HID String Output
35 void usb_debug_putstr( char* s )
36 {
37         while ( *s != '\0' )
38                 usb_debug_putchar( *s++ );
39 }
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
47         // Get the first parameter
48         va_start( ap, first );
49         char *cur = first;
50
51         // Loop through the variadic list until "\0\0\0" is found
52         while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
53         {
54                 // Print out the given string
55                 usb_debug_putstr( cur );
56
57                 // Get the next argument ready
58                 cur = va_arg( ap, char* );
59         }
60
61         va_end( ap ); // Not required, but good practice
62 }
63
64 // Print a constant string
65 void _print(const char *s)
66 {
67         char c;
68
69         // Acquire the character from flash, and print it, as long as it's not NULL
70         // Also, if a newline is found, print a carrige return as well
71         while ( ( c = pgm_read_byte(s++) ) != '\0' )
72         {
73                 if ( c == '\n' )
74                         usb_debug_putchar('\r');
75                 usb_debug_putchar(c);
76         }
77 }
78
79
80
81
82 // String Functions
83 void int8ToStr( uint8_t in, char* out )
84 {
85         // Position and sign containers
86         uint8_t pos;
87         pos = 0;
88
89         // Evaluate through digits as decimal
90         do
91         {
92                 out[pos++] = in % 10 + '0';
93         }
94         while ( (in /= 10) > 0 );
95
96         // Append null
97         out[pos] = '\0';
98
99         // Reverse the string to the correct order
100         revsStr(out);
101 }
102
103
104 void int16ToStr( uint16_t in, char* out )
105 {
106         // Position and sign containers
107         uint16_t pos;
108         pos = 0;
109
110         // Evaluate through digits as decimal
111         do
112         {
113                 out[pos++] = in % 10 + '0';
114         }
115         while ( (in /= 10) > 0 );
116
117         // Append null
118         out[pos] = '\0';
119
120         // Reverse the string to the correct order
121         revsStr(out);
122 }
123
124
125 void hexToStr_op( uint16_t in, char* out, uint8_t op )
126 {
127         // Position container
128         uint16_t pos = 0;
129
130         // Evaluate through digits as hex
131         do
132         {
133                 uint16_t cur = in % 16;
134                 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
135         }
136         while ( (in /= 16) > 0 );
137
138         // Output formatting options
139         switch ( op )
140         {
141         case 1: // Add 0x
142                 out[pos++] = 'x';
143                 out[pos++] = '0';
144                 break;
145         case 2: //  8-bit padding
146         case 4: // 16-bit padding
147                 while ( pos < op )
148                         out[pos++] = '0';
149                 break;
150         }
151
152         // Append null
153         out[pos] = '\0';
154
155         // Reverse the string to the correct order
156         revsStr(out);
157 }
158
159
160 void revsStr( char* in )
161 {
162         // Iterators
163         int i, j;
164
165         // Temp storage
166         char c;
167
168         // Loop through the string, and reverse the order of the characters
169         for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
170         {
171                 c = in[i];
172                 in[i] = in[j];
173                 in[j] = c;
174         }
175 }
176
177
178 uint16_t lenStr( char* in )
179 {
180         // Iterator
181         char *pos;
182
183         // Loop until null is found
184         for ( pos = in; *pos; pos++ );
185
186         // Return the difference between the pointers of in and pos (which is the string length)
187         return (pos - in);
188 }
189