Reputation: 597
I use the library “golang.org/x/exp/io/i2c” to handle a certain module (AS3935). The module provides 9 registers with the ability to read and write values to them, they are addressed from 0x00 to 0x08. When the ReadReg function is called (its description is: ReadReg is similar to Read but it reads from a register.), no matter which address byte I specify as the first parameter, the first register is always returned. Only after the buffer is increased I can see that the values of subsequent registers are in the next fields of the buffer. What is the register parameter used for then?
This is how the helper functions look like, which I will use to write and read values:
type module struct {
// I want to read/write only one byte per call so the buffers size is 1.
BufferRead []uint8
BufferWrite []uint8
// ... other not relevent fields
}
// Read a value from the register specified by the offset parameter.
func (m *module) RegRead(offset uint8) (uint8, error) {
err := m.Device.ReadReg(offset, m.BufferRead)
if err != nil {
return 0x00, err
}
return m.BufferRead[0], nil
}
// Write a value byte parameter to the register specified by the offset parameter.
func (m *module) RegWrite(offset, value uint8) error {
m.BufferWrite[0] = value
err := m.Device.WriteReg(offset, m.BufferWrite)
if err != nil {
return err
}
return nil
}
Upvotes: 1
Views: 71