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