Timestamp returns the timestamp of the last PPS output edge from the given PPS source
()
| 161 | |
| 162 | // Timestamp returns the timestamp of the last PPS output edge from the given PPS source |
| 163 | func (ppsSource *PPSSource) Timestamp() (time.Time, error) { |
| 164 | if ppsSource.state != PPSSet { |
| 165 | return time.Time{}, fmt.Errorf("PPS source not set") |
| 166 | } |
| 167 | |
| 168 | currTime, err := ppsSource.PHCDevice.Time() |
| 169 | if err != nil { |
| 170 | return time.Time{}, fmt.Errorf("error getting time (clock_gettime) on %s", ppsSource.PHCDevice.File().Name()) |
| 171 | } |
| 172 | |
| 173 | // subtract device perout phase from current time to get the time of the last perout output edge |
| 174 | // TODO: optimize section below using binary operations instead of type conversions |
| 175 | currTime = currTime.Add(-time.Duration(ppsSource.peroutPhase)) |
| 176 | sourceTs, err := unix.TimeToTimespec(currTime) |
| 177 | if err != nil { |
| 178 | return time.Time{}, err |
| 179 | } |
| 180 | |
| 181 | sourceTs.Nsec = 0 |
| 182 | //nolint:unconvert // explicit int64 conversions kept for portability across platforms where Timespec fields may be int32 |
| 183 | currTime = time.Unix(int64(sourceTs.Sec), int64(sourceTs.Nsec)) |
| 184 | currTime = currTime.Add(time.Duration(ppsSource.peroutPhase)) |
| 185 | |
| 186 | return currTime, nil |
| 187 | } |
| 188 | |
| 189 | // NewPiServo returns a servo.PiServo object configure for synchronizing the given device. maxFreq 0 is equivalent to no maxFreq |
| 190 | func NewPiServo(interval time.Duration, firstStepth time.Duration, stepth time.Duration, device FrequencyGetter, maxFreq float64) (*servo.PiServo, error) { |