-
Notifications
You must be signed in to change notification settings - Fork 271
/
print.c
559 lines (447 loc) · 10.7 KB
/
print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* Copyright (C) 2011-2020 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <stdarg.h>
// Project Includes
#include "print.h"
// ----- Functions -----
// Multiple string Output
void printstrs( char* first, ... )
{
// Initialize the variadic function parameter list
va_list ap;
// Get the first parameter
va_start( ap, first );
char *cur = first;
// Loop through the variadic list until "\0\0\0" is found
while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
{
// Print out the given string
Output_putstr( cur );
// Get the next argument ready
cur = va_arg( ap, char* );
}
va_end( ap ); // Not required, but good practice
}
// Print a constant string
void _print( const char* s )
{
#if defined(_avr_at_) // AVR
// Pull string out of flash
char c;
while ( ( c = pgm_read_byte( s++ ) ) != '\0' )
{
Output_putchar( c );
}
#elif defined(_kinetis_) || defined(_sam_) // ARM
Output_putstr( (char*)s );
#elif defined(_host_) // Host
Output_putstr( (char*)s );
#endif
}
// Print a char
void printChar( char c )
{
Output_putchar( c );
}
// Pad routine
static void pad( char *str, uint8_t len )
{
// Find final null, and move it
uint8_t pad_rest = 0;
for ( uint8_t c = 0; c < len - 1; c++ )
{
// Already found null, just pad now
if ( pad_rest )
{
str[c] = ' ';
continue;
}
// Looking for first null only
if ( str[c] == '\0' )
{
pad_rest = 1;
str[c] = ' ';
}
}
// Last character is always a null
str[len - 1] = '\0';
}
// Number Printing Functions
void printInt8( uint8_t in )
{
// Max number of characters is 3 + 1 for null
char tmpStr[4];
// Convert number
int8ToStr( in, tmpStr );
// Print number
dPrintStr( tmpStr );
}
void printInt8Pad( uint8_t in )
{
// Max number of characters is 3 + 1 for null
char tmpStr[4];
// Convert number
int8ToStr( in, tmpStr );
// Find final null, and move it
pad( tmpStr, sizeof(tmpStr) );
// Print number
dPrintStr( tmpStr );
}
void printInt16( uint16_t in )
{
// Max number of characters is 5 + 1 for null
char tmpStr[6];
// Convert number
int16ToStr( in, tmpStr );
// Print number
dPrintStr( tmpStr );
}
void printInt16Pad( uint16_t in )
{
// Max number of characters is 5 + 1 for null
char tmpStr[6];
// Convert number
int16ToStr( in, tmpStr );
// Find final null, and move it
pad( tmpStr, sizeof(tmpStr) );
// Print number
dPrintStr( tmpStr );
}
void printInt32( uint32_t in )
{
// Max number of characters is 10 + 1 for null
char tmpStr[11];
// Convert number
int32ToStr( in, tmpStr );
// Print number
dPrintStr( tmpStr );
}
void printSInt32( int32_t in )
{
// Max number of characters is 11 + 1 for null
char tmpStr[12];
// Convert number
sint32ToStr( in, tmpStr );
// Print number
dPrintStr( tmpStr );
}
void printInt32Pad( uint32_t in )
{
// Max number of characters is 10 + 1 for null
char tmpStr[11];
// Convert number
int32ToStr( in, tmpStr );
// Find final null, and move it
pad( tmpStr, sizeof(tmpStr) );
// Print number
dPrintStr( tmpStr );
}
void printDecimal32( uint32_t in, uint32_t mul )
{
// Max number of characters is 10 + 1 + 1 for null and decimal
char tmpStr[12];
// Determine number of decimal places
int32_t places = mul / 10;
// Convert base to a string
int32ToStr( in, tmpStr );
// Ignore if no decimal places
if ( places > 0 && tmpStr[0] != '\0' )
{
// Find final null
uint8_t null_pos = 0;
while ( tmpStr[++null_pos] != '\0' );
// Move least significant digit over 1 while places is greater than 0
while ( places-- + 1 > 0 )
{
tmpStr[null_pos + 1] = tmpStr[null_pos];
null_pos--;
}
// Place decimal
tmpStr[null_pos + 1] = '.';
}
// Print number
dPrintStr( tmpStr );
}
void printHex_op( uint16_t in, uint8_t op )
{
// With an op of 1, the max number of characters is 6 + 1 for null
// e.g. "0xFFFF\0"
// op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
char tmpStr[7];
// Convert number
hexToStr_op( in, tmpStr, op );
// Print number
dPrintStr( tmpStr );
}
void printHex32_op( uint32_t in, uint8_t op )
{
// With an op of 1, the max number of characters is 6 + 1 for null
// e.g. "0xFFFF\0"
// op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
char tmpStr[11];
// Convert number
hex32ToStr_op( in, tmpStr, op );
// Print number
dPrintStr( tmpStr );
}
void printTime( Time time )
{
printInt32( time.ms );
print(":");
printInt32( time.ticks );
}
// String Functions
void int8ToStr( uint8_t in, char* out )
{
// Position and sign containers
uint8_t pos;
pos = 0;
// Evaluate through digits as decimal
do
{
out[pos++] = in % 10 + '0';
}
while ( (in /= 10) > 0 );
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
void int16ToStr( uint16_t in, char* out )
{
// Position and sign containers
uint16_t pos;
pos = 0;
// Evaluate through digits as decimal
do
{
out[pos++] = in % 10 + '0';
}
while ( (in /= 10) > 0 );
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
void int32ToStr( uint32_t in, char* out )
{
// Position and sign containers
uint32_t pos;
pos = 0;
// Evaluate through digits as decimal
do
{
out[pos++] = in % 10 + '0';
}
while ( (in /= 10) > 0 );
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
void sint32ToStr( int32_t in, char* out )
{
// Position and sign containers
uint32_t pos;
pos = 0;
// Check if negative
uint8_t neg = 0;
if ( in < 0 )
{
neg = 1;
in = -in; // Convert number to positive
}
// Evaluate through digits as decimal
do
{
out[pos++] = in % 10 + '0';
}
while ( (in /= 10) > 0 );
// Append - if negative
if ( neg )
{
out[pos++] = '-';
}
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
void hexToStr_op( uint16_t in, char* out, uint8_t op )
{
// Position container
uint16_t pos = 0;
// Evaluate through digits as hex
do
{
uint16_t cur = in % 16;
out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
}
while ( (in /= 16) > 0 );
// Output formatting options
switch ( op )
{
case 1: // Add 0x
out[pos++] = 'x';
out[pos++] = '0';
break;
case 2: // 8-bit padding
case 4: // 16-bit padding
while ( pos < op )
out[pos++] = '0';
break;
}
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
void hex32ToStr_op( uint32_t in, char* out, uint8_t op )
{
// Position container
uint32_t pos = 0;
// Evaluate through digits as hex
do
{
uint32_t cur = in % 16;
out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
}
while ( (in /= 16) > 0 );
// Output formatting options
switch ( op )
{
case 1: // Add 0x
out[pos++] = 'x';
out[pos++] = '0';
break;
case 2: // 8-bit padding
case 4: // 16-bit padding
case 8: // 32-bit padding
while ( pos < op )
out[pos++] = '0';
break;
}
// Append null
out[pos] = '\0';
// Reverse the string to the correct order
revsStr(out);
}
// Converts a number to UTF-16LE
// Useful for fields in the USB Descriptor
void hex32ToStr16( uint32_t in, uint16_t* out, uint8_t op )
{
// Convert number to ASCII
char tmpStr[11];
hex32ToStr_op( in, tmpStr, op );
// Convert number to UTF-16LE
// Just add an extra NULL after every character
for ( uint8_t byte = 0; byte < sizeof( tmpStr ); byte++ )
{
// Don't copy the character if NULL and the current is not NULL
// Just stop
if ( tmpStr[byte] == '\0' && out[byte] != 0x0000 )
{
break;
}
out[byte] = tmpStr[byte] | 0x0000;
}
}
void revsStr( char* in )
{
// Iterators
int i, j;
// Temp storage
char c;
// Loop through the string, and reverse the order of the characters
for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
{
c = in[i];
in[i] = in[j];
in[j] = c;
}
}
uint16_t lenStr( char* in )
{
// Iterator
char *pos;
// Loop until null is found
for ( pos = in; *pos; pos++ );
// Return the difference between the pointers of in and pos (which is the string length)
return (pos - in);
}
int16_t eqStr( char* str1, char* str2 )
{
// Scan each string for NULLs and whether they are the same
while( *str1 != '\0' && *str1++ == *str2++ );
// If the strings are still identical (i.e. both NULL), then return -1, otherwise current *str1
// If *str1 is 0, then str1 ended (and str1 is "like" str2), otherwise strings are different
return *--str1 == *--str2 ? -1 : *++str1;
}
int numToInt( char* in )
{
// Pointers to the LSD (Least Significant Digit) and MSD
char* lsd = in;
char* msd = in;
int total = 0;
int sign = 1; // Default to positive
uint8_t base = 10; // Use base 10 by default TODO Add support for bases other than 10 and 16
// Scan the string once to determine the length
while ( *lsd != '\0' )
{
// Check for positive/negative
switch ( *lsd++ )
{
// Fall through is intentional, only do something on negative, ignore the rest
// Update the MSD to remove leading spaces and signs
case '-': sign = -1;
case '+':
case ' ':
msd = lsd;
break;
case 'x': // Hex Mode
base = 0x10;
msd = lsd;
break;
}
}
// Process string depending on which base
switch ( base )
{
case 10: // Decimal
// Rescan the string from the LSD to MSD to convert it to a decimal number
for ( unsigned int digit = 1; lsd > msd ; digit *= 10 )
total += ( (*--lsd) - '0' ) * digit;
break;
case 0x10: // Hex
// Rescan the string from the LSD to MSD to convert it to a hexadecimal number
for ( unsigned int digit = 1; lsd > msd ; digit *= 0x10 )
{
if ( *--lsd <= '9' ) total += ( *lsd - '0' ) * digit;
else if ( *lsd <= 'F' ) total += ( *lsd - 'A' + 10 ) * digit;
else if ( *lsd <= 'f' ) total += ( *lsd - 'a' + 10 ) * digit;
}
break;
}
// Propagate sign and return
return total * sign;
}