hybridgroup.gobot/platforms/digispark/digispark_adaptor.go

70 lines
1.6 KiB
Go
Raw Normal View History

2014-04-28 19:23:15 +08:00
package digispark
import (
"github.com/hybridgroup/gobot"
"strconv"
)
type DigisparkAdaptor struct {
gobot.Adaptor
2014-04-28 19:23:15 +08:00
littleWire *LittleWire
servo bool
pwm bool
2014-04-28 19:23:15 +08:00
connect func(*DigisparkAdaptor)
}
2014-05-23 11:13:38 +08:00
func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
2014-04-28 19:23:15 +08:00
return &DigisparkAdaptor{
2014-07-08 07:44:21 +08:00
Adaptor: *gobot.NewAdaptor(
name,
"DigisparkAdaptor",
),
2014-04-28 19:23:15 +08:00
connect: func(d *DigisparkAdaptor) {
d.littleWire = LittleWireConnect()
},
}
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) Connect() bool {
d.connect(d)
2014-07-08 07:44:21 +08:00
d.SetConnected(true)
return true
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) Reconnect() bool {
return d.Connect()
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) Finalize() bool { return true }
func (d *DigisparkAdaptor) Disconnect() bool { return true }
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) {
p, _ := strconv.Atoi(pin)
2014-04-28 19:23:15 +08:00
d.littleWire.PinMode(uint8(p), 0)
d.littleWire.DigitalWrite(uint8(p), level)
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) DigitalRead(pin string, level byte) {}
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) {
if d.pwm == false {
d.littleWire.PwmInit()
d.littleWire.PwmUpdatePrescaler(1)
d.pwm = true
}
2014-04-28 19:23:15 +08:00
d.littleWire.PwmUpdateCompare(value, value)
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) AnalogRead(string) int { return -1 }
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) InitServo() {}
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) {
if d.servo == false {
d.littleWire.ServoInit()
d.servo = true
}
2014-04-28 19:23:15 +08:00
d.littleWire.ServoUpdateLocation(angle, angle)
}
2014-04-28 19:23:15 +08:00
func (d *DigisparkAdaptor) I2cStart(byte) {}
func (d *DigisparkAdaptor) I2cRead(uint16) []uint16 { return make([]uint16, 0) }
func (d *DigisparkAdaptor) I2cWrite([]uint16) {}