-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigitalControl.c
84 lines (72 loc) · 1.86 KB
/
digitalControl.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
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
#include "digitalControl.h"
#include <Arduino.h>
#include <stdint.h>
#include <stdbool.h>
#define CONTROL_VALUE_MAX 4
struct DigitalControl createDigitalControl(
uint8_t pin,
uint8_t midiChannel,
uint8_t midiControlNumber
) {
pinMode(pin, INPUT_PULLUP);
bool on = digitalRead(pin);
return (struct DigitalControl){
.pin=pin,
.midiChannel=midiChannel,
.midiControlNumber=midiControlNumber,
.value=on,
.on=on
};
}
void createConsecutiveDigitalControlsInArray(
struct DigitalControl * const controls,
uint8_t numControls,
uint8_t midiChannel,
uint8_t firstMidiControlNumber,
uint8_t firstPin
) {
uint8_t i;
for (i=0; i<numControls; i++) {
controls[i] = createDigitalControl(
firstPin + i,
midiChannel,
firstMidiControlNumber + i
);
}
}
void digitalControl_map(
struct DigitalControl * self,
void (*controlChange)(const struct DigitalControl * const control)
) {
// TODO: This code is VERY similar to what we have in `mapNote`. Extract it to a common function?
// Simulate a capacitor to debounce the pin.
// If the pin is low, the switch is connected, and we increase the value.
// If the pin is high, the switch is open, and we decrease the value.
// We cap the value between 0 and CONTROL_VALUE_MAX.
// When one of the limits are reached, we trigger the control on or off.
bool pin = digitalRead(self->pin);
if (pin) {
// Switch is opening.
if (self->value > 0) {
self->value--;
}
} else {
// Switch is closing.
if (self->value < CONTROL_VALUE_MAX) {
self->value++;
}
}
if (self->on) {
if (self->value == 0) {
// Control should turn off.
self->on = false;
controlChange(self);
}
} else {
if (self->value == CONTROL_VALUE_MAX) {
// Control should turn on.
self->on = true;
controlChange(self);
}
}
}