giuliomoro@135: #ifndef VIRTUAL_CLOCK_H_INCLUDED giuliomoro@135: #define VIRTUAL_CLOCK_H_INCLUDED giuliomoro@135: giuliomoro@141: #include "math.h" giuliomoro@135: #include "stats.hpp" giuliomoro@135: #include "Clock.h" giuliomoro@149: #include "IirFilter.h" giuliomoro@149: #include "Kalman.h" giuliomoro@141: #ifdef USE_JUCE giuliomoro@141: #else giuliomoro@141: #include giuliomoro@150: #include giuliomoro@150: extern I2c_Codec* gAudioCodec; giuliomoro@141: #endif /* USE_JUCE */ giuliomoro@135: giuliomoro@135: class VirtualClock{ giuliomoro@135: private: giuliomoro@135: myClock_t startTime; giuliomoro@141: myClock_t startTimeOffset; giuliomoro@149: myClock_t lastSyncTime; giuliomoro@149: // myClock_t lastSyncEstimatedTime; giuliomoro@149: double lastSyncEstimatedTime; giuliomoro@135: bool firstRun; giuliomoro@141: double elapsedPeriods; giuliomoro@141: double elapsedPeriodsOffset; giuliomoro@149: double blockPeriod; giuliomoro@135: double period; giuliomoro@135: MovingAverage movingAverage; giuliomoro@149: IirFilter iir; giuliomoro@149: KalmanOne kalman; giuliomoro@135: public: giuliomoro@149: void init(float initialValueUs); giuliomoro@135: VirtualClock(); giuliomoro@135: /** giuliomoro@135: Call this method at regular intervals to sync che virtual clock giuliomoro@135: */ giuliomoro@135: void sync(); giuliomoro@135: /** giuliomoro@135: Call this method asynchronously, passing a number of equally spaced events that have elapsed since the last call. giuliomoro@135: */ giuliomoro@141: void sync(double numPeriods); giuliomoro@135: /** giuliomoro@135: Get the current time according to the VirtualClock. giuliomoro@135: giuliomoro@135: @return Time elapsed since the first call to sync(), in period units. giuliomoro@135: */ giuliomoro@135: double getNow(); giuliomoro@135: /** giuliomoro@135: Get the length of the period. giuliomoro@135: giuliomoro@135: Get the length of the period (difference between calls to sync() after various filtering operations) giuliomoro@135: */ giuliomoro@135: double getPeriod(); giuliomoro@141: /** giuliomoro@141: * Add an offset to the number of elapsed periods. giuliomoro@141: * giuliomoro@141: * Add an offset to the number of elapsed periods. It also compensates for the corresponding time offset. giuliomoro@141: */ giuliomoro@141: void addOffset(double periodOffset); giuliomoro@135: }; giuliomoro@135: giuliomoro@135: #endif /* VIRTUAL_CLOCK_H_INCLUDED */