FastLED 3.9.15
Loading...
Searching...
No Matches
tile2x2.cpp
Go to the documentation of this file.
1#include "fl/tile2x2.h"
2#include "crgb.h"
3#include "fl/draw_visitor.h"
4#include "fl/math_macros.h"
5#include "fl/raster.h"
6#include "fl/raster_sparse.h"
7#include "fl/unused.h"
8#include "fl/warn.h"
9#include "fl/xymap.h"
10
11namespace fl {
12
13namespace {
14static vec2i16 wrap(const vec2i16 &v, const vec2i16 &size) {
15 // Wrap the vector v around the size
16 return vec2i16(v.x % size.x, v.y % size.y);
17}
18
19static vec2i16 wrap_x(const vec2i16 &v, const uint16_t width) {
20 // Wrap the x component of the vector v around the size
21 return vec2i16(v.x % width, v.y);
22}
23} // namespace
24
26 XYRasterU8Sparse *out_raster) {
27 out_raster->rasterize(tiles);
28}
29
30void Tile2x2_u8::draw(const CRGB &color, const XYMap &xymap, CRGB *out) const {
31 XYDrawComposited visitor(color, xymap, out);
32 draw(xymap, visitor);
33}
34
35void Tile2x2_u8::scale(uint8_t scale) {
36 // scale the tile
37 if (scale == 255) {
38 return;
39 }
40 for (int x = 0; x < 2; ++x) {
41 for (int y = 0; y < 2; ++y) {
42 uint16_t value = at(x, y);
43 at(x, y) = (value * scale) >> 8;
44 }
45 }
46}
47
48Tile2x2_u8_wrap::Tile2x2_u8_wrap(const Tile2x2_u8 &from, uint16_t width) {
49 const vec2i16 origin = from.origin();
50 at(0, 0) = {wrap_x(vec2i16(origin.x, origin.y), width), from.at(0, 0)};
51 at(0, 1) = {wrap_x(vec2i16(origin.x, origin.y + 1), width), from.at(0, 1)};
52 at(1, 0) = {wrap_x(vec2i16(origin.x + 1, origin.y), width), from.at(1, 0)};
53 at(1, 1) = {wrap_x(vec2i16(origin.x + 1, origin.y + 1), width),
54 from.at(1, 1)};
55}
56
58 // Wrap around the edges
59 x = (x + 2) % 2;
60 y = (y + 2) % 2;
61 return tile[y][x];
62}
63
64const Tile2x2_u8_wrap::Data &Tile2x2_u8_wrap::at(uint16_t x, uint16_t y) const {
65 // Wrap around the edges
66 x = (x + 2) % 2;
67 y = (y + 2) % 2;
68 return tile[y][x];
69}
70
71Tile2x2_u8_wrap::Tile2x2_u8_wrap(const Tile2x2_u8 &from, uint16_t width,
72 uint16_t height) {
73 const vec2i16 origin = from.origin();
74 at(0, 0) = {wrap(vec2i16(origin.x, origin.y), vec2i16(width, height)),
75 from.at(0, 0)};
76 at(0, 1) = {wrap(vec2i16(origin.x, origin.y + 1), vec2i16(width, height)),
77 from.at(0, 1)};
78 at(1, 0) = {wrap(vec2i16(origin.x + 1, origin.y), vec2i16(width, height)),
79 from.at(1, 0)};
80 at(1,
81 1) = {wrap(vec2i16(origin.x + 1, origin.y + 1), vec2i16(width, height)),
82 from.at(1, 1)};
83}
84
85uint8_t Tile2x2_u8::maxValue() const {
86 uint8_t max = 0;
87 max = MAX(max, at(0, 0));
88 max = MAX(max, at(0, 1));
89 max = MAX(max, at(1, 0));
90 max = MAX(max, at(1, 1));
91 return max;
92}
93
95 Tile2x2_u8 result;
96 for (int x = 0; x < 2; ++x) {
97 for (int y = 0; y < 2; ++y) {
98 result.at(x, y) = MAX(a.at(x, y), b.at(x, y));
99 }
100 }
101 return result;
102}
103
106 vec2<int16_t> max = mOrigin + vec2<int16_t>(2, 2);
107 return rect<int16_t>(min, max);
108}
109
110} // namespace fl
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
Data & at(uint16_t x, uint16_t y)
Definition tile2x2.cpp:57
fl::pair< vec2i16, uint8_t > Data
Definition tile2x2.h:94
Tile2x2_u8_wrap()=default
Data tile[2][2]
Definition tile2x2.h:105
void scale(uint8_t scale)
Definition tile2x2.cpp:35
static Tile2x2_u8 MaxTile(const Tile2x2_u8 &a, const Tile2x2_u8 &b)
Definition tile2x2.cpp:94
static void Rasterize(const Slice< const Tile2x2_u8 > &tiles, XYRasterU8Sparse *output)
Definition tile2x2.cpp:25
vec2< int16_t > mOrigin
Definition tile2x2.h:85
rect< int16_t > bounds() const
bounds => [begin_x, end_x) (where end_x is exclusive)
Definition tile2x2.cpp:104
uint8_t maxValue() const
Definition tile2x2.cpp:85
void draw(const CRGB &color, const XYMap &xymap, CRGB *out) const
Definition tile2x2.cpp:30
vec2< int16_t > origin() const
Definition tile2x2.h:55
uint8_t & at(int x, int y)
Definition tile2x2.h:38
Tile2x2_u8()=default
void rasterize(const vec2< int16_t > &pt, uint8_t value)
Defines the red, green, and blue (RGB) pixel struct.
#define MAX(a, b)
Definition math_macros.h:11
static vec2i16 wrap_x(const vec2i16 &v, const uint16_t width)
Definition tile2x2.cpp:19
static vec2i16 wrap(const vec2i16 &v, const vec2i16 &size)
Definition tile2x2.cpp:14
vec2< int16_t > vec2i16
Definition geometry.h:320
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55