OneNet_IoT_demo for ASC platform

Dependencies:   Common_lib ESP8266 EdpKit_lib cJSON_lib driver_mbed_HP20x driver_mbed_TH02 wifi_example

Fork of mbed-os-example-esp8266 by ESP8266

Committer:
sarahmarshy
Date:
Thu Jan 12 22:05:15 2017 +0000
Revision:
1:b4a718e62e0b
Parent:
0:b887535f68bf
Update esp8266-driver

Who changed what in which revision?

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