NXShield  v1.07
NXShield Library Reference by OpenElectrons.com
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
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  return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
52 }
53 
55 inline void writeByteToBuffer(uint8_t* buf, uint8_t data)
56 {
57  buf[0] = data;
58 }
59 
60 inline void writeByteToBuffer(uint8_t* buf, int8_t data)
61 {
62  writeByteToBuffer(buf, (uint8_t)data);
63 }
64 
66 inline void writeIntToBuffer(uint8_t* buf, uint16_t data)
67 {
68  buf[0] = data & 0xFF;
69  buf[1] = (data >> 8) & 0xFF;
70 }
71 
72 inline void writeIntToBuffer(uint8_t* buf, int16_t data)
73 {
74  writeIntToBuffer(buf, (uint16_t)data);
75 }
76 
78 inline void writeLongToBuffer(uint8_t* buf, uint32_t data)
79 {
80  buf[0] = data & 0xFF;
81  buf[1] = (data >> 8) & 0xFF;
82  buf[2] = (data >> 16) & 0xFF;
83  buf[3] = (data >> 24) & 0xFF;
84 }
85 
86 inline void writeLongToBuffer(uint8_t* buf, int32_t data)
87 {
88  writeLongToBuffer(buf, (uint32_t)data);
89 }
90 
95 {
96  // Note that this class is a base class, but not an abstract base class
97  // Feel free to instantiate BaseI2CDevice.
98 
99 public:
101  BaseI2CDevice(uint8_t i2c_address);
102 
104  void initProtocol();
105 
114  uint8_t* readRegisters (uint8_t start_register, uint8_t bytes_to_read,
115  uint8_t* buffer = 0, uint8_t buffer_length = 0, bool clear_buffer = false);
116 
121  uint8_t readByte (uint8_t location);
122 
127  int16_t readInteger (uint8_t location);
128 
133  uint32_t readLong (uint8_t location);
134 
142  char* readString (uint8_t location, uint8_t bytes_to_read,
143  uint8_t* buffer = 0, uint8_t buffer_length = 0);
144 
145 
151  bool writeRegisters (uint8_t start_register, uint8_t bytes_to_write,
152  uint8_t* buffer = 0);
153 
158  bool writeByte (uint8_t location, uint8_t data);
159 
164  bool writeInteger(uint8_t location, uint16_t data);
165 
170  bool writeLong (uint8_t location, uint32_t data);
171 
173  bool checkAddress();
174 
178  bool setAddress(uint8_t i2c_address);
179 
181  uint8_t getAddress();
182 
184  uint8_t getWriteErrorCode();
185 
187  char* getFirmwareVersion();
188 
190  char* getVendorID();
191 
193  char* getDeviceID();
194 
196  char* getFeatureSet();
197 
200  static uint8_t* _buffer;
201 
202  static bool b_initialized;
203 
204 protected:
207  void setWriteErrorCode(uint8_t code);
208 
209 private:
210  uint8_t _device_address; // I2C address of the I2C device
211  uint8_t _write_error_code; // Error code from last write
212 };
213 
214 #endif // BASEI2CDEVICE_H
uint8_t getAddress()
Definition: BaseI2CDevice.cpp:292
void initProtocol()
Definition: BaseI2CDevice.cpp:53
uint32_t readLong(uint8_t location)
Definition: BaseI2CDevice.cpp:133
bool writeLong(uint8_t location, uint32_t data)
Definition: BaseI2CDevice.cpp:202
char * getFirmwareVersion()
Definition: BaseI2CDevice.cpp:222
bool writeRegisters(uint8_t start_register, uint8_t bytes_to_write, uint8_t *buffer=0)
Definition: BaseI2CDevice.cpp:155
uint8_t readByte(uint8_t location)
Definition: BaseI2CDevice.cpp:117
char * getVendorID()
Definition: BaseI2CDevice.cpp:228
uint8_t getWriteErrorCode()
Definition: BaseI2CDevice.cpp:215
bool writeByte(uint8_t location, uint8_t data)
Definition: BaseI2CDevice.cpp:189
bool setAddress(uint8_t i2c_address)
Definition: BaseI2CDevice.cpp:286
bool writeInteger(uint8_t location, uint16_t data)
Definition: BaseI2CDevice.cpp:195
bool checkAddress()
Definition: BaseI2CDevice.cpp:260
char * readString(uint8_t location, uint8_t bytes_to_read, uint8_t *buffer=0, uint8_t buffer_length=0)
Definition: BaseI2CDevice.cpp:141
BaseI2CDevice(uint8_t i2c_address)
Definition: BaseI2CDevice.cpp:39
char * getDeviceID()
Definition: BaseI2CDevice.cpp:234
char * getFeatureSet()
Definition: BaseI2CDevice.cpp:242
static uint8_t * _buffer
Definition: BaseI2CDevice.h:200
int16_t readInteger(uint8_t location)
Definition: BaseI2CDevice.cpp:124
void setWriteErrorCode(uint8_t code)
Definition: BaseI2CDevice.cpp:248
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:65
This class implements hardware I2C protocol used by NXShield on an Arduino.
Definition: BaseI2CDevice.h:94