-
Notifications
You must be signed in to change notification settings - Fork 52
/
DefaultI2CRegister.java
158 lines (132 loc) · 4.38 KB
/
DefaultI2CRegister.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.pi4j.io.i2c.impl;
/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : DefaultI2CRegister.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.pi4j.io.i2c.I2C;
import com.pi4j.io.i2c.I2CRegister;
import com.pi4j.io.i2c.I2CRegisterDataReader;
import com.pi4j.io.i2c.I2CRegisterDataWriter;
import java.nio.charset.Charset;
import java.util.Objects;
/**
* <p>DefaultI2CRegister class.</p>
*
* @author Robert Savage (<a href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
* @version $Id: $Id
*/
public class DefaultI2CRegister implements I2CRegister, I2CRegisterDataReader, I2CRegisterDataWriter {
protected final int address;
protected final I2C i2c;
/**
* <p>Constructor for DefaultI2CRegister.</p>
*
* @param i2c a {@link com.pi4j.io.i2c.I2C} object.
* @param address a int.
*/
public DefaultI2CRegister(I2C i2c, int address){
this.i2c = i2c;
this.address = address;
}
/** {@inheritDoc} */
@Override
public int getAddress() {
return this.address;
}
/** {@inheritDoc} */
@Override
public int write(byte b) {
return this.i2c.writeRegister(this.address, b);
}
/**
* {@inheritDoc}
*
* Write a single word value (16-bit) to the I2C device register.
*/
@Override
public void writeWord(int word) {
this.i2c.writeRegisterWord(this.address, word);
}
/** {@inheritDoc} */
@Override
public int write(byte[] data, int offset, int length) {
Objects.checkFromIndexSize(offset, length, data.length);
return this.i2c.writeRegister(this.address, data, offset, length);
}
/** {@inheritDoc} */
@Override
public int readWord() {
return this.i2c.readRegisterWord(this.address);
}
/** {@inheritDoc} */
@Override
public int writeReadWord(int word) {
return this.i2c.writeReadRegisterWord(this.address, word);
}
/** {@inheritDoc} */
@Override
public int read() {
return this.i2c.readRegister(this.address);
}
/** {@inheritDoc} */
@Override
public int read(byte[] buffer, int offset, int length) {
Objects.checkFromIndexSize(offset, length, buffer.length);
return this.i2c.readRegister(this.address, buffer, offset, length);
}
/** {@inheritDoc} */
@Override
public String readString(Charset charset, int length) {
return this.i2c.readRegisterString(this.address, charset, length);
}
/** {@inheritDoc} */
@Override
public int readRegister(int register) {
return this.i2c.readRegister(register);
}
/** {@inheritDoc} */
@Override
public int readRegister(byte[] register, byte[] buffer, int offset, int length) {
return this.i2c.readRegister(register,buffer,offset,length);
}
/** {@inheritDoc} */
@Override
public int readRegister(int register, byte[] buffer, int offset, int length) {
return this.i2c.readRegister(register,buffer,offset,length);
}
/** {@inheritDoc} */
@Override
public int writeRegister(int register, byte b) {
return this.i2c.writeRegister(register, b);
}
/** {@inheritDoc} */
@Override
public int writeRegister(int register, byte[] data, int offset, int length) {
return this.i2c.writeRegister(register,data,offset,length);
}
/** {@inheritDoc} */
@Override
public int writeRegister(byte[] register, byte[] data, int offset, int length) {
return this.i2c.writeRegister(register,data,offset,length);
}
}