-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol_input.cpp
More file actions
79 lines (63 loc) · 2.07 KB
/
Copy pathcontrol_input.cpp
File metadata and controls
79 lines (63 loc) · 2.07 KB
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
#include "control_input.h"
#include "knob_pins.h"
namespace {
constexpr uint16_t kAnalogMax = 4095;
constexpr float kSmoothing = 0.20f;
constexpr float kPotThreshold = 0.015f;
}
void ControlInput::begin() {
for (const uint8_t pin : KnobPins::kKnobPins) {
pinMode(pin, INPUT_DISABLE); // pure analog input, no digital keeper
}
analogReadResolution(12);
analogReadAveraging(8);
for (uint8_t index = 0; index < kPotCount; ++index) {
const uint16_t raw = readKnob(KnobPins::kKnobPins[index]);
raw_values_[index] = raw;
const float normalized = static_cast<float>(raw) / static_cast<float>(kAnalogMax);
filtered_values_[index] = normalized;
pending_values_[index] = normalized;
}
}
void ControlInput::update() {
for (uint8_t index = 0; index < kPotCount; ++index) {
const uint16_t raw = readKnob(KnobPins::kKnobPins[index]);
raw_values_[index] = raw;
const float normalized = static_cast<float>(raw) / static_cast<float>(kAnalogMax);
const float filtered = filtered_values_[index] + ((normalized - filtered_values_[index]) * kSmoothing);
filtered_values_[index] = filtered;
if (fabsf(filtered - pending_values_[index]) >= kPotThreshold) {
pending_values_[index] = filtered;
pending_changes_[index] = true;
}
}
}
bool ControlInput::consumePotChange(uint8_t index, float &value) {
if (index >= kPotCount || !pending_changes_[index]) {
return false;
}
pending_changes_[index] = false;
value = pending_values_[index];
return true;
}
float ControlInput::potValue(uint8_t index) const {
if (index >= kPotCount) {
return 0.0f;
}
return filtered_values_[index];
}
uint16_t ControlInput::rawChannelValue(uint8_t index) const {
if (index >= kPotCount) {
return 0;
}
return raw_values_[index];
}
uint16_t ControlInput::readKnob(uint8_t pin) const {
analogRead(pin); // discard first sample after the ADC switches channel
uint32_t total = 0;
for (uint8_t sample = 0; sample < 4; ++sample) {
total += analogRead(pin);
}
return static_cast<uint16_t>(total / 4U);
}
ControlInput control_input;