ワークショップ用のサンプル

Dependencies:   Milkcocoa mbed

Fork of MilkcocoaSampleESP8266 by Junichi Katsu

SoftSerialSendOnly/SoftSerialSendOnry.h

Committer:
jksoft
Date:
2015-12-18
Revision:
0:82d5445a9461

File content as of revision 0:82d5445a9461:

#ifndef SOFTSERIAL_SEND_ONRY_H
#define SOFTSERIAL_SEND_ONRY_H

#include "mbed.h"
#include "SoftSerial_Ticker.h"
/** A software serial implementation
 *
 */
class SoftSerialSendOnry: public Stream {

public:
    /**
    * Constructor
    *
    * @param TX Name of the TX pin, NC for not connected
    * @param name Name of the connection
    */
    SoftSerialSendOnry(PinName TX, const char* name = NULL);
    virtual ~SoftSerialSendOnry();
    
    /** Set the baud rate of the serial port
     *
     *  @param baudrate The baudrate of the serial port (default = 9600).
     */
    void baud(int baudrate);

    enum Parity {
        None = 0,
        Odd,
        Even,
        Forced1,
        Forced0
    };

    enum IrqType {
        RxIrq = 0,
        TxIrq
    };

    /** Set the transmission format used by the serial port
     *
     *  @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)
     *  @param stop The number of stop bits (default = 1)
     */
    void format(int bits=8, Parity parity=SoftSerialSendOnry::None, int stop_bits=1);

    /** Determine if there is space available to write a character
     *
     *  @returns
     *    1 if there is space to write a character,
     *    0 otherwise
     */
    int writeable();

    /** 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)
     */
    void attach(void (*fptr)(void), IrqType type=RxIrq) {
        fpointer[type].attach(fptr);
    }

    /** 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)
     */
    template<typename T>
    void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
        fpointer[type].attach(tptr, mptr);
    }

    /** Generate a break condition on the serial line
     */
    void send_break();

protected:
    DigitalOut *tx;
    
    bool tx_en;
    int bit_period;
    int _bits, _stop_bits, _total_bits;
    Parity _parity;
    
    FunctionPointer fpointer[2];
    
    //tx
    void tx_handler(void);
    void prepare_tx(int c);
    FlexTicker txticker;
    int _char;
    volatile int tx_bit;
    
    
    
    virtual int _getc();
    virtual int _putc(int c);
};


#endif