2014-04-28 04:23:15 -07:00
|
|
|
package digispark
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-17 16:25:01 -08:00
|
|
|
"errors"
|
2018-08-12 00:51:47 +02:00
|
|
|
"fmt"
|
2014-04-26 03:11:51 -07:00
|
|
|
"strconv"
|
2017-02-02 16:01:41 +01:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// ErrConnection is the error resulting of a connection error with the digispark
|
2014-12-18 12:37:17 -08:00
|
|
|
var ErrConnection = errors.New("connection error")
|
|
|
|
|
2016-09-25 20:43:09 +02:00
|
|
|
// Adaptor is the Gobot Adaptor for the Digispark
|
|
|
|
type Adaptor struct {
|
2014-11-22 19:01:27 -08:00
|
|
|
name string
|
2014-11-04 20:37:07 -08:00
|
|
|
littleWire lw
|
2014-04-26 03:11:51 -07:00
|
|
|
servo bool
|
|
|
|
pwm bool
|
2018-08-12 00:51:47 +02:00
|
|
|
i2c bool
|
2023-11-15 20:51:52 +01:00
|
|
|
connect func(*Adaptor) error
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:43:09 +02:00
|
|
|
// NewAdaptor returns a new Digispark Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
return &Adaptor{
|
2017-02-02 16:01:41 +01:00
|
|
|
name: gobot.DefaultName("Digispark"),
|
2023-11-15 20:51:52 +01:00
|
|
|
connect: func(d *Adaptor) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
d.littleWire = littleWireConnect()
|
2023-11-15 20:51:52 +01:00
|
|
|
//nolint:forcetypeassert // ok here
|
2014-11-17 16:25:01 -08:00
|
|
|
if d.littleWire.(*littleWire).lwHandle == nil {
|
2014-12-18 12:37:17 -08:00
|
|
|
return ErrConnection
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
2023-11-15 20:51:52 +01:00
|
|
|
return nil
|
2014-04-28 04:23:15 -07:00
|
|
|
},
|
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2024-02-12 16:27:08 +01:00
|
|
|
// Name returns the Digispark adaptors name
|
2016-09-25 20:43:09 +02:00
|
|
|
func (d *Adaptor) Name() string { return d.name }
|
|
|
|
|
2024-02-12 16:27:08 +01:00
|
|
|
// SetName sets the Digispark adaptors name
|
2016-09-25 20:43:09 +02:00
|
|
|
func (d *Adaptor) SetName(n string) { d.name = n }
|
2014-11-22 19:01:27 -08:00
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// Connect starts a connection to the digispark
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) Connect() error {
|
|
|
|
return d.connect(d)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// Finalize implements the Adaptor interface
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) Finalize() error { return nil }
|
2014-10-20 12:17:47 -05:00
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) DigitalWrite(pin string, level byte) error {
|
2014-11-17 16:25:01 -08:00
|
|
|
p, err := strconv.Atoi(pin)
|
|
|
|
if err != nil {
|
2023-11-15 20:51:52 +01:00
|
|
|
return err
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
|
|
|
|
2023-11-15 20:51:52 +01:00
|
|
|
if err := d.littleWire.pinMode(uint8(p), 0); err != nil {
|
|
|
|
return err
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
2014-12-18 12:37:17 -08:00
|
|
|
|
|
|
|
return d.littleWire.digitalWrite(uint8(p), level)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-10-20 12:17:47 -05:00
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// PwmWrite writes the 0-254 value to the specified pin
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) PwmWrite(pin string, value byte) error {
|
2016-07-13 09:32:22 -06:00
|
|
|
if !d.pwm {
|
2023-11-15 20:51:52 +01:00
|
|
|
if err := d.littleWire.pwmInit(); err != nil {
|
|
|
|
return err
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
2014-12-18 12:37:17 -08:00
|
|
|
|
2023-11-15 20:51:52 +01:00
|
|
|
if err := d.littleWire.pwmUpdatePrescaler(1); err != nil {
|
|
|
|
return err
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
2014-04-28 04:23:15 -07:00
|
|
|
d.pwm = true
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-12-18 12:37:17 -08:00
|
|
|
|
|
|
|
return d.littleWire.pwmUpdateCompare(value, value)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-10-20 12:17:47 -05:00
|
|
|
|
2015-01-02 07:26:15 -08:00
|
|
|
// ServoWrite writes the 0-180 degree val to the specified pin.
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) ServoWrite(pin string, angle uint8) error {
|
2016-07-13 09:32:22 -06:00
|
|
|
if !d.servo {
|
2023-11-15 20:51:52 +01:00
|
|
|
if err := d.littleWire.servoInit(); err != nil {
|
|
|
|
return err
|
2014-11-17 16:25:01 -08:00
|
|
|
}
|
2014-04-28 04:23:15 -07:00
|
|
|
d.servo = true
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-12-18 12:37:17 -08:00
|
|
|
return d.littleWire.servoUpdateLocation(angle, angle)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2018-08-12 00:51:47 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
// GetI2cConnection returns an i2c connection to a device on a specified bus.
|
2018-08-12 00:51:47 +02:00
|
|
|
// Only supports bus number 0
|
2023-11-15 20:51:52 +01:00
|
|
|
func (d *Adaptor) GetI2cConnection(address int, bus int) (i2c.Connection, error) {
|
2018-08-12 00:51:47 +02:00
|
|
|
if bus != 0 {
|
|
|
|
return nil, fmt.Errorf("Invalid bus number %d, only 0 is supported", bus)
|
|
|
|
}
|
2018-08-17 20:02:45 +02:00
|
|
|
c := NewDigisparkI2cConnection(d, uint8(address))
|
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return i2c.Connection(c), nil
|
2018-08-12 00:51:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
// DefaultI2cBus returns the default i2c bus for this platform
|
|
|
|
func (d *Adaptor) DefaultI2cBus() int {
|
2018-08-12 00:51:47 +02:00
|
|
|
return 0
|
|
|
|
}
|