Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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