-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmx.hh
207 lines (167 loc) · 6.34 KB
/
dmx.hh
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
#include <vector>
class ChannelEffect {
public:
virtual ~ChannelEffect() = default;
uint8_t process(uint8_t value, uint32_t now_ms) {
if (pass_) { return value; }
if (multiply_) {
float min_level = min_ / 255.0;
float max_level = max_ / 255.0;
float l = level(now_ms);
uint8_t result = std::clamp(static_cast<float>(value) * (l * (max_level - min_level) + min_level), 0.0f, 255.0f);
return result;
}
return clip((max_ - min_) * level(now_ms) + min_ + input_gain_ * value);
}
void set_values(uint8_t min, uint8_t max) { set_min(min); set_max(max); }
void set_min(uint8_t min) { min_ = min; }
void set_max(uint8_t max) { max_ = max; }
void set_input_gain(float gain) { input_gain_ = gain; }
void set_multiply(bool multiply) { multiply_ = multiply; }
void set_passthrough(bool pass) { pass_ = pass; }
uint8_t min_value() const { return min_; }
uint8_t max_value() const { return max_; }
float input_gain() const { return input_gain_; }
bool multiply() const { return multiply_; }
bool passthrough() const { return pass_; }
protected:
uint8_t clip(float in) const { return in < 0 ? 0 : in > 255 ? 255 : static_cast<uint8_t>(in); }
// Return the current level, between 0.0 and 1.0
virtual float level(uint32_t now_ms) { return 0.0; }
private:
float input_gain_ = 1.0;
uint8_t min_ = 0;
uint8_t max_ = 255;
bool multiply_ = false;
bool pass_ = false;
};
///
/// @brief Each Channel is composed of a stack of ChannelEffects which are processed serially to
/// generate the value at each timestamp. The channel does NOT own its effects.
///
class Channel {
public:
uint8_t get_value(uint32_t now_ms) {
uint8_t value = value_;
for (auto* effect : effects_) {
value = effect->process(value, now_ms);
}
return value;
}
void set_value(uint8_t value) { value_ = value; }
void add_effect(ChannelEffect* effect) {
effects_.push_back(effect);
}
template <typename Effect=ChannelEffect>
Effect* effect(uint8_t layer) const {
return layer < effects_.size() ? dynamic_cast<ChannelEffect*>(effects_[layer]) : nullptr;
}
private:
uint8_t value_ = 0;
std::vector<ChannelEffect*> effects_;
};
///
/// @brief Main controller class, call write_frame periodically to query the latest state and write it.
/// Computing channel values is done between writing slots, since the spec allows for flexible gaps.
///
class DMXController {
public:
static constexpr uint16_t MAX_CHANNELS = 512;
DMXController(uint8_t pos_pin, uint8_t neg_pin) : pos_pin_(pos_pin), neg_pin_(neg_pin), time_us_(micros()) {
pinMode(pos_pin, OUTPUT);
pinMode(neg_pin, OUTPUT);
}
uint16_t max_channel() const { return channel_count_; }
void set_max_channel(uint16_t index) {
if (index <= MAX_CHANNELS) {
channel_count_ = index > channel_count_ ? index : channel_count_;
}
}
Channel& channel(uint16_t index) {
set_max_channel(index);
return channels_[index];
}
void write_frame()
{
uint32_t dt_us = dt();
// If we're exceeding the BREAK to BREAK time, don't send an update.
if (dt_us < BREAK_TO_BREAK_US) {
return;
}
time_us_ = micros();
uint32_t now_ms = time_us_ / 1000;
// [1]: https://tsp.esta.org/tsp/documents/docs/ANSI-ESTA_E1-11_2008R2018.pdf
write_bit(0, SPACE_FOR_BREAK_US);
// Mark After Break
write_bit(1, MARK_AFTER_BREAK_US);
// Start Code
write_byte(0x00);
// Now the rest of the channels
for (uint16_t i = 1; i <= channel_count_; ++i) {
// Compute the value of this channel, the time between slots is arbitrary so do the processing here.
uint8_t value = channels_[i].get_value(now_ms);
write_byte(value);
}
// Padding, it seems some lights don't like being the last channel.
for (int16_t i = channel_count_; i < 10; ++i) {
if (i > 512) break;
write_byte(0);
}
write_bit(1, MARK_BEFORE_BREAK_US);
}
private:
// Delays defined in microseconds
// [1]: https://tsp.esta.org/tsp/documents/docs/ANSI-ESTA_E1-11_2008R2018.pdf
static constexpr uint32_t BIT_US = 4;
static constexpr uint32_t SPACE_FOR_BREAK_US = 92;
static constexpr uint32_t MARK_AFTER_BREAK_US = 12;
static constexpr uint32_t MARK_BEFORE_BREAK_US = 100; // arbitrary
static constexpr uint32_t BREAK_TO_BREAK_US = 1204;
// This method copies the delayMicroseconds method, but includes the digitalWriteFast calls.
// With an oscope, this shows about 30ns of error (or 12 instructions) which I've adjusted for here.
inline void write_bit(bool value, uint32_t delay_us) const {
uint32_t begin = ARM_DWT_CYCCNT;
digitalWriteFast(pos_pin_, value);
digitalWriteFast(neg_pin_, !value);
uint32_t cycles = F_CPU_ACTUAL / 1000000 * delay_us;
while (ARM_DWT_CYCCNT - begin < cycles - 12); // wait
}
void write_byte(uint8_t b) const
{
// The mark time between slots is arbitrary, so use it to pre-process.
bool bits[8];
for (uint8_t i = 0; i < 8; ++i) {
bits[i] = (b & (1u << i)) != 0;
}
noInterrupts();
write_bit(0, BIT_US);
// Unrolling the loop so the timing is a bit more straightforward.
write_bit(bits[0], BIT_US);
write_bit(bits[1], BIT_US);
write_bit(bits[2], BIT_US);
write_bit(bits[3], BIT_US);
write_bit(bits[4], BIT_US);
write_bit(bits[5], BIT_US);
write_bit(bits[6], BIT_US);
write_bit(bits[7], BIT_US);
write_bit(1, 2 * BIT_US);
interrupts();
}
uint32_t dt() const {
const uint32_t now_us = micros();
uint32_t last_us = time_us_;
if (now_us > last_us) {
return now_us - last_us;
} else {
// Time wrapped! Assume dt is small and just calculate how much it wrapped
return (static_cast<uint32_t>(-1) - last_us) + now_us;
}
}
private:
uint8_t pos_pin_;
uint8_t neg_pin_;
uint32_t time_us_;
uint16_t channel_count_ = 0;
Channel channels_[MAX_CHANNELS + 1];
};