From 441dfbc2a4303146331857761b76de2bc80aad08 Mon Sep 17 00:00:00 2001 From: M-A Date: Sun, 20 Jan 2019 11:40:40 -0500 Subject: [PATCH] physic: Improve Frequency rounding, add Period() (#386) - Improve rounding and add unit tests to confirm. - Period() is the same function than Duration() but better describes what is returned. - Fix support for negative value. - Upgrade all call sites inside periph. - Fix Frequency documentation that incorrectly stated it's an int32, it's in fact an int64. - Duration() will be removed in v4.0.0 as part of issue #379. --- experimental/devices/ads1x15/ads1x15.go | 2 +- experimental/devices/bitbang/i2c.go | 4 ++-- experimental/devices/bitbang/spi.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/experimental/devices/ads1x15/ads1x15.go b/experimental/devices/ads1x15/ads1x15.go index c169930..0bb5985 100644 --- a/experimental/devices/ads1x15/ads1x15.go +++ b/experimental/devices/ads1x15/ads1x15.go @@ -424,7 +424,7 @@ func (p *analogPin) ReadContinuous() <-chan analog.Sample { } reading := make(chan analog.Sample, 16) p.stop = make(chan struct{}) - t := time.NewTicker(p.requestedFrequency.Duration()) + t := time.NewTicker(p.requestedFrequency.Period()) go func(s <-chan struct{}) { defer t.Stop() diff --git a/experimental/devices/bitbang/i2c.go b/experimental/devices/bitbang/i2c.go index cd62f5c..56c9585 100644 --- a/experimental/devices/bitbang/i2c.go +++ b/experimental/devices/bitbang/i2c.go @@ -52,7 +52,7 @@ func New(clk gpio.PinIO, data gpio.PinIO, f physic.Frequency) (*I2C, error) { i := &I2C{ scl: clk, sda: data, - halfCycle: f.Duration() / 2, + halfCycle: f.Period() / 2, } return i, nil } @@ -126,7 +126,7 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error { func (i *I2C) SetSpeed(f physic.Frequency) error { i.mu.Lock() defer i.mu.Unlock() - i.halfCycle = f.Duration() / 2 + i.halfCycle = f.Period() / 2 return nil } diff --git a/experimental/devices/bitbang/spi.go b/experimental/devices/bitbang/spi.go index 04d1278..c6a2813 100644 --- a/experimental/devices/bitbang/spi.go +++ b/experimental/devices/bitbang/spi.go @@ -73,7 +73,7 @@ func (s *SPI) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, er defer s.spiConn.mu.Unlock() s.spiConn.freqDev = f if s.spiConn.freqDev != 0 && (s.spiConn.freqPort == 0 || s.spiConn.freqDev < s.spiConn.freqPort) { - s.spiConn.halfCycle = f.Duration() / 2 + s.spiConn.halfCycle = f.Period() / 2 } s.spiConn.mode = mode s.spiConn.bits = bits @@ -101,7 +101,7 @@ func (s *SPI) LimitSpeed(f physic.Frequency) error { defer s.spiConn.mu.Unlock() s.spiConn.freqPort = f if s.spiConn.freqDev == 0 || s.spiConn.freqPort < s.spiConn.freqDev { - s.spiConn.halfCycle = f.Duration() / 2 + s.spiConn.halfCycle = f.Period() / 2 } return nil }