Software UART program using Infra-Red LED and IR-Detector
Dependents: TestVirtualisation Bf_SoftSerial_IR
Diff: SoftSerial_IR.h
- Revision:
- 15:8d343be3382d
- Parent:
- 14:dc766032cdd6
--- a/SoftSerial_IR.h Fri Dec 28 10:03:35 2018 +0000 +++ b/SoftSerial_IR.h Fri May 15 04:11:01 2020 +0000 @@ -1,29 +1,54 @@ -// Apply for Infrared LED and IR-Detector -// Modified by JH1PJL Dec. 28th, 2018 +/* + Original Library by Erik- & SonyPony + https://os.mbed.com/users/Sissors/code/SoftSerial/ + + Modified by K.Arai / JH1PJL May 15th, 2020 -#ifndef SOFTSERIALIR_H -#define SOFTSERIALIR_H + modified functions + tx_handler() + prepare_tx() + prime() + rx_gpio_irq_handler() and others + */ + +#ifndef SOFTSERIAL_IR_H +#define SOFTSERIAL_IR_H #include "mbed.h" #include "SoftSerial_Ticker_IR.h" +#define DEBUG_TIMING 2 // 0=no debug, 1=tx, 2=rx + +#if (MBED_MAJOR_VERSION == 2) +# define YIELD {;} +#elif (MBED_MAJOR_VERSION == 5) +# define YIELD {ThisThread::yield();} +#endif + /** A software serial implementation * */ -class SoftSerial_IR: public Stream { +class SoftSerial_IR: public Stream +{ public: /** * Constructor * * @param TX Name of the TX pin, NC for not connected - * @param RX Name of the RX pin, NC for not connected, - * must be capable of being InterruptIn + * @param RX Name of the RX pin, NC for not connected, + * must be capable of being InterruptIn * @param name Name of the connection */ SoftSerial_IR(PinName TX, PinName RX, const char* name = NULL); virtual ~SoftSerial_IR(); + /** Set the baud rate of the serial port + * + * @param baudrate The baudrate of the serial port (default = 2400). + */ + void baud(int baudrate); + enum Parity { None = 0, Odd, @@ -41,8 +66,9 @@ * * @param bits The number of bits in a word (default = 8) * @param parity The parity used - * (SerialBase::None, SerialBase::Odd, SerialBase::Even, - * SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) + * (SoftSerial_IR::None, SoftSerial_IR::Odd, SoftSerial_IR::Even, + * SoftSerial_IR::Forced1, SoftSerial_IR::Forced0; + * default = SoftSerial_IR::None) * @param stop The number of stop bits (default = 1) */ void format(int bits=8, Parity parity=SoftSerial_IR::None, int stop_bits=1); @@ -66,51 +92,43 @@ /** Attach a function to call whenever a serial interrupt is generated * * @param fptr A pointer to a void function, or 0 to set as none - * @param type Which serial interrupt to attach the member function to - * (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + * @param type Which serial interrupt to attach the member function + * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) */ - void attach(void (*fptr)(void), IrqType type=RxIrq) { - fpointer[type].attach(fptr); + void attach(void (*fptr)(void), IrqType type=RxIrq) + { + fpointer[type] = Callback<void()>(fptr); } - /** Attach a member function to call whenever a serial interrupt - * is generated + /** Attach a member function to call + * whenever a serial interrupt is generated * * @param tptr pointer to the object to call the member function on * @param mptr pointer to the member function to be called * @param type Which serial interrupt to attach the member function - * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) */ template<typename T> - void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - fpointer[type].attach(tptr, mptr); + void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) + { + fpointer[type] = Callback<void()>(tptr, mptr); } /** Generate a break condition on the serial line */ void send_break(); - /** Set adjust parameter - * - * @param tick_offset Due to timestamp offset, need adjust it - * @param rx_detect_center_offset offset for RX bit Hi&Lo detection timing - */ - void set_parameter(uint32_t tick_offset = 3550 , - int32_t rx_detect_center_offset = -50); - protected: + //DigitalOut *tx; PwmOut *tx; InterruptIn *rx; - + bool tx_en, rx_en; int bit_period; int _bits, _stop_bits, _total_bits; Parity _parity; - - FunctionPointer fpointer[2]; - int32_t overhead_us_IR; - uint32_t timestamp_offset; + Callback<void()> fpointer[2]; //rx void rx_gpio_irq_handler(void); @@ -119,19 +137,20 @@ volatile int out_buffer; volatile bool out_valid; bool rx_error; - FlexTicker_IR rxticker; - + FlexTicker rxticker; + + bool rx_1st; + uint32_t rx_st_time; + //tx void tx_handler(void); void prepare_tx(int c); - FlexTicker_IR txticker; + FlexTicker txticker; int _char; volatile int tx_bit; virtual int _getc(); virtual int _putc(int c); - - void baud(int baudrate); }; #endif