Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Walter-Stroebel
Copy link

Added support for the LPS25H on the SenseHat.

@googlebot
Copy link

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. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@Walter-Stroebel
Copy link
Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

Added usage for new BaroTemp sensor class.
@proppy
Copy link
Contributor

proppy commented May 4, 2018

Related #24 #86

Copy link
Contributor

@Fleker Fleker left a 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.

@@ -54,6 +54,17 @@ display.draw(bitmap);
// Close the display when done.
display.close();
```
```
try (BaroTemp bt = SenseHat.openBaroTemp()) {
Copy link
Contributor

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.

Copy link
Author

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 {
Copy link
Contributor

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.

Copy link
Author

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;
Copy link
Contributor

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>

Copy link
Author

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
Copy link
Contributor

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?

Copy link
Author

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
Copy link
Contributor

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?

Copy link
Author

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 {
Copy link
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
Author

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.
Copy link
Contributor

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?

Copy link
Author

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.
Copy link
Contributor

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.
Copy link
Contributor

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

Copy link
Author

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()) {
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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
Copy link

MacroYau commented May 5, 2018

Hey @proppy, what is the current plan for the Sense HAT drivers?

Is there anything I can help further #24 (and possibly #86 which fills the gap of my PR)?

@proppy
Copy link
Contributor

proppy commented May 5, 2018

@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.

@Walter-Stroebel
Copy link
Author

@MacroYau @proppy
I'm sorry, I was not aware of #24
Unfortunately time constraints prohibit me from merging the two related PR's at the moment.
Feel free to either decline my PR, merge it with #24 or merge #24 with my PR, at your convenience.

Copy link
Contributor

@Fleker Fleker left a 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
Copy link
Contributor

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();
```
```
Copy link
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);
}

@@ -54,6 +54,19 @@ display.draw(bitmap);
// Close the display when done.
display.close();
```
```
// just a snippet that verifies the device is working
Copy link
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.

display.draw(Color.MAGENTA);
// Close the display when done.
display.close();
try (LedMatrix display = SenseHat.openDisplay()) {
Copy link
Contributor

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());
Copy link
Contributor

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 {
Copy link
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.

// wait for data available
while (0 == (mDevice.readRegByte(LPS_STATUS_REG) & 2)) {
try {
Thread.sleep(1);
Copy link
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 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 {
Copy link
Contributor

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;
Copy link
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.

* @return The autoclosing LedMatrix object.
* @throws IOException If caused by the hardware.
*/
public static LedMatrix openDisplay(final String busName) throws IOException {
Copy link
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants