convert USBSerial and HardwareSerial to derived from Stream instead of Print

This commit is contained in:
Martin Ayotte 2015-08-11 18:50:38 -04:00
parent c05570dbcc
commit 82f15f091a
6 changed files with 79 additions and 14 deletions

View File

@ -138,10 +138,15 @@ int HardwareSerial::read(void) {
} }
} }
uint32 HardwareSerial::available(void) { int HardwareSerial::available(void) {
return usart_data_available(usart_device); return usart_data_available(usart_device);
} }
int HardwareSerial::peek(void)
{
return usart_peek(usart_device);
}
uint32 HardwareSerial::pending(void) { uint32 HardwareSerial::pending(void) {
return usart_data_pending(usart_device); return usart_data_pending(usart_device);
} }

View File

@ -35,7 +35,7 @@
#include "libmaple_types.h" #include "libmaple_types.h"
#include "usart.h" #include "usart.h"
#include "Print.h" #include "Stream.h"
/* /*
* IMPORTANT: * IMPORTANT:
@ -47,7 +47,7 @@
* the documentation accordingly. * the documentation accordingly.
*/ */
class HardwareSerial : public Print { class HardwareSerial : public Stream {
public: public:
HardwareSerial(usart_dev *usart_device, HardwareSerial(usart_dev *usart_device,
uint8 tx_pin, uint8 tx_pin,
@ -58,10 +58,11 @@ public:
void end(void); void end(void);
/* I/O */ /* I/O */
uint32 available(void); virtual int available(void);
virtual int peek(void);
virtual void flush(void);
uint32 pending(void); uint32 pending(void);
int read(void); virtual int read(void);
void flush(void);
virtual size_t write(unsigned char); virtual size_t write(unsigned char);
using Print::write; using Print::write;

View File

@ -126,6 +126,25 @@ static inline uint8 rb_remove(ring_buffer *rb) {
return ch; return ch;
} }
/*
* Roger Clark. 20141125,
* added peek function.
* @brief Return the first item from a ring buffer, without removing it
* @param rb Buffer to remove from, must contain at least one element.
*/
static inline int rb_peek(ring_buffer *rb)
{
if (rb->head == rb->tail)
{
return -1;
}
else
{
return rb->buf[rb->head];
}
}
/** /**
* @brief Attempt to remove the first item from a ring buffer. * @brief Attempt to remove the first item from a ring buffer.
* *

View File

@ -323,6 +323,18 @@ static inline uint8 usart_getc(usart_dev *dev) {
return rb_remove(&dev->rbRX); return rb_remove(&dev->rbRX);
} }
/*
* Roger Clark. 20141125,
* added peek function.
* @param dev Serial port to read from
* @return byte read
*/
static inline int usart_peek(usart_dev *dev)
{
return rb_peek(&dev->rbRX);
}
/** /**
* @brief Return the amount of data available in a serial port's RX buffer. * @brief Return the amount of data available in a serial port's RX buffer.
* @param dev Serial port to check * @param dev Serial port to check

View File

@ -78,11 +78,11 @@ size_t USBSerial::write(const void *buf, uint32 len) {
return txed; return txed;
} }
uint32 USBSerial::available(void) { int USBSerial::available(void) {
return usbBytesAvailable(); return usbBytesAvailable();
} }
uint32 USBSerial::read(void *buf, uint32 len) { int USBSerial::read(void *buf, uint32 len) {
if (!buf) { if (!buf) {
return 0; return 0;
} }
@ -96,12 +96,38 @@ uint32 USBSerial::read(void *buf, uint32 len) {
} }
/* Blocks forever until 1 byte is received */ /* Blocks forever until 1 byte is received */
uint8 USBSerial::read(void) { int USBSerial::read(void) {
uint8 buf[1]; uint8 buf[1];
this->read(buf, 1); this->read(buf, 1);
return buf[0]; return buf[0];
} }
int USBSerial::peek(void)
{
// ThingToDo : Don't do any thing yet, since F4 doesn't have usb_cdcacm_peek() yet.
/*
uint8 b;
if (usb_cdcacm_peek(&b, 1)==1)
{
return b;
}
else */
{
return -1;
}
}
void USBSerial::flush(void)
{
/*Roger Clark. Rather slow method. Need to improve this */
uint8 b;
while(usbBytesAvailable())
{
this->read(&b, 1);
}
return;
}
uint8 USBSerial::pending(void) { uint8 USBSerial::pending(void) {
return usbGetPending(); return usbGetPending();
} }

View File

@ -31,12 +31,12 @@
#ifndef _USB_SERIAL_H_ #ifndef _USB_SERIAL_H_
#define _USB_SERIAL_H_ #define _USB_SERIAL_H_
#include "Print.h" #include "Stream.h"
/** /**
* @brief Virtual serial terminal. * @brief Virtual serial terminal.
*/ */
class USBSerial : public Print { class USBSerial : public Stream {
public: public:
USBSerial(void); USBSerial(void);
@ -44,10 +44,12 @@ public:
void begin(int); void begin(int);
void end(void); void end(void);
uint32 available(void); virtual int available(void);
virtual int peek(void);
virtual void flush(void);
uint32 read(void *buf, uint32 len); virtual int read(void *buf, uint32 len);
uint8 read(void); virtual int read(void);
size_t write(uint8); size_t write(uint8);
size_t write(const char *str); size_t write(const char *str);