2014-04-28 19:23:15 +08:00
|
|
|
package digispark
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-11-18 08:25:01 +08:00
|
|
|
"errors"
|
2014-04-26 18:11:51 +08:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// ErrConnection is the error resulting of a connection error with the digispark
|
2014-12-19 04:37:17 +08:00
|
|
|
var ErrConnection = errors.New("connection error")
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
// Adaptor is the Gobot Adaptor for the Digispark
|
|
|
|
type Adaptor struct {
|
2014-11-23 11:01:27 +08:00
|
|
|
name string
|
2014-11-05 12:37:07 +08:00
|
|
|
littleWire lw
|
2014-04-26 18:11:51 +08:00
|
|
|
servo bool
|
|
|
|
pwm bool
|
2016-09-26 02:43:09 +08:00
|
|
|
connect func(*Adaptor) (err error)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
// NewAdaptor returns a new Digispark Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
return &Adaptor{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "Digispark",
|
2016-09-26 02:43:09 +08:00
|
|
|
connect: func(d *Adaptor) (err error) {
|
2014-11-05 12:37:07 +08:00
|
|
|
d.littleWire = littleWireConnect()
|
2014-11-18 08:25:01 +08:00
|
|
|
if d.littleWire.(*littleWire).lwHandle == nil {
|
2014-12-19 04:37:17 +08:00
|
|
|
return ErrConnection
|
2014-11-18 08:25:01 +08:00
|
|
|
}
|
|
|
|
return
|
2014-04-28 19:23:15 +08:00
|
|
|
},
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
// Name returns the Digispark Adaptors name
|
|
|
|
func (d *Adaptor) Name() string { return d.name }
|
|
|
|
|
|
|
|
// SetName sets the Digispark Adaptors name
|
|
|
|
func (d *Adaptor) SetName(n string) { d.name = n }
|
2014-11-23 11:01:27 +08:00
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// Connect starts a connection to the digispark
|
2016-11-08 02:10:22 +08:00
|
|
|
func (d *Adaptor) Connect() (err error) {
|
|
|
|
err = d.connect(d)
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// Finalize implements the Adaptor interface
|
2016-11-08 02:10:22 +08:00
|
|
|
func (d *Adaptor) Finalize() (err error) { return }
|
2014-10-21 01:17:47 +08:00
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
|
2016-09-26 02:43:09 +08:00
|
|
|
func (d *Adaptor) DigitalWrite(pin string, level byte) (err error) {
|
2014-11-18 08:25:01 +08:00
|
|
|
p, err := strconv.Atoi(pin)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-11-18 08:25:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-19 04:37:17 +08:00
|
|
|
if err = d.littleWire.pinMode(uint8(p), 0); err != nil {
|
2014-11-18 08:25:01 +08:00
|
|
|
return
|
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
return d.littleWire.digitalWrite(uint8(p), level)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-10-21 01:17:47 +08:00
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// PwmWrite writes the 0-254 value to the specified pin
|
2016-09-26 02:43:09 +08:00
|
|
|
func (d *Adaptor) PwmWrite(pin string, value byte) (err error) {
|
2016-07-13 23:32:22 +08:00
|
|
|
if !d.pwm {
|
2014-12-19 04:37:17 +08:00
|
|
|
if err = d.littleWire.pwmInit(); err != nil {
|
|
|
|
return
|
2014-11-18 08:25:01 +08:00
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
if err = d.littleWire.pwmUpdatePrescaler(1); err != nil {
|
|
|
|
return
|
2014-11-18 08:25:01 +08:00
|
|
|
}
|
2014-04-28 19:23:15 +08:00
|
|
|
d.pwm = true
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
return d.littleWire.pwmUpdateCompare(value, value)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-10-21 01:17:47 +08:00
|
|
|
|
2015-01-02 23:26:15 +08:00
|
|
|
// ServoWrite writes the 0-180 degree val to the specified pin.
|
2016-09-26 02:43:09 +08:00
|
|
|
func (d *Adaptor) ServoWrite(pin string, angle uint8) (err error) {
|
2016-07-13 23:32:22 +08:00
|
|
|
if !d.servo {
|
2014-12-19 04:37:17 +08:00
|
|
|
if err = d.littleWire.servoInit(); err != nil {
|
|
|
|
return
|
2014-11-18 08:25:01 +08:00
|
|
|
}
|
2014-04-28 19:23:15 +08:00
|
|
|
d.servo = true
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
return d.littleWire.servoUpdateLocation(angle, angle)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|