FastLED 3.9.12
Loading...
Searching...
No Matches
strstream.h
1#pragma once
2
3#include "crgb.h"
4#include "str.h"
5
6
7#ifndef FASTLED_STRSTREAM_USES_SIZE_T
8#if defined(__AVR__) || defined(ESP8266) || defined(ESP32)
9#define FASTLED_STRSTREAM_USES_SIZE_T 0
10#else
11#define FASTLED_STRSTREAM_USES_SIZE_T 1
12#endif
13#endif
14
15namespace fl {
16
17template <typename T> struct StrStreamHelper {
18 static void append(Str &str, const T& n) { str.append(n); }
19};
20
21template <> struct StrStreamHelper<int> {
22 static void append(Str &str, const int& n) { str.append(int32_t(n)); }
23};
24
25template <> struct StrStreamHelper<uint8_t> {
26 static void append(Str &str, const uint8_t& n) { str.append(uint16_t(n)); }
27};
28
29template <> struct StrStreamHelper<char> {
30 static void append(Str &str, const char& n) { str.append(uint16_t(n)); }
31};
32
33template <> struct StrStreamHelper<unsigned int> {
34 static void append(Str &str, const unsigned int& n) { str.append(uint32_t(n)); }
35};
36
37class StrStream {
38 public:
39 StrStream() = default;
40 StrStream(const Str &str) : mStr(str) {}
41
42 void setTreatCharAsInt(bool treatCharAsInt) {
43 mTreatCharAsInt = treatCharAsInt;
44 }
45
46 const Str &str() const { return mStr; }
47 const char *c_str() const { return mStr.c_str(); }
48
49 StrStream &operator<<(const CRGB &rgb) {
50 mStr.append("CRGB(");
51 mStr.append(rgb.r);
52 mStr.append(",");
53 mStr.append(rgb.g);
54 mStr.append(",");
55 mStr.append(rgb.b);
56 mStr.append(")");
57 return *this;
58 }
59
60 StrStream &operator=(uint16_t n) {
61 mStr.clear();
62 (*this) << n;
63 return *this;
64 }
65
66 StrStream &operator=(uint8_t n) {
67 mStr.clear();
68 (*this) << n;
69 return *this;
70 }
71
72 StrStream &operator=(char c) {
73 mStr.clear();
74 (*this) << c;
75 return *this;
76 }
77
78 // << operator section
79 StrStream &operator<<(const Str &str) {
80 mStr.append(str);
81 return *this;
82 }
83
84 StrStream &operator<<(const char *str) {
85 mStr.append(str);
86 return *this;
87 }
88
89 StrStream &operator<<(char c) {
90 if (mTreatCharAsInt) {
92 } else {
94 }
95 return *this;
96 }
97
98 #if FASTLED_STRSTREAM_USES_SIZE_T
99 StrStream &operator<<(size_t n) {
100 mStr.append(uint32_t(n));
101 return *this;
102 }
103 #endif
104
105 template<typename T>
106 StrStream &operator<<(T n) {
108 return *this;
109 }
110
111 StrStream &operator<<(uint8_t n) {
112 if (mTreatCharAsInt) {
113 mStr.append(uint16_t(n));
114 } else {
115 mStr.append(n);
116 }
117 return *this;
118 }
119
120 StrStream &operator<<(uint16_t n) {
121 mStr.append(n);
122 return *this;
123 }
124
125 StrStream &operator<<(int16_t n) {
126 mStr.append(n);
127 return *this;
128 }
129
130 StrStream &operator<<(uint32_t n) {
131 mStr.append(uint32_t(n));
132 return *this;
133 }
134
135 StrStream &operator<<(int32_t n) {
136 mStr.append(n);
137 return *this;
138 }
139
140 // assignment operator completely replaces the current string
141 StrStream &operator=(const Str &str) {
142 mStr = str;
143 return *this;
144 }
145
146 StrStream &operator=(const char *str) {
147 mStr = str;
148 return *this;
149 }
150
151 // crgb
152 StrStream &operator=(const CRGB &rgb) {
153 mStr.clear();
154 (*this) << rgb;
155 return *this;
156 }
157
158
159 private:
160 Str mStr;
161 bool mTreatCharAsInt = true;
162};
163
165 public:
166 template<typename T>
167 FakeStrStream& operator<<(const T&) { return *this; }
168
169 FakeStrStream& operator<<(const char*) { return *this; }
170
171 template<typename T>
172 FakeStrStream& operator=(const T&) { return *this; }
173
174 FakeStrStream& operator<<(const CRGB&) { return *this; }
175 FakeStrStream& operator<<(const Str&) { return *this; }
176 FakeStrStream& operator<<(char) { return *this; }
177
178 #if FASTLED_STRSTREAM_USES_SIZE_T
179 FakeStrStream& operator<<(size_t) { return *this; }
180 #endif
181
182 FakeStrStream& operator<<(uint8_t) { return *this; }
183 FakeStrStream& operator<<(uint16_t) { return *this; }
184 FakeStrStream& operator<<(int16_t) { return *this; }
185 FakeStrStream& operator<<(uint32_t) { return *this; }
186 FakeStrStream& operator<<(int32_t) { return *this; }
187
188 FakeStrStream& operator=(const Str&) { return *this; }
189 FakeStrStream& operator=(const CRGB&) { return *this; }
190 FakeStrStream& operator=(uint16_t) { return *this; }
191 FakeStrStream& operator=(uint8_t) { return *this; }
192 FakeStrStream& operator=(char) { return *this; }
193
194};
195
196} // namespace fl
Definition str.h:368
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:54
uint8_t r
Red channel value.
Definition crgb.h:58
uint8_t g
Green channel value.
Definition crgb.h:62
uint8_t b
Blue channel value.
Definition crgb.h:66