Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ethaderu 3:78f223d34f36 1 /* mbed Microcontroller Library
ethaderu 3:78f223d34f36 2 * Copyright (c) 2006-2013 ARM Limited
ethaderu 3:78f223d34f36 3 *
ethaderu 3:78f223d34f36 4 * Licensed under the Apache License, Version 2.0 (the "License");
ethaderu 3:78f223d34f36 5 * you may not use this file except in compliance with the License.
ethaderu 3:78f223d34f36 6 * You may obtain a copy of the License at
ethaderu 3:78f223d34f36 7 *
ethaderu 3:78f223d34f36 8 * http://www.apache.org/licenses/LICENSE-2.0
ethaderu 3:78f223d34f36 9 *
ethaderu 3:78f223d34f36 10 * Unless required by applicable law or agreed to in writing, software
ethaderu 3:78f223d34f36 11 * distributed under the License is distributed on an "AS IS" BASIS,
ethaderu 3:78f223d34f36 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ethaderu 3:78f223d34f36 13 * See the License for the specific language governing permissions and
ethaderu 3:78f223d34f36 14 * limitations under the License.
ethaderu 3:78f223d34f36 15 */
ethaderu 3:78f223d34f36 16 #ifndef MBED_I2C_SLAVE_H
ethaderu 3:78f223d34f36 17 #define MBED_I2C_SLAVE_H
ethaderu 3:78f223d34f36 18
ethaderu 3:78f223d34f36 19 #include "platform.h"
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 #if DEVICE_I2CSLAVE
ethaderu 3:78f223d34f36 22
ethaderu 3:78f223d34f36 23 #include "i2c_api.h"
ethaderu 3:78f223d34f36 24
ethaderu 3:78f223d34f36 25 namespace mbed {
ethaderu 3:78f223d34f36 26
ethaderu 3:78f223d34f36 27 /** An I2C Slave, used for communicating with an I2C Master device
ethaderu 3:78f223d34f36 28 *
ethaderu 3:78f223d34f36 29 * Example:
ethaderu 3:78f223d34f36 30 * @code
ethaderu 3:78f223d34f36 31 * // Simple I2C responder
ethaderu 3:78f223d34f36 32 * #include <mbed.h>
ethaderu 3:78f223d34f36 33 *
ethaderu 3:78f223d34f36 34 * I2CSlave slave(p9, p10);
ethaderu 3:78f223d34f36 35 *
ethaderu 3:78f223d34f36 36 * int main() {
ethaderu 3:78f223d34f36 37 * char buf[10];
ethaderu 3:78f223d34f36 38 * char msg[] = "Slave!";
ethaderu 3:78f223d34f36 39 *
ethaderu 3:78f223d34f36 40 * slave.address(0xA0);
ethaderu 3:78f223d34f36 41 * while (1) {
ethaderu 3:78f223d34f36 42 * int i = slave.receive();
ethaderu 3:78f223d34f36 43 * switch (i) {
ethaderu 3:78f223d34f36 44 * case I2CSlave::ReadAddressed:
ethaderu 3:78f223d34f36 45 * slave.write(msg, strlen(msg) + 1); // Includes null char
ethaderu 3:78f223d34f36 46 * break;
ethaderu 3:78f223d34f36 47 * case I2CSlave::WriteGeneral:
ethaderu 3:78f223d34f36 48 * slave.read(buf, 10);
ethaderu 3:78f223d34f36 49 * printf("Read G: %s\n", buf);
ethaderu 3:78f223d34f36 50 * break;
ethaderu 3:78f223d34f36 51 * case I2CSlave::WriteAddressed:
ethaderu 3:78f223d34f36 52 * slave.read(buf, 10);
ethaderu 3:78f223d34f36 53 * printf("Read A: %s\n", buf);
ethaderu 3:78f223d34f36 54 * break;
ethaderu 3:78f223d34f36 55 * }
ethaderu 3:78f223d34f36 56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
ethaderu 3:78f223d34f36 57 * }
ethaderu 3:78f223d34f36 58 * }
ethaderu 3:78f223d34f36 59 * @endcode
ethaderu 3:78f223d34f36 60 */
ethaderu 3:78f223d34f36 61 class I2CSlave {
ethaderu 3:78f223d34f36 62
ethaderu 3:78f223d34f36 63 public:
ethaderu 3:78f223d34f36 64 enum RxStatus {
ethaderu 3:78f223d34f36 65 NoData = 0,
ethaderu 3:78f223d34f36 66 ReadAddressed = 1,
ethaderu 3:78f223d34f36 67 WriteGeneral = 2,
ethaderu 3:78f223d34f36 68 WriteAddressed = 3
ethaderu 3:78f223d34f36 69 };
ethaderu 3:78f223d34f36 70
ethaderu 3:78f223d34f36 71 /** Create an I2C Slave interface, connected to the specified pins.
ethaderu 3:78f223d34f36 72 *
ethaderu 3:78f223d34f36 73 * @param sda I2C data line pin
ethaderu 3:78f223d34f36 74 * @param scl I2C clock line pin
ethaderu 3:78f223d34f36 75 */
ethaderu 3:78f223d34f36 76 I2CSlave(PinName sda, PinName scl);
ethaderu 3:78f223d34f36 77
ethaderu 3:78f223d34f36 78 /** Set the frequency of the I2C interface
ethaderu 3:78f223d34f36 79 *
ethaderu 3:78f223d34f36 80 * @param hz The bus frequency in hertz
ethaderu 3:78f223d34f36 81 */
ethaderu 3:78f223d34f36 82 void frequency(int hz);
ethaderu 3:78f223d34f36 83
ethaderu 3:78f223d34f36 84 /** Checks to see if this I2C Slave has been addressed.
ethaderu 3:78f223d34f36 85 *
ethaderu 3:78f223d34f36 86 * @returns
ethaderu 3:78f223d34f36 87 * A status indicating if the device has been addressed, and how
ethaderu 3:78f223d34f36 88 * - NoData - the slave has not been addressed
ethaderu 3:78f223d34f36 89 * - ReadAddressed - the master has requested a read from this slave
ethaderu 3:78f223d34f36 90 * - WriteAddressed - the master is writing to this slave
ethaderu 3:78f223d34f36 91 * - WriteGeneral - the master is writing to all slave
ethaderu 3:78f223d34f36 92 */
ethaderu 3:78f223d34f36 93 int receive(void);
ethaderu 3:78f223d34f36 94
ethaderu 3:78f223d34f36 95 /** Read from an I2C master.
ethaderu 3:78f223d34f36 96 *
ethaderu 3:78f223d34f36 97 * @param data pointer to the byte array to read data in to
ethaderu 3:78f223d34f36 98 * @param length maximum number of bytes to read
ethaderu 3:78f223d34f36 99 *
ethaderu 3:78f223d34f36 100 * @returns
ethaderu 3:78f223d34f36 101 * 0 on success,
ethaderu 3:78f223d34f36 102 * non-0 otherwise
ethaderu 3:78f223d34f36 103 */
ethaderu 3:78f223d34f36 104 int read(char *data, int length);
ethaderu 3:78f223d34f36 105
ethaderu 3:78f223d34f36 106 /** Read a single byte from an I2C master.
ethaderu 3:78f223d34f36 107 *
ethaderu 3:78f223d34f36 108 * @returns
ethaderu 3:78f223d34f36 109 * the byte read
ethaderu 3:78f223d34f36 110 */
ethaderu 3:78f223d34f36 111 int read(void);
ethaderu 3:78f223d34f36 112
ethaderu 3:78f223d34f36 113 /** Write to an I2C master.
ethaderu 3:78f223d34f36 114 *
ethaderu 3:78f223d34f36 115 * @param data pointer to the byte array to be transmitted
ethaderu 3:78f223d34f36 116 * @param length the number of bytes to transmite
ethaderu 3:78f223d34f36 117 *
ethaderu 3:78f223d34f36 118 * @returns
ethaderu 3:78f223d34f36 119 * 0 on success,
ethaderu 3:78f223d34f36 120 * non-0 otherwise
ethaderu 3:78f223d34f36 121 */
ethaderu 3:78f223d34f36 122 int write(const char *data, int length);
ethaderu 3:78f223d34f36 123
ethaderu 3:78f223d34f36 124 /** Write a single byte to an I2C master.
ethaderu 3:78f223d34f36 125 *
ethaderu 3:78f223d34f36 126 * @data the byte to write
ethaderu 3:78f223d34f36 127 *
ethaderu 3:78f223d34f36 128 * @returns
ethaderu 3:78f223d34f36 129 * '1' if an ACK was received,
ethaderu 3:78f223d34f36 130 * '0' otherwise
ethaderu 3:78f223d34f36 131 */
ethaderu 3:78f223d34f36 132 int write(int data);
ethaderu 3:78f223d34f36 133
ethaderu 3:78f223d34f36 134 /** Sets the I2C slave address.
ethaderu 3:78f223d34f36 135 *
ethaderu 3:78f223d34f36 136 * @param address The address to set for the slave (ignoring the least
ethaderu 3:78f223d34f36 137 * signifcant bit). If set to 0, the slave will only respond to the
ethaderu 3:78f223d34f36 138 * general call address.
ethaderu 3:78f223d34f36 139 */
ethaderu 3:78f223d34f36 140 void address(int address);
ethaderu 3:78f223d34f36 141
ethaderu 3:78f223d34f36 142 /** Reset the I2C slave back into the known ready receiving state.
ethaderu 3:78f223d34f36 143 */
ethaderu 3:78f223d34f36 144 void stop(void);
ethaderu 3:78f223d34f36 145
ethaderu 3:78f223d34f36 146 protected:
ethaderu 3:78f223d34f36 147 i2c_t _i2c;
ethaderu 3:78f223d34f36 148 };
ethaderu 3:78f223d34f36 149
ethaderu 3:78f223d34f36 150 } // namespace mbed
ethaderu 3:78f223d34f36 151
ethaderu 3:78f223d34f36 152 #endif
ethaderu 3:78f223d34f36 153
ethaderu 3:78f223d34f36 154 #endif