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