2014-11-10 10:14:13 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2014-11-10 10:14:13 +08:00
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-04 09:57:29 +08:00
|
|
|
const mpl115a2Address = 0x60
|
|
|
|
|
2014-11-10 10:14:13 +08:00
|
|
|
const MPL115A2_REGISTER_PRESSURE_MSB = 0x00
|
|
|
|
const MPL115A2_REGISTER_PRESSURE_LSB = 0x01
|
|
|
|
const MPL115A2_REGISTER_TEMP_MSB = 0x02
|
|
|
|
const MPL115A2_REGISTER_TEMP_LSB = 0x03
|
|
|
|
const MPL115A2_REGISTER_A0_COEFF_MSB = 0x04
|
|
|
|
const MPL115A2_REGISTER_A0_COEFF_LSB = 0x05
|
|
|
|
const MPL115A2_REGISTER_B1_COEFF_MSB = 0x06
|
|
|
|
const MPL115A2_REGISTER_B1_COEFF_LSB = 0x07
|
|
|
|
const MPL115A2_REGISTER_B2_COEFF_MSB = 0x08
|
|
|
|
const MPL115A2_REGISTER_B2_COEFF_LSB = 0x09
|
|
|
|
const MPL115A2_REGISTER_C12_COEFF_MSB = 0x0A
|
|
|
|
const MPL115A2_REGISTER_C12_COEFF_LSB = 0x0B
|
|
|
|
const MPL115A2_REGISTER_STARTCONVERSION = 0x12
|
|
|
|
|
2017-02-11 19:34:12 +08:00
|
|
|
// MPL115A2Driver is a Gobot Driver for the MPL115A2 I2C digitial pressure/temperature sensor.
|
2014-11-10 10:14:13 +08:00
|
|
|
type MPL115A2Driver struct {
|
2014-11-23 11:38:39 +08:00
|
|
|
name string
|
2017-02-10 18:08:32 +08:00
|
|
|
connector Connector
|
|
|
|
connection Connection
|
|
|
|
Config
|
2014-11-23 11:38:39 +08:00
|
|
|
gobot.Eventer
|
2017-02-11 19:34:12 +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:
|
2017-02-10 18:08:32 +08:00
|
|
|
// conn Connector - the Adaptor to use with this Driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
2017-02-10 18:08:32 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
2017-02-10 18:08:32 +08:00
|
|
|
func NewMPL115A2Driver(a Connector, options ...func(Config)) *MPL115A2Driver {
|
2014-11-20 08:56:48 +08:00
|
|
|
m := &MPL115A2Driver{
|
2017-02-06 07:19:42 +08:00
|
|
|
name: gobot.DefaultName("MPL115A2"),
|
|
|
|
connector: a,
|
2017-02-10 18:08:32 +08:00
|
|
|
Config: NewConfig(),
|
2017-02-06 07:19:42 +08:00
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
2014-11-29 10:37:03 +08:00
|
|
|
|
2017-02-09 18:23:36 +08:00
|
|
|
for _, option := range options {
|
|
|
|
option(m)
|
2014-11-29 10:37:03 +08:00
|
|
|
}
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
// TODO: add commands to API
|
2014-11-30 04:10:23 +08:00
|
|
|
m.AddEvent(Error)
|
2017-02-09 18:23:36 +08:00
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
return m
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
// Name returns the name of the device.
|
|
|
|
func (h *MPL115A2Driver) Name() string { return h.name }
|
|
|
|
|
|
|
|
// SetName sets the name of the device.
|
|
|
|
func (h *MPL115A2Driver) SetName(n string) { h.name = n }
|
|
|
|
|
|
|
|
// Connection returns the Connection of the device.
|
2017-02-06 07:19:42 +08:00
|
|
|
func (h *MPL115A2Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
|
2014-11-10 10:14:13 +08:00
|
|
|
|
|
|
|
// Start writes initialization bytes and reads from adaptor
|
|
|
|
// using specified interval to accelerometer andtemperature data
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *MPL115A2Driver) Start() (err error) {
|
2014-11-20 15:21:19 +08:00
|
|
|
if err := h.initialization(); err != nil {
|
2016-11-07 21:55:21 +08:00
|
|
|
return err
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-11-10 10:14:13 +08:00
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Halt returns true if devices is halted successfully
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *MPL115A2Driver) Halt() (err error) { return }
|
2014-11-10 10:14:13 +08:00
|
|
|
|
2017-02-11 19:34:12 +08:00
|
|
|
// Pressure fetches the latest data from the MPL115A2, and returns the pressure
|
2017-04-04 18:34:42 +08:00
|
|
|
func (h *MPL115A2Driver) Pressure() (p float32, err error) {
|
|
|
|
p, _, err = h.getData()
|
2017-02-11 19:34:12 +08:00
|
|
|
return
|
|
|
|
}
|
2017-02-11 17:37:34 +08:00
|
|
|
|
2017-02-11 19:34:12 +08:00
|
|
|
// Temperature fetches the latest data from the MPL115A2, and returns the temperature
|
2017-04-04 18:34:42 +08:00
|
|
|
func (h *MPL115A2Driver) Temperature() (t float32, err error) {
|
|
|
|
_, t, err = h.getData()
|
2017-02-11 19:34:12 +08:00
|
|
|
return
|
2017-02-11 17:37:34 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
func (h *MPL115A2Driver) initialization() (err error) {
|
2014-11-10 10:14:13 +08:00
|
|
|
var coA0 int16
|
|
|
|
var coB1 int16
|
|
|
|
var coB2 int16
|
|
|
|
var coC12 int16
|
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
|
|
|
|
address := h.GetAddressOrDefault(mpl115a2Address)
|
2017-02-09 18:23:36 +08:00
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
h.connection, err = h.connector.GetConnection(address, bus)
|
2017-02-06 07:19:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
|
|
|
|
if _, err = h.connection.Write([]byte{MPL115A2_REGISTER_A0_COEFF_MSB}); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
|
|
|
|
data := make([]byte, 8)
|
|
|
|
if _, err = h.connection.Read(data); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
buf := bytes.NewBuffer(data)
|
2014-11-10 10:14:13 +08:00
|
|
|
|
|
|
|
binary.Read(buf, binary.BigEndian, &coA0)
|
|
|
|
binary.Read(buf, binary.BigEndian, &coB1)
|
|
|
|
binary.Read(buf, binary.BigEndian, &coB2)
|
|
|
|
binary.Read(buf, binary.BigEndian, &coC12)
|
|
|
|
|
|
|
|
coC12 = coC12 >> 2
|
|
|
|
|
|
|
|
h.A0 = float32(coA0) / 8.0
|
|
|
|
h.B1 = float32(coB1) / 8192.0
|
|
|
|
h.B2 = float32(coB2) / 16384.0
|
|
|
|
h.C12 = float32(coC12) / 4194304.0
|
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
2014-11-10 10:14:13 +08:00
|
|
|
}
|
2017-02-11 19:34:12 +08:00
|
|
|
|
|
|
|
// getData fetches the latest data from the MPL115A2
|
|
|
|
func (h *MPL115A2Driver) getData() (p, t float32, err error) {
|
|
|
|
var temperature uint16
|
|
|
|
var pressure uint16
|
|
|
|
var pressureComp float32
|
|
|
|
|
|
|
|
if _, err = h.connection.Write([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
if _, err = h.connection.Write([]byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte{0, 0, 0, 0}
|
|
|
|
bytesRead, err1 := h.connection.Read(data)
|
|
|
|
if err1 != nil {
|
|
|
|
err = err1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytesRead == 4 {
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
binary.Read(buf, binary.BigEndian, &pressure)
|
|
|
|
binary.Read(buf, binary.BigEndian, &temperature)
|
|
|
|
|
|
|
|
temperature = temperature >> 6
|
|
|
|
pressure = pressure >> 6
|
|
|
|
|
|
|
|
pressureComp = float32(h.A0) + (float32(h.B1)+float32(h.C12)*float32(temperature))*float32(pressure) + float32(h.B2)*float32(temperature)
|
|
|
|
p = (65.0/1023.0)*pressureComp + 50.0
|
|
|
|
t = ((float32(temperature) - 498.0) / -5.35) + 25.0
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|