Christopher Foley
/
ESE519_WIFI_CONTROLLER
a
Fork of ESE519_Lab6_part1_skeleton by
esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedSerial.h@12:ea030e3181d3, 2019-11-01 (annotated)
- Committer:
- hydroguy45
- Date:
- Fri Nov 01 18:52:45 2019 +0000
- Revision:
- 12:ea030e3181d3
- Parent:
- 7:ca226305d28b
Cleaned up the header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hydroguy45 | 7:ca226305d28b | 1 | |
hydroguy45 | 7:ca226305d28b | 2 | /** |
hydroguy45 | 7:ca226305d28b | 3 | * @file BufferedSerial.h |
hydroguy45 | 7:ca226305d28b | 4 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
hydroguy45 | 7:ca226305d28b | 5 | * @author sam grove |
hydroguy45 | 7:ca226305d28b | 6 | * @version 1.0 |
hydroguy45 | 7:ca226305d28b | 7 | * @see |
hydroguy45 | 7:ca226305d28b | 8 | * |
hydroguy45 | 7:ca226305d28b | 9 | * Copyright (c) 2013 |
hydroguy45 | 7:ca226305d28b | 10 | * |
hydroguy45 | 7:ca226305d28b | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hydroguy45 | 7:ca226305d28b | 12 | * you may not use this file except in compliance with the License. |
hydroguy45 | 7:ca226305d28b | 13 | * You may obtain a copy of the License at |
hydroguy45 | 7:ca226305d28b | 14 | * |
hydroguy45 | 7:ca226305d28b | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
hydroguy45 | 7:ca226305d28b | 16 | * |
hydroguy45 | 7:ca226305d28b | 17 | * Unless required by applicable law or agreed to in writing, software |
hydroguy45 | 7:ca226305d28b | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
hydroguy45 | 7:ca226305d28b | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hydroguy45 | 7:ca226305d28b | 20 | * See the License for the specific language governing permissions and |
hydroguy45 | 7:ca226305d28b | 21 | * limitations under the License. |
hydroguy45 | 7:ca226305d28b | 22 | */ |
hydroguy45 | 7:ca226305d28b | 23 | |
hydroguy45 | 7:ca226305d28b | 24 | #ifndef BUFFEREDSERIAL_H |
hydroguy45 | 7:ca226305d28b | 25 | #define BUFFEREDSERIAL_H |
hydroguy45 | 7:ca226305d28b | 26 | |
hydroguy45 | 7:ca226305d28b | 27 | #include "mbed.h" |
hydroguy45 | 7:ca226305d28b | 28 | #include "MyBuffer.h" |
hydroguy45 | 7:ca226305d28b | 29 | |
hydroguy45 | 7:ca226305d28b | 30 | /** A serial port (UART) for communication with other serial devices |
hydroguy45 | 7:ca226305d28b | 31 | * |
hydroguy45 | 7:ca226305d28b | 32 | * Can be used for Full Duplex communication, or Simplex by specifying |
hydroguy45 | 7:ca226305d28b | 33 | * one pin as NC (Not Connected) |
hydroguy45 | 7:ca226305d28b | 34 | * |
hydroguy45 | 7:ca226305d28b | 35 | * Example: |
hydroguy45 | 7:ca226305d28b | 36 | * @code |
hydroguy45 | 7:ca226305d28b | 37 | * #include "mbed.h" |
hydroguy45 | 7:ca226305d28b | 38 | * #include "BufferedSerial.h" |
hydroguy45 | 7:ca226305d28b | 39 | * |
hydroguy45 | 7:ca226305d28b | 40 | * BufferedSerial pc(USBTX, USBRX); |
hydroguy45 | 7:ca226305d28b | 41 | * |
hydroguy45 | 7:ca226305d28b | 42 | * int main() |
hydroguy45 | 7:ca226305d28b | 43 | * { |
hydroguy45 | 7:ca226305d28b | 44 | * while(1) |
hydroguy45 | 7:ca226305d28b | 45 | * { |
hydroguy45 | 7:ca226305d28b | 46 | * Timer s; |
hydroguy45 | 7:ca226305d28b | 47 | * |
hydroguy45 | 7:ca226305d28b | 48 | * s.start(); |
hydroguy45 | 7:ca226305d28b | 49 | * pc.printf("Hello World - buffered\n"); |
hydroguy45 | 7:ca226305d28b | 50 | * int buffered_time = s.read_us(); |
hydroguy45 | 7:ca226305d28b | 51 | * wait(0.1f); // give time for the buffer to empty |
hydroguy45 | 7:ca226305d28b | 52 | * |
hydroguy45 | 7:ca226305d28b | 53 | * s.reset(); |
hydroguy45 | 7:ca226305d28b | 54 | * printf("Hello World - blocking\n"); |
hydroguy45 | 7:ca226305d28b | 55 | * int polled_time = s.read_us(); |
hydroguy45 | 7:ca226305d28b | 56 | * s.stop(); |
hydroguy45 | 7:ca226305d28b | 57 | * wait(0.1f); // give time for the buffer to empty |
hydroguy45 | 7:ca226305d28b | 58 | * |
hydroguy45 | 7:ca226305d28b | 59 | * pc.printf("printf buffered took %d us\n", buffered_time); |
hydroguy45 | 7:ca226305d28b | 60 | * pc.printf("printf blocking took %d us\n", polled_time); |
hydroguy45 | 7:ca226305d28b | 61 | * wait(0.5f); |
hydroguy45 | 7:ca226305d28b | 62 | * } |
hydroguy45 | 7:ca226305d28b | 63 | * } |
hydroguy45 | 7:ca226305d28b | 64 | * @endcode |
hydroguy45 | 7:ca226305d28b | 65 | */ |
hydroguy45 | 7:ca226305d28b | 66 | |
hydroguy45 | 7:ca226305d28b | 67 | /** |
hydroguy45 | 7:ca226305d28b | 68 | * @class BufferedSerial |
hydroguy45 | 7:ca226305d28b | 69 | * @brief Software buffers and interrupt driven tx and rx for Serial |
hydroguy45 | 7:ca226305d28b | 70 | */ |
hydroguy45 | 7:ca226305d28b | 71 | class BufferedSerial : public RawSerial |
hydroguy45 | 7:ca226305d28b | 72 | { |
hydroguy45 | 7:ca226305d28b | 73 | private: |
hydroguy45 | 7:ca226305d28b | 74 | MyBuffer <char> _rxbuf; |
hydroguy45 | 7:ca226305d28b | 75 | MyBuffer <char> _txbuf; |
hydroguy45 | 7:ca226305d28b | 76 | uint32_t _buf_size; |
hydroguy45 | 7:ca226305d28b | 77 | uint32_t _tx_multiple; |
hydroguy45 | 7:ca226305d28b | 78 | |
hydroguy45 | 7:ca226305d28b | 79 | void rxIrq(void); |
hydroguy45 | 7:ca226305d28b | 80 | void txIrq(void); |
hydroguy45 | 7:ca226305d28b | 81 | void prime(void); |
hydroguy45 | 7:ca226305d28b | 82 | |
hydroguy45 | 7:ca226305d28b | 83 | Callback<void()> _cbs[2]; |
hydroguy45 | 7:ca226305d28b | 84 | |
hydroguy45 | 7:ca226305d28b | 85 | public: |
hydroguy45 | 7:ca226305d28b | 86 | /** Create a BufferedSerial port, connected to the specified transmit and receive pins |
hydroguy45 | 7:ca226305d28b | 87 | * @param tx Transmit pin |
hydroguy45 | 7:ca226305d28b | 88 | * @param rx Receive pin |
hydroguy45 | 7:ca226305d28b | 89 | * @param buf_size printf() buffer size |
hydroguy45 | 7:ca226305d28b | 90 | * @param tx_multiple amount of max printf() present in the internal ring buffer at one time |
hydroguy45 | 7:ca226305d28b | 91 | * @param name optional name |
hydroguy45 | 7:ca226305d28b | 92 | * @note Either tx or rx may be specified as NC if unused |
hydroguy45 | 7:ca226305d28b | 93 | */ |
hydroguy45 | 7:ca226305d28b | 94 | BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); |
hydroguy45 | 7:ca226305d28b | 95 | |
hydroguy45 | 7:ca226305d28b | 96 | /** Destroy a BufferedSerial port |
hydroguy45 | 7:ca226305d28b | 97 | */ |
hydroguy45 | 7:ca226305d28b | 98 | virtual ~BufferedSerial(void); |
hydroguy45 | 7:ca226305d28b | 99 | |
hydroguy45 | 7:ca226305d28b | 100 | /** Check on how many bytes are in the rx buffer |
hydroguy45 | 7:ca226305d28b | 101 | * @return 1 if something exists, 0 otherwise |
hydroguy45 | 7:ca226305d28b | 102 | */ |
hydroguy45 | 7:ca226305d28b | 103 | virtual int readable(void); |
hydroguy45 | 7:ca226305d28b | 104 | |
hydroguy45 | 7:ca226305d28b | 105 | /** Check to see if the tx buffer has room |
hydroguy45 | 7:ca226305d28b | 106 | * @return 1 always has room and can overwrite previous content if too small / slow |
hydroguy45 | 7:ca226305d28b | 107 | */ |
hydroguy45 | 7:ca226305d28b | 108 | virtual int writeable(void); |
hydroguy45 | 7:ca226305d28b | 109 | |
hydroguy45 | 7:ca226305d28b | 110 | /** Get a single byte from the BufferedSerial Port. |
hydroguy45 | 7:ca226305d28b | 111 | * Should check readable() before calling this. |
hydroguy45 | 7:ca226305d28b | 112 | * @return A byte that came in on the Serial Port |
hydroguy45 | 7:ca226305d28b | 113 | */ |
hydroguy45 | 7:ca226305d28b | 114 | virtual int getc(void); |
hydroguy45 | 7:ca226305d28b | 115 | |
hydroguy45 | 7:ca226305d28b | 116 | /** Write a single byte to the BufferedSerial Port. |
hydroguy45 | 7:ca226305d28b | 117 | * @param c The byte to write to the Serial Port |
hydroguy45 | 7:ca226305d28b | 118 | * @return The byte that was written to the Serial Port Buffer |
hydroguy45 | 7:ca226305d28b | 119 | */ |
hydroguy45 | 7:ca226305d28b | 120 | virtual int putc(int c); |
hydroguy45 | 7:ca226305d28b | 121 | |
hydroguy45 | 7:ca226305d28b | 122 | /** Write a string to the BufferedSerial Port. Must be NULL terminated |
hydroguy45 | 7:ca226305d28b | 123 | * @param s The string to write to the Serial Port |
hydroguy45 | 7:ca226305d28b | 124 | * @return The number of bytes written to the Serial Port Buffer |
hydroguy45 | 7:ca226305d28b | 125 | */ |
hydroguy45 | 7:ca226305d28b | 126 | virtual int puts(const char *s); |
hydroguy45 | 7:ca226305d28b | 127 | |
hydroguy45 | 7:ca226305d28b | 128 | /** Write a formatted string to the BufferedSerial Port. |
hydroguy45 | 7:ca226305d28b | 129 | * @param format The string + format specifiers to write to the Serial Port |
hydroguy45 | 7:ca226305d28b | 130 | * @return The number of bytes written to the Serial Port Buffer |
hydroguy45 | 7:ca226305d28b | 131 | */ |
hydroguy45 | 7:ca226305d28b | 132 | virtual int printf(const char* format, ...); |
hydroguy45 | 7:ca226305d28b | 133 | |
hydroguy45 | 7:ca226305d28b | 134 | /** Write data to the Buffered Serial Port |
hydroguy45 | 7:ca226305d28b | 135 | * @param s A pointer to data to send |
hydroguy45 | 7:ca226305d28b | 136 | * @param length The amount of data being pointed to |
hydroguy45 | 7:ca226305d28b | 137 | * @return The number of bytes written to the Serial Port Buffer |
hydroguy45 | 7:ca226305d28b | 138 | */ |
hydroguy45 | 7:ca226305d28b | 139 | virtual ssize_t write(const void *s, std::size_t length); |
hydroguy45 | 7:ca226305d28b | 140 | |
hydroguy45 | 7:ca226305d28b | 141 | /** Attach a function to call whenever a serial interrupt is generated |
hydroguy45 | 7:ca226305d28b | 142 | * @param func A pointer to a void function, or 0 to set as none |
hydroguy45 | 7:ca226305d28b | 143 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
hydroguy45 | 7:ca226305d28b | 144 | */ |
hydroguy45 | 7:ca226305d28b | 145 | virtual void attach(Callback<void()> func, IrqType type=RxIrq); |
hydroguy45 | 7:ca226305d28b | 146 | |
hydroguy45 | 7:ca226305d28b | 147 | /** Attach a member function to call whenever a serial interrupt is generated |
hydroguy45 | 7:ca226305d28b | 148 | * @param obj pointer to the object to call the member function on |
hydroguy45 | 7:ca226305d28b | 149 | * @param method pointer to the member function to call |
hydroguy45 | 7:ca226305d28b | 150 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
hydroguy45 | 7:ca226305d28b | 151 | */ |
hydroguy45 | 7:ca226305d28b | 152 | template <typename T> |
hydroguy45 | 7:ca226305d28b | 153 | void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { |
hydroguy45 | 7:ca226305d28b | 154 | attach(Callback<void()>(obj, method), type); |
hydroguy45 | 7:ca226305d28b | 155 | } |
hydroguy45 | 7:ca226305d28b | 156 | |
hydroguy45 | 7:ca226305d28b | 157 | /** Attach a member function to call whenever a serial interrupt is generated |
hydroguy45 | 7:ca226305d28b | 158 | * @param obj pointer to the object to call the member function on |
hydroguy45 | 7:ca226305d28b | 159 | * @param method pointer to the member function to call |
hydroguy45 | 7:ca226305d28b | 160 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
hydroguy45 | 7:ca226305d28b | 161 | */ |
hydroguy45 | 7:ca226305d28b | 162 | template <typename T> |
hydroguy45 | 7:ca226305d28b | 163 | void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { |
hydroguy45 | 7:ca226305d28b | 164 | attach(Callback<void()>(obj, method), type); |
hydroguy45 | 7:ca226305d28b | 165 | } |
hydroguy45 | 7:ca226305d28b | 166 | }; |
hydroguy45 | 7:ca226305d28b | 167 | |
hydroguy45 | 7:ca226305d28b | 168 | #endif |