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