-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampler.cpp
98 lines (85 loc) · 1.42 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "sampler.h"
#include <Arduino.h>
/**
Create a sampler with the specified delay between samples
*/
Sampler::Sampler(unsigned long delay_ms) : delay_us(delay_ms * 1000)
{
}
/**
Initialize an unconfigured sampler
*/
Sampler::Sampler()
{
delay_us = 0;
}
void Sampler::setDelayMs(unsigned long delay)
{
// Set the delay
delay_us = delay * 1000;
}
void Sampler::setDelayUs(unsigned long delay)
{
// Set the delay
delay_us = delay;
}
unsigned int Sampler::getDelayMs()
{
return delay_us / 1000;
}
unsigned long Sampler::getDelayUs()
{
return delay_us;
}
unsigned int Sampler::getFrequency()
{
return 1000 / (delay_us / 1000);
}
/**
Set the frequency that this sampler should sample at
*/
void Sampler::setFrequency(int frequency)
{
// Set the delay by using the frequency
delay_us = (int)1000.0 / (float)frequency * 1000.0;
}
/**
Enable the sampler
*/
void Sampler::enable()
{
enabled = true;
last_trigger_us = micros();
}
/**
Enable the sampler, which will cause it to trigger instantly
*/
void Sampler::enable_and_trigger()
{
enabled = true;
last_trigger_us = micros() - delay_us;
}
/**
Disable the sampler
*/
void Sampler::disable()
{
enabled = false;
}
/**
Returns true if this sampler is ready to take another sample
*/
bool Sampler::shouldSample()
{
if (!enabled)
return false;
if (micros() - last_trigger_us >= delay_us)
{
last_trigger_us = micros();
return true;
}
else
{
return false;
}
}