Skip to content

Commit d7d6264

Browse files
committed
feat: enhance Print class with float support and printf functionality
Signed-off-by: Aymane Bahssain <[email protected]>
1 parent 227cd6f commit d7d6264

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

Common.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ typedef enum {
3636
} PinStatus;
3737

3838
typedef enum {
39-
INPUT = 0x0,
40-
OUTPUT = 0x1,
41-
INPUT_PULLUP = 0x2,
42-
INPUT_PULLDOWN = 0x3,
43-
OUTPUT_OPENDRAIN = 0x4,
39+
INPUT = 0x0,
40+
OUTPUT = 0x1,
41+
INPUT_PULLUP = 0x2,
42+
INPUT_FLOATING = INPUT,
43+
INPUT_PULLDOWN = 0x3,
44+
INPUT_ANALOG = 0x4,
45+
OUTPUT_OPEN_DRAIN = 0x5,
46+
OUTPUT_OPENDRAIN = OUTPUT_OPEN_DRAIN,
4447
} PinMode;
4548

4649
typedef enum {
@@ -93,12 +96,12 @@ typedef void (*voidFuncPtrParam)(void*);
9396
#endif
9497

9598
/* TODO: request for removal */
96-
typedef bool boolean;
99+
typedef bool boolean __attribute__((deprecated));
97100
typedef uint8_t byte;
98101
typedef uint16_t word;
99102

100103
void init(void);
101-
void initVariant(void);
104+
extern void initVariant() __attribute__((weak));
102105

103106
#ifndef HOST
104107
int atexit(void (*func)()) __attribute__((weak));
@@ -141,13 +144,13 @@ void loop(void);
141144
#endif
142145

143146
#ifdef __cplusplus
144-
template<class T, class L>
147+
template<class T, class L>
145148
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
146149
{
147150
return (b < a) ? b : a;
148151
}
149152

150-
template<class T, class L>
153+
template<class T, class L>
151154
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
152155
{
153156
return (a < b) ? b : a;

Print.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
#include <stdio.h>
2222
#include <string.h>
2323
#include <math.h>
24+
#include <unistd.h>
2425

26+
#include "uart.h"
2527
#include "Print.h"
2628

2729
using namespace arduino;
2830

31+
#if defined (VIRTIO_LOG)
32+
#include "virtio_log.h"
33+
#endif
34+
2935
// Public Methods //////////////////////////////////////////////////////////////
3036

3137
/* default implementation: may be overridden */
@@ -130,6 +136,11 @@ size_t Print::print(unsigned long long n, int base)
130136
else return printULLNumber(n, base);
131137
}
132138

139+
size_t Print::print(float n, int digits)
140+
{
141+
return printFloat(n, digits);
142+
}
143+
133144
size_t Print::print(double n, int digits)
134145
{
135146
return printFloat(n, digits);
@@ -222,6 +233,13 @@ size_t Print::println(unsigned long long num, int base)
222233
return n;
223234
}
224235

236+
size_t Print::println(float num, int digits)
237+
{
238+
size_t n = print(num, digits);
239+
n += println();
240+
return n;
241+
}
242+
225243
size_t Print::println(double num, int digits)
226244
{
227245
size_t n = print(num, digits);
@@ -236,6 +254,58 @@ size_t Print::println(const Printable& x)
236254
return n;
237255
}
238256

257+
extern "C" {
258+
__attribute__((weak))
259+
int _write(int file, char *ptr, int len)
260+
{
261+
switch (file) {
262+
case STDOUT_FILENO:
263+
case STDERR_FILENO:
264+
/* Used for core_debug() */
265+
#if defined (VIRTIO_LOG)
266+
virtio_log((uint8_t *)ptr, (uint32_t)len);
267+
#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
268+
uart_debug_write((uint8_t *)ptr, (uint32_t)len);
269+
#endif
270+
break;
271+
case STDIN_FILENO:
272+
break;
273+
default:
274+
((class Print *)file)->write((uint8_t *)ptr, len);
275+
break;
276+
}
277+
return len;
278+
}
279+
}
280+
281+
int Print::printf(const char *format, ...)
282+
{
283+
va_list ap;
284+
va_start(ap, format);
285+
int retval = vdprintf((int)this, format, ap);
286+
va_end(ap);
287+
return retval;
288+
}
289+
290+
int Print::printf(const __FlashStringHelper *format, ...)
291+
{
292+
va_list ap;
293+
va_start(ap, format);
294+
int retval = vdprintf((int)this, (const char *)format, ap);
295+
va_end(ap);
296+
return retval;
297+
}
298+
299+
int Print::vprintf(const char *format, va_list ap)
300+
{
301+
return vdprintf((int)this, format, ap);
302+
}
303+
304+
int Print::vprintf(const __FlashStringHelper *format, va_list ap)
305+
{
306+
return vdprintf((int)this, (const char *)format, ap);
307+
}
308+
239309
// Private Methods /////////////////////////////////////////////////////////////
240310

241311
size_t Print::printNumber(unsigned long n, uint8_t base)

Print.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <inttypes.h>
2323
#include <stdio.h> // for size_t
24+
#include <stdarg.h> // for printf
2425

2526
#include "String.h"
2627
#include "Printable.h"
@@ -72,6 +73,7 @@ class Print
7273
size_t print(unsigned long, int = DEC);
7374
size_t print(long long, int = DEC);
7475
size_t print(unsigned long long, int = DEC);
76+
size_t print(float, int = 2);
7577
size_t print(double, int = 2);
7678
size_t print(const Printable&);
7779

@@ -86,12 +88,18 @@ class Print
8688
size_t println(unsigned long, int = DEC);
8789
size_t println(long long, int = DEC);
8890
size_t println(unsigned long long, int = DEC);
91+
size_t println(float, int = 2);
8992
size_t println(double, int = 2);
9093
size_t println(const Printable&);
9194
size_t println(void);
9295

96+
int printf(const char *format, ...);
97+
int printf(const __FlashStringHelper *format, ...);
98+
int vprintf(const __FlashStringHelper *format, va_list ap);
99+
int vprintf(const char *format, va_list ap);
100+
93101
virtual void flush() { /* Empty implementation for backward compatibility */ }
94102
};
95103

96104
}
97-
using arduino::Print;
105+
using arduino::Print;

RingBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace arduino {
3131
// using a ring buffer (I think), in which head is the index of the location
3232
// to which to write the next incoming character and tail is the index of the
3333
// location from which to read.
34-
#define SERIAL_BUFFER_SIZE 64
34+
#define SERIAL_BUFFER_SIZE 128 //64
3535

3636
template <int N>
3737
class RingBufferN

0 commit comments

Comments
 (0)