From 6c7c916c78751efdb1cb013e0c30ac1411b306d3 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 22 Mar 2018 17:53:32 +1100 Subject: [PATCH] AP_HAL: eliminate AP_HAL::Print and AP_HAL::Stream Just *way* too many layers involved here --- libraries/AP_HAL/AP_HAL.h | 2 - libraries/AP_HAL/UARTDriver.cpp | 38 ---- libraries/AP_HAL/UARTDriver.h | 9 - libraries/AP_HAL/Util.cpp | 13 +- libraries/AP_HAL/Util.h | 2 +- libraries/AP_HAL/utility/BetterStream.cpp | 31 +++ libraries/AP_HAL/utility/BetterStream.h | 26 ++- libraries/AP_HAL/utility/Print.cpp | 222 --------------------- libraries/AP_HAL/utility/Print.h | 75 ------- libraries/AP_HAL/utility/Stream.h | 20 -- libraries/AP_HAL/utility/print_vprintf.cpp | 2 +- libraries/AP_HAL/utility/print_vprintf.h | 2 +- libraries/AP_HAL_PX4/Util.h | 4 +- libraries/AP_HAL_VRBRAIN/Util.h | 4 +- 14 files changed, 67 insertions(+), 383 deletions(-) delete mode 100644 libraries/AP_HAL/UARTDriver.cpp create mode 100644 libraries/AP_HAL/utility/BetterStream.cpp delete mode 100644 libraries/AP_HAL/utility/Print.cpp delete mode 100644 libraries/AP_HAL/utility/Print.h delete mode 100644 libraries/AP_HAL/utility/Stream.h diff --git a/libraries/AP_HAL/AP_HAL.h b/libraries/AP_HAL/AP_HAL.h index 61863f76b8..e0d83acd12 100644 --- a/libraries/AP_HAL/AP_HAL.h +++ b/libraries/AP_HAL/AP_HAL.h @@ -23,8 +23,6 @@ #include "CAN.h" #endif -#include "utility/Print.h" -#include "utility/Stream.h" #include "utility/BetterStream.h" /* HAL Class definition */ diff --git a/libraries/AP_HAL/UARTDriver.cpp b/libraries/AP_HAL/UARTDriver.cpp deleted file mode 100644 index 8795efc60b..0000000000 --- a/libraries/AP_HAL/UARTDriver.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ - -#include "AP_HAL.h" - -#include "utility/print_vprintf.h" -#include "UARTDriver.h" - -/* - BetterStream method implementations - These are implemented in AP_HAL to ensure consistent behaviour on - all boards, although they can be overridden by a port - */ - -void AP_HAL::UARTDriver::printf(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); -} - -void AP_HAL::UARTDriver::vprintf(const char *fmt, va_list ap) -{ - print_vprintf(this, fmt, ap); -} diff --git a/libraries/AP_HAL/UARTDriver.h b/libraries/AP_HAL/UARTDriver.h index 4433251206..2eded01295 100644 --- a/libraries/AP_HAL/UARTDriver.h +++ b/libraries/AP_HAL/UARTDriver.h @@ -2,8 +2,6 @@ #include -#include - #include "AP_HAL_Namespace.h" #include "utility/BetterStream.h" @@ -53,13 +51,6 @@ public: */ virtual bool set_unbuffered_writes(bool on){ return false; }; - /* Implementations of BetterStream virtual methods. These are - * provided by AP_HAL to ensure consistency between ports to - * different boards - */ - void printf(const char *s, ...) FMT_PRINTF(2, 3); - void vprintf(const char *s, va_list ap); - /* wait for at least n bytes of incoming data, with timeout in milliseconds. Return true if n bytes are available, false if diff --git a/libraries/AP_HAL/Util.cpp b/libraries/AP_HAL/Util.cpp index ecc91d7885..628656475d 100644 --- a/libraries/AP_HAL/Util.cpp +++ b/libraries/AP_HAL/Util.cpp @@ -11,10 +11,11 @@ #endif /* Helper class implements AP_HAL::Print so we can use utility/vprintf */ -class BufferPrinter : public AP_HAL::Print { +class BufferPrinter : public AP_HAL::BetterStream { public: BufferPrinter(char* str, size_t size) : _offs(0), _str(str), _size(size) {} - size_t write(uint8_t c) { + + size_t write(uint8_t c) override { if (_offs < _size) { _str[_offs] = c; _offs++; @@ -23,7 +24,7 @@ public: return 0; } } - size_t write(const uint8_t *buffer, size_t size) { + size_t write(const uint8_t *buffer, size_t size) override { size_t n = 0; while (size--) { n += write(*buffer++); @@ -31,9 +32,13 @@ public: return n; } - size_t _offs; + size_t _offs; char* const _str; const size_t _size; + + uint32_t available() override { return 0; } + int16_t read() override { return -1; } + uint32_t txspace() override { return 0; } }; int AP_HAL::Util::snprintf(char* str, size_t size, const char *format, ...) diff --git a/libraries/AP_HAL/Util.h b/libraries/AP_HAL/Util.h index 8ad428a227..1460ffdf12 100644 --- a/libraries/AP_HAL/Util.h +++ b/libraries/AP_HAL/Util.h @@ -81,7 +81,7 @@ public: /* return a stream for access to a system shell, if available */ - virtual AP_HAL::Stream *get_shell_stream() { return nullptr; } + virtual AP_HAL::BetterStream *get_shell_stream() { return nullptr; } /* Support for an imu heating system */ virtual void set_imu_temp(float current) {} diff --git a/libraries/AP_HAL/utility/BetterStream.cpp b/libraries/AP_HAL/utility/BetterStream.cpp new file mode 100644 index 0000000000..87f8f0e9c9 --- /dev/null +++ b/libraries/AP_HAL/utility/BetterStream.cpp @@ -0,0 +1,31 @@ +#include "BetterStream.h" + +#include "print_vprintf.h" + +void AP_HAL::BetterStream::printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} + +void AP_HAL::BetterStream::vprintf(const char *fmt, va_list ap) +{ + print_vprintf(this, fmt, ap); +} + +size_t AP_HAL::BetterStream::write(const uint8_t *buffer, size_t size) +{ + for (size_t i=0; i #include -#include "Stream.h" - -class AP_HAL::BetterStream : public AP_HAL::Stream { +class AP_HAL::BetterStream { public: - BetterStream(void) {} - virtual void printf(const char *, ...) FMT_PRINTF(2, 3) = 0; - virtual void vprintf(const char *, va_list) = 0; + virtual void printf(const char *, ...) FMT_PRINTF(2, 3); + virtual void vprintf(const char *, va_list); + + void print(const char *str) { write(str); } + void println(const char *str) { printf("%s\r\n", str); } + + virtual size_t write(uint8_t) = 0; + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *str); + + virtual uint32_t available() = 0; + + /* return value for read(): + * -1 if nothing available, uint8_t value otherwise. */ + virtual int16_t read() = 0; + + /* NB txspace was traditionally a member of BetterStream in the + * FastSerial library. As far as concerns go, it belongs with available() */ + virtual uint32_t txspace() = 0; }; diff --git a/libraries/AP_HAL/utility/Print.cpp b/libraries/AP_HAL/utility/Print.cpp deleted file mode 100644 index d054a6a366..0000000000 --- a/libraries/AP_HAL/utility/Print.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* - Print.cpp - Base class that provides print() and println() - Copyright (c) 2008 David A. Mellis. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - Modified 23 November 2006 by David A. Mellis - */ - -#include -#include -#include -#include - -#include -#include -#include "Print.h" -using namespace AP_HAL; - -size_t Print::print(const char str[]) -{ - return write(str); -} - -size_t Print::print(char c) -{ - return write(c); -} - -size_t Print::print(unsigned char b, int base) -{ - return print((unsigned long) b, base); -} - -size_t Print::print(int n, int base) -{ - return print((long) n, base); -} - -size_t Print::print(unsigned int n, int base) -{ - return print((unsigned long) n, base); -} - -size_t Print::print(long n, int base) -{ - if (base == 0) { - return write(n); - } else if (base == 10) { - if (n < 0) { - int t = print('-'); - n = -n; - return printNumber(n, 10) + t; - } - return printNumber(n, 10); - } else { - return printNumber(n, base); - } -} - -size_t Print::print(unsigned long n, int base) -{ - if (base == 0) return write(n); - else return printNumber(n, base); -} - -size_t Print::print(float n, int digits) -{ - return printFloat(n, digits); -} - -// the compiler promotes to double if we do arithmetic in the -// argument, but we only actually want float precision, so just wrap -// it with a double method -size_t Print::print(double n, int digits) -{ - return print((float)n, digits); -} - -size_t Print::println(void) -{ - size_t n = print('\r'); - n += print('\n'); - return n; -} - -size_t Print::println(const char c[]) -{ - size_t n = print(c); - n += println(); - return n; -} - -size_t Print::println(char c) -{ - size_t n = print(c); - n += println(); - return n; -} - -size_t Print::println(unsigned char b, int base) -{ - size_t n = print(b, base); - n += println(); - return n; -} - -size_t Print::println(int num, int base) -{ - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(unsigned int num, int base) -{ - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(long num, int base) -{ - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(unsigned long num, int base) -{ - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(float num, int digits) -{ - size_t n = print(num, digits); - n += println(); - return n; -} - -// the compiler promotes to double if we do arithmetic in the -// argument, but we only actually want float precision, so just wrap -// it with a double method -size_t Print::println(double num, int digits) -{ - return println((float)num, digits); -} - -// Private Methods ///////////////////////////////////////////////////////////// - -size_t Print::printNumber(unsigned long n, uint8_t base) { - char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. - char *str = &buf[sizeof(buf) - 1]; - - *str = '\0'; - - // prevent crash if called with base == 1 - if (base < 2) base = 10; - - do { - unsigned long m = n; - n /= base; - char c = m - base * n; - *--str = c < 10 ? c + '0' : c + 'A' - 10; - } while(n); - - return write(str); -} - -size_t Print::printFloat(float number, uint8_t digits) -{ - size_t n = 0; - - // Handle negative numbers - if (number < 0.0f) - { - n += print('-'); - number = -number; - } - - // Round correctly so that print(1.999, 2) prints as "2.00" - float rounding = 0.5; - for (uint8_t i=0; i 0) { - n += print("."); - } - - // Extract digits from the remainder one at a time - while (digits-- > 0) - { - remainder *= 10.0f; - int toPrint = int(remainder); - n += print(toPrint); - remainder -= toPrint; - } - - return n; -} diff --git a/libraries/AP_HAL/utility/Print.h b/libraries/AP_HAL/utility/Print.h deleted file mode 100644 index 32dd99ddac..0000000000 --- a/libraries/AP_HAL/utility/Print.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - Print.h - Base class that provides print() and println() - Copyright (c) 2008 David A. Mellis. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#pragma once - -#include - -#include -#include - -/** - * This is the Arduino (v1.0) Print class, with some changes: - * - Removed methods for class String or _FlashStringHelper - * - printFloat takes a float, not a double. (double === float on AVR, but - * not on other platforms) - */ - -enum { - BASE_DEFAULT = 0, - BASE_BIN = 2, - BASE_OCT = 8, - BASE_DEC = 10, - BASE_HEX = 16 -}; - - -class AP_HAL::Print { - private: - size_t printNumber(unsigned long, uint8_t); - size_t printFloat(float, uint8_t); - public: - Print() {} - - virtual size_t write(uint8_t) = 0; - virtual size_t write(const uint8_t *buffer, size_t size) = 0; - - size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); } - public: - size_t print(const char[]); - size_t print(char); - size_t print(unsigned char, int = BASE_DEC); - size_t print(int, int = BASE_DEC); - size_t print(unsigned int, int = BASE_DEC); - size_t print(long, int = BASE_DEC); - size_t print(unsigned long, int = BASE_DEC); - size_t print(float , int = 2); - size_t print(double , int = 2); - - size_t println(const char[]); - size_t println(char); - size_t println(unsigned char, int = BASE_DEC); - size_t println(int, int = BASE_DEC); - size_t println(unsigned int, int = BASE_DEC); - size_t println(long, int = BASE_DEC); - size_t println(unsigned long, int = BASE_DEC); - size_t println(float , int = 2); - size_t println(double , int = 2); - size_t println(void); - -}; diff --git a/libraries/AP_HAL/utility/Stream.h b/libraries/AP_HAL/utility/Stream.h deleted file mode 100644 index 7416ce0be2..0000000000 --- a/libraries/AP_HAL/utility/Stream.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include -#include "Print.h" - -/* A simple Stream library modeled after the bits we actually use - * from Arduino Stream */ - -class AP_HAL::Stream : public AP_HAL::Print { -public: - virtual uint32_t available() = 0; - /* NB txspace was traditionally a member of BetterStream in the - * FastSerial library. As far as concerns go, it belongs with available() */ - virtual uint32_t txspace() = 0; - - /* return value for read(): - * -1 if nothing available, uint8_t value otherwise. */ - virtual int16_t read() = 0; - -}; diff --git a/libraries/AP_HAL/utility/print_vprintf.cpp b/libraries/AP_HAL/utility/print_vprintf.cpp index bf829a2d37..8333ba8d62 100644 --- a/libraries/AP_HAL/utility/print_vprintf.cpp +++ b/libraries/AP_HAL/utility/print_vprintf.cpp @@ -66,7 +66,7 @@ #define FL_FLTEXP FL_PREC #define FL_FLTFIX FL_LONG -void print_vprintf(AP_HAL::Print *s, const char *fmt, va_list ap) +void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap) { unsigned char c; /* holds a char from the format string */ uint16_t flags; diff --git a/libraries/AP_HAL/utility/print_vprintf.h b/libraries/AP_HAL/utility/print_vprintf.h index c165566b8f..7eee919a81 100644 --- a/libraries/AP_HAL/utility/print_vprintf.h +++ b/libraries/AP_HAL/utility/print_vprintf.h @@ -4,4 +4,4 @@ #include -void print_vprintf(AP_HAL::Print *s, const char *fmt, va_list ap); +void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap); diff --git a/libraries/AP_HAL_PX4/Util.h b/libraries/AP_HAL_PX4/Util.h index 7fc72802f1..1c36fd67ea 100644 --- a/libraries/AP_HAL_PX4/Util.h +++ b/libraries/AP_HAL_PX4/Util.h @@ -4,7 +4,7 @@ #include "AP_HAL_PX4_Namespace.h" #include "Semaphores.h" -class PX4::NSHShellStream : public AP_HAL::Stream { +class PX4::NSHShellStream : public AP_HAL::BetterStream { public: size_t write(uint8_t); size_t write(const uint8_t *buffer, size_t size); @@ -49,7 +49,7 @@ public: /* return a stream for access to nsh shell */ - AP_HAL::Stream *get_shell_stream() { return &_shell_stream; } + AP_HAL::BetterStream *get_shell_stream() { return &_shell_stream; } perf_counter_t perf_alloc(perf_counter_type t, const char *name) override; void perf_begin(perf_counter_t ) override; void perf_end(perf_counter_t) override; diff --git a/libraries/AP_HAL_VRBRAIN/Util.h b/libraries/AP_HAL_VRBRAIN/Util.h index db1afffa3a..f12226b5f9 100644 --- a/libraries/AP_HAL_VRBRAIN/Util.h +++ b/libraries/AP_HAL_VRBRAIN/Util.h @@ -4,7 +4,7 @@ #include "AP_HAL_VRBRAIN_Namespace.h" #include "Semaphores.h" -class VRBRAIN::NSHShellStream : public AP_HAL::Stream { +class VRBRAIN::NSHShellStream : public AP_HAL::BetterStream { public: size_t write(uint8_t); size_t write(const uint8_t *buffer, size_t size); @@ -49,7 +49,7 @@ public: /* return a stream for access to nsh shell */ - AP_HAL::Stream *get_shell_stream() { return &_shell_stream; } + AP_HAL::BetterStream *get_shell_stream() { return &_shell_stream; } perf_counter_t perf_alloc(perf_counter_type t, const char *name) override; void perf_begin(perf_counter_t ) override; void perf_end(perf_counter_t) override;