@@ -11,84 +11,108 @@ import (
1111// Device wraps an I2C connection to a LIS3DH device.
1212type Device struct {
1313 bus drivers.I2C
14- Address uint16
14+ address uint16
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.
2126func New (bus drivers.I2C ) Device {
22- return Device {bus : bus , Address : Address0 }
27+ return Device {bus : bus , address : Address0 }
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+ return err
40+ }
2941
3042 // 400Hz rate
31- d .SetDataRate (DATARATE_400_HZ )
43+ err = d .SetDataRate (DATARATE_400_HZ )
44+ if err != nil {
45+ return err
46+ }
3247
3348 // High res & BDU enabled
34- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , []byte {0x88 })
49+ err = legacy .WriteRegister (d .bus , uint8 (d .address ), REG_CTRL4 , []byte {0x88 })
50+ if err != nil {
51+ return err
52+ }
3553
3654 // get current range
37- d .r = d .ReadRange ()
55+ d .r , err = d .ReadRange ()
56+ return err
3857}
3958
4059// Connected returns whether a LIS3DH has been found.
4160// It does a "who am I" request and checks the response.
4261func (d * Device ) Connected () bool {
4362 data := []byte {0 }
44- err := legacy .ReadRegister (d .bus , uint8 (d .Address ), WHO_AM_I , data )
63+ err := legacy .ReadRegister (d .bus , uint8 (d .address ), WHO_AM_I , data )
4564 if err != nil {
4665 return false
4766 }
4867 return data [0 ] == 0x33
4968}
5069
5170// SetDataRate sets the speed of data collected by the LIS3DH.
52- func (d * Device ) SetDataRate (rate DataRate ) {
71+ func (d * Device ) SetDataRate (rate DataRate ) error {
5372 ctl1 := []byte {0 }
54- err := legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , ctl1 )
73+ err := legacy .ReadRegister (d .bus , uint8 (d .address ), REG_CTRL1 , ctl1 )
5574 if err != nil {
56- println ( err . Error ())
75+ return err
5776 }
5877 // mask off bits
5978 ctl1 [0 ] &^= 0xf0
6079 ctl1 [0 ] |= (byte (rate ) << 4 )
61- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL1 , ctl1 )
80+ return legacy .WriteRegister (d .bus , uint8 (d .address ), REG_CTRL1 , ctl1 )
6281}
6382
6483// SetRange sets the G range for LIS3DH.
65- func (d * Device ) SetRange (r Range ) {
84+ func (d * Device ) SetRange (r Range ) error {
6685 ctl := []byte {0 }
67- err := legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
86+ err := legacy .ReadRegister (d .bus , uint8 (d .address ), REG_CTRL4 , ctl )
6887 if err != nil {
69- println ( err . Error ())
88+ return err
7089 }
7190 // mask off bits
7291 ctl [0 ] &^= 0x30
7392 ctl [0 ] |= (byte (r ) << 4 )
74- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
93+ err = legacy .WriteRegister (d .bus , uint8 (d .address ), REG_CTRL4 , ctl )
94+ if err != nil {
95+ return err
96+ }
7597
7698 // store the new range
7799 d .r = r
100+
101+ return nil
78102}
79103
80104// ReadRange returns the current G range for LIS3DH.
81- func (d * Device ) ReadRange () (r Range ) {
105+ func (d * Device ) ReadRange () (r Range , err error ) {
82106 ctl := []byte {0 }
83- err : = legacy .ReadRegister (d .bus , uint8 (d .Address ), REG_CTRL4 , ctl )
107+ err = legacy .ReadRegister (d .bus , uint8 (d .address ), REG_CTRL4 , ctl )
84108 if err != nil {
85- println ( err . Error ())
109+ return 0 , err
86110 }
87111 // mask off bits
88112 r = Range (ctl [0 ] >> 4 )
89113 r &= 0x03
90114
91- return r
115+ return r , nil
92116}
93117
94118// ReadAcceleration reads the current acceleration from the device and returns
@@ -114,10 +138,10 @@ func (d *Device) ReadAcceleration() (int32, int32, int32, error) {
114138
115139// ReadRawAcceleration returns the raw x, y and z axis from the LIS3DH
116140func (d * Device ) ReadRawAcceleration () (x int16 , y int16 , z int16 ) {
117- legacy .WriteRegister (d .bus , uint8 (d .Address ), REG_OUT_X_L | 0x80 , nil )
141+ legacy .WriteRegister (d .bus , uint8 (d .address ), REG_OUT_X_L | 0x80 , nil )
118142
119143 data := []byte {0 , 0 , 0 , 0 , 0 , 0 }
120- d .bus .Tx (d .Address , nil , data )
144+ d .bus .Tx (d .address , nil , data )
121145
122146 x = int16 ((uint16 (data [1 ]) << 8 ) | uint16 (data [0 ]))
123147 y = int16 ((uint16 (data [3 ]) << 8 ) | uint16 (data [2 ]))
0 commit comments