PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
Pokitto
Date:
Sat Oct 07 21:31:12 2017 +0000
Revision:
5:7e5c566b1760
mbed-pokitto integrated

Who changed what in which revision?

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