FastLED 3.9.15
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
leds.h
Go to the documentation of this file.
1#pragma once
2
3#include "crgb.h"
4#include "fl/xymap.h"
5
6namespace fl {
7
8// Leds definition.
9// Drawing operations on a block of leds requires information about the layout
10// of the leds. Hence this class.
11class Leds {
12 public:
13 Leds(CRGB *leds, uint16_t width, uint16_t height);
14 Leds(CRGB *leds, const XYMap &xymap);
15
16 // Copy constructor and assignment operator.
17 Leds(const Leds &) = default;
18 Leds &operator=(const Leds &) = default;
19 Leds(Leds &&) = default;
20
21 // out of bounds access returns empty() led and is safe to read/write.
22 CRGB &operator()(int x, int y);
23 const CRGB &operator()(int x, int y) const;
24
25 CRGB &at(int x, int y) { return (*this)(x, y); }
26 const CRGB &at(int x, int y) const { return (*this)(x, y); }
27
28 size_t width() const { return mXyMap.getHeight(); }
29 size_t height() const { return mXyMap.getWidth(); }
30
31 // Allows normal matrix array (row major) access, bypassing the XYMap.
32 // Will assert if XYMap is not serpentine or line by line.
33 CRGB *operator[](int x);
34 const CRGB *operator[](int x) const;
35 // Raw data access.
36 CRGB *rgb() { return mLeds; }
37 const CRGB *rgb() const { return mLeds; }
38
39 const XYMap &xymap() const { return mXyMap; }
40
41 operator CRGB *() { return mLeds; }
42 operator const CRGB *() const { return mLeds; }
43
44 void fill(const CRGB &color) {
45 for (size_t i = 0; i < mXyMap.getTotal(); ++i) {
46 mLeds[i] = color;
47 }
48 }
49
50 protected:
51 static CRGB &empty(); // Allows safe out of bounds access.
54};
55
56template <size_t W, size_t H> class LedsXY : public Leds {
57 public:
58 LedsXY() : Leds(mLeds, XYMap::constructSerpentine(W, H)) {}
59 explicit LedsXY(bool is_serpentine)
60 : Leds(mLeds, is_serpentine ? XYMap::constructSerpentine(W, H)
61 : XYMap::constructRectangularGrid(W, H)) {}
62 LedsXY(const LedsXY &) = default;
63 LedsXY &operator=(const LedsXY &) = default;
64 void setXyMap(const XYMap &xymap) { mXyMap = xymap; }
65 void setSerpentine(bool is_serpentine) {
66 mXyMap = is_serpentine ? XYMap::constructSerpentine(W, H)
68 }
69
70 private:
71 CRGB mLeds[W * H] = {};
72};
73
74} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
const XYMap & xymap() const
Definition leds.h:39
Leds(CRGB *leds, uint16_t width, uint16_t height)
Definition leds.cpp:44
Leds(Leds &&)=default
size_t height() const
Definition leds.h:29
void fill(const CRGB &color)
Definition leds.h:44
CRGB & operator()(int x, int y)
Definition leds.cpp:14
const CRGB * rgb() const
Definition leds.h:37
CRGB * rgb()
Definition leds.h:36
XYMap mXyMap
Definition leds.h:52
const CRGB & at(int x, int y) const
Definition leds.h:26
static CRGB & empty()
Definition leds.cpp:21
Leds & operator=(const Leds &)=default
CRGB * operator[](int x)
Definition leds.cpp:33
Leds(const Leds &)=default
CRGB & at(int x, int y)
Definition leds.h:25
size_t width() const
Definition leds.h:28
CRGB * mLeds
Definition leds.h:53
LedsXY(bool is_serpentine)
Definition leds.h:59
LedsXY(const LedsXY &)=default
void setXyMap(const XYMap &xymap)
Definition leds.h:64
CRGB mLeds[W *H]
Definition leds.h:71
LedsXY()
Definition leds.h:58
void setSerpentine(bool is_serpentine)
Definition leds.h:65
LedsXY & operator=(const LedsXY &)=default
static XYMap constructSerpentine(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:54
static XYMap constructRectangularGrid(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:36
Defines the red, green, and blue (RGB) pixel struct.
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