![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
ワークショップ用に公開しているものです。
Fork of Arch_Analog_Thermistor_Blinker by
SoftSerialSendOnly/SoftSerialSendOnry.h@9:5a7dca00d87b, 2016-02-27 (annotated)
- Committer:
- jksoft
- Date:
- Sat Feb 27 05:38:49 2016 +0000
- Revision:
- 9:5a7dca00d87b
????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 9:5a7dca00d87b | 1 | #ifndef SOFTSERIAL_SEND_ONRY_H |
jksoft | 9:5a7dca00d87b | 2 | #define SOFTSERIAL_SEND_ONRY_H |
jksoft | 9:5a7dca00d87b | 3 | |
jksoft | 9:5a7dca00d87b | 4 | #include "mbed.h" |
jksoft | 9:5a7dca00d87b | 5 | #include "SoftSerial_Ticker.h" |
jksoft | 9:5a7dca00d87b | 6 | /** A software serial implementation |
jksoft | 9:5a7dca00d87b | 7 | * |
jksoft | 9:5a7dca00d87b | 8 | */ |
jksoft | 9:5a7dca00d87b | 9 | class SoftSerialSendOnry: public Stream { |
jksoft | 9:5a7dca00d87b | 10 | |
jksoft | 9:5a7dca00d87b | 11 | public: |
jksoft | 9:5a7dca00d87b | 12 | /** |
jksoft | 9:5a7dca00d87b | 13 | * Constructor |
jksoft | 9:5a7dca00d87b | 14 | * |
jksoft | 9:5a7dca00d87b | 15 | * @param TX Name of the TX pin, NC for not connected |
jksoft | 9:5a7dca00d87b | 16 | * @param name Name of the connection |
jksoft | 9:5a7dca00d87b | 17 | */ |
jksoft | 9:5a7dca00d87b | 18 | SoftSerialSendOnry(PinName TX, const char* name = NULL); |
jksoft | 9:5a7dca00d87b | 19 | virtual ~SoftSerialSendOnry(); |
jksoft | 9:5a7dca00d87b | 20 | |
jksoft | 9:5a7dca00d87b | 21 | /** Set the baud rate of the serial port |
jksoft | 9:5a7dca00d87b | 22 | * |
jksoft | 9:5a7dca00d87b | 23 | * @param baudrate The baudrate of the serial port (default = 9600). |
jksoft | 9:5a7dca00d87b | 24 | */ |
jksoft | 9:5a7dca00d87b | 25 | void baud(int baudrate); |
jksoft | 9:5a7dca00d87b | 26 | |
jksoft | 9:5a7dca00d87b | 27 | enum Parity { |
jksoft | 9:5a7dca00d87b | 28 | None = 0, |
jksoft | 9:5a7dca00d87b | 29 | Odd, |
jksoft | 9:5a7dca00d87b | 30 | Even, |
jksoft | 9:5a7dca00d87b | 31 | Forced1, |
jksoft | 9:5a7dca00d87b | 32 | Forced0 |
jksoft | 9:5a7dca00d87b | 33 | }; |
jksoft | 9:5a7dca00d87b | 34 | |
jksoft | 9:5a7dca00d87b | 35 | enum IrqType { |
jksoft | 9:5a7dca00d87b | 36 | RxIrq = 0, |
jksoft | 9:5a7dca00d87b | 37 | TxIrq |
jksoft | 9:5a7dca00d87b | 38 | }; |
jksoft | 9:5a7dca00d87b | 39 | |
jksoft | 9:5a7dca00d87b | 40 | /** Set the transmission format used by the serial port |
jksoft | 9:5a7dca00d87b | 41 | * |
jksoft | 9:5a7dca00d87b | 42 | * @param bits The number of bits in a word (default = 8) |
jksoft | 9:5a7dca00d87b | 43 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
jksoft | 9:5a7dca00d87b | 44 | * @param stop The number of stop bits (default = 1) |
jksoft | 9:5a7dca00d87b | 45 | */ |
jksoft | 9:5a7dca00d87b | 46 | void format(int bits=8, Parity parity=SoftSerialSendOnry::None, int stop_bits=1); |
jksoft | 9:5a7dca00d87b | 47 | |
jksoft | 9:5a7dca00d87b | 48 | /** Determine if there is space available to write a character |
jksoft | 9:5a7dca00d87b | 49 | * |
jksoft | 9:5a7dca00d87b | 50 | * @returns |
jksoft | 9:5a7dca00d87b | 51 | * 1 if there is space to write a character, |
jksoft | 9:5a7dca00d87b | 52 | * 0 otherwise |
jksoft | 9:5a7dca00d87b | 53 | */ |
jksoft | 9:5a7dca00d87b | 54 | int writeable(); |
jksoft | 9:5a7dca00d87b | 55 | |
jksoft | 9:5a7dca00d87b | 56 | /** Attach a function to call whenever a serial interrupt is generated |
jksoft | 9:5a7dca00d87b | 57 | * |
jksoft | 9:5a7dca00d87b | 58 | * @param fptr A pointer to a void function, or 0 to set as none |
jksoft | 9:5a7dca00d87b | 59 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
jksoft | 9:5a7dca00d87b | 60 | */ |
jksoft | 9:5a7dca00d87b | 61 | void attach(void (*fptr)(void), IrqType type=RxIrq) { |
jksoft | 9:5a7dca00d87b | 62 | fpointer[type].attach(fptr); |
jksoft | 9:5a7dca00d87b | 63 | } |
jksoft | 9:5a7dca00d87b | 64 | |
jksoft | 9:5a7dca00d87b | 65 | /** Attach a member function to call whenever a serial interrupt is generated |
jksoft | 9:5a7dca00d87b | 66 | * |
jksoft | 9:5a7dca00d87b | 67 | * @param tptr pointer to the object to call the member function on |
jksoft | 9:5a7dca00d87b | 68 | * @param mptr pointer to the member function to be called |
jksoft | 9:5a7dca00d87b | 69 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
jksoft | 9:5a7dca00d87b | 70 | */ |
jksoft | 9:5a7dca00d87b | 71 | template<typename T> |
jksoft | 9:5a7dca00d87b | 72 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
jksoft | 9:5a7dca00d87b | 73 | fpointer[type].attach(tptr, mptr); |
jksoft | 9:5a7dca00d87b | 74 | } |
jksoft | 9:5a7dca00d87b | 75 | |
jksoft | 9:5a7dca00d87b | 76 | /** Generate a break condition on the serial line |
jksoft | 9:5a7dca00d87b | 77 | */ |
jksoft | 9:5a7dca00d87b | 78 | void send_break(); |
jksoft | 9:5a7dca00d87b | 79 | |
jksoft | 9:5a7dca00d87b | 80 | protected: |
jksoft | 9:5a7dca00d87b | 81 | DigitalOut *tx; |
jksoft | 9:5a7dca00d87b | 82 | |
jksoft | 9:5a7dca00d87b | 83 | bool tx_en; |
jksoft | 9:5a7dca00d87b | 84 | int bit_period; |
jksoft | 9:5a7dca00d87b | 85 | int _bits, _stop_bits, _total_bits; |
jksoft | 9:5a7dca00d87b | 86 | Parity _parity; |
jksoft | 9:5a7dca00d87b | 87 | |
jksoft | 9:5a7dca00d87b | 88 | FunctionPointer fpointer[2]; |
jksoft | 9:5a7dca00d87b | 89 | |
jksoft | 9:5a7dca00d87b | 90 | //tx |
jksoft | 9:5a7dca00d87b | 91 | void tx_handler(void); |
jksoft | 9:5a7dca00d87b | 92 | void prepare_tx(int c); |
jksoft | 9:5a7dca00d87b | 93 | FlexTicker txticker; |
jksoft | 9:5a7dca00d87b | 94 | int _char; |
jksoft | 9:5a7dca00d87b | 95 | volatile int tx_bit; |
jksoft | 9:5a7dca00d87b | 96 | |
jksoft | 9:5a7dca00d87b | 97 | |
jksoft | 9:5a7dca00d87b | 98 | |
jksoft | 9:5a7dca00d87b | 99 | virtual int _getc(); |
jksoft | 9:5a7dca00d87b | 100 | virtual int _putc(int c); |
jksoft | 9:5a7dca00d87b | 101 | }; |
jksoft | 9:5a7dca00d87b | 102 | |
jksoft | 9:5a7dca00d87b | 103 | |
jksoft | 9:5a7dca00d87b | 104 | #endif |
jksoft | 9:5a7dca00d87b | 105 |