Skip to content

Commit

Permalink
Merge pull request #1 from floitsch/review
Browse files Browse the repository at this point in the history
Minor suggestions. thanks for the review
  • Loading branch information
xal88 authored Oct 15, 2021
2 parents df14e57 + 115fd2e commit dec993a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SI70xx

**A low level TOIT driver for the Silabs Si70xx/HTU21D sensor family.**
A low level TOIT driver for the Silabs Si70xx/HTU21D sensor family.

[ derived and based on the work from https://github.com/toitware/toit-si7006/ ]

Expand All @@ -24,20 +24,20 @@ A simple usage example.
``` toit
import gpio
import serial.protocols.i2c as i2c
import .si70xx show *
import si70xx show *
import math
main:
sda := gpio.Pin 21
scl := gpio.Pin 22
bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000
i2c_device := bus.device Si70xx.I2C_ADDRESS_40
i2c_device := bus.device Si70xx.I2C_ADDRESS
sensor := Si70xx i2c_device
print "Device Type: $sensor.device_type"
print "Firmware: $sensor.firmware"
print "SerialNr#: $sensor.serial_number"
print "Temperature: $(%0.1f sensor.temperature)C"
print "Humidity: $(sensor.humidity.round)%"
```
Expand Down
5 changes: 5 additions & 0 deletions example/package.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prefixes:
si70xx: ..
packages:
..:
path: ..
3 changes: 3 additions & 0 deletions example/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
si70xx:
path: ..
2 changes: 1 addition & 1 deletion example/si7021.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ entrypoint: si7021_example.toit
triggers:
on_install: true
on_boot: true
on_interval: 30s
on_interval: 30s
10 changes: 5 additions & 5 deletions example/si7021_example.toit
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ main:
sda := gpio.Pin 21
scl := gpio.Pin 22
bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000
i2c_device := bus.device Si70xx.I2C_ADDRESS_40
i2c_device := bus.device Si70xx.I2C_ADDRESS
sensor := Si70xx i2c_device

print "Device Type: $sensor.device_type"
print "Firmware: $sensor.firmware"
print "SerialNr#: $sensor.serial_number"

temperature := (sensor.read_temperature)
humidity := (sensor.read_humidity)

// for dewpoint calculation
// dew = (bα(T,RH)) / (a - α(T,RH))
// α(T,RH) = ln(RH/100) + aT/(b+T)
Expand All @@ -31,5 +31,5 @@ main:
"Humidity": "$(humidity.round)",
"Dewpoint": dew,
}
print THD

print THD
26 changes: 12 additions & 14 deletions src/si70xx.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@ import serial.protocols.i2c as i2c
import gpio

/**
Driver for Si70xx Family and the HTU21D Humidity and Temperature Sensor
SI7006 addr: 0x40, device_id 0x06
SI7013 addr: 0x41, device_id 0x0D
SI7020 addr: 0x40, device_id 0x14
SI7021 addr: 0x40, device_id 0x15
HTU21D is the same as SI7021
Driver for Si70xx Family and the HTU21D Humidity and Temperature Sensor.
*/

class Si70xx:
device_ /i2c.Device
static I2C_ADDRESS_40 ::= 0x40
static I2C_ADDRESS_41 ::= 0x41

/** The default I2C address for the Si70xx/HTU21D sensor family. */
static I2C_ADDRESS ::= 0x40
/** The alternate I2C address for the Si70xx/HTU21D sensor family. */
static I2C_ADDRESS_ALT ::= 0x41

constructor .device_:

static DEVICE_TYPE_ ::= {
0x06 : "Si7006",
0x13 : "Si7013",
0x14 : "Si7020",
0x15 : "Si7021",
0x22 : "Si7034",
}

static FIRMWARE_VERSIONS_ ::= {
0xff : "Version 1.0",
0x20 : "Version 2.0",
Expand All @@ -49,7 +47,7 @@ class Si70xx:
--if_present=:
fw = FIRMWARE_VERSIONS_[bytes[0]]
return fw

/**
A 64 bit serial number for the sensor. It is an error if the sensor has a
serial number that indicates it is not an Si70xx sensor.
Expand All @@ -68,8 +66,8 @@ class Si70xx:
check_crc_ bytes2 [0, 1, 3, 4] 5
DEVICE_TYPE_.get bytes2[0]
--if_absent=:
throw "Not a supported model type: 0x$(%02x bytes2[0])"
throw "Unsupported model type: 0x$(%02x bytes2[0])"

return "$(%016x (bytes1[0] << 56 | bytes1[2] << 48 | bytes1[4] << 40 | bytes1[6] << 32
| bytes2[0] << 24 | bytes2[1] << 16 | bytes2[3] << 8 | bytes2[4]) )"

Expand All @@ -84,7 +82,7 @@ class Si70xx:
type = "Unknown model type: 0x$(%02x bytes[0])"
--if_present=:
type = DEVICE_TYPE_[bytes[0]]

return type

static check_crc_ bytes/ByteArray offsets/List expected/int:
Expand Down

0 comments on commit dec993a

Please sign in to comment.