1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

125 lines
3.2 KiB
Go
Raw Normal View History

//go:build libusb
// +build libusb
2014-04-28 04:23:15 -07:00
package digispark
//#cgo pkg-config: libusb
//#include "littleWire.h"
2014-11-04 20:37:07 -08:00
//#include "littleWire_servo.h"
// typedef usb_dev_handle littleWire;
import "C"
import (
"errors"
"fmt"
)
2014-11-17 16:25:01 -08:00
2014-11-04 20:37:07 -08:00
type lw interface {
digitalWrite(pin uint8, state uint8) error
pinMode(pin uint8, mode uint8) error
2014-11-17 16:25:01 -08:00
pwmInit() error
pwmStop() error
pwmUpdateCompare(channelA uint8, channelb uint8) error
pwmUpdatePrescaler(value uint) error
2014-11-17 16:25:01 -08:00
servoInit() error
servoUpdateLocation(locationA uint8, locationB uint8) error
i2cInit() error
i2cStart(address7bit uint8, direction uint8) error
i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error
i2cRead(readBuffer []byte, length int, endWithStop uint8) error
i2cUpdateDelay(duration uint) error
2014-11-17 16:25:01 -08:00
error() error
2014-11-04 20:37:07 -08:00
}
type littleWire struct {
lwHandle *C.littleWire
}
2014-11-04 20:37:07 -08:00
func littleWireConnect() *littleWire {
return &littleWire{
2014-04-28 04:23:15 -07:00
lwHandle: C.littleWire_connect(),
}
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) digitalWrite(pin uint8, state uint8) error {
2014-04-28 04:23:15 -07:00
C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state))
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) pinMode(pin uint8, mode uint8) error {
2014-04-28 04:23:15 -07:00
C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode))
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) pwmInit() error {
2014-04-28 04:23:15 -07:00
C.pwm_init(l.lwHandle)
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) pwmStop() error {
2014-04-28 04:23:15 -07:00
C.pwm_stop(l.lwHandle)
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) error {
2014-04-28 04:23:15 -07:00
C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB))
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) pwmUpdatePrescaler(value uint) error {
2014-04-28 04:23:15 -07:00
C.pwm_updatePrescaler(l.lwHandle, C.uint(value))
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) servoInit() error {
2014-11-04 20:37:07 -08:00
C.servo_init(l.lwHandle)
2014-11-17 16:25:01 -08:00
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) error {
2014-11-04 20:37:07 -08:00
C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB))
2014-11-17 16:25:01 -08:00
return l.error()
}
func (l *littleWire) i2cInit() error {
C.i2c_init(l.lwHandle)
return l.error()
}
// i2cStart starts the i2c communication; set direction to 1 for reading, 0 for writing
func (l *littleWire) i2cStart(address7bit uint8, direction uint8) error {
if C.i2c_start(l.lwHandle, C.uchar(address7bit), C.uchar(direction)) == 1 {
return nil
}
if err := l.error(); err != nil {
return err
}
return fmt.Errorf("Littlewire i2cStart failed for %d in direction %d", address7bit, direction)
}
// i2cWrite sends byte(s) over i2c with a given length <= 4
func (l *littleWire) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error {
C.i2c_write(l.lwHandle, (*C.uchar)(&sendBuffer[0]), C.uchar(length), C.uchar(endWithStop))
return l.error()
}
// i2cRead reads byte(s) over i2c with a given length <= 8
func (l *littleWire) i2cRead(readBuffer []byte, length int, endWithStop uint8) error {
C.i2c_read(l.lwHandle, (*C.uchar)(&readBuffer[0]), C.uchar(length), C.uchar(endWithStop))
return l.error()
}
// i2cUpdateDelay updates i2c signal delay amount. Tune if necessary to fit your requirements
func (l *littleWire) i2cUpdateDelay(duration uint) error {
C.i2c_updateDelay(l.lwHandle, C.uint(duration))
return l.error()
}
2014-11-17 16:25:01 -08:00
func (l *littleWire) error() error {
str := C.GoString(C.littleWire_errorName())
if str != "" {
return errors.New(str)
}
return nil
}