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