@@ -15,6 +15,11 @@ type Device struct {
1515 r Range
1616}
1717
18+ // Driver configuration, used for the Configure call. All fields are optional.
19+ type Config struct {
20+ Address uint16
21+ }
22+
1823// New creates a new LIS3DH connection. The I2C bus must already be configured.
1924//
2025// This function only creates the Device object, it does not touch the device.
@@ -23,18 +28,31 @@ func New(bus drivers.I2C) Device {
2328}
2429
2530// Configure sets up the device for communication
26- func (d * Device ) Configure () {
31+ func (d * Device ) Configure (config Config ) error {
32+ if config .Address != 0 {
33+ d .Address = config .Address
34+ }
35+
2736 // enable all axes, normal mode
28- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , []byte {0x07 })
37+ err := legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , []byte {0x07 })
38+ if err != nil {
39+ }
2940
3041 // 400Hz rate
31- d .SetDataRate (DATARATE_400_HZ )
42+ err = d .SetDataRate (DATARATE_400_HZ )
43+ if err != nil {
44+ return err
45+ }
3246
3347 // High res & BDU enabled
34- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , []byte {0x88 })
48+ err = legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , []byte {0x88 })
49+ if err != nil {
50+ return err
51+ }
3552
3653 // get current range
37- d .r = d .ReadRange ()
54+ d .r , err = d .ReadRange ()
55+ return err
3856}
3957
4058// Connected returns whether a LIS3DH has been found.
@@ -49,46 +67,51 @@ func (d *Device) Connected() bool {
4967}
5068
5169// SetDataRate sets the speed of data collected by the LIS3DH.
52- func (d * Device ) SetDataRate (rate DataRate ) {
70+ func (d * Device ) SetDataRate (rate DataRate ) error {
5371 ctl1 := []byte {0 }
5472 err := legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , ctl1 )
5573 if err != nil {
56- println ( err . Error ())
74+ return err
5775 }
5876 // mask off bits
5977 ctl1 [0 ] &^= 0xf0
6078 ctl1 [0 ] |= (byte (rate ) << 4 )
61- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , ctl1 )
79+ return legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , ctl1 )
6280}
6381
6482// SetRange sets the G range for LIS3DH.
65- func (d * Device ) SetRange (r Range ) {
83+ func (d * Device ) SetRange (r Range ) error {
6684 ctl := []byte {0 }
6785 err := legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
6886 if err != nil {
69- println ( err . Error ())
87+ return err
7088 }
7189 // mask off bits
7290 ctl [0 ] &^= 0x30
7391 ctl [0 ] |= (byte (r ) << 4 )
74- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
92+ err = legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
93+ if err != nil {
94+ return err
95+ }
7596
7697 // store the new range
7798 d .r = r
99+
100+ return nil
78101}
79102
80103// ReadRange returns the current G range for LIS3DH.
81- func (d * Device ) ReadRange () (r Range ) {
104+ func (d * Device ) ReadRange () (r Range , err error ) {
82105 ctl := []byte {0 }
83- err : = legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
106+ err = legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
84107 if err != nil {
85- println ( err . Error ())
108+ return 0 , err
86109 }
87110 // mask off bits
88111 r = Range (ctl [0 ] >> 4 )
89112 r &= 0x03
90113
91- return r
114+ return r , nil
92115}
93116
94117// ReadAcceleration reads the current acceleration from the device and returns
0 commit comments