/
Board.java
342 lines (307 loc) · 9.3 KB
/
Board.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* #%L
* **********************************************************************
* ORGANIZATION : DexterIndustries
* PROJECT : GoPiGo Java Library
* FILENAME : Board.java
* AUTHOR : Marcello Barile <marcello.barile@gmail.com>
*
* This file is part of the GoPiGo Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GoPiGo
* **********************************************************************
* %%
* GoPiGo for the Raspberry Pi: an open source robotics platform for the Raspberry Pi.
* Copyright (C) 2017 Dexter Industries
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
*
* #L%
*/
package com.dexterind.gopigo.components;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.util.*;
import com.dexterind.gopigo.*;
import com.dexterind.gopigo.events.StatusEvent;
import com.dexterind.gopigo.utils.*;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import com.pi4j.system.NetworkInfo;
import com.pi4j.system.SystemInfo;
/**
* Defines all the methods to get access to the resources connected to the board.
* It also returns the status, the voltage and the firmware version of the board.
* @author marcello
*
*/
public class Board {
/**
* The instance of the current object.
*/
private static Board instance = null;
/**
* The I2CDevice object.
*/
private final I2CDevice device;
/**
* The output mode for the pin.
*/
private static final byte PIN_MODE_OUTPUT = 1;
/**
* The input mode for the pin.
*/
private static final byte PIN_MODE_INPUT = 0;
/**
* The device's address.
*/
private static final byte ADDRESS = 0x08;
/**
* The debug object.
*/
private Debug debug;
public Board() throws IOException, InterruptedException {
int busId;
String type = SystemInfo.getBoardType().name();
if (type.indexOf("ModelA") > 0) {
busId = I2CBus.BUS_0;
} else {
busId = I2CBus.BUS_1;
}
I2CBus bus = null;
try {
bus = I2CFactory.getInstance(busId);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
device = bus.getDevice(ADDRESS);
}
/**
* Provides a global point of access to the Board instance.
* @return the <code>Board</code> instance.
*/
public static Board getInstance() throws IOException, InterruptedException {
if(instance == null) {
instance = new Board();
}
return instance;
}
/**
* Writes the bytes to the I2C device.
* @param bytes The buffer to write.
* @return A status code.
* @throws IOException
*/
public int writeI2c(int... bytes) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
// Convert array: int[] to byte[]
final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
for (int i = 0, len = bytes.length; i < len; i++) {
byteBuffer.put((byte) bytes[i]);
}
sleep(100); // TODO: Is correct to have this here or we let the external calls to handle the sleep?
device.write(0xfe, byteBuffer.array(), 0, byteBuffer.limit());
return Statuses.OK;
}
/**
* Reads the number of bytes from the I2C device.
* @param numberOfBytes The length of the buffer to read.
* @return The buffer.
* @throws IOException
*/
public byte[] readI2c(int numberOfBytes) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
byte[] buffer = new byte[numberOfBytes];
device.read(1, buffer, 0, buffer.length);
return buffer;
}
/**
* Executes a digital read on the pin.
* @param pin The pin to use for the reading.
* @return The read bytes.
* @throws IOException
*/
public int digitalRead(int pin) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
writeI2c(Commands.DIGITAL_READ, pin, Commands.UNUSED, Commands.UNUSED);
return device.read() & 0xff;
}
/**
* Executes a digital write on the pin.
* @param pin The ping to use for the writing.
* @param value The value to write.
* @return A status code.
* @throws IOException
*/
public int digitalWrite(int pin, int value) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
// if (pin == 10 || pin == 0 || pin == 1 || pin == 5 || pin == 16 || pin == 17)
if (value == 0 || value == 1) {
return writeI2c(Commands.DIGITAL_WRITE, pin, value, Commands.UNUSED);
} else {
return Statuses.ERROR;
}
}
/**
* Executes an analog read on the pin.
* @param pin The ping to use for the reading.
* @return The read value.
* @throws IOException
*/
public int analogRead(int pin) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
//if (pin == 1) {
writeI2c(Commands.ANALOG_READ, pin, Commands.UNUSED, Commands.UNUSED);
sleep(50);
byte[] b1 = readI2c(1);
byte[] b2 = readI2c(1); // Note: before was 2
int val1 = (int)b1[0] & 0xFF;
int val2 = (int)b2[0] & 0xFF;
return val1 * 256 + val2;
/*} else {
return -2;
}*/
}
/**
* Executes an analog write on the pin.
* @param pin The ping to use for the writing.
* @param value The value to write.
* @return A status code.
* @throws IOException
*/
public int analogWrite(int pin, int value) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
if (pin == 10) {
writeI2c(Commands.ANALOG_WRITE, pin, value, Commands.UNUSED);
sleep(5);
return Statuses.OK;
} else {
return -2; // TODO: Which kind of status is "-2"?
}
}
/**
* Sets the pin mode to use on the pin.
* @param pin The ping to set.
* @param pinMode The mode to set. 1 is OUTPUT, 0 is INPUT
* @return A status code.
* @throws IOException
*/
public int setPinMode(int pin, int pinMode) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
// if (pin == 10 || pin == 15 || pin == 0 || pin == 1)
return writeI2c(Commands.PIN_MODE, pin, pinMode, Commands.UNUSED);
}
/**
* Executes a sleep on the thread for the number of milliseconds.
* @param msec The value of the sleep in milliseconds.
*/
public void sleep(int msec) {
try {
Thread.sleep(msec);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
/**
* Initializes the board executing a writing test catching the error.
*/
public void init() {
try {
device.write(0xfe, (byte)0x04);
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
debug.log(Debug.SEVERE, exceptionDetails);
Gopigo.getInstance().halt();
}
}
/**
* Returns the current voltage on the board.
* @return The current voltage in volts.
* @throws IOException
*/
public double volt() throws IOException {
writeI2c(Commands.VOLT, Commands.UNUSED, Commands.UNUSED, Commands.UNUSED);
sleep(100);
byte[] b1 = readI2c(1);
byte[] b2 = readI2c(1);
int val1 = (int)b1[0] & 0xFF;
int val2 = (int)b2[0] & 0xFF;
if (val1 != -1 && val2 != -1) {
double v = val1 * 256 + val2;
v = (5 * (float) v / 1024) / .4;
v = Math.round(v * 100.0) / 100.0;
return v;
} else {
return Statuses.ERROR;
}
}
/**
* Returns the firmware version.
* @return The firmware version.
* @throws IOException
*/
public float version() throws IOException {
writeI2c(Commands.FW_VER, Commands.UNUSED, Commands.UNUSED, Commands.UNUSED);
sleep(100);
byte[] ver = readI2c(1);
readI2c(1);
return (float)ver[0] / 10;
}
/**
* Returns the board revision.
* @return The board revision.
* @throws IOException
*/
public int revision() throws IOException {
writeI2c(Commands.ANALOG_READ, 7, Commands.UNUSED, Commands.UNUSED);
sleep(100);
byte[] b1 = readI2c(1);
byte[] b2 = readI2c(1);
int val1 = (int)b1[0] & 0xFF;
int val2 = (int)b2[0] & 0xFF;
if (val1 != -1 && val2 != -1) {
int v = val1 * 256 + val2;
return v;
} else {
return Statuses.ERROR;
}
}
/**
* Reads the current status.
* @return Returns the current status.
* @throws IOException
*/
public int[] readStatus() throws IOException {
byte[] status = readI2c(1);
int[] status_reg = new int[2];
status_reg[0] = status[0] & (1 << 0);
status_reg[1] = (status[0] & (1 << 1)) / 2;
return status_reg;
}
}