Skip to content

Commit 5d0eafa

Browse files
committed
partial implementation of Print.c
1 parent 7aa0177 commit 5d0eafa

8 files changed

Lines changed: 459 additions & 4 deletions

File tree

sduino/Arduino.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333

3434
#include "binary.h"
3535

36+
// FIXME: workarounds for missing features or unimplemented functions
3637
// cancel out the PROGMEM attribute - used only for atmel CPUs
3738
#define PROGMEM
38-
3939
void yield(void);
4040

41+
42+
43+
4144
#define HIGH 0x1
4245
#define LOW 0x0
4346

sduino/HardwareSerial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Modified 15 December 2016 by Michael Mayer
2626
*/
2727

28+
#include <stdlib.h>
2829

2930

3031
// Define config for Serial.begin(baud, config);

sduino/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ CC=$(BINDIR)/sdcc
77
LD=$(BINDIR)/sdld
88

99
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
10-
-I/usr/share/sdcc/include/ -I$(LIBBASE)/inc -I.
10+
-I. -I$(SDCCBASE)/share/sdcc/include/ -I$(LIBBASE)/inc
1111

1212
LIBBASE=../STM8S_StdPeriph_Driver
13-
LDFLAGS=-L$(LIBBASE)/src -L/opt/sdcc/share/sdcc/lib/stm8 -lstm8s
13+
LDFLAGS=-L$(LIBBASE)/src -L$(SDCCBASE)/share/sdcc/lib/stm8 -lstm8s
1414

1515
OBJECTS=main.rel wiring.rel wiring_digital.rel \
1616
wiring_shift.rel wiring_pulse.rel \
17-
HardwareSerial.rel \
17+
HardwareSerial.rel Print.rel \
1818
sketch.rel
1919

2020
.PHONY: all clean flash

sduino/Print.c

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/*
2+
Print.cpp - Base class that provides print() and println()
3+
Copyright (c) 2008 David A. Mellis. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Modified 23 November 2006 by David A. Mellis
20+
Modified 03 August 2015 by Chuck Todd
21+
*/
22+
23+
#ifdef _GCC_
24+
#include <stdint.h>
25+
#include <stdio.h>
26+
#define PROGMEM
27+
#endif
28+
29+
//#include <stdlib.h>
30+
//#include <stdio.h>
31+
//#include <string.h>
32+
#include <math.h>
33+
#include "Arduino.h"
34+
35+
#include "Print.h"
36+
37+
#ifdef _GCC_
38+
#undef printChr
39+
#define printChr(C) putchar(C)
40+
#endif
41+
42+
43+
void printNL(void);
44+
45+
// Public Methods //////////////////////////////////////////////////////////////
46+
47+
size_t printBuf(const uint8_t *buffer, size_t size)
48+
{
49+
size_t n = 0;
50+
while (size--) {
51+
if (printChr(*buffer++)) n++;
52+
else break;
53+
}
54+
return n;
55+
}
56+
57+
size_t printStr(const char *str)
58+
{
59+
size_t n = 0;
60+
char c;
61+
62+
if (!str) return 0;
63+
64+
while ( c=*str++ ) { // assignment intented
65+
if (printChr(c)) n++;
66+
else break;
67+
}
68+
return n;
69+
}
70+
71+
72+
size_t Print_print_u(unsigned long n)
73+
{
74+
return printNumber(n,10);
75+
}
76+
77+
size_t Print_print_i(long n)
78+
{
79+
return printInt(n,10);
80+
}
81+
82+
size_t Print_println_u(unsigned long n)
83+
{
84+
size_t r;
85+
86+
r = printNumber(n,10);
87+
return r + println();
88+
}
89+
90+
size_t Print_println_i(long n)
91+
{
92+
size_t r;
93+
94+
r = printInt(n,10);
95+
return r + println();
96+
}
97+
98+
99+
/*
100+
size_t Print::print(long n, int base)
101+
{
102+
if (base == 0) {
103+
return write(n);
104+
} else if (base == 10) {
105+
if (n < 0) {
106+
int t = print('-');
107+
n = -n;
108+
return printNumber(n, 10) + t;
109+
}
110+
return printNumber(n, 10);
111+
} else {
112+
return printNumber(n, base);
113+
}
114+
}
115+
116+
size_t Print::print(unsigned long n, int base)
117+
{
118+
if (base == 0) return write(n);
119+
else return printNumber(n, base);
120+
}
121+
122+
size_t Print::print(double n, int digits)
123+
{
124+
return printFloat(n, digits);
125+
}
126+
127+
size_t Print::println(const __FlashStringHelper *ifsh)
128+
{
129+
size_t n = print(ifsh);
130+
n += println();
131+
return n;
132+
}
133+
134+
size_t Print::print(const Printable& x)
135+
{
136+
return x.printTo(*this);
137+
}
138+
139+
size_t Print::println(void)
140+
{
141+
return write("\r\n");
142+
}
143+
144+
size_t Print::println(const String &s)
145+
{
146+
size_t n = print(s);
147+
n += println();
148+
return n;
149+
}
150+
151+
size_t Print::println(const char c[])
152+
{
153+
size_t n = print(c);
154+
n += println();
155+
return n;
156+
}
157+
158+
size_t Print::println(char c)
159+
{
160+
size_t n = print(c);
161+
n += println();
162+
return n;
163+
}
164+
165+
size_t Print::println(unsigned char b, int base)
166+
{
167+
size_t n = print(b, base);
168+
n += println();
169+
return n;
170+
}
171+
172+
size_t Print::println(int num, int base)
173+
{
174+
size_t n = print(num, base);
175+
n += println();
176+
return n;
177+
}
178+
179+
size_t Print::println(unsigned int num, int base)
180+
{
181+
size_t n = print(num, base);
182+
n += println();
183+
return n;
184+
}
185+
186+
size_t Print::println(long num, int base)
187+
{
188+
size_t n = print(num, base);
189+
n += println();
190+
return n;
191+
}
192+
193+
size_t Print::println(unsigned long num, int base)
194+
{
195+
size_t n = print(num, base);
196+
n += println();
197+
return n;
198+
}
199+
200+
size_t Print::println(double num, int digits)
201+
{
202+
size_t n = print(num, digits);
203+
n += println();
204+
return n;
205+
}
206+
207+
size_t Print::println(const Printable& x)
208+
{
209+
size_t n = print(x);
210+
n += println();
211+
return n;
212+
}
213+
*/
214+
// Private Methods /////////////////////////////////////////////////////////////
215+
216+
size_t println(void)
217+
{
218+
printChr(13);
219+
printChr(10);
220+
return 2;
221+
}
222+
223+
224+
size_t printNumber(unsigned long n, uint8_t base)
225+
{
226+
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
227+
char *str = &buf[sizeof(buf) - 1];
228+
229+
*str = '\0';
230+
231+
// prevent crash if called with base == 1
232+
if (base < 2) base = 10;
233+
234+
do {
235+
char c = n % base;
236+
n /= base;
237+
238+
*--str = c < 10 ? c + '0' : c + 'A' - 10;
239+
} while(n);
240+
241+
return printStr(str);
242+
}
243+
244+
size_t printInt(long n, uint8_t base)
245+
{
246+
if (base == 0) {
247+
return printChr((unsigned char) n);
248+
} else if (base == 10) {
249+
if (n < 0) {
250+
int t = printChr('-');
251+
n = -n;
252+
return printNumber(n, 10) + t;
253+
}
254+
return printNumber(n, 10);
255+
} else {
256+
return printNumber(n, base);
257+
}
258+
}
259+
260+
261+
size_t Print_printFloat(double number, uint8_t digits)
262+
{
263+
size_t n = 0;
264+
uint8_t i;
265+
unsigned long int_part;
266+
double remainder, rounding;
267+
unsigned int toPrint;
268+
269+
if (isnan(number)) return printStr("nan");
270+
if (isinf(number)) return printStr("inf");
271+
if (number > 4294967040.0) return printStr ("ovf"); // constant determined empirically
272+
if (number <-4294967040.0) return printStr ("ovf"); // constant determined empirically
273+
274+
// Handle negative numbers
275+
if (number < 0.0)
276+
{
277+
n += printChr('-');
278+
number = -number;
279+
}
280+
281+
// Round correctly so that print(1.999, 2) prints as "2.00"
282+
rounding = 0.5;
283+
for (i=0; i<digits; ++i)
284+
rounding /= 10.0;
285+
286+
number += rounding;
287+
288+
// Extract the integer part of the number and print it
289+
int_part = (unsigned long)number;
290+
remainder = number - (double)int_part;
291+
n += printNumber(int_part,10);
292+
293+
// Print the decimal point, but only if there are digits beyond
294+
if (digits > 0) {
295+
n += printChr('.');
296+
}
297+
298+
// Extract digits from the remainder one at a time
299+
while (digits-- > 0)
300+
{
301+
remainder *= 10.0;
302+
toPrint = (unsigned int)(remainder);
303+
n += printNumber(toPrint,10);
304+
remainder -= toPrint;
305+
}
306+
307+
return n;
308+
}

0 commit comments

Comments
 (0)