Skip to content

Commit f267237

Browse files
authored
Merge pull request #128 from Pi4J/bmp280_writeThenRead
2 parents 29d694a + 695c128 commit f267237

8 files changed

Lines changed: 54 additions & 373 deletions

File tree

src/main/java/com/pi4j/devices/bmp280/BMP280Device.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ public abstract class BMP280Device implements BMP280Interface {
6161
public static final String ID = "BMP280";
6262

6363

64-
private static final int t1 = 0;
65-
private static final int t2 = 1;
66-
private static final int t3 = 2;
67-
private static final int p1 = 3;
68-
private static final int p2 = 4;
69-
private static final int p3 = 5;
70-
private static final int p4 = 6;
71-
private static final int p5 = 7;
72-
private static final int p6 = 8;
73-
private static final int p7 = 9;
74-
private static final int p8 = 10;
75-
private static final int p9 = 11;
7664

7765

7866
protected Logger logger;
@@ -167,7 +155,11 @@ public double[] readBMP280() {
167155

168156
this.writeRegister(BMP280Declares.ctrl_meas, ctlVal[0]);
169157

170-
// this.writeRegister(BMP280Declares.ctrl_meas, ctlReg);
158+
159+
byte[] writeBuffer = new byte[]{(byte) (0b01111111 & BMP280Declares.ctrl_meas),
160+
(byte) ctlVal[0], (byte) (0b10000000 | BMP280Declares.reg_dig_t1) } ;
161+
byte[] readBuffer = new byte[24];
162+
171163

172164

173165
// Next delay for 100 ms to provide chip time to perform measurements
@@ -182,56 +174,49 @@ public double[] readBMP280() {
182174
byte[] compVal = new byte[2];
183175

184176

185-
this.readRegister(BMP280Declares.reg_dig_t1, compVal);
177+
this.writeDelayRead(writeBuffer, (short)100, readBuffer) ;
186178

187-
long dig_t1 = castOffSignInt(compVal);
188179

180+
long dig_t1 = castOffSignInt(new byte[]{readBuffer[0],readBuffer[1]}) ;
189181
this.readRegister(BMP280Declares.reg_dig_t2, compVal);
190-
int dig_t2 = signedInt(compVal);
182+
int dig_t2 = signedInt(new byte[]{readBuffer[2],readBuffer[3]});
191183

192184
this.readRegister(BMP280Declares.reg_dig_t3, compVal);
193-
int dig_t3 = signedInt(compVal);
185+
int dig_t3 = signedInt(new byte[]{readBuffer[4],readBuffer[5]});
194186

195187
this.readRegister(BMP280Declares.reg_dig_p1, compVal);
196-
long dig_p1 = castOffSignInt(compVal);
188+
long dig_p1 = castOffSignInt(new byte[]{readBuffer[6],readBuffer[7]});
197189

198190
this.readRegister(BMP280Declares.reg_dig_p2, compVal);
199-
int dig_p2 = signedInt(compVal);
191+
int dig_p2 = signedInt(new byte[]{readBuffer[8],readBuffer[9]});
200192

201193
this.readRegister(BMP280Declares.reg_dig_p3, compVal);
202-
int dig_p3 = signedInt(compVal);
194+
int dig_p3 = signedInt(new byte[]{readBuffer[10],readBuffer[11]});
203195

204196
this.readRegister(BMP280Declares.reg_dig_p4, compVal);
205-
int dig_p4 = signedInt(compVal);
197+
int dig_p4 = signedInt(new byte[]{readBuffer[12],readBuffer[13]});
206198

207199
this.readRegister(BMP280Declares.reg_dig_p5, compVal);
208-
int dig_p5 = signedInt(compVal);
200+
int dig_p5 = signedInt(new byte[]{readBuffer[14],readBuffer[15]});
209201

210202
this.readRegister(BMP280Declares.reg_dig_p6, compVal);
211-
int dig_p6 = signedInt(compVal);
203+
int dig_p6 = signedInt(new byte[]{readBuffer[16],readBuffer[17]});
212204

213205
this.readRegister(BMP280Declares.reg_dig_p7, compVal);
214-
int dig_p7 = signedInt(compVal);
206+
int dig_p7 = signedInt(new byte[]{readBuffer[18],readBuffer[19]});
215207

216208
this.readRegister(BMP280Declares.reg_dig_p8, compVal);
217-
int dig_p8 = signedInt(compVal);
209+
int dig_p8 = signedInt(new byte[]{readBuffer[20],readBuffer[21]});
218210

219211
this.readRegister(BMP280Declares.reg_dig_p9, compVal);
220-
int dig_p9 = signedInt(compVal);
212+
int dig_p9 = signedInt(new byte[]{readBuffer[22],readBuffer[23]});
221213

222214

223215
byte[] buff = new byte[6];
224216

225217
this.readRegister(BMP280Declares.press_msb, buff);
226218

227219

228-
int p_msb = castOffSignByte(buff[0]);
229-
int p_lsb = castOffSignByte(buff[1]);
230-
int p_xlsb = castOffSignByte(buff[2]);
231-
232-
int t_msb = castOffSignByte(buff[3]);
233-
int t_lsb = castOffSignByte(buff[4]);
234-
int t_xlsb = castOffSignByte(buff[5]);
235220

236221

237222
long adc_T = (long) ((buff[3] & 0xFF) << 12) + (long) ((buff[4] & 0xFF) << 4) + (long) (buff[5] & 0xFF);

src/main/java/com/pi4j/devices/bmp280/BMP280DeviceI2C.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,18 @@ public int writeRegister(int register, int data) {
191191
return (rval);
192192
}
193193

194+
@Override
195+
public void writeDelayRead(byte[] writeData, short afterWriteDelay, byte[] readData) {
196+
197+
this.i2c.writeRegister(BMP280Declares.ctrl_meas, writeData);
198+
// Next delay for 100 ms to provide chip time to perform measurements
199+
try {
200+
Thread.sleep(afterWriteDelay);
201+
} catch (InterruptedException e) {
202+
e.printStackTrace();
203+
}
204+
this.i2c.readRegister(BMP280Declares.reg_dig_t1, readData);
205+
}
206+
194207

195208
}

src/main/java/com/pi4j/devices/bmp280/BMP280DeviceSPI.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ public class BMP280DeviceSPI extends BMP280Device {
6262
// local/internal SPI reference for communication with hardware chip
6363
Spi spi = null;
6464

65-
// SPI Provider name and unique ID
66-
/**
67-
* Constant <code>SPI_PROVIDER_NAME="NAME + SPI Provider"</code>
68-
*/
69-
public final String SPI_PROVIDER_NAME = NAME + " BMP280 SPI Provider";
70-
/**
71-
* Constant <code>SPI_PROVIDER_ID="ID + -spi"</code>
72-
*/
73-
public final String SPI_PROVIDER_ID = ID + "-spi";
7465

7566

7667
public BMP280DeviceSPI(Context pi4j, Console console, SpiBus spiBus, SpiChipSelect chipSlct, String traceLevel) {
@@ -118,7 +109,7 @@ private void createSPIDevice() {
118109

119110

120111
/**
121-
* @param register
112+
* @param register address of register to read
122113
* @return 8bit value read from register
123114
*/
124115
public int readRegister(int register) {
@@ -156,12 +147,24 @@ public int writeRegister(int register, int data) {
156147
byte[] buffer = new byte[]{(byte) (0b01111111 & register),
157148
(byte) data
158149
};
159-
byte[] dummy = new byte[2];
160150
// send read request to BMP chip via SPI channel
161151
byteswritten = this.spi.write(buffer);
162152

163153
this.logger.trace("<<< Exit writeRegister wrote : " + byteswritten);
164154
return (rval);
165155
}
156+
/**
157+
* @param writeData bytes to write
158+
* @param readData bytes to read
159+
* @param afterWriteDelay MS time to delay after write
160+
*/
161+
public void writeDelayRead(byte[] writeData, short afterWriteDelay, byte[] readData) {
162+
this.logger.trace(">>> Enter writeDelayRead ");
163+
164+
// send read request to BMP chip via SPI channel
165+
this.spi.writeThenRead(writeData, 0, writeData.length, afterWriteDelay,readData, 0, readData.length);
166+
167+
this.logger.trace("<<< Exit writeDelayRead ");
168+
}
166169
}
167170

src/main/java/com/pi4j/devices/bmp280/BMP280Interface.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ public interface BMP280Interface {
116116
* @return bytes written, else -1
117117
*/
118118
int writeRegister(int register, int data);
119+
/**
120+
* @param writeData bytes to write
121+
* @param readData bytes to read
122+
* @param afterWriteDelay MS time to delay after write
123+
*/
124+
public void writeDelayRead(byte[] writeData, short afterWriteDelay, byte[] readData) ;
125+
119126

120-
}
127+
}
121128

122129

src/main/java/com/pi4j/devices/cp2102n/CP2102N.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)