Mercurial > hg > beaglert
comparison core/intervals.cpp @ 42:24af9a14b203 ultra-staging
Included timer files
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 19 May 2015 16:42:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
41:4255ecbb9bec | 42:24af9a14b203 |
---|---|
1 /* | |
2 * intervals.h | |
3 * | |
4 * Created on: 18 May 2015 | |
5 * Author: unmanaged | |
6 */ | |
7 | |
8 #include <unistd.h> | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 | |
12 #include <native/timer.h> | |
13 #include <rtdk.h> | |
14 | |
15 #include "../include/intervals.h" | |
16 void Interval::init(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){ | |
17 enabled=false; | |
18 numAverages=aNumAverages; | |
19 intervals=(RTIME *)malloc(sizeof(RTIME)*numAverages); | |
20 samplingRate=aSamplingRate; | |
21 numFrames=aNumFrames; | |
22 maxTimeus=0; | |
23 intervalsPointer=0; | |
24 sum=0; | |
25 startTime=0; | |
26 | |
27 if(intervals!=0) | |
28 enabled=true; | |
29 int len=strlen(aName); | |
30 name=(char *)malloc(sizeof(char)*(len+1)); | |
31 strcpy(name, aName); | |
32 if(name == 0) | |
33 enabled=false; | |
34 }; | |
35 Interval::Interval(){ | |
36 init(100,1,44100,""); | |
37 } | |
38 Interval::Interval(int aNumAverages){ | |
39 init(aNumAverages,1,44100,""); | |
40 } | |
41 Interval::Interval(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){ | |
42 init(aNumAverages,aNumFrames,aSamplingRate,aName); | |
43 } | |
44 Interval::~Interval(){ | |
45 free(intervals); | |
46 // free(name); | |
47 } | |
48 void Interval::setNumFrames(int aNumFrames){ | |
49 numFrames=aNumFrames; | |
50 }; | |
51 | |
52 int Interval::start(){ | |
53 // printf("start: intervals: 0x%x, intervalsPointer: %d, numAverages: %d\n", intervals,intervalsPointer,numAverages); | |
54 if(!enabled) | |
55 return 0; | |
56 startTime=rt_timer_read(); | |
57 return 1; | |
58 } | |
59 int Interval::resetMax(){ | |
60 maxTimeus=0; | |
61 return 1; | |
62 } | |
63 int Interval::split(){ //updates | |
64 if(!enabled) | |
65 return 0; | |
66 int currentInterval=rt_timer_read()-startTime; | |
67 RTIME *currentPointer=&(intervals[intervalsPointer]); | |
68 sum-=*currentPointer; //take out the oldest value from the sum | |
69 *currentPointer=currentInterval; | |
70 sum+=*currentPointer; //add the new value to the sum | |
71 timeus=((float)sum)/numAverages/1000.0; | |
72 maxTimeus=timeus>maxTimeus?timeus:maxTimeus; | |
73 intervalsPointer++; | |
74 if(intervalsPointer>=(numAverages-1)){ | |
75 intervalsPointer=0; | |
76 } | |
77 return 1; | |
78 } | |
79 void Interval::setEnabled(bool aActive){ | |
80 enabled=aActive; | |
81 } | |
82 float Interval::getTimeus(){ | |
83 return timeus; | |
84 } | |
85 float Interval::getMaxTimeus(){ | |
86 return maxTimeus; | |
87 } | |
88 void Interval::print(){ | |
89 rt_printf(//"sleepTimer time: 29.484us, (1.30 samples, numFrames: 2, 65.01%%). MaxTime: 30.439us, (1.34 samples, 67.12%%)\n"); | |
90 "%s time: %.3fus, (%.2f samples, numFrames: %d, %.2f%%). MaxTime: %.3fus, (%.2f samples, %.2f%%)\n", | |
91 name, | |
92 timeus, timeus/1000000.0*samplingRate, numFrames, timeus/1000000.0*samplingRate/numFrames * 100, | |
93 maxTimeus, maxTimeus/1000000.0*samplingRate, maxTimeus/1000000.0*samplingRate/numFrames * 100); | |
94 } |