Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Display
MCU
VUDisplay.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <AH/STL/utility>
// std::forward
4
#include <
Display/DisplayElement.hpp
>
5
#include <
MIDI_Inputs/InterfaceMIDIInputElements.hpp
>
6
#include <
Settings/SettingsWrapper.hpp
>
7
8
BEGIN_CS_NAMESPACE
9
10
namespace
MCU
{
15
template
<
class
VU_t = Interfaces::MCU::IVU &>
16
class
VUDisplay
:
public
DisplayElement
{
17
public
:
18
VUDisplay
(
DisplayInterface
&
display
, VU_t &&
vu
,
PixelLocation
loc,
19
uint16_t
width
, uint8_t
blockheight
, uint8_t
spacing
,
20
uint16_t
color
)
21
:
DisplayElement
(
display
),
vu
(std::forward<VU_t>(
vu
)),
x
(loc.
x
),
22
y
(loc.
y
-
blockheight
+ 1),
width
(
width
),
blockheight
(
blockheight
),
23
spacing
(
spacing
),
color
(
color
),
24
decayTime
(
VU_PEAK_SMOOTH_DECAY
25
?
VU_PEAK_DECAY_TIME
/ (
blockheight
+
spacing
)
26
:
VU_PEAK_DECAY_TIME
) {}
27
28
void
draw
()
override
{
29
uint8_t value =
vu
.getValue();
30
updatePeak
(value);
31
if
(
peak
> 0) {
32
drawPeak
(
peak
);
33
drawBlocks
(value);
34
}
35
vu
.clearDirty();
36
}
37
38
bool
getDirty
()
const override
{
39
return
vu
.getDirty() ||
shouldStartDecaying
() ||
shouldUpdateDecay
();
40
}
41
42
protected
:
43
virtual
void
drawPeak
(uint8_t
peak
) {
44
display
.drawFastHLine(
x
,
//
45
y
-
spacing
+
blockheight
-
peak
,
//
46
width
,
//
47
color
);
48
}
49
50
virtual
void
drawBlocks
(uint8_t value) {
51
for
(uint8_t i = 0; i < value; i++)
52
display
.fillRect(
x
,
//
53
y
- i * (
blockheight
+
spacing
),
//
54
width
,
//
55
blockheight
,
//
56
color
);
57
}
58
59
private
:
60
void
updatePeak
(uint8_t value) {
61
int16_t newPeak = (int16_t)value * (
blockheight
+
spacing
);
62
if
(newPeak >=
peak
) {
63
peak
= newPeak;
64
previousDecay
= millis();
65
decaying
=
false
;
66
}
else
if
(
shouldStartDecaying
()) {
67
decaying
=
true
;
68
previousDecay
+=
VU_PEAK_HOLD_TIME
-
decayTime
;
69
}
else
if
(
shouldUpdateDecay
()) {
70
peak
-=
VU_PEAK_SMOOTH_DECAY
? 1 : (
blockheight
+
spacing
);
71
previousDecay
+=
decayTime
;
72
}
73
}
74
75
bool
shouldStartDecaying
()
const
{
76
return
!
decaying
&&
peak
> 0 &&
77
(millis() -
previousDecay
>
VU_PEAK_HOLD_TIME
);
78
}
79
80
bool
shouldUpdateDecay
()
const
{
81
return
decaying
&&
peak
> 0 &&
//
82
(millis() -
previousDecay
>
decayTime
);
83
}
84
85
VU_t
vu
;
86
87
int16_t
x
;
88
int16_t
y
;
89
uint16_t
width
;
90
uint8_t
blockheight
;
91
uint8_t
spacing
;
92
uint16_t
color
;
93
94
int16_t
peak
= 0;
95
unsigned
long
previousDecay
= 0;
96
bool
decaying
=
false
;
97
98
unsigned
long
decayTime
;
99
};
100
101
}
// namespace MCU
102
103
END_CS_NAMESPACE
104
105
#include <
Display/Helpers/Bresenham.hpp
>
106
107
BEGIN_CS_NAMESPACE
108
109
namespace
MCU
{
110
111
template
<
class
VU_t = Interfaces::MCU::IVU>
112
class
AnalogVUDisplay
:
public
DisplayElement
{
113
public
:
114
AnalogVUDisplay
(
DisplayInterface
&
display
, VU_t &
vu
,
PixelLocation
loc,
115
uint16_t radius,
float
theta_min
,
float
theta_diff
,
116
uint16_t
color
)
117
:
DisplayElement
(
display
),
vu
(
vu
),
x
(loc.
x
),
y
(loc.
y
),
118
r_sq
(radius * radius),
theta_min
(
theta_min
),
theta_diff
(
theta_diff
),
119
color
(
color
) {}
120
121
void
draw
()
override
{
122
float
value =
vu
.getFloatValue();
123
drawNeedle
(
theta_min
+ value *
theta_diff
);
124
vu
.clearDirty();
125
}
126
127
void
drawNeedle
(
float
angle) {
128
BresenhamLine
line = {{
x
,
y
}, angle};
129
BresenhamLine::Pixel
p = line.
next
();
130
while
(p.
distanceSquared
({x, y}) <=
r_sq
) {
131
display
.drawPixel(p.
x
, p.
y
,
color
);
132
p = line.
next
();
133
}
134
}
135
136
bool
getDirty
()
const override
{
return
vu
.getDirty(); }
137
138
private
:
139
VU_t &
vu
;
140
141
int16_t
x
;
142
int16_t
y
;
143
uint16_t
r_sq
;
144
float
theta_min
;
145
float
theta_diff
;
146
uint16_t
color
;
147
};
148
149
}
// namespace MCU
150
151
END_CS_NAMESPACE
Bresenham.hpp
DisplayElement.hpp
InterfaceMIDIInputElements.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
SettingsWrapper.hpp
VU_PEAK_SMOOTH_DECAY
constexpr bool VU_PEAK_SMOOTH_DECAY
If set to true, the VU meter display peak bars will decay smoothly (i.e.
Definition
Settings/Settings.hpp:36
VU_PEAK_HOLD_TIME
constexpr unsigned long VU_PEAK_HOLD_TIME
The time in milliseconds that a VU meter display peak bar stays at the peak before decaying.
Definition
Settings/Settings.hpp:32
VU_PEAK_DECAY_TIME
constexpr unsigned long VU_PEAK_DECAY_TIME
The time in milliseconds it takes for the VU meter display peak bar to drop one unit (i....
Definition
Settings/Settings.hpp:28
BresenhamLine
Line rasterization algorithm for drawing lines to the display.
Definition
Bresenham.hpp:18
BresenhamLine::next
Pixel next()
Definition
Bresenham.hpp:56
DisplayElement::DisplayElement
DisplayElement(DisplayInterface &display)
Create a new DisplayElement.
Definition
DisplayElement.hpp:18
DisplayElement::display
DisplayInterface & display
Definition
DisplayElement.hpp:103
DisplayInterface
An interface for displays.
Definition
DisplayInterface.hpp:16
MCU::AnalogVUDisplay::AnalogVUDisplay
AnalogVUDisplay(DisplayInterface &display, VU_t &vu, PixelLocation loc, uint16_t radius, float theta_min, float theta_diff, uint16_t color)
Definition
VUDisplay.hpp:114
MCU::AnalogVUDisplay::getDirty
bool getDirty() const override
Check if this DisplayElement has to be re-drawn.
Definition
VUDisplay.hpp:136
MCU::AnalogVUDisplay::color
uint16_t color
Definition
VUDisplay.hpp:146
MCU::AnalogVUDisplay::r_sq
uint16_t r_sq
Definition
VUDisplay.hpp:143
MCU::AnalogVUDisplay::theta_diff
float theta_diff
Definition
VUDisplay.hpp:145
MCU::AnalogVUDisplay::vu
VU_t & vu
Definition
VUDisplay.hpp:139
MCU::AnalogVUDisplay::drawNeedle
void drawNeedle(float angle)
Definition
VUDisplay.hpp:127
MCU::AnalogVUDisplay::theta_min
float theta_min
Definition
VUDisplay.hpp:144
MCU::AnalogVUDisplay::x
int16_t x
Definition
VUDisplay.hpp:141
MCU::AnalogVUDisplay::y
int16_t y
Definition
VUDisplay.hpp:142
MCU::AnalogVUDisplay::draw
void draw() override
Draw this DisplayElement to the display buffer.
Definition
VUDisplay.hpp:121
MCU::VUDisplay::getDirty
bool getDirty() const override
Check if this DisplayElement has to be re-drawn.
Definition
VUDisplay.hpp:38
MCU::VUDisplay::spacing
uint8_t spacing
Definition
VUDisplay.hpp:91
MCU::VUDisplay::color
uint16_t color
Definition
VUDisplay.hpp:92
MCU::VUDisplay::decaying
bool decaying
Definition
VUDisplay.hpp:96
MCU::VUDisplay::decayTime
unsigned long decayTime
Definition
VUDisplay.hpp:98
MCU::VUDisplay::shouldUpdateDecay
bool shouldUpdateDecay() const
Definition
VUDisplay.hpp:80
MCU::VUDisplay::vu
VU_t vu
Definition
VUDisplay.hpp:85
MCU::VUDisplay::previousDecay
unsigned long previousDecay
Definition
VUDisplay.hpp:95
MCU::VUDisplay::peak
int16_t peak
Definition
VUDisplay.hpp:94
MCU::VUDisplay::blockheight
uint8_t blockheight
Definition
VUDisplay.hpp:90
MCU::VUDisplay::VUDisplay
VUDisplay(DisplayInterface &display, VU_t &&vu, PixelLocation loc, uint16_t width, uint8_t blockheight, uint8_t spacing, uint16_t color)
Definition
VUDisplay.hpp:18
MCU::VUDisplay::drawPeak
virtual void drawPeak(uint8_t peak)
Definition
VUDisplay.hpp:43
MCU::VUDisplay::updatePeak
void updatePeak(uint8_t value)
Definition
VUDisplay.hpp:60
MCU::VUDisplay::shouldStartDecaying
bool shouldStartDecaying() const
Definition
VUDisplay.hpp:75
MCU::VUDisplay::drawBlocks
virtual void drawBlocks(uint8_t value)
Definition
VUDisplay.hpp:50
MCU::VUDisplay::width
uint16_t width
Definition
VUDisplay.hpp:89
MCU::VUDisplay::x
int16_t x
Definition
VUDisplay.hpp:87
MCU::VUDisplay::y
int16_t y
Definition
VUDisplay.hpp:88
MCU::VUDisplay::draw
void draw() override
Draw this DisplayElement to the display buffer.
Definition
VUDisplay.hpp:28
MCU
Definition
LCDDisplay.hpp:10
BresenhamLine::Pixel
Definition
Bresenham.hpp:21
BresenhamLine::Pixel::y
pint y
Definition
Bresenham.hpp:22
BresenhamLine::Pixel::distanceSquared
unsigned int distanceSquared(Pixel other) const
Definition
Bresenham.hpp:26
BresenhamLine::Pixel::x
pint x
Definition
Bresenham.hpp:22
PixelLocation
A simple struct representing a pixel with integer coordinates.
Definition
Def.hpp:64
Generated by
1.17.0