UVic Assistive Technology Lab / Mbed 2 deprecated DSLR_Camera_Gimbal

Dependencies:   mbed ros_lib_kinetic

Committer:
group-UVic-Assistive-Technolog
Date:
Wed Jan 31 05:24:12 2018 +0000
Revision:
0:3a767f41cf04
Initial commit

Who changed what in which revision?

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