mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 14:14:00 +00:00
bmp280: remove alloc on read sensor data
This commit is contained in:
committed by
Ron Evans
parent
dbc9022f6a
commit
c4168864fd
+7
-8
@@ -23,6 +23,7 @@ type Filter uint
|
|||||||
type Device struct {
|
type Device struct {
|
||||||
bus drivers.I2C
|
bus drivers.I2C
|
||||||
Address uint16
|
Address uint16
|
||||||
|
buf [6]byte
|
||||||
cali calibrationCoefficients
|
cali calibrationCoefficients
|
||||||
Temperature Oversampling
|
Temperature Oversampling
|
||||||
Pressure Oversampling
|
Pressure Oversampling
|
||||||
@@ -134,8 +135,8 @@ func (d *Device) PrintCali() {
|
|||||||
|
|
||||||
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
|
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
|
||||||
func (d *Device) ReadTemperature() (temperature int32, err error) {
|
func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||||
data, err := d.readData(REG_TEMP, 3)
|
data := d.buf[:3]
|
||||||
if err != nil {
|
if err = d.readData(REG_TEMP, data); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,8 +159,8 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
|
|||||||
// ReadPressure returns the pressure in milli pascals (mPa).
|
// ReadPressure returns the pressure in milli pascals (mPa).
|
||||||
func (d *Device) ReadPressure() (pressure int32, err error) {
|
func (d *Device) ReadPressure() (pressure int32, err error) {
|
||||||
// First 3 bytes are Pressure, last 3 bytes are Temperature
|
// First 3 bytes are Pressure, last 3 bytes are Temperature
|
||||||
data, err := d.readData(REG_PRES, 6)
|
data := d.buf[:6]
|
||||||
if err != nil {
|
if err = d.readData(REG_PRES, data); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +204,7 @@ func (d *Device) ReadPressure() (pressure int32, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// readData reads n number of bytes of the specified register
|
// readData reads n number of bytes of the specified register
|
||||||
func (d *Device) readData(register int, n int) ([]byte, error) {
|
func (d *Device) readData(register int, data []byte) error {
|
||||||
// If not in normal mode, set the mode to FORCED mode, to prevent incorrect measurements
|
// If not in normal mode, set the mode to FORCED mode, to prevent incorrect measurements
|
||||||
// After the measurement in FORCED mode, the sensor will return to SLEEP mode
|
// After the measurement in FORCED mode, the sensor will return to SLEEP mode
|
||||||
if d.Mode != MODE_NORMAL {
|
if d.Mode != MODE_NORMAL {
|
||||||
@@ -218,9 +219,7 @@ func (d *Device) readData(register int, n int) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read the requested register
|
// Read the requested register
|
||||||
data := make([]byte, n)
|
return legacy.ReadRegister(d.bus, uint8(d.Address), uint8(register), data[:])
|
||||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), uint8(register), data[:])
|
|
||||||
return data, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert3Bytes converts three bytes to int32
|
// convert3Bytes converts three bytes to int32
|
||||||
|
|||||||
Reference in New Issue
Block a user