-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalogControl.c
58 lines (47 loc) · 1.35 KB
/
analogControl.c
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
#include "analogControl.h"
#include "analogMux.h"
#include <stdint.h>
#define ANALOG_HYSTERESIS 32
void createConsecutiveAnalogControlsInArray(
const struct AnalogMux8 * const muxes,
uint8_t numMuxes,
struct AnalogControl * const controls,
uint8_t numControls,
uint8_t midiChannel,
uint8_t firstMidiControlNumber
) {
uint8_t i;
for (i=0; i<numControls; i++) {
uint8_t muxIndex = i/8;
uint8_t pinIndex = i%8;
if (muxIndex >= numMuxes) {
// Not enough muxes for the number of analog inputs!
return;
}
controls[i] = (struct AnalogControl){
.mux=&muxes[muxIndex],
.selector=pinIndex,
.channel=midiChannel,
.number=firstMidiControlNumber + i,
.value=0,
.previousValue=0
};
}
}
void analogControl_init(
struct AnalogControl * self
) {
self->value = analogMux8_selectAndRead(self->mux, self->selector);
self->previousValue = self->value;
}
void analogControl_map(
struct AnalogControl * self,
void (*controlChange)(const struct AnalogControl * const control)
) {
self->value = analogMux8_selectAndRead(self->mux, self->selector);
// Use hysteresis to avoid unnecessary updates, i.e., only emit a change if the value has changed enough.
if (abs(self->value - self->previousValue) >= ANALOG_HYSTERESIS) {
controlChange(self);
self->previousValue = self->value;
}
}