2014-11-10 10:14:13 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-11-10 10:14:13 +08:00
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
const mpl115a2DefaultAddress = 0x60
|
|
|
|
|
|
|
|
const (
|
|
|
|
mpl115A2Reg_PressureMSB = 0x00 // first ADC register
|
|
|
|
mpl115A2Reg_PressureLSB = 0x01
|
|
|
|
mpl115A2Reg_TempMSB = 0x02
|
|
|
|
mpl115A2Reg_TempLSB = 0x03
|
|
|
|
|
|
|
|
mpl115A2Reg_A0_MSB = 0x04 // first coefficient register
|
|
|
|
mpl115A2Reg_A0_LSB = 0x05
|
|
|
|
mpl115A2Reg_B1_MSB = 0x06
|
|
|
|
mpl115A2Reg_B1_LSB = 0x07
|
|
|
|
mpl115A2Reg_B2_MSB = 0x08
|
|
|
|
mpl115A2Reg_B2_LSB = 0x09
|
|
|
|
mpl115A2Reg_C12_MSB = 0x0A
|
|
|
|
mpl115A2Reg_C12_LSB = 0x0B
|
|
|
|
|
|
|
|
mpl115A2Reg_StartConversion = 0x12
|
|
|
|
)
|
|
|
|
|
|
|
|
// MPL115A2Driver is a Gobot Driver for the MPL115A2 I2C digital pressure/temperature sensor.
|
|
|
|
// datasheet:
|
|
|
|
// https://www.nxp.com/docs/en/data-sheet/MPL115A2.pdf
|
|
|
|
//
|
|
|
|
// reference implementations:
|
|
|
|
// * https://github.com/adafruit/Adafruit_MPL115A2
|
2014-11-10 10:14:13 +08:00
|
|
|
type MPL115A2Driver struct {
|
2022-09-21 01:35:55 +08:00
|
|
|
*Driver
|
2014-11-23 11:38:39 +08:00
|
|
|
gobot.Eventer
|
2022-09-21 01:35:55 +08:00
|
|
|
a0 float32
|
|
|
|
b1 float32
|
|
|
|
b2 float32
|
|
|
|
c12 float32
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 17:37:34 +08:00
|
|
|
// NewMPL115A2Driver creates a new Gobot Driver for an MPL115A2
|
|
|
|
// I2C Pressure/Temperature sensor.
|
|
|
|
//
|
2017-02-09 23:47:11 +08:00
|
|
|
// Params:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
|
|
|
// c Connector - the Adaptor to use with this Driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2022-09-21 01:35:55 +08:00
|
|
|
func NewMPL115A2Driver(c Connector, options ...func(Config)) *MPL115A2Driver {
|
|
|
|
d := &MPL115A2Driver{
|
|
|
|
Driver: NewDriver(c, "MPL115A2", mpl115a2DefaultAddress),
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
2022-09-21 01:35:55 +08:00
|
|
|
d.afterStart = d.initialization
|
2014-11-29 10:37:03 +08:00
|
|
|
|
2017-02-09 18:23:36 +08:00
|
|
|
for _, option := range options {
|
2022-09-21 01:35:55 +08:00
|
|
|
option(d)
|
2014-11-29 10:37:03 +08:00
|
|
|
}
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
// TODO: add commands to API
|
2022-09-21 01:35:55 +08:00
|
|
|
d.AddEvent(Error)
|
2017-02-09 18:23:36 +08:00
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
return d
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
// Pressure fetches the latest data from the MPL115A2, and returns the pressure in kPa
|
|
|
|
func (d *MPL115A2Driver) Pressure() (p float32, err error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2014-11-10 10:14:13 +08:00
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
p, _, err = d.getData()
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
// Temperature fetches the latest data from the MPL115A2, and returns the temperature in °C
|
|
|
|
func (d *MPL115A2Driver) Temperature() (t float32, err error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2014-11-10 10:14:13 +08:00
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
_, t, err = d.getData()
|
2017-02-11 19:34:12 +08:00
|
|
|
return
|
|
|
|
}
|
2017-02-11 17:37:34 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
func (d *MPL115A2Driver) initialization() error {
|
2022-09-21 01:35:55 +08:00
|
|
|
data := make([]byte, 8)
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := d.connection.ReadBlockData(mpl115A2Reg_A0_MSB, data); err != nil {
|
|
|
|
return err
|
2022-09-21 01:35:55 +08:00
|
|
|
}
|
2017-02-11 17:37:34 +08:00
|
|
|
|
2014-11-10 10:14:13 +08:00
|
|
|
var coA0 int16
|
|
|
|
var coB1 int16
|
|
|
|
var coB2 int16
|
|
|
|
var coC12 int16
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
buf := bytes.NewBuffer(data)
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := binary.Read(buf, binary.BigEndian, &coA0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &coB1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &coB2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &coC12); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-10 10:14:13 +08:00
|
|
|
|
|
|
|
coC12 = coC12 >> 2
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
d.a0 = float32(coA0) / 8.0
|
|
|
|
d.b1 = float32(coB1) / 8192.0
|
|
|
|
d.b2 = float32(coB2) / 16384.0
|
|
|
|
d.c12 = float32(coC12) / 4194304.0
|
2014-11-10 10:14:13 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
return nil
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
2017-02-11 19:34:12 +08:00
|
|
|
|
|
|
|
// getData fetches the latest data from the MPL115A2
|
2022-09-21 01:35:55 +08:00
|
|
|
func (d *MPL115A2Driver) getData() (p, t float32, err error) {
|
2017-02-11 19:34:12 +08:00
|
|
|
var temperature uint16
|
|
|
|
var pressure uint16
|
|
|
|
var pressureComp float32
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
if err = d.connection.WriteByteData(mpl115A2Reg_StartConversion, 0); err != nil {
|
2023-06-13 01:51:25 +08:00
|
|
|
return 0, 0, err
|
2017-02-11 19:34:12 +08:00
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
data := []byte{0, 0, 0, 0}
|
2022-09-21 01:35:55 +08:00
|
|
|
if err = d.connection.ReadBlockData(mpl115A2Reg_PressureMSB, data); err != nil {
|
2017-02-11 19:34:12 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
buf := bytes.NewBuffer(data)
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := binary.Read(buf, binary.BigEndian, &pressure); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &temperature); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2017-02-11 19:34:12 +08:00
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
temperature = temperature >> 6
|
|
|
|
pressure = pressure >> 6
|
2017-02-11 19:34:12 +08:00
|
|
|
|
2022-09-21 01:35:55 +08:00
|
|
|
pressureComp = d.a0 + (d.b1+d.c12*float32(temperature))*float32(pressure) + d.b2*float32(temperature)
|
|
|
|
p = (65.0/1023.0)*pressureComp + 50.0
|
|
|
|
t = ((float32(temperature) - 498.0) / -5.35) + 25.0
|
2017-02-11 19:34:12 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|