-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix negative temperatures #12
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
name: dhtxx | ||
description: Drivers for the DHT11 and DHT22 (aka AM2302 or RHT03) humidity and temperature sensors. | ||
description: | | ||
Drivers for the DHT11 and DHT22 (aka AM2302 or RHT03) humidity and temperature sensors. | ||
Other compatible sensors: DHT12, KY-015, DHT33, AM2320, AM2321, or AM2322. | ||
environment: | ||
sdk: ^2.0.0-alpha.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,14 @@ import gpio | |
import io show BIG_ENDIAN | ||
import .driver_ as driver | ||
|
||
/** | ||
Driver for the DHT22 sensor. | ||
|
||
Should also work for compatible sensors like the DHT33, AM2320, AM2321, or AM2322. | ||
*/ | ||
class Dht22 extends driver.Driver: | ||
static HUMIDITY_INDEX_ ::= 0 | ||
static TEMPERATURE_INDEX_ ::= 2 | ||
|
||
/** | ||
Constructs an instance of the Dht22 driver. | ||
|
@@ -20,7 +27,21 @@ class Dht22 extends driver.Driver: | |
super pin --in_channel_id=in_channel_id --out_channel_id=out_channel_id --max_retries=max_retries | ||
|
||
parse_temperature_ data/ByteArray -> float: | ||
return (BIG_ENDIAN.uint16 data driver.Driver.TEMPERATURE_INTEGRAL_PART_) / 10.0 | ||
// The temperature is a big-endian 16-bit integer. | ||
// Some sensors use the first bit to indicate the sign of the temperature; others | ||
// encode the value as 2's complement. | ||
// Since valid temperature values can only be in a small range, we can use the | ||
// second bit to determine which approach the sensor uses. | ||
byte1 := data[TEMPERATURE_INDEX_] | ||
temperature10/int := ? | ||
if (byte1 & 0x80 == 0) or (byte1 & 0x04 == 1): | ||
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. Maybe spell out the range implied by 0x04 constant? 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 and fixed a typo. (It should have been "0x40"). 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.
That's kinda what I expected :) |
||
// The temperature is positive or the sensor uses 2's complement. | ||
temperature10 = BIG-ENDIAN.int16 data TEMPERATURE_INDEX_ | ||
else: | ||
// The temperature is negative, but the sensor uses the first bit to indicate the sign. | ||
temperature10 = BIG-ENDIAN.uint16 data TEMPERATURE_INDEX_ | ||
temperature10 = -(temperature10 & 0x7FFF) | ||
return temperature10 * 0.1 | ||
|
||
parse_humidity_ data/ByteArray -> float: | ||
return (BIG_ENDIAN.uint16 data driver.Driver.HUMIDITY_INTEGRAL_PART_) / 10.0 | ||
return (BIG_ENDIAN.uint16 data HUMIDITY_INDEX_) * 0.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.
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.