| EVShield v1.3 EVShield Library Reference by mindsensors.com

BaseI2CDevice.h
1
2 // BaseI2CDevice.h
3 //
4 // This is a class for controlling some devices that
5 // communicate using the I2C protocol.
6 //
7 // Use at your own risk!
8 //
9 // Initial version: 2010-05-31 by Clinton Blackmore
10
11 /*
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2.1 of the License, or (at your option) any later version.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /* Change log
28 * 07/07/11 Deepak Patil Added getFeatureSet.
29 */
30
31 #ifndef BASEI2CDEVICE_H
32 #define BASEI2CDEVICE_H
33
34 #if defined(ARDUINO) && ARDUINO >= 100
35 #include "Arduino.h"
36 #else
37 #include "WProgram.h"
38 #endif
39
40 #include <inttypes.h>
41
43 inline uint16_t readIntFromBuffer(uint8_t* buf)
44 {
45 return buf[0] | (buf[1] << 8);
46 }
47
49 inline uint32_t readLongFromBuffer(uint8_t* buf)
50 {
51 /* typecasts added to make it compatible with 1.6.8 */
52 return (uint32_t)buf[0] |
53 (((uint32_t)buf[1]) << 8) |
54 (((uint32_t)buf[2]) << 16) |
55 (((uint32_t)buf[3]) << 24);
56 }
57
59 inline void writeByteToBuffer(uint8_t* buf, uint8_t data)
60 {
61 buf[0] = data;
62 }
63
64 inline void writeByteToBuffer(uint8_t* buf, int8_t data)
65 {
66 writeByteToBuffer(buf, (uint8_t)data);
67 }
68
70 inline void writeIntToBuffer(uint8_t* buf, uint16_t data)
71 {
72 buf[0] = data & 0xFF;
73 buf[1] = (data >> 8) & 0xFF;
74 }
75
76 inline void writeIntToBuffer(uint8_t* buf, int16_t data)
77 {
78 writeIntToBuffer(buf, (uint16_t)data);
79 }
80
82 inline void writeLongToBuffer(uint8_t* buf, uint32_t data)
83 {
84 buf[0] = data & 0xFF;
85 buf[1] = (data >> 8) & 0xFF;
86 buf[2] = (data >> 16) & 0xFF;
87 buf[3] = (data >> 24) & 0xFF;
88 }
89
90 inline void writeLongToBuffer(uint8_t* buf, int32_t data)
91 {
92 writeLongToBuffer(buf, (uint32_t)data);
93 }
94
98 class BaseI2CDevice
99 {
100 // Note that this class is a base class, but not an abstract base class
101 // Feel free to instantiate BaseI2CDevice.
102
103 public:
105 BaseI2CDevice(uint8_t i2c_address);
106
108 void initProtocol();
109
118 uint8_t* readRegisters (uint8_t start_register, uint8_t bytes_to_read,
119 uint8_t* buffer = 0, uint8_t buffer_length = 0, bool clear_buffer = false);
120
125 uint8_t readByte (uint8_t location);
126
131 int16_t readInteger (uint8_t location);
132
137 uint32_t readLong (uint8_t location);
138
146 char* readString (uint8_t location, uint8_t bytes_to_read,
147 uint8_t* buffer = 0, uint8_t buffer_length = 0);
148
149
155 bool writeRegisters (uint8_t start_register, uint8_t bytes_to_write,
156 uint8_t* buffer = 0);
157
162 bool writeByte (uint8_t location, uint8_t data);
163
168 bool writeInteger(uint8_t location, uint16_t data);
169
174 bool writeLong (uint8_t location, uint32_t data);
175
177 bool checkAddress();
178
182 bool setAddress(uint8_t i2c_address);
183
185 uint8_t getAddress();
186
188 uint8_t getWriteErrorCode();
189
191 char* getFirmwareVersion();
192
194 char* getVendorID();
195
197 char* getDeviceID();
198
200 char* getFeatureSet();
201
204 static uint8_t* _buffer;
205
206 static bool b_initialized;
207
208 protected:
211 void setWriteErrorCode(uint8_t code);
212
213 private:
214 uint8_t _device_address; // I2C address of the I2C device
215 uint8_t _write_error_code; // Error code from last write
216 };
217
218 #endif // BASEI2CDEVICE_H
BaseI2CDevice::getAddress
uint8_t getAddress()
Definition: BaseI2CDevice.cpp:331
BaseI2CDevice::initProtocol
void initProtocol()
Definition: BaseI2CDevice.cpp:68
BaseI2CDevice::readLong
uint32_t readLong(uint8_t location)
Definition: BaseI2CDevice.cpp:162
BaseI2CDevice::writeLong
bool writeLong(uint8_t location, uint32_t data)
Definition: BaseI2CDevice.cpp:241
BaseI2CDevice::getFirmwareVersion
char * getFirmwareVersion()
Definition: BaseI2CDevice.cpp:261
BaseI2CDevice::writeRegisters
bool writeRegisters(uint8_t start_register, uint8_t bytes_to_write, uint8_t *buffer=0)
Definition: BaseI2CDevice.cpp:184
BaseI2CDevice::readByte
uint8_t readByte(uint8_t location)
Definition: BaseI2CDevice.cpp:146
BaseI2CDevice::getVendorID
char * getVendorID()
Definition: BaseI2CDevice.cpp:267
BaseI2CDevice::getWriteErrorCode
uint8_t getWriteErrorCode()
Definition: BaseI2CDevice.cpp:254
BaseI2CDevice::writeByte
bool writeByte(uint8_t location, uint8_t data)
Definition: BaseI2CDevice.cpp:228
BaseI2CDevice::setAddress
bool setAddress(uint8_t i2c_address)
Definition: BaseI2CDevice.cpp:325
BaseI2CDevice::writeInteger
bool writeInteger(uint8_t location, uint16_t data)
Definition: BaseI2CDevice.cpp:234
BaseI2CDevice::checkAddress
bool checkAddress()
Definition: BaseI2CDevice.cpp:299
BaseI2CDevice::readString
char * readString(uint8_t location, uint8_t bytes_to_read, uint8_t *buffer=0, uint8_t buffer_length=0)
Definition: BaseI2CDevice.cpp:170
BaseI2CDevice::BaseI2CDevice
BaseI2CDevice(uint8_t i2c_address)
Definition: BaseI2CDevice.cpp:54
BaseI2CDevice::getDeviceID
char * getDeviceID()
Definition: BaseI2CDevice.cpp:273
BaseI2CDevice::getFeatureSet
char * getFeatureSet()
Definition: BaseI2CDevice.cpp:281
BaseI2CDevice::_buffer
static uint8_t * _buffer
Definition: BaseI2CDevice.h:204
BaseI2CDevice::readInteger
int16_t readInteger(uint8_t location)
Definition: BaseI2CDevice.cpp:153
BaseI2CDevice::setWriteErrorCode
void setWriteErrorCode(uint8_t code)
Definition: BaseI2CDevice.cpp:287
BaseI2CDevice::readRegisters
uint8_t * readRegisters(uint8_t start_register, uint8_t bytes_to_read, uint8_t *buffer=0, uint8_t buffer_length=0, bool clear_buffer=false)
Definition: BaseI2CDevice.cpp:84
BaseI2CDevice
This class implements hardware I2C protocol used by EVShield/NXShield on an Arduino.
Definition: BaseI2CDevice.h:98