2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
const lidarliteDefaultAddress = 0x62
|
2015-07-04 09:57:29 +08:00
|
|
|
|
2017-02-11 19:34:51 +08:00
|
|
|
// LIDARLiteDriver is the Gobot driver for the LIDARLite I2C LIDAR device.
|
2015-02-18 04:11:02 +08:00
|
|
|
type LIDARLiteDriver struct {
|
2022-12-10 20:10:23 +08:00
|
|
|
*Driver
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:34:51 +08:00
|
|
|
// NewLIDARLiteDriver creates a new driver for the LIDARLite I2C LIDAR device.
|
|
|
|
//
|
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 NewLIDARLiteDriver(c Connector, options ...func(Config)) *LIDARLiteDriver {
|
2017-02-09 18:23:36 +08:00
|
|
|
l := &LIDARLiteDriver{
|
2022-12-10 20:10:23 +08:00
|
|
|
Driver: NewDriver(c, "LIDARLite", lidarliteDefaultAddress),
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add commands to API
|
|
|
|
return l
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
// Distance returns the current distance in cm
|
2023-11-16 03:51:52 +08:00
|
|
|
func (h *LIDARLiteDriver) Distance() (int, error) {
|
|
|
|
if _, err := h.connection.Write([]byte{0x00, 0x04}); err != nil {
|
|
|
|
return 0, err
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(20 * time.Millisecond)
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if _, err := h.connection.Write([]byte{0x0F}); err != nil {
|
|
|
|
return 0, err
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
upper := []byte{0}
|
|
|
|
bytesRead, err := h.connection.Read(upper)
|
2015-02-18 04:11:02 +08:00
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, err
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2015-07-13 06:22:23 +08:00
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if bytesRead != 1 {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, ErrNotEnoughBytes
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if _, err := h.connection.Write([]byte{0x10}); err != nil {
|
|
|
|
return 0, err
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
lower := []byte{0}
|
|
|
|
bytesRead, err = h.connection.Read(lower)
|
2015-07-13 06:22:23 +08:00
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, err
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if bytesRead != 1 {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, ErrNotEnoughBytes
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2015-07-13 06:22:23 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
distance := ((int(upper[0]) & 0xff) << 8) | (int(lower[0]) & 0xff)
|
2015-07-13 06:22:23 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return distance, nil
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|