giuliomoro@42: /* giuliomoro@42: * intervals.h giuliomoro@42: * giuliomoro@42: * Created on: 18 May 2015 giuliomoro@42: * Author: unmanaged giuliomoro@42: */ giuliomoro@42: giuliomoro@42: #include giuliomoro@42: #include giuliomoro@42: #include giuliomoro@42: giuliomoro@42: #include giuliomoro@42: #include giuliomoro@42: giuliomoro@42: #include "../include/intervals.h" giuliomoro@42: void Interval::init(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){ giuliomoro@42: enabled=false; giuliomoro@42: numAverages=aNumAverages; giuliomoro@42: intervals=(RTIME *)malloc(sizeof(RTIME)*numAverages); giuliomoro@42: samplingRate=aSamplingRate; giuliomoro@42: numFrames=aNumFrames; giuliomoro@42: maxTimeus=0; giuliomoro@42: intervalsPointer=0; giuliomoro@42: sum=0; giuliomoro@42: startTime=0; giuliomoro@42: giuliomoro@42: if(intervals!=0) giuliomoro@42: enabled=true; giuliomoro@42: int len=strlen(aName); giuliomoro@42: name=(char *)malloc(sizeof(char)*(len+1)); giuliomoro@42: strcpy(name, aName); giuliomoro@42: if(name == 0) giuliomoro@42: enabled=false; giuliomoro@42: }; giuliomoro@42: Interval::Interval(){ giuliomoro@42: init(100,1,44100,""); giuliomoro@42: } giuliomoro@42: Interval::Interval(int aNumAverages){ giuliomoro@42: init(aNumAverages,1,44100,""); giuliomoro@42: } giuliomoro@42: Interval::Interval(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){ giuliomoro@42: init(aNumAverages,aNumFrames,aSamplingRate,aName); giuliomoro@42: } giuliomoro@42: Interval::~Interval(){ giuliomoro@42: free(intervals); giuliomoro@42: // free(name); giuliomoro@42: } giuliomoro@42: void Interval::setNumFrames(int aNumFrames){ giuliomoro@42: numFrames=aNumFrames; giuliomoro@42: }; giuliomoro@42: giuliomoro@42: int Interval::start(){ giuliomoro@42: // printf("start: intervals: 0x%x, intervalsPointer: %d, numAverages: %d\n", intervals,intervalsPointer,numAverages); giuliomoro@42: if(!enabled) giuliomoro@42: return 0; giuliomoro@42: startTime=rt_timer_read(); giuliomoro@42: return 1; giuliomoro@42: } giuliomoro@42: int Interval::resetMax(){ giuliomoro@42: maxTimeus=0; giuliomoro@42: return 1; giuliomoro@42: } giuliomoro@42: int Interval::split(){ //updates giuliomoro@42: if(!enabled) giuliomoro@42: return 0; giuliomoro@42: int currentInterval=rt_timer_read()-startTime; giuliomoro@42: RTIME *currentPointer=&(intervals[intervalsPointer]); giuliomoro@42: sum-=*currentPointer; //take out the oldest value from the sum giuliomoro@42: *currentPointer=currentInterval; giuliomoro@42: sum+=*currentPointer; //add the new value to the sum giuliomoro@42: timeus=((float)sum)/numAverages/1000.0; giuliomoro@42: maxTimeus=timeus>maxTimeus?timeus:maxTimeus; giuliomoro@42: intervalsPointer++; giuliomoro@42: if(intervalsPointer>=(numAverages-1)){ giuliomoro@42: intervalsPointer=0; giuliomoro@42: } giuliomoro@42: return 1; giuliomoro@42: } giuliomoro@42: void Interval::setEnabled(bool aActive){ giuliomoro@42: enabled=aActive; giuliomoro@42: } giuliomoro@42: float Interval::getTimeus(){ giuliomoro@42: return timeus; giuliomoro@42: } giuliomoro@42: float Interval::getMaxTimeus(){ giuliomoro@42: return maxTimeus; giuliomoro@42: } giuliomoro@42: void Interval::print(){ giuliomoro@42: rt_printf(//"sleepTimer time: 29.484us, (1.30 samples, numFrames: 2, 65.01%%). MaxTime: 30.439us, (1.34 samples, 67.12%%)\n"); giuliomoro@42: "%s time: %.3fus, (%.2f samples, numFrames: %d, %.2f%%). MaxTime: %.3fus, (%.2f samples, %.2f%%)\n", giuliomoro@42: name, giuliomoro@42: timeus, timeus/1000000.0*samplingRate, numFrames, timeus/1000000.0*samplingRate/numFrames * 100, giuliomoro@42: maxTimeus, maxTimeus/1000000.0*samplingRate, maxTimeus/1000000.0*samplingRate/numFrames * 100); giuliomoro@42: }