forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
littleWire.go
79 lines (65 loc) · 1.7 KB
/
littleWire.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package digispark
//#cgo LDFLAGS: -lusb
//#include "littleWire.h"
//#include "littleWire_servo.h"
//typedef usb_dev_handle littleWire;
import "C"
import "errors"
type lw interface {
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
}
type littleWire struct {
lwHandle *C.littleWire
}
func littleWireConnect() *littleWire {
return &littleWire{
lwHandle: C.littleWire_connect(),
}
}
func (l *littleWire) digitalWrite(pin uint8, state uint8) error {
C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state))
return l.error()
}
func (l *littleWire) pinMode(pin uint8, mode uint8) error {
C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode))
return l.error()
}
func (l *littleWire) pwmInit() error {
C.pwm_init(l.lwHandle)
return l.error()
}
func (l *littleWire) pwmStop() error {
C.pwm_stop(l.lwHandle)
return l.error()
}
func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) error {
C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB))
return l.error()
}
func (l *littleWire) pwmUpdatePrescaler(value uint) error {
C.pwm_updatePrescaler(l.lwHandle, C.uint(value))
return l.error()
}
func (l *littleWire) servoInit() error {
C.servo_init(l.lwHandle)
return l.error()
}
func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) error {
C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB))
return l.error()
}
func (l *littleWire) error() error {
str := C.GoString(C.littleWire_errorName())
if str != "" {
return errors.New(str)
}
return nil
}