-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheffect.h
More file actions
102 lines (75 loc) · 2.07 KB
/
effect.h
File metadata and controls
102 lines (75 loc) · 2.07 KB
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
#ifndef Effect_h
#define Effect_h
#include "FastLED.h"
#include "grids.h"
#include "palettes.h"
class Animation {
protected:
virtual void tick(unsigned long t) { }
unsigned long ticks;
CRGBPalette16 palette;
virtual CRGB atPixel(int x, int y) { }
public:
void clear();
int width, height;
Animation(int width, int height) : width(width), height(height) {
}
virtual void start() { };
virtual void loop(unsigned long t) {
tick(ticks++);
};
CRGB at(int x, int y) {
// Use an X position of -99 to turn on LEDs (makes it easy to identify single pixels)
// Other out-of-bounds values will return black
if (x == -99)
return CRGB::White;
else if (x < 0 || x >= 255)
return CRGB::Black;
else if (y < 0 || y > 255)
return CRGB::Black;
// OK, delegate to
return atPixel(x,y);
};
void setPalette(CRGBPalette16 pal) {
this->palette = pal;
}
};
// Effect that directly drives a strand of LEDs
class LEDEffect : public Animation {
protected:
void clear();
CRGB *leds;
CRGB atPixel(int x, int y) {
if (x < 0 || x >= width)
return CRGB::Black;
if (y < 0 || y >= width)
return CRGB::Black;
return leds[y * width + x];
}
public:
LEDEffect(CRGB *leds, int width, int height = 1) : leds(leds), Animation(width, height) {
}
};
#define CANVAS_WIDTH 25
#define CANVAS_HEIGHT 25
// Effect that drives a virtual matrix of palette values
class PaletteEffect : public Animation {
protected:
PaletteGridMap<CANVAS_WIDTH, CANVAS_HEIGHT, 255, 255> canvas;
uint8_t rotation = 0;
// Set a pixel
uint8_t& pixel(int x, int y) {
return canvas.pixel(x,y);
}
void rotate(uint8_t rot) {
rotation = rot;
canvas.rotate(rotation);
}
CRGB atPixel(int x, int y) {
uint8_t color = canvas( x , y );
return ColorFromPalette( this->palette, color);
}
public:
PaletteEffect() : Animation(CANVAS_WIDTH, CANVAS_HEIGHT) {}
};
#endif