FastLED 3.9.15
Loading...
Searching...
No Matches
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, u16 width, u16 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 fl::size width() const { return mXyMap.getHeight(); }
29 fl::size 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 (fl::size i = 0; i < mXyMap.getTotal(); ++i) {
46 mLeds[i] = color;
47 }
48 }
49
50
51
52 protected:
53 static CRGB &empty(); // Allows safe out of bounds access.
56};
57
58template <fl::size W, fl::size H> class LedsXY : public Leds {
59 public:
60 LedsXY() : Leds(mLedsData, XYMap::constructSerpentine(W, H)) {}
61 explicit LedsXY(bool is_serpentine)
62 : Leds(mLedsData, is_serpentine ? XYMap::constructSerpentine(W, H)
63 : XYMap::constructRectangularGrid(W, H)) {}
64 LedsXY(const LedsXY &) = default;
65 LedsXY &operator=(const LedsXY &) = default;
66 void setXyMap(const XYMap &xymap) { mXyMap = xymap; }
67 void setSerpentine(bool is_serpentine) {
68 mXyMap = is_serpentine ? XYMap::constructSerpentine(W, H)
70 }
71
72 private:
73 CRGB mLedsData[W * H] = {};
74};
75
76} // namespace fl
CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
int x
Definition simple.h:92
const XYMap & xymap() const
Definition leds.h:39
fl::size width() const
Definition leds.h:28
Leds(Leds &&)=default
void fill(const CRGB &color)
Definition leds.h:44
CRGB & operator()(int x, int y)
Definition leds.cpp:12
Leds(CRGB *leds, u16 width, u16 height)
Definition leds.cpp:42
const CRGB * rgb() const
Definition leds.h:37
CRGB * rgb()
Definition leds.h:36
XYMap mXyMap
Definition leds.h:54
const CRGB & at(int x, int y) const
Definition leds.h:26
static CRGB & empty()
Definition leds.cpp:19
fl::size height() const
Definition leds.h:29
Leds & operator=(const Leds &)=default
CRGB * operator[](int x)
Definition leds.cpp:31
Leds(const Leds &)=default
CRGB & at(int x, int y)
Definition leds.h:25
CRGB * mLeds
Definition leds.h:55
LedsXY(bool is_serpentine)
Definition leds.h:61
LedsXY(const LedsXY &)=default
void setXyMap(const XYMap &xymap)
Definition leds.h:66
LedsXY()
Definition leds.h:60
void setSerpentine(bool is_serpentine)
Definition leds.h:67
CRGB mLedsData[W *H]
Definition leds.h:73
LedsXY & operator=(const LedsXY &)=default
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0)
Definition xymap.cpp:52
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0)
Definition xymap.cpp:34
Defines the red, green, and blue (RGB) pixel struct.
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86