35 lines
520 B
Go
35 lines
520 B
Go
package main
|
|
|
|
type ringbuffer struct {
|
|
data []float32
|
|
idx int
|
|
}
|
|
|
|
func newRingbuffer() ringbuffer {
|
|
return ringbuffer {
|
|
data: make([]float32, 5),
|
|
idx: 0,
|
|
}
|
|
}
|
|
|
|
func (rb *ringbuffer) AddValue(value float32) {
|
|
rb.data[rb.idx] = value;
|
|
rb.idx = (rb.idx + 1) % len(rb.data)
|
|
}
|
|
|
|
func (rb *ringbuffer) MaxMinDiff() float32 {
|
|
min := rb.data[0]
|
|
max := rb.data[0]
|
|
|
|
for _, val := range rb.data {
|
|
if val < min {
|
|
min = val
|
|
}
|
|
if val > max {
|
|
max = val
|
|
}
|
|
}
|
|
|
|
return max - min
|
|
}
|