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.
pull/1/head
M-A 7 years ago committed by GitHub
parent c52d26920b
commit 441dfbc2a4

@ -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()

@ -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
}

@ -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
}

Loading…
Cancel
Save