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_H
mturner5 0:cf7af2656659 17 #define MBED_I2C_H
mturner5 0:cf7af2656659 18
mturner5 0:cf7af2656659 19 #include "platform.h"
mturner5 0:cf7af2656659 20
mturner5 0:cf7af2656659 21 #if DEVICE_I2C
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 Master, used for communicating with I2C slave devices
mturner5 0:cf7af2656659 28 *
mturner5 0:cf7af2656659 29 * Example:
mturner5 0:cf7af2656659 30 * @code
mturner5 0:cf7af2656659 31 * // Read from I2C slave at address 0x62
mturner5 0:cf7af2656659 32 *
mturner5 0:cf7af2656659 33 * #include "mbed.h"
mturner5 0:cf7af2656659 34 *
mturner5 0:cf7af2656659 35 * I2C i2c(p28, p27);
mturner5 0:cf7af2656659 36 *
mturner5 0:cf7af2656659 37 * int main() {
mturner5 0:cf7af2656659 38 * int address = 0x62;
mturner5 0:cf7af2656659 39 * char data[2];
mturner5 0:cf7af2656659 40 * i2c.read(address, data, 2);
mturner5 0:cf7af2656659 41 * }
mturner5 0:cf7af2656659 42 * @endcode
mturner5 0:cf7af2656659 43 */
mturner5 0:cf7af2656659 44 class I2C {
mturner5 0:cf7af2656659 45
mturner5 0:cf7af2656659 46 public:
mturner5 0:cf7af2656659 47 enum RxStatus {
mturner5 0:cf7af2656659 48 NoData,
mturner5 0:cf7af2656659 49 MasterGeneralCall,
mturner5 0:cf7af2656659 50 MasterWrite,
mturner5 0:cf7af2656659 51 MasterRead
mturner5 0:cf7af2656659 52 };
mturner5 0:cf7af2656659 53
mturner5 0:cf7af2656659 54 enum Acknowledge {
mturner5 0:cf7af2656659 55 NoACK = 0,
mturner5 0:cf7af2656659 56 ACK = 1
mturner5 0:cf7af2656659 57 };
mturner5 0:cf7af2656659 58
mturner5 0:cf7af2656659 59 /** Create an I2C Master interface, connected to the specified pins
mturner5 0:cf7af2656659 60 *
mturner5 0:cf7af2656659 61 * @param sda I2C data line pin
mturner5 0:cf7af2656659 62 * @param scl I2C clock line pin
mturner5 0:cf7af2656659 63 */
mturner5 0:cf7af2656659 64 I2C(PinName sda, PinName scl);
mturner5 0:cf7af2656659 65
mturner5 0:cf7af2656659 66 /** Set the frequency of the I2C interface
mturner5 0:cf7af2656659 67 *
mturner5 0:cf7af2656659 68 * @param hz The bus frequency in hertz
mturner5 0:cf7af2656659 69 */
mturner5 0:cf7af2656659 70 void frequency(int hz);
mturner5 0:cf7af2656659 71
mturner5 0:cf7af2656659 72 /** Read from an I2C slave
mturner5 0:cf7af2656659 73 *
mturner5 0:cf7af2656659 74 * Performs a complete read transaction. The bottom bit of
mturner5 0:cf7af2656659 75 * the address is forced to 1 to indicate a read.
mturner5 0:cf7af2656659 76 *
mturner5 0:cf7af2656659 77 * @param address 8-bit I2C slave address [ addr | 1 ]
mturner5 0:cf7af2656659 78 * @param data Pointer to the byte-array to read data in to
mturner5 0:cf7af2656659 79 * @param length Number of bytes to read
mturner5 0:cf7af2656659 80 * @param repeated Repeated start, true - don't send stop at end
mturner5 0:cf7af2656659 81 *
mturner5 0:cf7af2656659 82 * @returns
mturner5 0:cf7af2656659 83 * 0 on success (ack),
mturner5 0:cf7af2656659 84 * non-0 on failure (nack)
mturner5 0:cf7af2656659 85 */
mturner5 0:cf7af2656659 86 int read(int address, char *data, int length, bool repeated = false);
mturner5 0:cf7af2656659 87
mturner5 0:cf7af2656659 88 /** Read a single byte from the I2C bus
mturner5 0:cf7af2656659 89 *
mturner5 0:cf7af2656659 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
mturner5 0:cf7af2656659 91 *
mturner5 0:cf7af2656659 92 * @returns
mturner5 0:cf7af2656659 93 * the byte read
mturner5 0:cf7af2656659 94 */
mturner5 0:cf7af2656659 95 int read(int ack);
mturner5 0:cf7af2656659 96
mturner5 0:cf7af2656659 97 /** Write to an I2C slave
mturner5 0:cf7af2656659 98 *
mturner5 0:cf7af2656659 99 * Performs a complete write transaction. The bottom bit of
mturner5 0:cf7af2656659 100 * the address is forced to 0 to indicate a write.
mturner5 0:cf7af2656659 101 *
mturner5 0:cf7af2656659 102 * @param address 8-bit I2C slave address [ addr | 0 ]
mturner5 0:cf7af2656659 103 * @param data Pointer to the byte-array data to send
mturner5 0:cf7af2656659 104 * @param length Number of bytes to send
mturner5 0:cf7af2656659 105 * @param repeated Repeated start, true - do not send stop at end
mturner5 0:cf7af2656659 106 *
mturner5 0:cf7af2656659 107 * @returns
mturner5 0:cf7af2656659 108 * 0 on success (ack),
mturner5 0:cf7af2656659 109 * non-0 on failure (nack)
mturner5 0:cf7af2656659 110 */
mturner5 0:cf7af2656659 111 int write(int address, const char *data, int length, bool repeated = false);
mturner5 0:cf7af2656659 112
mturner5 0:cf7af2656659 113 /** Write single byte out on the I2C bus
mturner5 0:cf7af2656659 114 *
mturner5 0:cf7af2656659 115 * @param data data to write out on bus
mturner5 0:cf7af2656659 116 *
mturner5 0:cf7af2656659 117 * @returns
mturner5 0:cf7af2656659 118 * '1' if an ACK was received,
mturner5 0:cf7af2656659 119 * '0' otherwise
mturner5 0:cf7af2656659 120 */
mturner5 0:cf7af2656659 121 int write(int data);
mturner5 0:cf7af2656659 122
mturner5 0:cf7af2656659 123 /** Creates a start condition on the I2C bus
mturner5 0:cf7af2656659 124 */
mturner5 0:cf7af2656659 125
mturner5 0:cf7af2656659 126 void start(void);
mturner5 0:cf7af2656659 127
mturner5 0:cf7af2656659 128 /** Creates a stop condition on the I2C bus
mturner5 0:cf7af2656659 129 */
mturner5 0:cf7af2656659 130 void stop(void);
mturner5 0:cf7af2656659 131
mturner5 0:cf7af2656659 132 protected:
mturner5 0:cf7af2656659 133 void aquire();
mturner5 0:cf7af2656659 134
mturner5 0:cf7af2656659 135 i2c_t _i2c;
mturner5 0:cf7af2656659 136 static I2C *_owner;
mturner5 0:cf7af2656659 137 int _hz;
mturner5 0:cf7af2656659 138 };
mturner5 0:cf7af2656659 139
mturner5 0:cf7af2656659 140 } // namespace mbed
mturner5 0:cf7af2656659 141
mturner5 0:cf7af2656659 142 #endif
mturner5 0:cf7af2656659 143
mturner5 0:cf7af2656659 144 #endif