@@ -21,6 +21,7 @@ import (
2121 "periph.io/x/conn/v3"
2222 "periph.io/x/conn/v3/i2c"
2323 "periph.io/x/conn/v3/physic"
24+ "periph.io/x/devices/v3/common"
2425)
2526
2627type SampleRate uint16
@@ -203,21 +204,6 @@ func countToHumidity(bytes []byte) physic.RelativeHumidity {
203204 return physic .RelativeHumidity (f * float64 (physic .PercentRH ))
204205}
205206
206- func crc8 (bytes []byte ) byte {
207- var crc byte = 0xff
208- for _ , val := range bytes {
209- crc ^= val
210- for range 8 {
211- if (crc & 0x80 ) == 0 {
212- crc <<= 1
213- } else {
214- crc = (byte )((crc << 1 ) ^ 0x31 )
215- }
216- }
217- }
218- return crc
219- }
220-
221207// Halt shuts down the device. If a SenseContinuous operation is in progress,
222208// its aborted. Implements conn.Resource
223209func (dev * Dev ) Halt () error {
@@ -251,7 +237,7 @@ func (dev *Dev) Sense(env *physic.Env) error {
251237 if err := dev .d .Tx (read , res ); err != nil {
252238 return fmt .Errorf ("hdc302x: %w" , err )
253239 }
254- if crc8 (res [:2 ]) != res [2 ] || crc8 (res [3 :5 ]) != res [5 ] {
240+ if common . CRC8 (res [:2 ]) != res [2 ] || common . CRC8 (res [3 :5 ]) != res [5 ] {
255241 return errInvalidCRC
256242 }
257243 env .Temperature = countToTemperature (res )
@@ -320,7 +306,7 @@ func (dev *Dev) readSerialNumber() int64 {
320306 // this is a 6 byte value read in 3 parts
321307 for range 3 {
322308 err := dev .d .Tx (cmd , r )
323- if err != nil || (crc8 (r [:2 ]) != r [2 ]) {
309+ if err != nil || (common . CRC8 (r [:2 ]) != r [2 ]) {
324310 return result
325311 }
326312 result = result << 16 | (int64 (r [0 ])<< 8 | int64 (r [1 ]))
@@ -358,7 +344,7 @@ func (dev *Dev) readAlertValues(cfg *Configuration) error {
358344 if err != nil {
359345 return err
360346 }
361- if crc8 (r [:2 ]) != r [2 ] {
347+ if common . CRC8 (r [:2 ]) != r [2 ] {
362348 return errInvalidCRC
363349 }
364350 wValue := uint16 (r [0 ])<< 8 | uint16 (r [1 ])
@@ -381,7 +367,7 @@ func (dev *Dev) readOffsets(cfg *Configuration) error {
381367 if err := dev .d .Tx (readSetOffsets , r ); err != nil {
382368 return fmt .Errorf ("hdc302x: %w" , err )
383369 }
384- if crc8 (r [:2 ]) != r [2 ] {
370+ if common . CRC8 (r [:2 ]) != r [2 ] {
385371 return errInvalidCRC
386372 }
387373
@@ -424,7 +410,7 @@ func (dev *Dev) ReadStatus() (StatusWord, error) {
424410 if err := dev .d .Tx (readStatus , r ); err != nil {
425411 return 0 , err
426412 }
427- if crc8 (r [:2 ]) != r [2 ] {
413+ if common . CRC8 (r [:2 ]) != r [2 ] {
428414 return 0 , errInvalidCRC
429415 }
430416 _ = dev .d .Tx (clearStatus , nil )
@@ -463,7 +449,7 @@ func (dev *Dev) setOffsets(cfg *Configuration) error {
463449 computeTemperatureOffsetByte (cfg .TemperatureOffset ),
464450 0 ,
465451 }
466- w [4 ] = crc8 (w [2 :4 ])
452+ w [4 ] = common . CRC8 (w [2 :4 ])
467453 return dev .d .Tx (w , nil )
468454}
469455
@@ -553,7 +539,7 @@ func (dev *Dev) setThresholds(typeAlert bool, tp *ThresholdPair) error {
553539 wval := uint16 (0 )
554540 wval = (humBits & 0xfe00 ) | tempBits >> 7
555541 w := []byte {cmds [pair ][ix ][0 ], cmds [pair ][ix ][1 ], byte (wval >> 8 ), byte (wval & 0xff ), 0 }
556- w [4 ] = crc8 (w [2 :4 ])
542+ w [4 ] = common . CRC8 (w [2 :4 ])
557543 err := dev .d .Tx (w , nil )
558544 if err != nil {
559545 return err
@@ -609,7 +595,7 @@ func (dev *Dev) SetHeater(powerLevel HeaterPower) error {
609595 byte ((powerLevel >> 8 ) & 0xff ),
610596 byte (powerLevel & 0xff ),
611597 0 }
612- setValue [4 ] = crc8 (setValue [2 :4 ])
598+ setValue [4 ] = common . CRC8 (setValue [2 :4 ])
613599 err := dev .d .Tx (setValue , nil )
614600 if err != nil {
615601 return err
0 commit comments