2015-07-09 01:54:37 +08:00
|
|
|
package i2c
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
const mma7660DefaultAddress = 0x4c
|
2015-07-09 01:54:37 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
MMA7660_X = 0x00
|
|
|
|
MMA7660_Y = 0x01
|
|
|
|
MMA7660_Z = 0x02
|
|
|
|
MMA7660_TILT = 0x03
|
|
|
|
MMA7660_SRST = 0x04
|
|
|
|
MMA7660_SPCNT = 0x05
|
|
|
|
MMA7660_INTSU = 0x06
|
|
|
|
MMA7660_MODE = 0x07
|
|
|
|
MMA7660_STAND_BY = 0x00
|
|
|
|
MMA7660_ACTIVE = 0x01
|
|
|
|
MMA7660_SR = 0x08
|
|
|
|
MMA7660_AUTO_SLEEP_120 = 0x00
|
|
|
|
MMA7660_AUTO_SLEEP_64 = 0x01
|
|
|
|
MMA7660_AUTO_SLEEP_32 = 0x02
|
|
|
|
MMA7660_AUTO_SLEEP_16 = 0x03
|
|
|
|
MMA7660_AUTO_SLEEP_8 = 0x04
|
|
|
|
MMA7660_AUTO_SLEEP_4 = 0x05
|
|
|
|
MMA7660_AUTO_SLEEP_2 = 0x06
|
|
|
|
MMA7660_AUTO_SLEEP_1 = 0x07
|
|
|
|
MMA7660_PDET = 0x09
|
|
|
|
MMA7660_PD = 0x0A
|
|
|
|
)
|
|
|
|
|
|
|
|
type MMA7660Driver struct {
|
2022-12-10 20:10:23 +08:00
|
|
|
*Driver
|
2015-07-09 01:54:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
// NewMMA7660Driver creates a new driver with specified i2c interface
|
2017-02-09 23:47:11 +08:00
|
|
|
// Params:
|
2023-10-21 02:50:42 +08:00
|
|
|
//
|
|
|
|
// c Connector - the Adaptor to use with this Driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
//
|
2023-10-21 02:50:42 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2022-12-10 20:10:23 +08:00
|
|
|
func NewMMA7660Driver(c Connector, options ...func(Config)) *MMA7660Driver {
|
|
|
|
d := &MMA7660Driver{
|
|
|
|
Driver: NewDriver(c, "MMA7660", mma7660DefaultAddress),
|
2017-02-09 18:23:36 +08:00
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
d.afterStart = d.initialize
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
for _, option := range options {
|
2022-12-10 20:10:23 +08:00
|
|
|
option(d)
|
2015-07-09 01:54:37 +08:00
|
|
|
}
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
// TODO: add commands for API
|
2022-12-10 20:10:23 +08:00
|
|
|
return d
|
2015-07-09 01:54:37 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:34:51 +08:00
|
|
|
// Acceleration returns the acceleration of the provided x, y, z
|
2023-11-16 03:51:52 +08:00
|
|
|
//
|
|
|
|
//nolint:nonamedreturns // is sufficient here
|
2022-12-10 20:10:23 +08:00
|
|
|
func (d *MMA7660Driver) Acceleration(x, y, z float64) (ax, ay, az float64) {
|
2015-07-09 01:54:37 +08:00
|
|
|
return x / 21.0, y / 21.0, z / 21.0
|
|
|
|
}
|
|
|
|
|
2017-02-11 19:34:51 +08:00
|
|
|
// XYZ returns the raw x,y and z axis from the mma7660
|
2023-11-16 03:51:52 +08:00
|
|
|
//
|
|
|
|
//nolint:nonamedreturns // is sufficient here
|
2022-12-10 20:10:23 +08:00
|
|
|
func (d *MMA7660Driver) XYZ() (x float64, y float64, z float64, err error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
buf := []byte{0, 0, 0}
|
2022-12-10 20:10:23 +08:00
|
|
|
bytesRead, err := d.connection.Read(buf)
|
2015-07-09 01:54:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if bytesRead != 3 {
|
2015-07-09 01:54:37 +08:00
|
|
|
err = ErrNotEnoughBytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
for _, val := range buf {
|
2015-07-09 01:54:37 +08:00
|
|
|
if ((val >> 6) & 0x01) == 1 {
|
|
|
|
err = ErrNotReady
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
x = float64((int8(buf[0]) << 2)) / 4.0
|
|
|
|
y = float64((int8(buf[1]) << 2)) / 4.0
|
|
|
|
z = float64((int8(buf[2]) << 2)) / 4.0
|
2015-07-09 01:54:37 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
|
|
|
|
func (d *MMA7660Driver) initialize() error {
|
|
|
|
if _, err := d.connection.Write([]byte{MMA7660_MODE, MMA7660_STAND_BY}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := d.connection.Write([]byte{MMA7660_SR, MMA7660_AUTO_SLEEP_32}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := d.connection.Write([]byte{MMA7660_MODE, MMA7660_ACTIVE}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|