AP_HAL: eliminate AP_HAL::Print and AP_HAL::Stream
Just *way* too many layers involved here
This commit is contained in:
parent
b54ac274f5
commit
6c7c916c78
@ -23,8 +23,6 @@
|
||||
#include "CAN.h"
|
||||
#endif
|
||||
|
||||
#include "utility/Print.h"
|
||||
#include "utility/Stream.h"
|
||||
#include "utility/BetterStream.h"
|
||||
|
||||
/* HAL Class definition */
|
||||
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
}
|
@ -2,8 +2,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <AP_Common/AP_Common.h>
|
||||
|
||||
#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
|
||||
|
@ -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, ...)
|
||||
|
@ -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) {}
|
||||
|
31
libraries/AP_HAL/utility/BetterStream.cpp
Normal file
31
libraries/AP_HAL/utility/BetterStream.cpp
Normal file
@ -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<size;i++) {
|
||||
if (write(buffer[i] == 0)) {
|
||||
return i;
|
||||
}
|
||||
};
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t AP_HAL::BetterStream::write(const char *str)
|
||||
{
|
||||
return write((const uint8_t *)str, strlen(str));
|
||||
}
|
@ -24,12 +24,26 @@
|
||||
#include <AP_Common/AP_Common.h>
|
||||
#include <AP_HAL/AP_HAL_Namespace.h>
|
||||
|
||||
#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;
|
||||
};
|
||||
|
@ -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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <AP_Math/AP_Math.h>
|
||||
#include <AP_HAL/AP_HAL_Namespace.h>
|
||||
#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<digits; ++i)
|
||||
rounding /= 10.0f;
|
||||
|
||||
number += rounding;
|
||||
|
||||
// Extract the integer part of the number and print it
|
||||
unsigned long int_part = (unsigned long)number;
|
||||
float remainder = number - (float )int_part;
|
||||
n += print(int_part);
|
||||
|
||||
// Print the decimal point, but only if there are digits beyond
|
||||
if (digits > 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;
|
||||
}
|
@ -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 <AP_HAL/AP_HAL_Namespace.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
};
|
@ -1,20 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <AP_HAL/AP_HAL_Namespace.h>
|
||||
#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;
|
||||
|
||||
};
|
@ -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;
|
||||
|
@ -4,4 +4,4 @@
|
||||
|
||||
#include <AP_HAL/AP_HAL.h>
|
||||
|
||||
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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user