AP_HAL: add and use a "bool read(c)" method to AP_HAL

this is much less likely to not work vs the int16_t equivalent
This commit is contained in:
Peter Barker 2023-02-21 20:35:57 +11:00 committed by Peter Barker
parent ec40a9641b
commit 394d70abe0
4 changed files with 13 additions and 5 deletions

View File

@ -66,7 +66,7 @@ public:
virtual size_t write_locked(const uint8_t *buffer, size_t size, uint32_t key) { return 0; }
// read from a locked port. If port is locked and key is not correct then 0 is returned
virtual int16_t read_locked(uint32_t key) { return -1; }
virtual bool read_locked(uint32_t key, uint8_t &b) WARN_IF_UNUSED { return -1; }
// control optional features
virtual bool set_options(uint16_t options) { _last_options = options; return options==0; }

View File

@ -36,7 +36,7 @@ public:
const size_t _size;
uint32_t available() override { return 0; }
int16_t read() override { return -1; }
bool read(uint8_t &b) override { return false; }
uint32_t txspace() override { return 0; }
bool discard_input() override { return false; }
};

View File

@ -20,6 +20,15 @@ size_t AP_HAL::BetterStream::write(const char *str)
return write((const uint8_t *)str, strlen(str));
}
int16_t AP_HAL::BetterStream::read()
{
uint8_t b;
if (!read(b)) {
return -1;
}
return b;
}
ssize_t AP_HAL::BetterStream::read(uint8_t *buffer, uint16_t count) {
uint16_t offset = 0;
while (count--) {

View File

@ -40,9 +40,8 @@ public:
virtual uint32_t available() = 0;
/* return value for read():
* -1 if nothing available, uint8_t value otherwise. */
virtual int16_t read() = 0;
int16_t read(); // old function; prefer calling the boolean method
virtual bool read(uint8_t &b) WARN_IF_UNUSED = 0;
// no base-class implementation to force descendants to
// do things efficiently. Looping over 2^32-1 bytes would be bad.