hybridgroup.gobot/platforms/digispark/littleWire.go

80 lines
1.7 KiB
Go
Raw Normal View History

2014-04-28 19:23:15 +08:00
package digispark
//#cgo LDFLAGS: -lusb
//#include "littleWire.h"
2014-11-05 12:37:07 +08:00
//#include "littleWire_servo.h"
//typedef usb_dev_handle littleWire;
import "C"
2014-11-18 08:25:01 +08:00
import "errors"
2014-11-05 12:37:07 +08:00
type lw interface {
2014-11-18 08:25:01 +08:00
digitalWrite(uint8, uint8) error
pinMode(uint8, uint8) error
pwmInit() error
pwmStop() error
pwmUpdateCompare(uint8, uint8) error
pwmUpdatePrescaler(uint) error
servoInit() error
servoUpdateLocation(uint8, uint8) error
error() error
2014-11-05 12:37:07 +08:00
}
type littleWire struct {
lwHandle *C.littleWire
}
2014-11-05 12:37:07 +08:00
func littleWireConnect() *littleWire {
return &littleWire{
2014-04-28 19:23:15 +08:00
lwHandle: C.littleWire_connect(),
}
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) digitalWrite(pin uint8, state uint8) error {
2014-04-28 19:23:15 +08:00
C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state))
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) pinMode(pin uint8, mode uint8) error {
2014-04-28 19:23:15 +08:00
C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode))
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) pwmInit() error {
2014-04-28 19:23:15 +08:00
C.pwm_init(l.lwHandle)
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) pwmStop() error {
2014-04-28 19:23:15 +08:00
C.pwm_stop(l.lwHandle)
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) error {
2014-04-28 19:23:15 +08:00
C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB))
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) pwmUpdatePrescaler(value uint) error {
2014-04-28 19:23:15 +08:00
C.pwm_updatePrescaler(l.lwHandle, C.uint(value))
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) servoInit() error {
2014-11-05 12:37:07 +08:00
C.servo_init(l.lwHandle)
2014-11-18 08:25:01 +08:00
return l.error()
}
2014-11-18 08:25:01 +08:00
func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) error {
2014-11-05 12:37:07 +08:00
C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB))
2014-11-18 08:25:01 +08:00
return l.error()
}
func (l *littleWire) error() error {
str := C.GoString(C.littleWire_errorName())
if str != "" {
return errors.New(str)
}
return nil
}