tetete

Dependencies:   FIFO

Files at this revision

API Documentation at this revision

Comitter:
babylonica
Date:
Thu Mar 30 13:13:01 2017 +0000
Parent:
4:c7555051a851
Child:
6:08e3b1bba0ac
Commit message:
Version Update.

Changed in this revision

AsyncSerial.cpp Show annotated file Show diff for this revision Revisions of this file
AsyncSerial.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/AsyncSerial.cpp	Thu Mar 30 12:58:56 2017 +0000
+++ b/AsyncSerial.cpp	Thu Mar 30 13:13:01 2017 +0000
@@ -1,195 +1,195 @@
-// -*- coding: utf-8 -*-
-/**
- @file		AsyncSerial.cpp
- @brief	 	Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
- 
- @author	T.Kawamura
- @version	1.0
- @date		2017-03-29	T.Kawamura	Written for C++/mbed.
- @date		2017-03-30	T.Kawamura	Bug Fixed: Cannot use format(), baud().
- 
- @see 
- Copyright (C) 2017 T.Kawamura.
- Released under the MIT license.
- http://opensource.org/licenses/mit-license.php
- 
-*/
-
-#include "AsyncSerial.hpp"
-
-AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size) : RawSerial(txpin, rxpin, baudrate), fifo_tx(buffer_size), fifo_rx(buffer_size){
-	//Initialize ISR
-	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
-	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
-
-	fifo_tx.clear();
-	fifo_rx.clear();
-	
-	Is_Serial_Sending = false;
-
-	return;
-}
-
-AsyncSerial::~AsyncSerial(){
-	RawSerial::attach(NULL, RawSerial::TxIrq);
-	RawSerial::attach(NULL, RawSerial::RxIrq);
-
-	return;
-}
-
-void AsyncSerial::ISR_TX(void){
-	int data;
-
-	if( fifo_tx.available() > 0 ){
-		data = (int)fifo_tx.get();
-		RawSerial::putc(data);
-	}else{
-		Is_Serial_Sending = false;
-	}	
-}
-
-void AsyncSerial::ISR_RX(void){
-	uint8_t data;
-
-	data = (uint8_t)RawSerial::getc();
-	fifo_rx.put(data);
-}
-
-int AsyncSerial::readable(void){
-	return (int)fifo_rx.available();
-}
-
-int AsyncSerial::writeable(void){
-	return 1;
-}
-
-int AsyncSerial::getc(void){
-	return (int)fifo_rx.get();
-}
-
-int AsyncSerial::peekc(void){
-	return (int)fifo_rx.peek();
-}
-
-int AsyncSerial::putc(int c){
-	int status;
-
-	if( Is_Serial_Sending ){
-		status = fifo_tx.put((uint8_t)c);
-		if( status != 0 ){
-			return 1;
-		}else{
-			return 0;
-		}
-	}else{
-		Is_Serial_Sending = true;
-		RawSerial::putc(c);
-	}
-	return 1;
-}
-
-int AsyncSerial::puts(const char *str){
-	uint8_t temp;
-	int status = 0;
-
-	for(uint32_t i = 0; i < strlen(str); i++){
-		temp = (uint8_t)str[i];
-		status = fifo_tx.put(temp);
-	}
-
-	if( !Is_Serial_Sending ){
-		Is_Serial_Sending = true;
-		RawSerial::putc((int)fifo_tx.get());
-	}
-
-	if( status == 0 ){
-		return 0;
-	}
-
-	AsyncSerial::putc('\n');
-	return 1;
-}
-
-int AsyncSerial::printf(const char *format, ...){
-	int32_t wrote_length = 0;
-	char string_buffer[PRINTF_STRING_BUFFER_SIZE];
-
-	memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE);
-	
-	va_list arg;
-	va_start(arg, format);
-	wrote_length = vsprintf(string_buffer, format, arg);
-	
-	if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) {
-		error("%s @ %d : String is too large, string buffer overwrite. (Max buffer size: %d Wrote length: %d)\n", __FILE__, __LINE__, PRINTF_STRING_BUFFER_SIZE, wrote_length);
-		va_end(arg);
-		return 0;
-	}
-
-	if( wrote_length < 0 ){
-		va_end(arg);
-		error("Function vsprintf() was failed.");
-		return 0;
-	}
-
-	va_end(arg);
-	wrote_length = AsyncSerial::write((uint8_t*)string_buffer, wrote_length);
-	
-	return (uint16_t)wrote_length;
-}
-
-int AsyncSerial::write(const uint8_t *buffer, int length){
-	uint8_t temp;
-	int status;
-	
-	if ( length < 1 ){
-		return 0;
-	}
-
-	for(uint32_t i = 0; i < length; i++){
-		temp = (uint8_t)buffer[i];
-		status = fifo_tx.put(temp);
-	}
-
-	if( !Is_Serial_Sending ){
-		Is_Serial_Sending = true;
-		RawSerial::putc((int)fifo_tx.get());
-	}
-
-	if( status == 0 ){
-		return 0;
-	}
-
-	return 1;	
-}
-
-void AsyncSerial::flush(void){
-	fifo_rx.clear();
-	return;
-}
-
-void AsyncSerial::wait(void){
-	while( fifo_rx.available() > 0 ){}
-	return;
-}
-
-void AsyncSerial::format(int bits, Parity parity, int stop_bits){
-	RawSerial::attach(NULL, RawSerial::TxIrq);
-	RawSerial::attach(NULL, RawSerial::RxIrq);
-
-	RawSerial::format(bits, parity, stop_bits);
-
-	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
-	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
-}
-
-void AsyncSerial::baud(int baudrate){
-	RawSerial::attach(NULL, RawSerial::TxIrq);
-	RawSerial::attach(NULL, RawSerial::RxIrq);
-
-	RawSerial::baud(baudrate);
-
-	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
-	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);	
-}
+// -*- coding: utf-8 -*-
+/**
+ @file		AsyncSerial.cpp
+ @brief	 	Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
+ 
+ @author	T.Kawamura
+ @version	1.1
+ @date		2017-03-29	T.Kawamura	Written for C++/mbed.
+ @date		2017-03-30	T.Kawamura	Bug Fixed: Cannot use format(), baud().
+ 
+ @see 
+ Copyright (C) 2017 T.Kawamura.
+ Released under the MIT license.
+ http://opensource.org/licenses/mit-license.php
+ 
+*/
+
+#include "AsyncSerial.hpp"
+
+AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size) : RawSerial(txpin, rxpin, baudrate), fifo_tx(buffer_size), fifo_rx(buffer_size){
+	//Initialize ISR
+	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
+	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
+
+	fifo_tx.clear();
+	fifo_rx.clear();
+	
+	Is_Serial_Sending = false;
+
+	return;
+}
+
+AsyncSerial::~AsyncSerial(){
+	RawSerial::attach(NULL, RawSerial::TxIrq);
+	RawSerial::attach(NULL, RawSerial::RxIrq);
+
+	return;
+}
+
+void AsyncSerial::ISR_TX(void){
+	int data;
+
+	if( fifo_tx.available() > 0 ){
+		data = (int)fifo_tx.get();
+		RawSerial::putc(data);
+	}else{
+		Is_Serial_Sending = false;
+	}	
+}
+
+void AsyncSerial::ISR_RX(void){
+	uint8_t data;
+
+	data = (uint8_t)RawSerial::getc();
+	fifo_rx.put(data);
+}
+
+int AsyncSerial::readable(void){
+	return (int)fifo_rx.available();
+}
+
+int AsyncSerial::writeable(void){
+	return 1;
+}
+
+int AsyncSerial::getc(void){
+	return (int)fifo_rx.get();
+}
+
+int AsyncSerial::peekc(void){
+	return (int)fifo_rx.peek();
+}
+
+int AsyncSerial::putc(int c){
+	int status;
+
+	if( Is_Serial_Sending ){
+		status = fifo_tx.put((uint8_t)c);
+		if( status != 0 ){
+			return 1;
+		}else{
+			return 0;
+		}
+	}else{
+		Is_Serial_Sending = true;
+		RawSerial::putc(c);
+	}
+	return 1;
+}
+
+int AsyncSerial::puts(const char *str){
+	uint8_t temp;
+	int status = 0;
 
+	for(uint32_t i = 0; i < strlen(str); i++){
+		temp = (uint8_t)str[i];
+		status = fifo_tx.put(temp);
+	}
+
+	if( !Is_Serial_Sending ){
+		Is_Serial_Sending = true;
+		RawSerial::putc((int)fifo_tx.get());
+	}
+
+	if( status == 0 ){
+		return 0;
+	}
+
+	AsyncSerial::putc('\n');
+	return 1;
+}
+
+int AsyncSerial::printf(const char *format, ...){
+	int32_t wrote_length = 0;
+	char string_buffer[PRINTF_STRING_BUFFER_SIZE];
+
+	memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE);
+	
+	va_list arg;
+	va_start(arg, format);
+	wrote_length = vsprintf(string_buffer, format, arg);
+	
+	if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) {
+		error("%s @ %d : String is too large, string buffer overwrite. (Max buffer size: %d Wrote length: %d)\n", __FILE__, __LINE__, PRINTF_STRING_BUFFER_SIZE, wrote_length);
+		va_end(arg);
+		return 0;
+	}
+
+	if( wrote_length < 0 ){
+		va_end(arg);
+		error("Function vsprintf() was failed.");
+		return 0;
+	}
+
+	va_end(arg);
+	wrote_length = AsyncSerial::write((uint8_t*)string_buffer, wrote_length);
+	
+	return (uint16_t)wrote_length;
+}
+
+int AsyncSerial::write(const uint8_t *buffer, int length){
+	uint8_t temp;
+	int status;
+	
+	if ( length < 1 ){
+		return 0;
+	}
+
+	for(uint32_t i = 0; i < length; i++){
+		temp = (uint8_t)buffer[i];
+		status = fifo_tx.put(temp);
+	}
+
+	if( !Is_Serial_Sending ){
+		Is_Serial_Sending = true;
+		RawSerial::putc((int)fifo_tx.get());
+	}
+
+	if( status == 0 ){
+		return 0;
+	}
+
+	return 1;	
+}
+
+void AsyncSerial::flush(void){
+	fifo_rx.clear();
+	return;
+}
+
+void AsyncSerial::wait(void){
+	while( fifo_rx.available() > 0 ){}
+	return;
+}
+
+void AsyncSerial::format(int bits, Parity parity, int stop_bits){
+	RawSerial::attach(NULL, RawSerial::TxIrq);
+	RawSerial::attach(NULL, RawSerial::RxIrq);
+
+	RawSerial::format(bits, parity, stop_bits);
+
+	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
+	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
+}
+
+void AsyncSerial::baud(int baudrate){
+	RawSerial::attach(NULL, RawSerial::TxIrq);
+	RawSerial::attach(NULL, RawSerial::RxIrq);
+
+	RawSerial::baud(baudrate);
+
+	RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
+	RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);	
+}
+
--- a/AsyncSerial.hpp	Thu Mar 30 12:58:56 2017 +0000
+++ b/AsyncSerial.hpp	Thu Mar 30 13:13:01 2017 +0000
@@ -1,154 +1,154 @@
-// -*- coding: utf-8 -*-
-/**
- @file		AsyncSerial.cpp
- @brief	 	Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
- 
- @author	T.Kawamura
- @version	1.0
- @date		2017-03-29	T.Kawamura	Written for C++/mbed.
- @date		2017-03-30	T.Kawamura	Bug Fixed: Cannot use format(), baud().
-
- @see 
- Copyright (C) 2017 T.Kawamura.
- Released under the MIT license.
- http://opensource.org/licenses/mit-license.php
- 
-*/
-
-#ifndef ASYNCSERIAL_H
-#define ASYNCSERIAL_H
-
-#include "mbed.h"
-#include "FIFO.hpp"
-#include <stdarg.h>
-#include <string.h>
-
-using namespace std;
-
-#define PRINTF_STRING_BUFFER_SIZE 256
-
-/**
-	@class	AsyncSerial
-  @brief	Asynchronous Serial Communication with FIFO
-*/
-class AsyncSerial : public RawSerial{
-private:
-
-	// FIFO is uint8_t(unsigned char) buffer;
-	FIFO<uint8_t> fifo_tx;
-	FIFO<uint8_t> fifo_rx;
-
-	bool Is_Serial_Sending;
-
-	void ISR_TX(void);
-	void ISR_RX(void);
-
-public:
-
-	/**
-		 @brief	Create a new AsyncSerial Port.
-		 @param	txpin	Tx pin name (Defined in PinName.h)
-		 @param	rxpin	Rx pin name (Defined in PinName.h)
-		 @param	baudrate	Baudrate (ex: 115200). Default value is 9600.
-		 @param	buffer_size	Buffer size. Default value is 256. (byte)
-	*/
-	AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256);
-	/**
-		 @brief	Disable the AsyncSerial Port.
-		 @param	No parameters.
-	*/
-	virtual ~AsyncSerial(void);
-
-	/**
-		 @brief	Get how many bytes are in the rx buffer.
-		 @param	No parameters.
-		 @return	Size of readable data. (byte)
-	*/
-	virtual int readable(void);
-
-	/**
-		 @brief	Check writeable or not.
-		 @param	No Parameters.
-		 @retval	1	Always return 1 because this library provides asynchronous serial.
-	*/
-	virtual int writeable(void);
-
-	/**
-		 @brief	Get 1byte from the AsyncSerial port.
-		 @param	No parameters.
-		 @retval	All Got Data
-		 @retval	0	Error.
-	*/
-	virtual int getc(void);
-
-	/**
-		 @brief	Peek 1byte from the AsyncSerial port.
-		 @param	No parameters.
-		 @retval	ALL	Got Data
-		 @retval	0	Error.
-	*/
-	virtual int peekc(void);
-
-	/**
-		 @brief	Put 1byte to the AsyncSerial port.
-		 @param	data	A Data for put
-		 @retval	0	Error.
-		 @retval	1	Success.
-	*/
-	virtual int putc(int c);
-
-	/**
-		 @brief	Write a string with new line. The string must be NULL terminated.
-		 @param	*str	A String for write (Must be NULL terminated).
-		 @retval	0	Error.
-		 @retval	1	Success.
-	*/
-	virtual int puts(const char *str);
-
-	/**
-		 @brief	Write a formatted string to the AsyncSerial port. 
-		 @param	*format	A Formatted string for write.
-		 @retval	0	Error.
-		 @retval	1+	Wrote string size (byte).
-	*/
-	virtual int printf(const char *format, ...);
-
-	/**
-		 @brief	Write byte array to the AsyncSerial port.
-		 @param	*s	A pointer to the array for write.
-		 @param	length	Write size (byte).
-		 @retval	0	Error.
-		 @retval	1	Success.
-	*/
-	virtual int write(const uint8_t *buffer, int length);
-
-	/**
-		 @brief	Clear the rx buffer by compulsion.
-		 @param	No parameters.
-	*/
-	virtual void flush(void);
-
-	/**
-		 @brief	Wait until finish all sending.
-		 @param	No parameters.
-	*/
-	virtual void wait(void);
-
-	/**
-		 @brief	Set bits, parity and stop bits.
-		 @param	bits	Bits (5 ~ 8)
-		 @param	parity	Parity
-		 @param	stop_bits	Stop bits (1 or 2)
-	*/
-	virtual void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
-
-	/**
-		 @brief	Set baud rate.
-		 @param	baudrate	baudrate (bps).
-	*/
-	virtual void baud(int baudrate);
-
-};
-
-#endif
+// -*- coding: utf-8 -*-
+/**
+ @file		AsyncSerial.hpp
+ @brief	 	Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
+ 
+ @author	T.Kawamura
+ @version	1.1
+ @date		2017-03-29	T.Kawamura	Written for C++/mbed.
+ @date		2017-03-30	T.Kawamura	Bug Fixed: Cannot use format(), baud().
+
+ @see 
+ Copyright (C) 2017 T.Kawamura.
+ Released under the MIT license.
+ http://opensource.org/licenses/mit-license.php
+ 
+*/
+
+#ifndef ASYNCSERIAL_H
+#define ASYNCSERIAL_H
+
+#include "mbed.h"
+#include "FIFO.hpp"
+#include <stdarg.h>
+#include <string.h>
+
+using namespace std;
+
+#define PRINTF_STRING_BUFFER_SIZE 256
+
+/**
+	@class	AsyncSerial
+  @brief	Asynchronous Serial Communication with FIFO
+*/
+class AsyncSerial : public RawSerial{
+private:
+
+	// FIFO is uint8_t(unsigned char) buffer;
+	FIFO<uint8_t> fifo_tx;
+	FIFO<uint8_t> fifo_rx;
+
+	bool Is_Serial_Sending;
+
+	void ISR_TX(void);
+	void ISR_RX(void);
+
+public:
+
+	/**
+		 @brief	Create a new AsyncSerial Port.
+		 @param	txpin	Tx pin name (Defined in PinName.h)
+		 @param	rxpin	Rx pin name (Defined in PinName.h)
+		 @param	baudrate	Baudrate (ex: 115200). Default value is 9600.
+		 @param	buffer_size	Buffer size. Default value is 256. (byte)
+	*/
+	AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256);
+	/**
+		 @brief	Disable the AsyncSerial Port.
+		 @param	No parameters.
+	*/
+	virtual ~AsyncSerial(void);
+
+	/**
+		 @brief	Get how many bytes are in the rx buffer.
+		 @param	No parameters.
+		 @return	Size of readable data. (byte)
+	*/
+	virtual int readable(void);
+
+	/**
+		 @brief	Check writeable or not.
+		 @param	No Parameters.
+		 @retval	1	Always return 1 because this library provides asynchronous serial.
+	*/
+	virtual int writeable(void);
 
+	/**
+		 @brief	Get 1byte from the AsyncSerial port.
+		 @param	No parameters.
+		 @retval	All Got Data
+		 @retval	0	Error.
+	*/
+	virtual int getc(void);
+
+	/**
+		 @brief	Peek 1byte from the AsyncSerial port.
+		 @param	No parameters.
+		 @retval	ALL	Got Data
+		 @retval	0	Error.
+	*/
+	virtual int peekc(void);
+
+	/**
+		 @brief	Put 1byte to the AsyncSerial port.
+		 @param	data	A Data for put
+		 @retval	0	Error.
+		 @retval	1	Success.
+	*/
+	virtual int putc(int c);
+
+	/**
+		 @brief	Write a string with new line. The string must be NULL terminated.
+		 @param	*str	A String for write (Must be NULL terminated).
+		 @retval	0	Error.
+		 @retval	1	Success.
+	*/
+	virtual int puts(const char *str);
+
+	/**
+		 @brief	Write a formatted string to the AsyncSerial port. 
+		 @param	*format	A Formatted string for write.
+		 @retval	0	Error.
+		 @retval	1+	Wrote string size (byte).
+	*/
+	virtual int printf(const char *format, ...);
+
+	/**
+		 @brief	Write byte array to the AsyncSerial port.
+		 @param	*s	A pointer to the array for write.
+		 @param	length	Write size (byte).
+		 @retval	0	Error.
+		 @retval	1	Success.
+	*/
+	virtual int write(const uint8_t *buffer, int length);
+
+	/**
+		 @brief	Clear the rx buffer by compulsion.
+		 @param	No parameters.
+	*/
+	virtual void flush(void);
+
+	/**
+		 @brief	Wait until finish all sending.
+		 @param	No parameters.
+	*/
+	virtual void wait(void);
+
+	/**
+		 @brief	Set bits, parity and stop bits.
+		 @param	bits	Bits (5 ~ 8)
+		 @param	parity	Parity
+		 @param	stop_bits	Stop bits (1 or 2)
+	*/
+	virtual void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
+
+	/**
+		 @brief	Set baud rate.
+		 @param	baudrate	baudrate (bps).
+	*/
+	virtual void baud(int baudrate);
+
+};
+
+#endif
+