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