PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skrawool 0:3954a906acc2 1 /* mbed Microcontroller Library
skrawool 0:3954a906acc2 2 * Copyright (c) 2006-2013 ARM Limited
skrawool 0:3954a906acc2 3 *
skrawool 0:3954a906acc2 4 * Licensed under the Apache License, Version 2.0 (the "License");
skrawool 0:3954a906acc2 5 * you may not use this file except in compliance with the License.
skrawool 0:3954a906acc2 6 * You may obtain a copy of the License at
skrawool 0:3954a906acc2 7 *
skrawool 0:3954a906acc2 8 * http://www.apache.org/licenses/LICENSE-2.0
skrawool 0:3954a906acc2 9 *
skrawool 0:3954a906acc2 10 * Unless required by applicable law or agreed to in writing, software
skrawool 0:3954a906acc2 11 * distributed under the License is distributed on an "AS IS" BASIS,
skrawool 0:3954a906acc2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
skrawool 0:3954a906acc2 13 * See the License for the specific language governing permissions and
skrawool 0:3954a906acc2 14 * limitations under the License.
skrawool 0:3954a906acc2 15 */
skrawool 0:3954a906acc2 16 #ifndef MBED_SERIALBASE_H
skrawool 0:3954a906acc2 17 #define MBED_SERIALBASE_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 #if DEVICE_SERIAL
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 #include "Stream.h"
skrawool 0:3954a906acc2 24 #include "Callback.h"
skrawool 0:3954a906acc2 25 #include "serial_api.h"
skrawool 0:3954a906acc2 26 #include "toolchain.h"
skrawool 0:3954a906acc2 27
skrawool 0:3954a906acc2 28 #if DEVICE_SERIAL_ASYNCH
skrawool 0:3954a906acc2 29 #include "CThunk.h"
skrawool 0:3954a906acc2 30 #include "dma_api.h"
skrawool 0:3954a906acc2 31 #endif
skrawool 0:3954a906acc2 32
skrawool 0:3954a906acc2 33 namespace mbed {
skrawool 0:3954a906acc2 34 /** \addtogroup drivers */
skrawool 0:3954a906acc2 35 /** @{*/
skrawool 0:3954a906acc2 36
skrawool 0:3954a906acc2 37 /** A base class for serial port implementations
skrawool 0:3954a906acc2 38 * Can't be instantiated directly (use Serial or RawSerial)
skrawool 0:3954a906acc2 39 *
skrawool 0:3954a906acc2 40 * @Note Synchronization level: Set by subclass
skrawool 0:3954a906acc2 41 */
skrawool 0:3954a906acc2 42 class SerialBase {
skrawool 0:3954a906acc2 43
skrawool 0:3954a906acc2 44 public:
skrawool 0:3954a906acc2 45 /** Set the baud rate of the serial port
skrawool 0:3954a906acc2 46 *
skrawool 0:3954a906acc2 47 * @param baudrate The baudrate of the serial port (default = 9600).
skrawool 0:3954a906acc2 48 */
skrawool 0:3954a906acc2 49 void baud(int baudrate);
skrawool 0:3954a906acc2 50
skrawool 0:3954a906acc2 51 enum Parity {
skrawool 0:3954a906acc2 52 None = 0,
skrawool 0:3954a906acc2 53 Odd,
skrawool 0:3954a906acc2 54 Even,
skrawool 0:3954a906acc2 55 Forced1,
skrawool 0:3954a906acc2 56 Forced0
skrawool 0:3954a906acc2 57 };
skrawool 0:3954a906acc2 58
skrawool 0:3954a906acc2 59 enum IrqType {
skrawool 0:3954a906acc2 60 RxIrq = 0,
skrawool 0:3954a906acc2 61 TxIrq,
skrawool 0:3954a906acc2 62
skrawool 0:3954a906acc2 63 IrqCnt
skrawool 0:3954a906acc2 64 };
skrawool 0:3954a906acc2 65
skrawool 0:3954a906acc2 66 enum Flow {
skrawool 0:3954a906acc2 67 Disabled = 0,
skrawool 0:3954a906acc2 68 RTS,
skrawool 0:3954a906acc2 69 CTS,
skrawool 0:3954a906acc2 70 RTSCTS
skrawool 0:3954a906acc2 71 };
skrawool 0:3954a906acc2 72
skrawool 0:3954a906acc2 73 /** Set the transmission format used by the serial port
skrawool 0:3954a906acc2 74 *
skrawool 0:3954a906acc2 75 * @param bits The number of bits in a word (5-8; default = 8)
skrawool 0:3954a906acc2 76 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
skrawool 0:3954a906acc2 77 * @param stop The number of stop bits (1 or 2; default = 1)
skrawool 0:3954a906acc2 78 */
skrawool 0:3954a906acc2 79 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
skrawool 0:3954a906acc2 80
skrawool 0:3954a906acc2 81 /** Determine if there is a character available to read
skrawool 0:3954a906acc2 82 *
skrawool 0:3954a906acc2 83 * @returns
skrawool 0:3954a906acc2 84 * 1 if there is a character available to read,
skrawool 0:3954a906acc2 85 * 0 otherwise
skrawool 0:3954a906acc2 86 */
skrawool 0:3954a906acc2 87 int readable();
skrawool 0:3954a906acc2 88
skrawool 0:3954a906acc2 89 /** Determine if there is space available to write a character
skrawool 0:3954a906acc2 90 *
skrawool 0:3954a906acc2 91 * @returns
skrawool 0:3954a906acc2 92 * 1 if there is space to write a character,
skrawool 0:3954a906acc2 93 * 0 otherwise
skrawool 0:3954a906acc2 94 */
skrawool 0:3954a906acc2 95 int writeable();
skrawool 0:3954a906acc2 96
skrawool 0:3954a906acc2 97 /** Attach a function to call whenever a serial interrupt is generated
skrawool 0:3954a906acc2 98 *
skrawool 0:3954a906acc2 99 * @param func A pointer to a void function, or 0 to set as none
skrawool 0:3954a906acc2 100 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
skrawool 0:3954a906acc2 101 */
skrawool 0:3954a906acc2 102 void attach(Callback<void()> func, IrqType type=RxIrq);
skrawool 0:3954a906acc2 103
skrawool 0:3954a906acc2 104 /** Attach a member function to call whenever a serial interrupt is generated
skrawool 0:3954a906acc2 105 *
skrawool 0:3954a906acc2 106 * @param obj pointer to the object to call the member function on
skrawool 0:3954a906acc2 107 * @param method pointer to the member function to be called
skrawool 0:3954a906acc2 108 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
skrawool 0:3954a906acc2 109 * @deprecated
skrawool 0:3954a906acc2 110 * The attach function does not support cv-qualifiers. Replaced by
skrawool 0:3954a906acc2 111 * attach(callback(obj, method), type).
skrawool 0:3954a906acc2 112 */
skrawool 0:3954a906acc2 113 template<typename T>
skrawool 0:3954a906acc2 114 MBED_DEPRECATED_SINCE("mbed-os-5.1",
skrawool 0:3954a906acc2 115 "The attach function does not support cv-qualifiers. Replaced by "
skrawool 0:3954a906acc2 116 "attach(callback(obj, method), type).")
skrawool 0:3954a906acc2 117 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
skrawool 0:3954a906acc2 118 attach(callback(obj, method), type);
skrawool 0:3954a906acc2 119 }
skrawool 0:3954a906acc2 120
skrawool 0:3954a906acc2 121 /** Attach a member function to call whenever a serial interrupt is generated
skrawool 0:3954a906acc2 122 *
skrawool 0:3954a906acc2 123 * @param obj pointer to the object to call the member function on
skrawool 0:3954a906acc2 124 * @param method pointer to the member function to be called
skrawool 0:3954a906acc2 125 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
skrawool 0:3954a906acc2 126 * @deprecated
skrawool 0:3954a906acc2 127 * The attach function does not support cv-qualifiers. Replaced by
skrawool 0:3954a906acc2 128 * attach(callback(obj, method), type).
skrawool 0:3954a906acc2 129 */
skrawool 0:3954a906acc2 130 template<typename T>
skrawool 0:3954a906acc2 131 MBED_DEPRECATED_SINCE("mbed-os-5.1",
skrawool 0:3954a906acc2 132 "The attach function does not support cv-qualifiers. Replaced by "
skrawool 0:3954a906acc2 133 "attach(callback(obj, method), type).")
skrawool 0:3954a906acc2 134 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
skrawool 0:3954a906acc2 135 attach(callback(obj, method), type);
skrawool 0:3954a906acc2 136 }
skrawool 0:3954a906acc2 137
skrawool 0:3954a906acc2 138 /** Generate a break condition on the serial line
skrawool 0:3954a906acc2 139 */
skrawool 0:3954a906acc2 140 void send_break();
skrawool 0:3954a906acc2 141
skrawool 0:3954a906acc2 142 protected:
skrawool 0:3954a906acc2 143
skrawool 0:3954a906acc2 144 /** Acquire exclusive access to this serial port
skrawool 0:3954a906acc2 145 */
skrawool 0:3954a906acc2 146 virtual void lock(void);
skrawool 0:3954a906acc2 147
skrawool 0:3954a906acc2 148 /** Release exclusive access to this serial port
skrawool 0:3954a906acc2 149 */
skrawool 0:3954a906acc2 150 virtual void unlock(void);
skrawool 0:3954a906acc2 151
skrawool 0:3954a906acc2 152 public:
skrawool 0:3954a906acc2 153
skrawool 0:3954a906acc2 154 #if DEVICE_SERIAL_FC
skrawool 0:3954a906acc2 155 /** Set the flow control type on the serial port
skrawool 0:3954a906acc2 156 *
skrawool 0:3954a906acc2 157 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
skrawool 0:3954a906acc2 158 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
skrawool 0:3954a906acc2 159 * @param flow2 the second flow control pin (CTS for RTSCTS)
skrawool 0:3954a906acc2 160 */
skrawool 0:3954a906acc2 161 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
skrawool 0:3954a906acc2 162 #endif
skrawool 0:3954a906acc2 163
skrawool 0:3954a906acc2 164 static void _irq_handler(uint32_t id, SerialIrq irq_type);
skrawool 0:3954a906acc2 165
skrawool 0:3954a906acc2 166 #if DEVICE_SERIAL_ASYNCH
skrawool 0:3954a906acc2 167
skrawool 0:3954a906acc2 168 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
skrawool 0:3954a906acc2 169 *
skrawool 0:3954a906acc2 170 * @param buffer The buffer where received data will be stored
skrawool 0:3954a906acc2 171 * @param length The buffer length in bytes
skrawool 0:3954a906acc2 172 * @param callback The event callback function
skrawool 0:3954a906acc2 173 * @param event The logical OR of TX events
skrawool 0:3954a906acc2 174 */
skrawool 0:3954a906acc2 175 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
skrawool 0:3954a906acc2 176
skrawool 0:3954a906acc2 177 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
skrawool 0:3954a906acc2 178 *
skrawool 0:3954a906acc2 179 * @param buffer The buffer where received data will be stored
skrawool 0:3954a906acc2 180 * @param length The buffer length in bytes
skrawool 0:3954a906acc2 181 * @param callback The event callback function
skrawool 0:3954a906acc2 182 * @param event The logical OR of TX events
skrawool 0:3954a906acc2 183 */
skrawool 0:3954a906acc2 184 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
skrawool 0:3954a906acc2 185
skrawool 0:3954a906acc2 186 /** Abort the on-going write transfer
skrawool 0:3954a906acc2 187 */
skrawool 0:3954a906acc2 188 void abort_write();
skrawool 0:3954a906acc2 189
skrawool 0:3954a906acc2 190 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
skrawool 0:3954a906acc2 191 *
skrawool 0:3954a906acc2 192 * @param buffer The buffer where received data will be stored
skrawool 0:3954a906acc2 193 * @param length The buffer length in bytes
skrawool 0:3954a906acc2 194 * @param callback The event callback function
skrawool 0:3954a906acc2 195 * @param event The logical OR of RX events
skrawool 0:3954a906acc2 196 * @param char_match The matching character
skrawool 0:3954a906acc2 197 */
skrawool 0:3954a906acc2 198 int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
skrawool 0:3954a906acc2 199
skrawool 0:3954a906acc2 200 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
skrawool 0:3954a906acc2 201 *
skrawool 0:3954a906acc2 202 * @param buffer The buffer where received data will be stored
skrawool 0:3954a906acc2 203 * @param length The buffer length in bytes
skrawool 0:3954a906acc2 204 * @param callback The event callback function
skrawool 0:3954a906acc2 205 * @param event The logical OR of RX events
skrawool 0:3954a906acc2 206 * @param char_match The matching character
skrawool 0:3954a906acc2 207 */
skrawool 0:3954a906acc2 208 int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
skrawool 0:3954a906acc2 209
skrawool 0:3954a906acc2 210 /** Abort the on-going read transfer
skrawool 0:3954a906acc2 211 */
skrawool 0:3954a906acc2 212 void abort_read();
skrawool 0:3954a906acc2 213
skrawool 0:3954a906acc2 214 /** Configure DMA usage suggestion for non-blocking TX transfers
skrawool 0:3954a906acc2 215 *
skrawool 0:3954a906acc2 216 * @param usage The usage DMA hint for peripheral
skrawool 0:3954a906acc2 217 * @return Zero if the usage was set, -1 if a transaction is on-going
skrawool 0:3954a906acc2 218 */
skrawool 0:3954a906acc2 219 int set_dma_usage_tx(DMAUsage usage);
skrawool 0:3954a906acc2 220
skrawool 0:3954a906acc2 221 /** Configure DMA usage suggestion for non-blocking RX transfers
skrawool 0:3954a906acc2 222 *
skrawool 0:3954a906acc2 223 * @param usage The usage DMA hint for peripheral
skrawool 0:3954a906acc2 224 * @return Zero if the usage was set, -1 if a transaction is on-going
skrawool 0:3954a906acc2 225 */
skrawool 0:3954a906acc2 226 int set_dma_usage_rx(DMAUsage usage);
skrawool 0:3954a906acc2 227
skrawool 0:3954a906acc2 228 protected:
skrawool 0:3954a906acc2 229 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
skrawool 0:3954a906acc2 230 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
skrawool 0:3954a906acc2 231 void interrupt_handler_asynch(void);
skrawool 0:3954a906acc2 232 #endif
skrawool 0:3954a906acc2 233
skrawool 0:3954a906acc2 234 protected:
skrawool 0:3954a906acc2 235 SerialBase(PinName tx, PinName rx, int baud);
skrawool 0:3954a906acc2 236 virtual ~SerialBase() {
skrawool 0:3954a906acc2 237 }
skrawool 0:3954a906acc2 238
skrawool 0:3954a906acc2 239 int _base_getc();
skrawool 0:3954a906acc2 240 int _base_putc(int c);
skrawool 0:3954a906acc2 241
skrawool 0:3954a906acc2 242 #if DEVICE_SERIAL_ASYNCH
skrawool 0:3954a906acc2 243 CThunk<SerialBase> _thunk_irq;
skrawool 0:3954a906acc2 244 event_callback_t _tx_callback;
skrawool 0:3954a906acc2 245 event_callback_t _rx_callback;
skrawool 0:3954a906acc2 246 DMAUsage _tx_usage;
skrawool 0:3954a906acc2 247 DMAUsage _rx_usage;
skrawool 0:3954a906acc2 248 #endif
skrawool 0:3954a906acc2 249
skrawool 0:3954a906acc2 250 serial_t _serial;
skrawool 0:3954a906acc2 251 Callback<void()> _irq[IrqCnt];
skrawool 0:3954a906acc2 252 int _baud;
skrawool 0:3954a906acc2 253
skrawool 0:3954a906acc2 254 };
skrawool 0:3954a906acc2 255
skrawool 0:3954a906acc2 256 } // namespace mbed
skrawool 0:3954a906acc2 257
skrawool 0:3954a906acc2 258 #endif
skrawool 0:3954a906acc2 259
skrawool 0:3954a906acc2 260 #endif
skrawool 0:3954a906acc2 261
skrawool 0:3954a906acc2 262 /** @}*/