-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPiADCs.go
78 lines (65 loc) · 2.84 KB
/
PiADCs.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
package piadcs
import (
"log"
adc "github.com/AnnaKnapp/piadcs/ads126x"
spi "periph.io/x/periph/conn/spi"
)
type Register struct {
Name string
Address byte
Defaultvalue byte
//use the function Setregister with values from the constants file or you can also set this manually by looking at the datasheet.
Setvalue byte
}
//This function sets the register data byte according to the settings specified. PLEASE make sure to list either ALL settings for a given register OR the default value. For examp if you want to change the POWER register on the ADS126x to enable vbias, make sure to also include the setting for the internal reference which is also controlled by that register. This will ensure that the registers are set exactly as you intended. The lists of all possible settings for registers are listed in the constants file for that device.
func (reg *Register) Setregister(settings []byte) {
for i := range settings {
reg.Setvalue = settings[i] | reg.Setvalue
}
}
//Use this to create a register to then set using the setregister function
func NewRegister(name string, address byte) Register {
return Register{
Name: name,
Address: address,
}
}
//This writes data to consecutive registers. You need to specify the starting register and the byte slice of data to write. It will go down the slice and write one byte to each consecutive register starting from the one specified. Please see the datasheet for more information. The WREG opcode is used here and uses the same programming on multiple different TI ADCs
func WriteToConsecutiveRegisters(connection spi.Conn, startingreg byte, datatowrite []byte) {
towrite := []byte{adc.WREG | startingreg, byte(len(datatowrite) - 1)}
// for i := range datatowrite {
// towrite = append(towrite, datatowrite[i])
// }
// ... destructures a slice
towrite = append(towrite, datatowrite...)
toread := make([]byte, len(towrite))
if err := connection.Tx(towrite, toread); err != nil {
log.Fatal(err)
}
}
//Use this function to check what data is stored at what registers. The starting register and the number of registers to read must be specified.
func ReadFromConsecutiveRegisters(connection spi.Conn, startingreg byte, numbertoread byte) []byte {
asktoread := []byte{adc.RREG | startingreg, numbertoread - 1}
blank1 := make([]byte, 2)
registerdata := make([]byte, int(numbertoread))
blank2 := make([]byte, int(numbertoread))
if err := connection.Tx(asktoread, blank1); err != nil {
log.Fatal(err)
}
if err := connection.Tx(blank2, registerdata); err != nil {
log.Fatal(err)
}
return registerdata
}
//checks if the data returned from a register read matches the data that was initially sent - use this to ensure you properly set your registers
func RegisterMatch(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}