Software UART program using Infra-Red LED and IR-Detector
Dependents: TestVirtualisation Bf_SoftSerial_IR
SoftSerial_IR.h@14:dc766032cdd6, 2018-12-28 (annotated)
- Committer:
- kenjiArai
- Date:
- Fri Dec 28 10:03:35 2018 +0000
- Revision:
- 14:dc766032cdd6
- Parent:
- 13:2b5649a1278a
- Child:
- 15:8d343be3382d
public release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 13:2b5649a1278a | 1 | // Apply for Infrared LED and IR-Detector |
kenjiArai | 14:dc766032cdd6 | 2 | // Modified by JH1PJL Dec. 28th, 2018 |
kenjiArai | 13:2b5649a1278a | 3 | |
kenjiArai | 12:79d63607bbb1 | 4 | #ifndef SOFTSERIALIR_H |
kenjiArai | 12:79d63607bbb1 | 5 | #define SOFTSERIALIR_H |
Sissors | 0:8edaa7abe724 | 6 | |
Sissors | 0:8edaa7abe724 | 7 | #include "mbed.h" |
kenjiArai | 12:79d63607bbb1 | 8 | #include "SoftSerial_Ticker_IR.h" |
kenjiArai | 13:2b5649a1278a | 9 | |
Sissors | 0:8edaa7abe724 | 10 | /** A software serial implementation |
Sissors | 0:8edaa7abe724 | 11 | * |
Sissors | 0:8edaa7abe724 | 12 | */ |
kenjiArai | 12:79d63607bbb1 | 13 | class SoftSerial_IR: public Stream { |
Sissors | 0:8edaa7abe724 | 14 | |
Sissors | 0:8edaa7abe724 | 15 | public: |
Sissors | 0:8edaa7abe724 | 16 | /** |
Sissors | 0:8edaa7abe724 | 17 | * Constructor |
Sissors | 0:8edaa7abe724 | 18 | * |
Sissors | 0:8edaa7abe724 | 19 | * @param TX Name of the TX pin, NC for not connected |
kenjiArai | 13:2b5649a1278a | 20 | * @param RX Name of the RX pin, NC for not connected, |
kenjiArai | 13:2b5649a1278a | 21 | * must be capable of being InterruptIn |
Sissors | 5:acfd0329f648 | 22 | * @param name Name of the connection |
Sissors | 0:8edaa7abe724 | 23 | */ |
kenjiArai | 12:79d63607bbb1 | 24 | SoftSerial_IR(PinName TX, PinName RX, const char* name = NULL); |
kenjiArai | 12:79d63607bbb1 | 25 | virtual ~SoftSerial_IR(); |
Sissors | 0:8edaa7abe724 | 26 | |
Sissors | 0:8edaa7abe724 | 27 | enum Parity { |
Sissors | 0:8edaa7abe724 | 28 | None = 0, |
Sissors | 0:8edaa7abe724 | 29 | Odd, |
Sissors | 0:8edaa7abe724 | 30 | Even, |
Sissors | 0:8edaa7abe724 | 31 | Forced1, |
Sissors | 0:8edaa7abe724 | 32 | Forced0 |
Sissors | 0:8edaa7abe724 | 33 | }; |
Sissors | 0:8edaa7abe724 | 34 | |
Sissors | 0:8edaa7abe724 | 35 | enum IrqType { |
Sissors | 0:8edaa7abe724 | 36 | RxIrq = 0, |
Sissors | 0:8edaa7abe724 | 37 | TxIrq |
Sissors | 0:8edaa7abe724 | 38 | }; |
Sissors | 0:8edaa7abe724 | 39 | |
Sissors | 0:8edaa7abe724 | 40 | /** Set the transmission format used by the serial port |
Sissors | 0:8edaa7abe724 | 41 | * |
Sissors | 0:8edaa7abe724 | 42 | * @param bits The number of bits in a word (default = 8) |
kenjiArai | 13:2b5649a1278a | 43 | * @param parity The parity used |
kenjiArai | 13:2b5649a1278a | 44 | * (SerialBase::None, SerialBase::Odd, SerialBase::Even, |
kenjiArai | 13:2b5649a1278a | 45 | * SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
Sissors | 0:8edaa7abe724 | 46 | * @param stop The number of stop bits (default = 1) |
Sissors | 0:8edaa7abe724 | 47 | */ |
kenjiArai | 12:79d63607bbb1 | 48 | void format(int bits=8, Parity parity=SoftSerial_IR::None, int stop_bits=1); |
Sissors | 0:8edaa7abe724 | 49 | |
Sissors | 0:8edaa7abe724 | 50 | /** Determine if there is a character available to read |
Sissors | 0:8edaa7abe724 | 51 | * |
Sissors | 0:8edaa7abe724 | 52 | * @returns |
Sissors | 0:8edaa7abe724 | 53 | * 1 if there is a character available to read, |
Sissors | 0:8edaa7abe724 | 54 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 55 | */ |
Sissors | 0:8edaa7abe724 | 56 | int readable(); |
Sissors | 0:8edaa7abe724 | 57 | |
Sissors | 0:8edaa7abe724 | 58 | /** Determine if there is space available to write a character |
Sissors | 0:8edaa7abe724 | 59 | * |
Sissors | 0:8edaa7abe724 | 60 | * @returns |
Sissors | 0:8edaa7abe724 | 61 | * 1 if there is space to write a character, |
Sissors | 0:8edaa7abe724 | 62 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 63 | */ |
Sissors | 0:8edaa7abe724 | 64 | int writeable(); |
Sissors | 0:8edaa7abe724 | 65 | |
Sissors | 0:8edaa7abe724 | 66 | /** Attach a function to call whenever a serial interrupt is generated |
Sissors | 0:8edaa7abe724 | 67 | * |
Sissors | 0:8edaa7abe724 | 68 | * @param fptr A pointer to a void function, or 0 to set as none |
kenjiArai | 13:2b5649a1278a | 69 | * @param type Which serial interrupt to attach the member function to |
kenjiArai | 13:2b5649a1278a | 70 | * (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 71 | */ |
Sissors | 4:c010265ed202 | 72 | void attach(void (*fptr)(void), IrqType type=RxIrq) { |
Sissors | 4:c010265ed202 | 73 | fpointer[type].attach(fptr); |
Sissors | 4:c010265ed202 | 74 | } |
Sissors | 0:8edaa7abe724 | 75 | |
kenjiArai | 13:2b5649a1278a | 76 | /** Attach a member function to call whenever a serial interrupt |
kenjiArai | 13:2b5649a1278a | 77 | * is generated |
Sissors | 0:8edaa7abe724 | 78 | * |
Sissors | 0:8edaa7abe724 | 79 | * @param tptr pointer to the object to call the member function on |
Sissors | 0:8edaa7abe724 | 80 | * @param mptr pointer to the member function to be called |
kenjiArai | 13:2b5649a1278a | 81 | * @param type Which serial interrupt to attach the member function |
kenjiArai | 13:2b5649a1278a | 82 | * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 83 | */ |
Sissors | 0:8edaa7abe724 | 84 | template<typename T> |
Sissors | 4:c010265ed202 | 85 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
Sissors | 4:c010265ed202 | 86 | fpointer[type].attach(tptr, mptr); |
Sissors | 4:c010265ed202 | 87 | } |
Sissors | 0:8edaa7abe724 | 88 | |
Sissors | 0:8edaa7abe724 | 89 | /** Generate a break condition on the serial line |
Sissors | 0:8edaa7abe724 | 90 | */ |
Sissors | 0:8edaa7abe724 | 91 | void send_break(); |
Sissors | 0:8edaa7abe724 | 92 | |
kenjiArai | 13:2b5649a1278a | 93 | /** Set adjust parameter |
kenjiArai | 13:2b5649a1278a | 94 | * |
kenjiArai | 13:2b5649a1278a | 95 | * @param tick_offset Due to timestamp offset, need adjust it |
kenjiArai | 13:2b5649a1278a | 96 | * @param rx_detect_center_offset offset for RX bit Hi&Lo detection timing |
kenjiArai | 13:2b5649a1278a | 97 | */ |
kenjiArai | 13:2b5649a1278a | 98 | void set_parameter(uint32_t tick_offset = 3550 , |
kenjiArai | 13:2b5649a1278a | 99 | int32_t rx_detect_center_offset = -50); |
kenjiArai | 13:2b5649a1278a | 100 | |
Sissors | 0:8edaa7abe724 | 101 | protected: |
kenjiArai | 12:79d63607bbb1 | 102 | PwmOut *tx; |
Sissors | 0:8edaa7abe724 | 103 | InterruptIn *rx; |
kenjiArai | 13:2b5649a1278a | 104 | |
Sissors | 0:8edaa7abe724 | 105 | bool tx_en, rx_en; |
Sissors | 0:8edaa7abe724 | 106 | int bit_period; |
Sissors | 2:9e01a38606b4 | 107 | int _bits, _stop_bits, _total_bits; |
Sissors | 0:8edaa7abe724 | 108 | Parity _parity; |
Sissors | 0:8edaa7abe724 | 109 | |
Sissors | 4:c010265ed202 | 110 | FunctionPointer fpointer[2]; |
kenjiArai | 12:79d63607bbb1 | 111 | |
kenjiArai | 13:2b5649a1278a | 112 | int32_t overhead_us_IR; |
kenjiArai | 13:2b5649a1278a | 113 | uint32_t timestamp_offset; |
kenjiArai | 13:2b5649a1278a | 114 | |
Sissors | 4:c010265ed202 | 115 | //rx |
Sissors | 0:8edaa7abe724 | 116 | void rx_gpio_irq_handler(void); |
Sissors | 1:f8b4b764ace7 | 117 | void rx_handler(void); |
Sissors | 1:f8b4b764ace7 | 118 | int read_buffer, rx_bit; |
Sissors | 1:f8b4b764ace7 | 119 | volatile int out_buffer; |
Sissors | 1:f8b4b764ace7 | 120 | volatile bool out_valid; |
Sissors | 1:f8b4b764ace7 | 121 | bool rx_error; |
kenjiArai | 12:79d63607bbb1 | 122 | FlexTicker_IR rxticker; |
Sissors | 0:8edaa7abe724 | 123 | |
Sissors | 0:8edaa7abe724 | 124 | //tx |
Sissors | 0:8edaa7abe724 | 125 | void tx_handler(void); |
Sissors | 2:9e01a38606b4 | 126 | void prepare_tx(int c); |
kenjiArai | 12:79d63607bbb1 | 127 | FlexTicker_IR txticker; |
Sissors | 0:8edaa7abe724 | 128 | int _char; |
Sissors | 0:8edaa7abe724 | 129 | volatile int tx_bit; |
kenjiArai | 12:79d63607bbb1 | 130 | |
Sissors | 0:8edaa7abe724 | 131 | virtual int _getc(); |
Sissors | 0:8edaa7abe724 | 132 | virtual int _putc(int c); |
kenjiArai | 13:2b5649a1278a | 133 | |
kenjiArai | 13:2b5649a1278a | 134 | void baud(int baudrate); |
Sissors | 0:8edaa7abe724 | 135 | }; |
Sissors | 0:8edaa7abe724 | 136 | |
Sissors | 0:8edaa7abe724 | 137 | #endif |