-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgpio.go
92 lines (81 loc) · 1.95 KB
/
gpio.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
80
81
82
83
84
85
86
87
88
89
90
91
92
/// Author: Bernhard Tittelbach, btittelbach@github (c) 2014
package bbhw
import (
"log"
"time"
)
type GPIOControllablePin interface {
SetState(bool) error
SetStateNow(bool) error
GetState() (bool, error)
CheckDirection() (int, error)
SetActiveLow(bool) error
}
type GPIOCollectionFactory interface {
EndTransactionApplySetStates()
BeginTransactionRecordSetStates()
NewGPIO(uint, int) GPIOControllablePinInCollection
}
type GPIOControllablePinInCollection interface {
SetState(bool) error
SetStateNow(bool) error
GetState() (bool, error)
CheckDirection() (int, error)
SetActiveLow(bool) error
SetFutureState(state bool) error
GetFutureState() (state_known, state bool, err error)
}
const (
IN = iota
OUT
IN_PULLDOWN
IN_PULLUP
)
type ADC interface {
ReadValue() uint16
CheckErrorOccurred() error
ReadValueCheckError() (uint16, error)
}
/// GPIOControllablePin Interface and Methods -----------------
func GetStateOrPanic(gpio GPIOControllablePin) bool {
r, err := gpio.GetState()
if err != nil {
panic(err)
}
return r
}
func CheckDirectionOrPanic(gpio GPIOControllablePin) int {
r, err := gpio.CheckDirection()
if err != nil {
panic(err)
}
return r
}
func Step(gpio GPIOControllablePin, steps uint32, delay time.Duration, abortcheck func() bool) (c uint32, err error) {
var curstate, oldstate bool
oldstate, err = gpio.GetState()
if err != nil {
log.Println("Step GetState Error:", err)
return
}
//on abort or return set old state
defer gpio.SetState(oldstate)
// fix return value
// due to defer above, we always finish last step, so the number of actual steps takes is roundup(c/2)
// which is what we want to return
defer func() { c += c % 2; c /= 2 }()
curstate = oldstate
for c = 0; c < steps*2; c++ {
curstate = !curstate
err = gpio.SetState(curstate)
if err != nil {
log.Println("Step SetState Error:", err)
return
}
time.Sleep(delay)
if c%2 == 0 && abortcheck != nil && abortcheck() {
break
}
}
return
}