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:
group-ESP8266
Date:
Thu Jan 12 20:21:45 2017 +0000
Revision:
0:b887535f68bf
Child:
1:b4a718e62e0b
Initial commit

Who changed what in which revision?

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