This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSampler.cpp
67 lines (50 loc) · 1.5 KB
/
Sampler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include "Config.h"
#include "Sampler.h"
Sampler::Sampler() : totalSamplesInBuffer(0) {
}
void Sampler::prepareForUpdate(void) {
for (unsigned int index = SAMPLE_STEP; index < totalSamplesInBuffer; index++) {
buffer[index - SAMPLE_STEP].x = buffer[index].x;
buffer[index - SAMPLE_STEP].y = buffer[index].y;
buffer[index - SAMPLE_STEP].z = buffer[index].z;
}
totalSamplesInBuffer -= SAMPLE_STEP;
}
void Sampler::addAccelerationData(AccelerationData& accelerationData) {
if (isFull()) {
return;
}
buffer[totalSamplesInBuffer++] = accelerationData;
}
bool Sampler::isReady(void) {
return totalSamplesInBuffer % SAMPLE_WINDOW == 0;
}
bool Sampler::isFull(void) {
return totalSamplesInBuffer == SAMPLE_BUFFER_SIZE;
}
bool Sampler::isEmpty(void) {
return totalSamplesInBuffer == 0;
}
bool Sampler::hasRemainingData(void) {
return !isReady() && !isEmpty();
}
AccelerationData& Sampler::getSample(void) {
long int totalX, totalY, totalZ;
totalX = 0;
totalY = 0;
totalZ = 0;
unsigned int window = SAMPLE_WINDOW;
if (window > totalSamplesInBuffer) {
window = totalSamplesInBuffer;
}
for(unsigned int index = 0; index < SAMPLE_WINDOW; index++) {
totalX += buffer[index].x;
totalY += buffer[index].y;
totalZ += buffer[index].z;
}
sample.x = totalX * 1.0 / SAMPLE_WINDOW;
sample.y = totalY * 1.0 / SAMPLE_WINDOW;
sample.z = totalZ * 1.0 / SAMPLE_WINDOW;
return sample;
}