-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNixieDisplay.h
215 lines (198 loc) · 6.1 KB
/
NixieDisplay.h
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
207
208
209
210
211
212
213
214
215
#pragma once
#include <Arduino.h>
#include <util/atomic.h>
#include "NixieDefines.h"
#include "NixieBoostConverter.h"
#include "utils.h"
namespace Nixie {
template <class Model>
class Effect;
template <class Model>
class Display {
friend class Effect<Model>;
public:
typedef Model MODEL;
Display();
~Display() {} // NOT virtual to save some RAM
void setAnodePin(uint8_t idx, pin_t pin);
void setDotsAnodePin(pin_t pin);
void setDriverPins(pin_t a, pin_t b, pin_t c, pin_t d);
void setBoostPins(pin_t pwm, pin_t fb) { boostcvt.setPins(pwm, fb); }
void setBoostUnregulated() { boostcvt.setUnregulated(); }
void setBoostFeedback(uint16_t fb) { boostcvt.setDesiredFeedback(fb); }
void begin(int boost_required_feedback);
void end();
void setEffect(Effect<Model> *e);
void setProgressSpeed(uint8_t speed) { progress_speed = speed; }
void setDigits(uint_for_tubes<Model::TUBES> number);
uint_for_tubes<Model::TUBES> getDigits() const;
void setDigit(uint8_t idx, uint8_t digit);
uint8_t getDigit(uint8_t idx) const { return digits[reverse_order(idx)]; }
void setDots(bool enabled) { dots = enabled; }
bool getDots() const { return dots; }
void enableDigit(uint8_t idx, bool enable=true) { digit_enabled[reverse_order(idx)] = enable; }
bool isDigitEnabled(uint8_t idx) { return digit_enabled[reverse_order(idx)]; }
void adjustVoltage();
uint8_t getPWM() const { return boostcvt.getPWM(); }
protected:
uint8_t reverse_order(uint8_t idx) const {
return Model::TUBES-idx-1;
}
__attribute__((always_inline)) void setAnode(uint8_t index, bool enabled) {
digitalWrite(anode_pins[index], enabled && digit_enabled[index]);
}
__attribute__((always_inline)) void setDriver(uint8_t v) {
if (v < 10) {
digitalWrite(drv_a_pin, v&0b0001);
digitalWrite(drv_b_pin, v&0b0010);
digitalWrite(drv_c_pin, v&0b0100);
digitalWrite(drv_d_pin, v&0b1000);
}
}
__attribute__((always_inline)) void setAnodeDots(bool enabled) {
digitalWrite(anode_dots_pin, enabled);
}
protected:
pin_t anode_pins[Model::TUBES], anode_dots_pin;
bool digit_enabled[Model::TUBES];
pin_t drv_a_pin, drv_b_pin, drv_c_pin, drv_d_pin;
uint8_t digits[Model::TUBES];
uint8_t prevdigits[Model::TUBES];
uint8_t progress[Model::TUBES+1];
bool dots;
uint8_t tick, tube;
Effect<Model> *effect;
uint8_t progress_speed;
BoostConverter boostcvt;
bool started;
bool dots_present;
private:
static Display<Model> *instance;
void update();
void default_effect(uint8_t tube, uint8_t tick, uint8_t progress);
};
void startTimer1(void (*handler)());
template <class Model>
void Display<Model>::adjustVoltage() {
if (started)
boostcvt.adjustVoltage();
}
template <class Model>
void Display<Model>::setEffect(Effect<Model> *e) {
ATOMIC_BLOCK(ATOMIC_FORCEON) {
effect = e;
e->setDisplay(this);
}
}
template <class Model>
Display<Model>::Display()
: progress_speed(NORMAL_PROGRESS),
effect(nullptr),
dots_present(false),
started(false)
{
Model::initializeDisplay(*this);
}
template <class Model>
void Display<Model>::setDigits(uint_for_tubes<Model::TUBES> number) {
for(uint8_t i=0; i<Model::TUBES; ++i) {
setDigit(i, number%10);
number /= 10;
}
}
template <class Model>
uint_for_tubes<Model::TUBES> Display<Model>::getDigits() const {
uint_for_tubes<Model::TUBES> res = getDigit(0);
uint_for_tubes<Model::TUBES> coeff = 10;
for(uint8_t i=1; i<Model::TUBES; ++i) {
res += getDigit(i)*coeff;
coeff *= 10;
}
return res;
}
template <class Model>
void Display<Model>::setDigit(uint8_t i, uint8_t digit) {
uint8_t idx = reverse_order(i);
if (digit != digits[idx]) {
progress[idx] = 0;
prevdigits[idx] = digits[idx];
digits[idx] = digit;
}
}
template <class Model>
void Display<Model>::setAnodePin(uint8_t anode_idx, pin_t pin) {
anode_pins[anode_idx] = pin;
}
template <class Model>
void Display<Model>::setDotsAnodePin(pin_t pin) {
anode_dots_pin = pin;
dots_present = true;
}
template <class Model>
void Display<Model>::setDriverPins(pin_t drv_a, pin_t drv_b, pin_t drv_c, pin_t drv_d) {
drv_a_pin = drv_a;
drv_b_pin = drv_b;
drv_c_pin = drv_c;
drv_d_pin = drv_d;
}
template <class Model>
void Display<Model>::begin(int boost_required_feedback) {
if (started) return;
tick = 0;
tube = 0;
pinMode(drv_a_pin, OUTPUT);
pinMode(drv_b_pin, OUTPUT);
pinMode(drv_c_pin, OUTPUT);
pinMode(drv_d_pin, OUTPUT);
if (dots_present) pinMode(anode_dots_pin, OUTPUT);
for(int i=0; i<Model::TUBES; ++i)
pinMode(anode_pins[i], OUTPUT);
for(uint8_t i=0; i<Model::TUBES; ++i) {
digits[i] = 0;
prevdigits[i] = 0;
progress[i] = 0;
digit_enabled[i] = true;
}
boostcvt.begin(boost_required_feedback);
instance = this;
startTimer1([]() {
Display<Model>::instance->update();
});
started = true;
}
template <class Model>
void Display<Model>::update() {
++tick;
if (tick >= (
tube == Model::TUBES?
(DOTS_ON_TICKS + DOTS_OFF_TICKS)
: (DIGIT_ON_TICKS + DIGIT_OFF_TICKS)
))
{
tube = (tube + 1) % (Model::TUBES+(dots_present? 1 : 0));
tick = 0;
if (progress[tube] < MAX_PROGRESS)
progress[tube] += progress_speed;
}
if (tube == Model::TUBES && dots_present)
setAnodeDots(dots && tick < DOTS_ON_TICKS);
if (tube != Model::TUBES) {
if (tick < DIGIT_ON_TICKS) {
if (effect)
effect->updateTube(tube, tick, progress[tube]);
else
default_effect(tube, tick, progress[tube]);
}
else {
setAnode(tube, false);
}
}
}
template <class Model>
void Display<Model>::default_effect(uint8_t tube, uint8_t tick, uint8_t progress) {
setDriver(digits[tube]);
setAnode(tube, true);
}
template <class Model>
Display<Model> *Display<Model>::instance = nullptr;
}