-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a bit more support for the SenseHat. #101
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
I signed it! |
CLAs look good, thanks! |
Added usage for new BaroTemp sensor class.
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.
Thanks for submitting this pull request. I have a few comments about it, to better match our code style in other drivers.
sensehat/README.md
Outdated
@@ -54,6 +54,17 @@ display.draw(bitmap); | |||
// Close the display when done. | |||
display.close(); | |||
``` | |||
``` | |||
try (BaroTemp bt = SenseHat.openBaroTemp()) { |
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.
I think you're indenting too much here.
Your snippet also lacks a comment of what it does.
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.
Fixed. And thanks for the awesome code review!
* See also: https://www.pololu.com/file/download/LPS25H.pdf?file_id=0J761</p> | ||
* <p>Source code referenced: https://github.com/tkurbad/mipSIE/blob/master/python/AltIMU-10v5/i2c.py</p> | ||
*/ | ||
public class BaroTemp implements AutoCloseable { |
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.
Generally our class names closely reflect the actual name of the part, this being the LPS25H. Then the comments in the class would describe that it is a temperature/barometric pressure sensor.
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.
I followed the style of the original code for the LedMatrix. I'm willing to follow this suggestion in a later phase but atm time constraints prohibit me to do so now.
// 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; |
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.
Nit, code style for fields are m<Name>
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.
Ah, ok.
* 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. | ||
* | ||
* @param hPa difference to actual air pressure |
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.
Can you mention the units?
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.
Done.
} | ||
|
||
/** | ||
* 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 |
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.
Code style is that the length of each line be no more than 100 chars. Can you review your line lengths?
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.
Fixed.
public static final String BUS_DISPLAY = "I2C1"; | ||
public static final int I2C_DISPLAY_ADDRESS = 0x46; | ||
public static final int I2C_LPS25H_ADDRESS = 0x46; | ||
public static final String BUS_NAME = "I2C1"; | ||
public static final int DISPLAY_WIDTH = LedMatrix.WIDTH; | ||
public static final int DISPLAY_HEIGHT = LedMatrix.HEIGHT; | ||
|
||
public static LedMatrix openDisplay() throws IOException { |
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.
Can you add Javadoc for this method as well?
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.
Done.
} | ||
|
||
/** | ||
* Fetch raw value, see the data sheet. Note that this call waits for data to be available. |
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.
Can you link to the datasheet in this comment?
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.
There is a global reference to the datasheet for the entire class.
} | ||
|
||
/** | ||
* Fetch raw value, see the data sheet. Note that this call waits for data to be available. |
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.
Can you link to the datasheet in this comment?
import java.io.IOException; | ||
|
||
/** | ||
* This class allows access to the LPS25H on the SenseHat. |
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.
Generally with our sensors we have two classes. One implements the raw protocol, Lps25h
, and the other plugs into the sensor framework, ie. Lps25hSensorDriver
.
One example is in our BMX280
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.
I followed the style of the original code for the LedMatrix. I'm willing to follow this suggestion in a later phase but atm time constraints prohibit me to do so now.
display.draw(Color.MAGENTA); | ||
// Close the display when done. | ||
display.close(); | ||
try (LedMatrix display = SenseHat.openDisplay()) { |
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.
Should we be doing try
here? If it doesn't work, I think we'd want the test to fail.
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.
It will, there is no catch(). I changed this to use the autoclose feature which was implemented but never used / demonstrated by the original author.
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.
So what happens if SenseHat.openDisplay()
fails? Does the test still pass because it's within a try
block?
@MacroYau Yep, sorry I never got to send an update update to your PR about the SenseHat, I'd love to make progress on this and I'm happy to help with the review. |
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.
Hey I'm taking another look at this PR.
One thing that I don't see is an update to the gradle.properties
file or CHANGELOG.md
which should be updated.
@@ -4,7 +4,7 @@ Sense Hat driver for Android Things | |||
This driver provides easy access to the peripherals available on the Raspberry Pi [Sense Hat][product]: | |||
- 8x8 LED matrix | |||
- TODO: 5 buttons joystick | |||
- TODO: Sensors | |||
- TODO: Sensors other then LPS25H |
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.
I'd rephrase this list:
- 8x8 LED matrix
- LPS25H
... - TODO: Other sensors
@@ -54,6 +54,19 @@ display.draw(bitmap); | |||
// Close the display when done. | |||
display.close(); | |||
``` | |||
``` |
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.
// 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);
}
@@ -54,6 +54,19 @@ display.draw(bitmap); | |||
// Close the display when done. | |||
display.close(); | |||
``` | |||
``` | |||
// just a snippet that verifies the device is working |
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.
I'm not sure if we need the comments, as the snippet is fairly descriptive.
display.draw(Color.MAGENTA); | ||
// Close the display when done. | ||
display.close(); | ||
try (LedMatrix display = SenseHat.openDisplay()) { |
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.
So what happens if SenseHat.openDisplay()
fails? Does the test still pass because it's within a try
block?
@Test | ||
public void senseHat_BaroTemp() throws IOException { | ||
try (BaroTemp baroTemp = SenseHat.openBaroTemp()) { | ||
Log.i(BaroTemp.class.getName(),"SenseHat Barotemp raw pressure: "+baroTemp.getBarometerRaw()); |
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.
Is there any way we can add assertions to this? Can we verify that the values are non-zero?
* @param hPa difference to actual air pressure in hectoPascal (hPa) or millibar. | ||
* @throws IOException from I2cDevice | ||
*/ | ||
public void setBarometerOffset(double hPa) throws IOException { |
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.
I don't think this method does any I2C writing, so it should not need to throw IOException.
// wait for data available | ||
while (0 == (mDevice.readRegByte(LPS_STATUS_REG) & 2)) { | ||
try { | ||
Thread.sleep(1); |
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.
I'm not sure if we should sleep a thread in trying to read a value. What happens if we remove it? Will the data just not be refreshed?
* @return The raw sensor value, adjusted by the given offset (if any). | ||
* @throws IOException from I2cDevice | ||
*/ | ||
public int getBarometerRaw() throws IOException { |
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.
In general, do we need to make these raw readings public
? It doesn't seem like they'd be very useful to have.
@@ -23,12 +23,47 @@ | |||
*/ | |||
@SuppressWarnings({"unused", "WeakerAccess"}) | |||
public class SenseHat { | |||
public static final int I2C_ADDRESS = 0x46; | |||
public static final String BUS_DISPLAY = "I2C1"; | |||
public static final int I2C_DISPLAY_ADDRESS = 0x46; |
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.
We probably should not rename existing public constants as that will break the API for developers.
* @return The autoclosing LedMatrix object. | ||
* @throws IOException If caused by the hardware. | ||
*/ | ||
public static LedMatrix openDisplay(final String busName) throws IOException { |
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.
Do these need to be final?
Added support for the LPS25H on the SenseHat.