-
Notifications
You must be signed in to change notification settings - Fork 1
/
Print.c
executable file
·151 lines (125 loc) · 2.17 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
/*********************************
* Marc Verdiell
* Aug 18, 2013
* A print library that resembles Arduino
* No polymorphism in C, so naming of the function changes
***************************************/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include "serial.h"
#include "Print.h"
// Public Methods //////////////////////////////////////////////////////////////
void print(uint8_t b)
{
// print on uart0 by default. Must be initialized firts
serial_putc(b);
}
void printchar(char c)
{
print((uint8_t) c);
}
void printstr(char c[])
{
while (*c)
print(*c++);
}
// to print from program memory
void printstr_p(const char *progmem_s )
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) )
print(c);
}
void printuint(uint8_t n)
{
printNumber(n, 10);
}
void printint(int n)
{
printlong((long) n);
}
void printlong(long n)
{
if (n < 0) {
print('-');
n = -n;
}
printNumber(n, 10);
}
void printulong(unsigned long n)
{
printNumber(n, 10);
}
void printbase(long n, int base)
{
if (base == 0)
print((char) n);
else
printNumber(n, base);
}
void println(void)
{
print('\r');
print('\n');
}
void printlnchar(char c)
{
printchar(c);
println();
}
void printlnstr(char c[])
{
printstr(c);
println();
}
// same for string in program memory
void printlnstr_p(const char c[])
{
printstr_p(c);
println();
}
void printlnuint(uint8_t n)
{
printNumber(n, 10);
println();
}
void printlnint(int n)
{
printint(n);
println();
}
void printlnlong(long n)
{
printlong(n);
println();
}
void printlnulong(unsigned long n)
{
printulong(n);
println();
}
void printlnbase(long n, int base)
{
printbase(n, base);
println();
}
// Private Methods /////////////////////////////////////////////////////////////
void printNumber(unsigned long n, uint8_t base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
print('0');
return;
}
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--)
print((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
}