Merge pull request #98 from martinayotte/master
Changes to F4 USBSerial and HardwareSerial to make them derived from Stream instead of Print
This commit is contained in:
commit
32649c83f4
|
@ -138,10 +138,15 @@ int HardwareSerial::read(void) {
|
|||
}
|
||||
}
|
||||
|
||||
uint32 HardwareSerial::available(void) {
|
||||
int HardwareSerial::available(void) {
|
||||
return usart_data_available(usart_device);
|
||||
}
|
||||
|
||||
int HardwareSerial::peek(void)
|
||||
{
|
||||
return usart_peek(usart_device);
|
||||
}
|
||||
|
||||
uint32 HardwareSerial::pending(void) {
|
||||
return usart_data_pending(usart_device);
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include "libmaple_types.h"
|
||||
#include "usart.h"
|
||||
|
||||
#include "Print.h"
|
||||
#include "Stream.h"
|
||||
|
||||
/*
|
||||
* IMPORTANT:
|
||||
|
@ -47,7 +47,7 @@
|
|||
* the documentation accordingly.
|
||||
*/
|
||||
|
||||
class HardwareSerial : public Print {
|
||||
class HardwareSerial : public Stream {
|
||||
public:
|
||||
HardwareSerial(usart_dev *usart_device,
|
||||
uint8 tx_pin,
|
||||
|
@ -58,10 +58,11 @@ public:
|
|||
void end(void);
|
||||
|
||||
/* I/O */
|
||||
uint32 available(void);
|
||||
virtual int available(void);
|
||||
virtual int peek(void);
|
||||
virtual void flush(void);
|
||||
uint32 pending(void);
|
||||
int read(void);
|
||||
void flush(void);
|
||||
virtual int read(void);
|
||||
virtual size_t write(unsigned char);
|
||||
using Print::write;
|
||||
|
||||
|
|
|
@ -126,6 +126,25 @@ static inline uint8 rb_remove(ring_buffer *rb) {
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -323,6 +323,18 @@ static inline uint8 usart_getc(usart_dev *dev) {
|
|||
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.
|
||||
* @param dev Serial port to check
|
||||
|
|
|
@ -78,11 +78,11 @@ size_t USBSerial::write(const void *buf, uint32 len) {
|
|||
return txed;
|
||||
}
|
||||
|
||||
uint32 USBSerial::available(void) {
|
||||
int USBSerial::available(void) {
|
||||
return usbBytesAvailable();
|
||||
}
|
||||
|
||||
uint32 USBSerial::read(void *buf, uint32 len) {
|
||||
int USBSerial::read(void *buf, uint32 len) {
|
||||
if (!buf) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -96,12 +96,38 @@ uint32 USBSerial::read(void *buf, uint32 len) {
|
|||
}
|
||||
|
||||
/* Blocks forever until 1 byte is received */
|
||||
uint8 USBSerial::read(void) {
|
||||
int USBSerial::read(void) {
|
||||
uint8 buf[1];
|
||||
this->read(buf, 1);
|
||||
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) {
|
||||
return usbGetPending();
|
||||
}
|
||||
|
|
|
@ -31,12 +31,12 @@
|
|||
#ifndef _USB_SERIAL_H_
|
||||
#define _USB_SERIAL_H_
|
||||
|
||||
#include "Print.h"
|
||||
#include "Stream.h"
|
||||
|
||||
/**
|
||||
* @brief Virtual serial terminal.
|
||||
*/
|
||||
class USBSerial : public Print {
|
||||
class USBSerial : public Stream {
|
||||
public:
|
||||
USBSerial(void);
|
||||
|
||||
|
@ -44,10 +44,12 @@ public:
|
|||
void begin(int);
|
||||
void end(void);
|
||||
|
||||
uint32 available(void);
|
||||
virtual int available(void);
|
||||
virtual int peek(void);
|
||||
virtual void flush(void);
|
||||
|
||||
uint32 read(void *buf, uint32 len);
|
||||
uint8 read(void);
|
||||
virtual int read(void *buf, uint32 len);
|
||||
virtual int read(void);
|
||||
|
||||
size_t write(uint8);
|
||||
size_t write(const char *str);
|
||||
|
|
Loading…
Reference in New Issue