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