Skip to content

Commit

Permalink
AP_HAL: eliminate AP_HAL::Print and AP_HAL::Stream
Browse files Browse the repository at this point in the history
Just *way* too many layers involved here
  • Loading branch information
peterbarker authored and tridge committed Mar 22, 2018
1 parent b54ac27 commit 6c7c916
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 383 deletions.
2 changes: 0 additions & 2 deletions libraries/AP_HAL/AP_HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include "CAN.h"
#endif

#include "utility/Print.h"
#include "utility/Stream.h"
#include "utility/BetterStream.h"

/* HAL Class definition */
Expand Down
38 changes: 0 additions & 38 deletions libraries/AP_HAL/UARTDriver.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions libraries/AP_HAL/UARTDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <stdint.h>

#include <AP_Common/AP_Common.h>

#include "AP_HAL_Namespace.h"
#include "utility/BetterStream.h"

Expand Down Expand Up @@ -53,13 +51,6 @@ class AP_HAL::UARTDriver : public AP_HAL::BetterStream {
*/
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
Expand Down
13 changes: 9 additions & 4 deletions libraries/AP_HAL/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -23,17 +24,21 @@ class BufferPrinter : public AP_HAL::Print {
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++);
}
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, ...)
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_HAL/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AP_HAL::Util {
/*
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) {}
Expand Down
31 changes: 31 additions & 0 deletions libraries/AP_HAL/utility/BetterStream.cpp
Original file line number Diff line number Diff line change
@@ -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));
}
26 changes: 20 additions & 6 deletions libraries/AP_HAL/utility/BetterStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Loading

0 comments on commit 6c7c916

Please sign in to comment.