This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigitizer.cpp
75 lines (61 loc) · 1.76 KB
/
digitizer.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
#include "logging.h"
#include "digitizer.h"
namespace fskube {
LOG_HANDLE("digitizer")
void DigitizerBase::setSamplesPerSecond(unsigned int samplesPerSecond) {
this->samplesPerSecond = samplesPerSecond;
}
void DigitizerBase::setBitsPerSecond(unsigned int bitsPerSecond) {
this->bitsPerSecond = bitsPerSecond;
}
Digitizer::Digitizer() {
}
Digitizer::Digitizer(unsigned int samplesPerSecond, unsigned int bitsPerSecond) {
setSamplesPerSecond(samplesPerSecond);
setBitsPerSecond(bitsPerSecond);
reset();
}
void Digitizer::maybeSendCurrentBit() {
// Only fire a bit if we've seen a significant (ie: >= 1/2) of a bit.
if(samplesSeen >= samplesPerBit() / 2) {
LOG2("after %d samples, sending %d", samplesSeen, currentBit);
send(currentBit);
}
samplesSeen = 0;
}
void Digitizer::receive(double sample) {
LOG4("sample: %f currentBit: %d", sample, currentBit);
if(currentBit == 0 && sample >= DIGITIZER_HIGH_WATER_MARK) {
maybeSendCurrentBit();
currentBit = 1;
} else if(currentBit == 1 && sample <= DIGITIZER_LOW_WATER_MARK) {
maybeSendCurrentBit();
maybeSendCurrentBit();
currentBit = 0;
} else {
samplesSeen++;
if(samplesSeen >= samplesPerBit()) {
maybeSendCurrentBit();
}
}
}
void Digitizer::reset() {
samplesSeen = 0;
currentBit = 0;
}
Analogizer::Analogizer() {
}
Analogizer::Analogizer(unsigned int samplesPerSecond, unsigned int bitsPerSecond) {
setSamplesPerSecond(samplesPerSecond);
setBitsPerSecond(bitsPerSecond);
reset();
}
void Analogizer::receive(bool bit) {
LOG4("bit: %d", bit);
for(int i = 0; i < samplesPerBit(); ++i) {
send(bit ? 1 : -1);
}
}
void Analogizer::reset() {
}
} // namespace fskube