| 1 |
//-------------------------------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) 2018 Denis Dyakov |
| 4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 6 |
// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 7 |
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 |
// subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 12 |
// portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 15 |
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 17 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
//-------------------------------------------------------------------------------------------------- |
| 21 |
|
| 22 |
package bsbmp |
| 23 |
|
| 24 |
import ( |
| 25 |
"bytes" |
| 26 |
"encoding/binary" |
| 27 |
"fmt" |
| 28 |
"time" |
| 29 |
|
| 30 |
i2c "github.com/d2r2/go-i2c" |
| 31 |
) |
| 32 |
|
| 33 |
// Utility functions |
| 34 |
|
| 35 |
// getS16BE extract 2-byte integer as signed big-endian. |
| 36 |
func getS16BE(buf []byte) int16 { |
| 37 |
v := int16(buf[0])<<8 + int16(buf[1]) |
| 38 |
return v |
| 39 |
} |
| 40 |
|
| 41 |
// getS16LE extract 2-byte integer as signed little-endian. |
| 42 |
func getS16LE(buf []byte) int16 { |
| 43 |
w := getS16BE(buf) |
| 44 |
// exchange bytes |
| 45 |
v := (w&0xFF)<<8 + w>>8 |
| 46 |
return v |
| 47 |
} |
| 48 |
|
| 49 |
// getU16BE extract 2-byte integer as unsigned big-endian. |
| 50 |
func getU16BE(buf []byte) uint16 { |
| 51 |
v := uint16(buf[0])<<8 + uint16(buf[1]) |
| 52 |
return v |
| 53 |
} |
| 54 |
|
| 55 |
// getU16LE extract 2-byte integer as unsigned little-endian. |
| 56 |
func getU16LE(buf []byte) uint16 { |
| 57 |
w := getU16BE(buf) |
| 58 |
// exchange bytes |
| 59 |
v := (w&0xFF)<<8 + w>>8 |
| 60 |
return v |
| 61 |
} |
| 62 |
|
| 63 |
// checkCoefficient verify that compensation parameter looks valid. |
| 64 |
func checkCoefficient(coef uint16, name string) error { |
| 65 |
if coef == 0 || coef == 0xFFFF { |
| 66 |
return fmt.Errorf("Coefficient %s is invalid: 0x%X", name, coef) |
| 67 |
} |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
// waitForCompletion Wait until sensor completes measurements and calculations, |
| 72 |
// otherwise return on timeout. |
| 73 |
func waitForCompletion(sensor SensorInterface, i2c *i2c.I2C) (timeout bool, err error) { |
| 74 |
for i := 0; i < 10; i++ { |
| 75 |
flag, err := sensor.IsBusy(i2c) |
| 76 |
if err != nil { |
| 77 |
return false, err |
| 78 |
} |
| 79 |
if flag == false { |
| 80 |
return false, nil |
| 81 |
} |
| 82 |
time.Sleep(5 * time.Millisecond) |
| 83 |
} |
| 84 |
return true, nil |
| 85 |
} |
| 86 |
|
| 87 |
// Read byte block from i2c device to struct object. |
| 88 |
func readDataToStruct(i2c *i2c.I2C, byteCount int, |
| 89 |
byteOrder binary.ByteOrder, obj interface{}) error { |
| 90 |
buf1 := make([]byte, byteCount) |
| 91 |
_, err := i2c.ReadBytes(buf1) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
buf := bytes.NewBuffer(buf1) |
| 96 |
err = binary.Read(buf, byteOrder, obj) |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |