summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-01-16 11:04:22 +0100
committerDaniel Friesel <derf@finalrewind.org>2018-01-16 11:04:22 +0100
commit17a7c6d3783288c3ed0b28f898232cd0ea00c0c9 (patch)
tree6874ee9be1bf6845f0c1c6a524e9792645872f08
parent7b8c37d235c77d881db4184a798fc4b72a1fa279 (diff)
esp8266 stdout: add printf_uint8 and printf_float
-rw-r--r--include/esp8266/driver/stdout.h2
-rw-r--r--src/arch/esp8266/driver/stdout.cc35
2 files changed, 37 insertions, 0 deletions
diff --git a/include/esp8266/driver/stdout.h b/include/esp8266/driver/stdout.h
index 5a75096..038de2e 100644
--- a/include/esp8266/driver/stdout.h
+++ b/include/esp8266/driver/stdout.h
@@ -14,6 +14,8 @@ class StandardOutput {
void put(char c);
void write(const char *s);
void flush() {}
+ void printf_uint8(unsigned char num);
+ void printf_float(float num);
StandardOutput & operator<<(char c);
StandardOutput & operator<<(unsigned char c);
diff --git a/src/arch/esp8266/driver/stdout.cc b/src/arch/esp8266/driver/stdout.cc
index 9638a81..2064b38 100644
--- a/src/arch/esp8266/driver/stdout.cc
+++ b/src/arch/esp8266/driver/stdout.cc
@@ -141,6 +141,41 @@ void StandardOutput::setBase(uint8_t b)
}
}
+static inline char format_hex_nibble(uint8_t num)
+{
+ if (num > 9) {
+ return 'a' + num - 10;
+ }
+ return '0' + num;
+}
+
+void StandardOutput::printf_uint8(uint8_t num)
+{
+ put(format_hex_nibble(num / 16));
+ put(format_hex_nibble(num % 16));
+}
+
+void StandardOutput::printf_float(float num)
+{
+ if (num < 0) {
+ put('-');
+ num *= -1;
+ }
+ if (num > 1000) {
+ put('0' + (((int)num % 10000) / 1000));
+ }
+ if (num > 100) {
+ put('0' + (((int)num % 1000) / 100));
+ }
+ if (num > 10) {
+ put('0' + (((int)num % 100) / 10));
+ }
+ put('0' + ((int)num % 10));
+ put('.');
+ put('0' + ((int)(num * 10) % 10));
+ put('0' + ((int)(num * 100) % 10));
+}
+
// FLUSH
StandardOutput & flush(StandardOutput & os)
{