This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathads.go
99 lines (86 loc) · 2.66 KB
/
ads.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
93
94
95
96
97
98
99
package ads
import (
"encoding/binary"
"time"
"periph.io/x/periph/host"
)
// HostInit calls periph.io host.Init(). This needs to be done before ADS can be used.
func HostInit() error {
_, err := host.Init()
return err
}
// Close closes bus
func (ads *ADS) Close() error {
if ads.busCloser == nil {
return nil
}
busCloser := *ads.busCloser
ads.busCloser = nil
return busCloser.Close()
}
// SetConfigInputMultiplexer sets input multiplexer
func (ads *ADS) SetConfigInputMultiplexer(configInputMultiplexer ConfigInputMultiplexer) {
ads.configInputMultiplexer = uint16(configInputMultiplexer)
binary.BigEndian.PutUint16(ads.config, configDefault|ads.configInputMultiplexer|ads.configGain|ads.configDataRate)
ads.write[1] = ads.config[0]
ads.write[2] = ads.config[1]
}
// SetConfigGain sets gain
func (ads *ADS) SetConfigGain(configGain ConfigGain) {
ads.configGain = uint16(configGain)
binary.BigEndian.PutUint16(ads.config, configDefault|ads.configInputMultiplexer|ads.configGain|ads.configDataRate)
ads.write[1] = ads.config[0]
ads.write[2] = ads.config[1]
}
// SetConfigDataRate sets data rate
func (ads *ADS) SetConfigDataRate(configDataRate ConfigDataRate) {
ads.configDataRate = uint16(configDataRate)
binary.BigEndian.PutUint16(ads.config, configDefault|ads.configInputMultiplexer|ads.configGain|ads.configDataRate)
ads.write[1] = ads.config[0]
ads.write[2] = ads.config[1]
}
// ReadRetry will call Read until there is no errors or the maxRetries is hit.
// Suggest maxRetries to be set around 5.
func (ads *ADS) ReadRetry(maxRetries int) (result uint16, err error) {
for i := 0; i < maxRetries; i++ {
result, err = ads.Read()
if err == nil {
return
}
}
return
}
// ReadBackground it meant to be run in the background, run as a Goroutine.
// sleepDuration is how long it will try to sleep between reads.
// If there is ongoing read errors there will be no notice except that the result will not be updated.
// Will continue to read ads chip until stop is closed.
// After it has been stopped, the stopped chan will be closed.
// Will panic if result or stop are nil.
func (ads *ADS) ReadBackground(result *uint16, sleepDuration time.Duration, stop chan struct{}, stopped chan struct{}) {
var resultTemp uint16
var err error
var startTime time.Time
Loop:
for {
startTime = time.Now()
resultTemp, err = ads.Read()
if err == nil {
// no read error, save result
*result = resultTemp
// wait for sleepDuration or stop
select {
case <-time.After(sleepDuration - time.Since(startTime)):
case <-stop:
break Loop
}
} else {
// read error, just check for stop
select {
case <-stop:
break Loop
default:
}
}
}
close(stopped)
}