Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/i2c.toit
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ main:
--sda=gpio.Pin 21
--scl=gpio.Pin 22

device := bus.device bmp180.I2C_ADDRESS
device := bus.device bmp180.I2C-ADDRESS

driver := bmp180.Driver device

print "$driver.read_temperature C"
print "$driver.read_pressure Pa"
print "$driver.read-temperature C"
print "$driver.read-pressure Pa"
102 changes: 51 additions & 51 deletions src/driver.toit
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import binary
import serial.device as serial
import serial.registers as serial

I2C_ADDRESS ::= 0x77
I2C-ADDRESS ::= 0x77

//
// Driver for the Bosch BMP180 environmental sensor with I2C interface
//
class Driver:
static CAL_COEF_AC1_ ::= 0xAA
static CAL_COEF_AC2_ ::= 0xAC
static CAL_COEF_AC3_ ::= 0xAE
static CAL_COEF_AC4_ ::= 0xB0
static CAL_COEF_AC5_ ::= 0xB2
static CAL_COEF_AC6_ ::= 0xB4
static CAL_COEF_B1_ ::= 0xB6
static CAL_COEF_B2_ ::= 0xB8
static CAL_COEF_MB_ ::= 0xBA
static CAL_COEF_MC_ ::= 0xBC
static CAL_COEF_MD_ ::= 0xBE

static REG_CONTROL_ ::= 0xF4
static REG_TEMPDATA_ ::= 0xF6
static REG_PRESSUREDATA_ ::= 0xF6
static REG_READTEMP_CMD_ ::= 0x2E
static REG_READPRESSURE_CMD_ ::= 0x34

static REG_CHIPID_ ::= 0xD0
static CAL-COEF-AC1_ ::= 0xAA
static CAL-COEF-AC2_ ::= 0xAC
static CAL-COEF-AC3_ ::= 0xAE
static CAL-COEF-AC4_ ::= 0xB0
static CAL-COEF-AC5_ ::= 0xB2
static CAL-COEF-AC6_ ::= 0xB4
static CAL-COEF-B1_ ::= 0xB6
static CAL-COEF-B2_ ::= 0xB8
static CAL-COEF-MB_ ::= 0xBA
static CAL-COEF-MC_ ::= 0xBC
static CAL-COEF-MD_ ::= 0xBE

static REG-CONTROL_ ::= 0xF4
static REG-TEMPDATA_ ::= 0xF6
static REG-PRESSUREDATA_ ::= 0xF6
static REG-READTEMP-CMD_ ::= 0x2E
static REG-READPRESSURE-CMD_ ::= 0x34

static REG-CHIPID_ ::= 0xD0

reg_/serial.Registers ::= ?

Expand All @@ -47,51 +47,51 @@ class Driver:
md := null

// hardware pressure sampling accuracy modes
// 0 = ultra low power, 1 = standard, 2 = high resolution, 3 ultra high resolution
// 0 = ultra low power, 1 = standard, 2 = high resolution, 3 ultra high resolution
oversampling := 1

constructor dev/serial.Device:
reg_ = dev.registers

tries := 5
while (reg_.read_u8 REG_CHIPID_) != 0x55:
while (reg_.read-u8 REG-CHIPID_) != 0x55:
tries--
if tries == 0: throw "INVALID_CHIP"
sleep --ms=1

// read calibration coefficients
ac1 = reg_.read_i16_be CAL_COEF_AC1_
ac2 = reg_.read_i16_be CAL_COEF_AC2_
ac3 = reg_.read_i16_be CAL_COEF_AC3_
ac4 = reg_.read_u16_be CAL_COEF_AC4_
ac5 = reg_.read_u16_be CAL_COEF_AC5_
ac6 = reg_.read_u16_be CAL_COEF_AC6_
b1 = reg_.read_i16_be CAL_COEF_B1_
b2 = reg_.read_i16_be CAL_COEF_B2_
mb = reg_.read_i16_be CAL_COEF_MB_
mc = reg_.read_i16_be CAL_COEF_MC_
md = reg_.read_i16_be CAL_COEF_MD_

// Reads the temperature and returns it in degrees Celsius
read_temperature:
ut := read_raw_temperature_
b5 := compute_b5_ ut
ac1 = reg_.read-i16-be CAL-COEF-AC1_
ac2 = reg_.read-i16-be CAL-COEF-AC2_
ac3 = reg_.read-i16-be CAL-COEF-AC3_
ac4 = reg_.read-u16-be CAL-COEF-AC4_
ac5 = reg_.read-u16-be CAL-COEF-AC5_
ac6 = reg_.read-u16-be CAL-COEF-AC6_
b1 = reg_.read-i16-be CAL-COEF-B1_
b2 = reg_.read-i16-be CAL-COEF-B2_
mb = reg_.read-i16-be CAL-COEF-MB_
mc = reg_.read-i16-be CAL-COEF-MC_
md = reg_.read-i16-be CAL-COEF-MD_

// Reads the temperature and returns it in degrees Celsius
read-temperature:
ut := read-raw-temperature_
b5 := compute-b5_ ut
t := (b5 + 8) / 16.0
t = t / 10.0
return t

// Reads the barometric pressure and returns it in Pascals
read_pressure:
ut := read_raw_temperature_
b5 := compute_b5_ ut
up := read_raw_pressure_
read-pressure:
ut := read-raw-temperature_
b5 := compute-b5_ ut
up := read-raw-pressure_

b6 := b5 - 4000
x1 := (b2 * ((b6 * b6) >> 12)) >> 11
x2 := (ac2 * b6) >> 11
x3 := x1 + x2
b3 := (((ac1 * 4 + x3) << oversampling) + 2) / 4

x1 = (ac3 * b6) >> 13
x2 = (b1 * ((b6 * b6) >> 12)) >> 16
x3 = ((x1 + x3) + 2) >> 2
Expand All @@ -112,22 +112,22 @@ class Driver:
return p

// compute b5
compute_b5_ ut:
compute-b5_ ut:
x1 := (ut - ac6) * ac5 >> 15
x2 := (mc << 11) / (x1 + md)
return x1 + x2

// read uncompensated temperature value
read_raw_temperature_ :
reg_.write_u8 REG_CONTROL_ REG_READTEMP_CMD_
read-raw-temperature_ :
reg_.write-u8 REG-CONTROL_ REG-READTEMP-CMD_
sleep --ms=5
ut := reg_.read_u16_be REG_TEMPDATA_
ut := reg_.read-u16-be REG-TEMPDATA_
return ut

// read uncompensated pressure value
read_raw_pressure_ :
reg_.write_u8 REG_CONTROL_ (REG_READPRESSURE_CMD_ + (oversampling << 6))
read-raw-pressure_ :
reg_.write-u8 REG-CONTROL_ (REG-READPRESSURE-CMD_ + (oversampling << 6))
sleep --ms=5
up := reg_.read_u24_be REG_PRESSUREDATA_
up := reg_.read-u24-be REG-PRESSUREDATA_
up >>= (8 - oversampling)
return up