-
Notifications
You must be signed in to change notification settings - Fork 174
Added a bit more support for the SenseHat. #101
base: master
Are you sure you want to change the base?
Changes from 1 commit
30185d2
281a22a
e7c8c23
7b94d10
3eb7beb
1c64936
00a6d54
140be97
2a716a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,15 +55,17 @@ display.draw(bitmap); | |
| display.close(); | ||
| ``` | ||
| ``` | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of |
||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,16 +24,46 @@ | |
| @SuppressWarnings({"unused", "WeakerAccess"}) | ||
| public class SenseHat { | ||
| public static final int I2C_DISPLAY_ADDRESS = 0x46; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add Javadoc for this method as well?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add JavaDoc for this method?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.