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-2015 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_I2C_H
skrawool 0:3954a906acc2 17 #define MBED_I2C_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 #if DEVICE_I2C
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 #include "hal/i2c_api.h"
skrawool 0:3954a906acc2 24 #include "platform/SingletonPtr.h"
skrawool 0:3954a906acc2 25 #include "platform/PlatformMutex.h"
skrawool 0:3954a906acc2 26
skrawool 0:3954a906acc2 27 #if DEVICE_I2C_ASYNCH
skrawool 0:3954a906acc2 28 #include "platform/CThunk.h"
skrawool 0:3954a906acc2 29 #include "hal/dma_api.h"
skrawool 0:3954a906acc2 30 #include "platform/FunctionPointer.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 /** An I2C Master, used for communicating with I2C slave devices
skrawool 0:3954a906acc2 38 *
skrawool 0:3954a906acc2 39 * @Note Synchronization level: Thread safe
skrawool 0:3954a906acc2 40 *
skrawool 0:3954a906acc2 41 * Example:
skrawool 0:3954a906acc2 42 * @code
skrawool 0:3954a906acc2 43 * // Read from I2C slave at address 0x62
skrawool 0:3954a906acc2 44 *
skrawool 0:3954a906acc2 45 * #include "mbed.h"
skrawool 0:3954a906acc2 46 *
skrawool 0:3954a906acc2 47 * I2C i2c(p28, p27);
skrawool 0:3954a906acc2 48 *
skrawool 0:3954a906acc2 49 * int main() {
skrawool 0:3954a906acc2 50 * int address = 0x62;
skrawool 0:3954a906acc2 51 * char data[2];
skrawool 0:3954a906acc2 52 * i2c.read(address, data, 2);
skrawool 0:3954a906acc2 53 * }
skrawool 0:3954a906acc2 54 * @endcode
skrawool 0:3954a906acc2 55 */
skrawool 0:3954a906acc2 56 class I2C {
skrawool 0:3954a906acc2 57
skrawool 0:3954a906acc2 58 public:
skrawool 0:3954a906acc2 59 enum RxStatus {
skrawool 0:3954a906acc2 60 NoData,
skrawool 0:3954a906acc2 61 MasterGeneralCall,
skrawool 0:3954a906acc2 62 MasterWrite,
skrawool 0:3954a906acc2 63 MasterRead
skrawool 0:3954a906acc2 64 };
skrawool 0:3954a906acc2 65
skrawool 0:3954a906acc2 66 enum Acknowledge {
skrawool 0:3954a906acc2 67 NoACK = 0,
skrawool 0:3954a906acc2 68 ACK = 1
skrawool 0:3954a906acc2 69 };
skrawool 0:3954a906acc2 70
skrawool 0:3954a906acc2 71 /** Create an I2C Master interface, connected to the specified pins
skrawool 0:3954a906acc2 72 *
skrawool 0:3954a906acc2 73 * @param sda I2C data line pin
skrawool 0:3954a906acc2 74 * @param scl I2C clock line pin
skrawool 0:3954a906acc2 75 */
skrawool 0:3954a906acc2 76 I2C(PinName sda, PinName scl);
skrawool 0:3954a906acc2 77
skrawool 0:3954a906acc2 78 /** Set the frequency of the I2C interface
skrawool 0:3954a906acc2 79 *
skrawool 0:3954a906acc2 80 * @param hz The bus frequency in hertz
skrawool 0:3954a906acc2 81 */
skrawool 0:3954a906acc2 82 void frequency(int hz);
skrawool 0:3954a906acc2 83
skrawool 0:3954a906acc2 84 /** Read from an I2C slave
skrawool 0:3954a906acc2 85 *
skrawool 0:3954a906acc2 86 * Performs a complete read transaction. The bottom bit of
skrawool 0:3954a906acc2 87 * the address is forced to 1 to indicate a read.
skrawool 0:3954a906acc2 88 *
skrawool 0:3954a906acc2 89 * @param address 8-bit I2C slave address [ addr | 1 ]
skrawool 0:3954a906acc2 90 * @param data Pointer to the byte-array to read data in to
skrawool 0:3954a906acc2 91 * @param length Number of bytes to read
skrawool 0:3954a906acc2 92 * @param repeated Repeated start, true - don't send stop at end
skrawool 0:3954a906acc2 93 *
skrawool 0:3954a906acc2 94 * @returns
skrawool 0:3954a906acc2 95 * 0 on success (ack),
skrawool 0:3954a906acc2 96 * non-0 on failure (nack)
skrawool 0:3954a906acc2 97 */
skrawool 0:3954a906acc2 98 int read(int address, char *data, int length, bool repeated = false);
skrawool 0:3954a906acc2 99
skrawool 0:3954a906acc2 100 /** Read a single byte from the I2C bus
skrawool 0:3954a906acc2 101 *
skrawool 0:3954a906acc2 102 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
skrawool 0:3954a906acc2 103 *
skrawool 0:3954a906acc2 104 * @returns
skrawool 0:3954a906acc2 105 * the byte read
skrawool 0:3954a906acc2 106 */
skrawool 0:3954a906acc2 107 int read(int ack);
skrawool 0:3954a906acc2 108
skrawool 0:3954a906acc2 109 /** Write to an I2C slave
skrawool 0:3954a906acc2 110 *
skrawool 0:3954a906acc2 111 * Performs a complete write transaction. The bottom bit of
skrawool 0:3954a906acc2 112 * the address is forced to 0 to indicate a write.
skrawool 0:3954a906acc2 113 *
skrawool 0:3954a906acc2 114 * @param address 8-bit I2C slave address [ addr | 0 ]
skrawool 0:3954a906acc2 115 * @param data Pointer to the byte-array data to send
skrawool 0:3954a906acc2 116 * @param length Number of bytes to send
skrawool 0:3954a906acc2 117 * @param repeated Repeated start, true - do not send stop at end
skrawool 0:3954a906acc2 118 *
skrawool 0:3954a906acc2 119 * @returns
skrawool 0:3954a906acc2 120 * 0 on success (ack),
skrawool 0:3954a906acc2 121 * non-0 on failure (nack)
skrawool 0:3954a906acc2 122 */
skrawool 0:3954a906acc2 123 int write(int address, const char *data, int length, bool repeated = false);
skrawool 0:3954a906acc2 124
skrawool 0:3954a906acc2 125 /** Write single byte out on the I2C bus
skrawool 0:3954a906acc2 126 *
skrawool 0:3954a906acc2 127 * @param data data to write out on bus
skrawool 0:3954a906acc2 128 *
skrawool 0:3954a906acc2 129 * @returns
skrawool 0:3954a906acc2 130 * '1' if an ACK was received,
skrawool 0:3954a906acc2 131 * '0' otherwise
skrawool 0:3954a906acc2 132 */
skrawool 0:3954a906acc2 133 int write(int data);
skrawool 0:3954a906acc2 134
skrawool 0:3954a906acc2 135 /** Creates a start condition on the I2C bus
skrawool 0:3954a906acc2 136 */
skrawool 0:3954a906acc2 137
skrawool 0:3954a906acc2 138 void start(void);
skrawool 0:3954a906acc2 139
skrawool 0:3954a906acc2 140 /** Creates a stop condition on the I2C bus
skrawool 0:3954a906acc2 141 */
skrawool 0:3954a906acc2 142 void stop(void);
skrawool 0:3954a906acc2 143
skrawool 0:3954a906acc2 144 /** Acquire exclusive access to this I2C bus
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 I2C bus
skrawool 0:3954a906acc2 149 */
skrawool 0:3954a906acc2 150 virtual void unlock(void);
skrawool 0:3954a906acc2 151
skrawool 0:3954a906acc2 152 virtual ~I2C() {
skrawool 0:3954a906acc2 153 // Do nothing
skrawool 0:3954a906acc2 154 }
skrawool 0:3954a906acc2 155
skrawool 0:3954a906acc2 156 #if DEVICE_I2C_ASYNCH
skrawool 0:3954a906acc2 157
skrawool 0:3954a906acc2 158 /** Start non-blocking I2C transfer.
skrawool 0:3954a906acc2 159 *
skrawool 0:3954a906acc2 160 * @param address 8/10 bit I2c slave address
skrawool 0:3954a906acc2 161 * @param tx_buffer The TX buffer with data to be transfered
skrawool 0:3954a906acc2 162 * @param tx_length The length of TX buffer in bytes
skrawool 0:3954a906acc2 163 * @param rx_buffer The RX buffer which is used for received data
skrawool 0:3954a906acc2 164 * @param rx_length The length of RX buffer in bytes
skrawool 0:3954a906acc2 165 * @param event The logical OR of events to modify
skrawool 0:3954a906acc2 166 * @param callback The event callback function
skrawool 0:3954a906acc2 167 * @param repeated Repeated start, true - do not send stop at end
skrawool 0:3954a906acc2 168 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
skrawool 0:3954a906acc2 169 */
skrawool 0:3954a906acc2 170 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
skrawool 0:3954a906acc2 171
skrawool 0:3954a906acc2 172 /** Abort the on-going I2C transfer
skrawool 0:3954a906acc2 173 */
skrawool 0:3954a906acc2 174 void abort_transfer();
skrawool 0:3954a906acc2 175 protected:
skrawool 0:3954a906acc2 176 void irq_handler_asynch(void);
skrawool 0:3954a906acc2 177 event_callback_t _callback;
skrawool 0:3954a906acc2 178 CThunk<I2C> _irq;
skrawool 0:3954a906acc2 179 DMAUsage _usage;
skrawool 0:3954a906acc2 180 #endif
skrawool 0:3954a906acc2 181
skrawool 0:3954a906acc2 182 protected:
skrawool 0:3954a906acc2 183 void aquire();
skrawool 0:3954a906acc2 184
skrawool 0:3954a906acc2 185 i2c_t _i2c;
skrawool 0:3954a906acc2 186 static I2C *_owner;
skrawool 0:3954a906acc2 187 int _hz;
skrawool 0:3954a906acc2 188 static SingletonPtr<PlatformMutex> _mutex;
skrawool 0:3954a906acc2 189 };
skrawool 0:3954a906acc2 190
skrawool 0:3954a906acc2 191 } // namespace mbed
skrawool 0:3954a906acc2 192
skrawool 0:3954a906acc2 193 #endif
skrawool 0:3954a906acc2 194
skrawool 0:3954a906acc2 195 #endif
skrawool 0:3954a906acc2 196
skrawool 0:3954a906acc2 197 /** @}*/