gobot.io/x/gobot/v2@v2.1.0/platforms/digispark/littleWire.go (about) 1 package digispark 2 3 //#cgo pkg-config: libusb 4 //#include "littleWire.h" 5 //#include "littleWire_servo.h" 6 //typedef usb_dev_handle littleWire; 7 import "C" 8 9 import ( 10 "errors" 11 "fmt" 12 ) 13 14 type lw interface { 15 digitalWrite(uint8, uint8) error 16 pinMode(uint8, uint8) error 17 pwmInit() error 18 pwmStop() error 19 pwmUpdateCompare(uint8, uint8) error 20 pwmUpdatePrescaler(uint) error 21 servoInit() error 22 servoUpdateLocation(uint8, uint8) error 23 i2cInit() error 24 i2cStart(address7bit uint8, direction uint8) error 25 i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error 26 i2cRead(readBuffer []byte, length int, endWithStop uint8) error 27 i2cUpdateDelay(duration uint) error 28 error() error 29 } 30 31 type littleWire struct { 32 lwHandle *C.littleWire 33 } 34 35 func littleWireConnect() *littleWire { 36 return &littleWire{ 37 lwHandle: C.littleWire_connect(), 38 } 39 } 40 41 func (l *littleWire) digitalWrite(pin uint8, state uint8) error { 42 C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state)) 43 return l.error() 44 } 45 46 func (l *littleWire) pinMode(pin uint8, mode uint8) error { 47 C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode)) 48 return l.error() 49 } 50 51 func (l *littleWire) pwmInit() error { 52 C.pwm_init(l.lwHandle) 53 return l.error() 54 } 55 56 func (l *littleWire) pwmStop() error { 57 C.pwm_stop(l.lwHandle) 58 return l.error() 59 } 60 61 func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) error { 62 C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB)) 63 return l.error() 64 } 65 66 func (l *littleWire) pwmUpdatePrescaler(value uint) error { 67 C.pwm_updatePrescaler(l.lwHandle, C.uint(value)) 68 return l.error() 69 } 70 71 func (l *littleWire) servoInit() error { 72 C.servo_init(l.lwHandle) 73 return l.error() 74 } 75 76 func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) error { 77 C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB)) 78 return l.error() 79 } 80 81 func (l *littleWire) i2cInit() error { 82 C.i2c_init(l.lwHandle) 83 return l.error() 84 } 85 86 // i2cStart starts the i2c communication; set direction to 1 for reading, 0 for writing 87 func (l *littleWire) i2cStart(address7bit uint8, direction uint8) error { 88 if C.i2c_start(l.lwHandle, C.uchar(address7bit), C.uchar(direction)) == 1 { 89 return nil 90 } 91 if err := l.error(); err != nil { 92 return err 93 } 94 return fmt.Errorf("Littlewire i2cStart failed for %d in direction %d", address7bit, direction) 95 } 96 97 // i2cWrite sends byte(s) over i2c with a given length <= 4 98 func (l *littleWire) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error { 99 C.i2c_write(l.lwHandle, (*C.uchar)(&sendBuffer[0]), C.uchar(length), C.uchar(endWithStop)) 100 return l.error() 101 } 102 103 // i2cRead reads byte(s) over i2c with a given length <= 8 104 func (l *littleWire) i2cRead(readBuffer []byte, length int, endWithStop uint8) error { 105 C.i2c_read(l.lwHandle, (*C.uchar)(&readBuffer[0]), C.uchar(length), C.uchar(endWithStop)) 106 return l.error() 107 } 108 109 // i2cUpdateDelay updates i2c signal delay amount. Tune if neccessary to fit your requirements 110 func (l *littleWire) i2cUpdateDelay(duration uint) error { 111 C.i2c_updateDelay(l.lwHandle, C.uint(duration)) 112 return l.error() 113 } 114 115 func (l *littleWire) error() error { 116 str := C.GoString(C.littleWire_errorName()) 117 if str != "" { 118 return errors.New(str) 119 } 120 return nil 121 }