Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

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