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" // IWYU pragma: keep
4#include "fl/math/xymap.h"
5#include "fl/stl/span.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9
10// Leds definition.
11// Drawing operations on a block of leds requires information about the layout
12// of the leds. Hence this class.
13class Leds {
14 public:
15 Leds(CRGB *leds, u16 width, u16 height);
16 Leds(CRGB *leds, const XYMap &xymap);
17
18 // Copy constructor and assignment operator.
19 Leds(const Leds &) FL_NOEXCEPT = default;
20 Leds &operator=(const Leds &) FL_NOEXCEPT = default;
21 Leds(Leds &&) FL_NOEXCEPT = default;
22
23 // out of bounds access returns empty() led and is safe to read/write.
24 CRGB &operator()(int x, int y);
25 const CRGB &operator()(int x, int y) const;
26
27 CRGB &at(int x, int y) { return (*this)(x, y); }
28 const CRGB &at(int x, int y) const { return (*this)(x, y); }
29
30 fl::size width() const { return mXyMap.getHeight(); }
31 fl::size height() const { return mXyMap.getWidth(); }
32
33 // Allows normal matrix array (row major) access, bypassing the XYMap.
34 // Will assert if XYMap is not serpentine or line by line.
35 CRGB *operator[](int x);
36 const CRGB *operator[](int x) const;
37 // Raw data access.
38 fl::span<CRGB> rgb() { return mLeds; }
39 fl::span<const CRGB> rgb() const { return mLeds; }
40
41 const XYMap &xymap() const { return mXyMap; }
42
43 operator CRGB *() { return mLeds.data(); }
44 operator const CRGB *() const { return mLeds.data(); }
45
46 void fill(const CRGB &color) {
47 for (fl::size i = 0; i < mXyMap.getTotal(); ++i) {
48 mLeds[i] = color;
49 }
50 }
51
52
53
54 protected:
55 static CRGB &empty(); // Allows safe out of bounds access.
58};
59
60template <fl::size W, fl::size H> class LedsXY : public Leds {
61 public:
62 LedsXY() FL_NOEXCEPT : Leds(mLedsData, XYMap::constructSerpentine(W, H)) {}
63 explicit LedsXY(bool is_serpentine)
64 : Leds(mLedsData, is_serpentine ? XYMap::constructSerpentine(W, H)
65 : XYMap::constructRectangularGrid(W, H)) {}
66 LedsXY(const LedsXY &) FL_NOEXCEPT = default;
67 LedsXY &operator=(const LedsXY &) FL_NOEXCEPT = default;
68 void setXyMap(const XYMap &xymap) { mXyMap = xymap; }
69 void setSerpentine(bool is_serpentine) {
70 mXyMap = is_serpentine ? XYMap::constructSerpentine(W, H)
72 }
73
74 private:
75 CRGB mLedsData[W * H] = {};
76};
77
78} // namespace fl
fl::CRGB leds[NUM_LEDS]
static const int H
Definition PerfDisc.ino:20
static const int W
Definition PerfDisc.ino:19
const XYMap & xymap() const
Definition leds.h:41
fl::size width() const
Definition leds.h:30
Leds & operator=(const Leds &) FL_NOEXCEPT=default
void fill(const CRGB &color)
Definition leds.h:46
fl::span< CRGB > mLeds
Definition leds.h:57
Leds(CRGB *leds, u16 width, u16 height)
Definition leds.cpp.hpp:42
Leds(Leds &&) FL_NOEXCEPT=default
XYMap mXyMap
Definition leds.h:56
const CRGB & at(int x, int y) const
Definition leds.h:28
static CRGB & empty()
Definition leds.cpp.hpp:19
Leds(const Leds &) FL_NOEXCEPT=default
fl::size height() const
Definition leds.h:31
CRGB * operator[](int x)
Definition leds.cpp.hpp:31
CRGB & at(int x, int y)
Definition leds.h:27
fl::span< const CRGB > rgb() const
Definition leds.h:39
fl::span< CRGB > rgb()
Definition leds.h:38
LedsXY(bool is_serpentine)
Definition leds.h:63
LedsXY & operator=(const LedsXY &) FL_NOEXCEPT=default
LedsXY(const LedsXY &) FL_NOEXCEPT=default
void setXyMap(const XYMap &xymap)
Definition leds.h:68
void setSerpentine(bool is_serpentine)
Definition leds.h:69
CRGB mLedsData[W *H]
Definition leds.h:75
LedsXY() FL_NOEXCEPT
Definition leds.h:62
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:53
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:35
Defines the 8-bit red, green, and blue (RGB) pixel type in the fl namespace.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38