Skip to content
This repository was archived by the owner on Apr 19, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes based on Fleker's review, with thanks!
  • Loading branch information
walter committed May 5, 2018
commit 00a6d54017ab44c015754b20dc5837adbe04f6ef
20 changes: 11 additions & 9 deletions sensehat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ display.draw(bitmap);
display.close();
```
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add Java after the backticks to add code highlighting to the code block. Also, you won't need to add additional indenting.

// just a snippet that verifies the device is working
// your actual use would of course be much more awesome
try (BaroTemp bt = SenseHat.openBaroTemp()) {
    bt.setBarometerOffset(-6.2);
    Log.i(TAG, "PressureRaw: " + bt.getBarometerRaw());
    Log.i(TAG, "TemperatureRaw: " + bt.getTemperatureRaw());
    Log.i(TAG, "Pressure: " + bt.getBarometer());
    Log.i(TAG, "Temperature: " + bt.getTemperature());
} catch (Exception e) {
    Log.e(TAG, "Oops", e);
}

try (BaroTemp bt = SenseHat.openBaroTemp()) {
bt.setBarometerOffset(-6.2);
Log.i(TAG, "PressureRaw: " + bt.getBarometerRaw());
Log.i(TAG, "TemperatureRaw: " + bt.getTemperatureRaw());
Log.i(TAG, "Pressure: " + bt.getBarometer());
Log.i(TAG, "Temperature: " + bt.getTemperature());
} catch (Exception e) {
Log.e(TAG, "Oops", e);
}
// just a snippet that verifies the device is working
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need the comments, as the snippet is fairly descriptive.

// your actual use would of course be much more awesome
try (BaroTemp bt = SenseHat.openBaroTemp()) {
bt.setBarometerOffset(-6.2);
Log.i(TAG, "PressureRaw: " + bt.getBarometerRaw());
Log.i(TAG, "TemperatureRaw: " + bt.getTemperatureRaw());
Log.i(TAG, "Pressure: " + bt.getBarometer());
Log.i(TAG, "Temperature: " + bt.getTemperature());
} catch (Exception e) {
Log.e(TAG, "Oops", e);
}
```

License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BaroTemp implements AutoCloseable {
// The next two registers need special soldering
private final int LPS_RPDS_L = 0x39;// Pressure offset for differential pressure computing, low byte
private final int LPS_RPDS_H = 0x3A; // Differential offset, high byte
private int milliBarAdjust = 0;
private int mMillibarAdjust = 0;

/**
* Create a new barometric pressure and temperature sensor driver connected on the given I2C bus.
Expand Down Expand Up @@ -101,18 +101,25 @@ private int readSigned16(int a0, int a1) throws IOException {
}

/**
* The sensor seems to have an offset to the actual pressure. You can find your local "real" pressure quite easily on the web. Get the measured value from the
* sensor and compute the difference. The value obtained can be passed to this method to "calibrate" your board's sensor.
* The sensor seems to have an offset to the actual pressure. You can find your local "real"
* pressure quite easily on the web. Get the measured value from the sensor and compute the
* difference. The value obtained can be passed to this method to "calibrate" your board's
* sensor. In the author's case the difference was 6.2 hPa which is quite significant. This
* error was confirmed with another (unrelated) sensor.
*
* @param hPa difference to actual air pressure
* @param hPa difference to actual air pressure in hectoPascal (hPa) or millibar.
* @throws IOException from I2cDevice
*/
public void setBarometerOffset(double hPa) throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this method does any I2C writing, so it should not need to throw IOException.

this.milliBarAdjust = (int) Math.round(hPa * 4096);
this.mMillibarAdjust = (int) Math.round(hPa * 4096);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of this is not necessary as the m prefix to the variable name implies it is part of the class.

}

/**
* Fetch raw value, see the data sheet. Note that this call waits for data to be available.
* Fetch raw value, see the data sheet. <p>Note that this call waits for data to be available.
* From the data sheet the (selected) refresh rate is 12.5 Hz so the max wait could be
* 1000/12.5 = 80 milliseconds with an average of 40 milliseconds. Call from asynchronous code
* if this is an issue. If your code calls this method less frequently then 12.5 times per
* second there will be no wait.</p>
*
* @return The raw sensor value, adjusted by the given offset (if any).
* @throws IOException from I2cDevice
Expand All @@ -126,11 +133,15 @@ public int getBarometerRaw() throws IOException {
throw new IOException(e);
}
}
return readSigned24(LPS_PRESS_OUT_XL, LPS_PRESS_OUT_L, LPS_PRESS_OUT_H) + milliBarAdjust;
return readSigned24(LPS_PRESS_OUT_XL, LPS_PRESS_OUT_L, LPS_PRESS_OUT_H) + mMillibarAdjust;
}

/**
* Fetch raw value, see the data sheet. Note that this call waits for data to be available.
* Fetch raw value, see the data sheet. <p>Note that this call waits for data to be available.
* From the data sheet the (selected) refresh rate is 12.5 Hz so the max wait could be
* 1000/12.5 = 80 milliseconds with an average of 40 milliseconds. Call from asynchronous code
* if this is an issue. If your code calls this method less frequently then 12.5 times per
* second there will be no wait.</p>
*
* @return The raw sensor value.
* @throws IOException from I2cDevice
Expand All @@ -148,20 +159,29 @@ public int getTemperatureRaw() throws IOException {
}

/**
* Fetch air pressure in hPa (milli bar). Note that this call waits for data to be available.
* Fetch air pressure in hPa (millibar). <p>Note that this call waits for data to be available.
* From the data sheet the (selected) refresh rate is 12.5 Hz so the max wait could be
* 1000/12.5 = 80 milliseconds with an average of 40 milliseconds. Call from asynchronous code
* if this is an issue. If your code calls this method less frequently then 12.5 times per
* second there will be no wait.</p>
*
* @return The current air pressure, adjusted by the given offset (if any).
* @return The current air pressure in hPa(millibar), adjusted by the given offset (if any).
* @throws IOException from I2cDevice
*/
public double getBarometer() throws IOException {
return getBarometerRaw() / 4096.0;
}

/**
* Fetch the temperature in degrees Celcius. Note that the design of the SenseHat makes this more the
* temperature of the board then the actual (room) temperature!
* Fetch the temperature in degrees Celcius. Note that the design of the SenseHat makes this
* more the temperature of the board then the actual (room) temperature!
* <p>Also note that this call waits for data to be available.
* From the data sheet the (selected) refresh rate is 12.5 Hz so the max wait could be
* 1000/12.5 = 80 milliseconds with an average of 40 milliseconds. Call from asynchronous code
* if this is an issue. If your code calls this method less frequently then 12.5 times per
* second there will be no wait.</p>
*
* @return The temperature as reported by the sensor.
* @return The temperature as reported by the sensor in degrees Celcius.
* @throws IOException from I2cDevice
*/
public double getTemperature() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,46 @@
@SuppressWarnings({"unused", "WeakerAccess"})
public class SenseHat {
public static final int I2C_DISPLAY_ADDRESS = 0x46;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should not rename existing public constants as that will break the API for developers.

public static final int I2C_LPS25H_ADDRESS = 0x46;
public static final int I2C_LPS25H_ADDRESS = 0x5C;
public static final String BUS_NAME = "I2C1";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this bus name a parameter that can be provided, for a variety of devices with different bus names.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should! However, this is from the original code and I daresay a common issue through the other libraries as well. Maybe there should be a global device data object to address this?
Meanwhile I've provided overloaded factory methods.

public static final int DISPLAY_WIDTH = LedMatrix.WIDTH;
public static final int DISPLAY_HEIGHT = LedMatrix.HEIGHT;

/**
* Connect to the LedMatrix of the SenseHat on a Raspberry Pi 3.
* @return The autoclosing LedMatrix object.
* @throws IOException If caused by the hardware.
*/
public static LedMatrix openDisplay() throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add Javadoc for this method as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return new LedMatrix(BUS_NAME);
}

/**
* Connect to the LPS25H barometer/temperature of the SenseHat sensor on a Raspberry Pi 3.
* @return The autoclosing BaroTemp object.
* @throws IOException If caused by the hardware.
*/
public static BaroTemp openBaroTemp() throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add JavaDoc for this method?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return new BaroTemp(BUS_NAME);
}

/**
* Connect to the LedMatrix of the SenseHat.
* @param busName The name of the I2C bus device on a non-raspi3 board.
* @return The autoclosing LedMatrix object.
* @throws IOException If caused by the hardware.
*/
public static LedMatrix openDisplay(final String busName) throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these need to be final?

return new LedMatrix(busName);
}

/**
* Connect to the LPS25H barometer/temperature of the SenseHat sensor.
* @param busName The name of the I2C bus device on a non-raspi3 board.
* @return The autoclosing BaroTemp object.
* @throws IOException If caused by the hardware.
*/
public static BaroTemp openBaroTemp(final String busName) throws IOException {
return new BaroTemp(busName);
}
}