Skip to content

Commit f5329da

Browse files
- SmartLeds library updated to make the code compatible with the ESP32-S3
- made the temparature reading comatible with the ESP32-S3 - added configuration for the esp32-s3-wroom-1 board to the platformio.ini
1 parent eb7f2b3 commit f5329da

File tree

13 files changed

+858
-517
lines changed

13 files changed

+858
-517
lines changed
Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,161 @@
11
#include "Color.h"
22
#include <algorithm>
3-
#include <cmath>
43
#include <cassert>
4+
#include <cmath>
55

66
namespace {
77

88
// Int -> fixed point
9-
int up( int x ) { return x * 255; }
9+
int up(int x) { return x * 255; }
1010

1111
} // namespace
1212

13-
int iRgbSqrt( int num ) {
13+
int iRgbSqrt(int num) {
1414
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
15-
assert( "sqrt input should be non-negative" && num >= 0 );
16-
assert( "sqrt input should no exceed 16 bits" && num <= 0xFFFF );
15+
assert("sqrt input should be non-negative" && num >= 0);
16+
assert("sqrt input should no exceed 16 bits" && num <= 0xFFFF);
1717
int res = 0;
1818
int bit = 1 << 16;
19-
while ( bit > num )
19+
while (bit > num)
2020
bit >>= 2;
21-
while ( bit != 0 ) {
22-
if ( num >= res + bit ) {
21+
while (bit != 0) {
22+
if (num >= res + bit) {
2323
num -= res + bit;
24-
res = ( res >> 1 ) + bit;
24+
res = (res >> 1) + bit;
2525
} else
2626
res >>= 1;
2727
bit >>= 2;
2828
}
2929
return res;
3030
}
3131

32-
Rgb::Rgb( Hsv y ) {
32+
Rgb::Rgb(const Hsv& y) {
3333
// https://stackoverflow.com/questions/24152553/hsv-to-rgb-and-back-without-floating-point-math-in-python
3434
// greyscale
35-
if( y.s == 0 ) {
35+
if (y.s == 0) {
3636
r = g = b = y.v;
3737
return;
3838
}
3939

4040
const int region = y.h / 43;
41-
const int remainder = ( y.h - ( region * 43 ) ) * 6;
42-
43-
const int p = ( y.v * ( 255 - y.s ) ) >> 8;
44-
const int q = ( y.v * ( 255 - ( ( y.s * remainder ) >> 8 ) ) ) >> 8;
45-
const int t = ( y.v * ( 255 - ( ( y.s * (255 -remainder ) ) >> 8 ) ) ) >> 8;
46-
47-
switch( region ) {
48-
case 0: r = y.v; g = t; b = p; break;
49-
case 1: r = q; g = y.v; b = p; break;
50-
case 2: r = p; g = y.v; b = t; break;
51-
case 3: r = p; g = q; b = y.v; break;
52-
case 4: r = t; g = p; b = y.v; break;
53-
case 5: r = y.v; g = p; b = q; break;
54-
default: __builtin_trap();
41+
const int remainder = (y.h - (region * 43)) * 6;
42+
43+
const int p = (y.v * (255 - y.s)) >> 8;
44+
const int q = (y.v * (255 - ((y.s * remainder) >> 8))) >> 8;
45+
const int t = (y.v * (255 - ((y.s * (255 - remainder)) >> 8))) >> 8;
46+
47+
switch (region) {
48+
case 0:
49+
r = y.v;
50+
g = t;
51+
b = p;
52+
break;
53+
case 1:
54+
r = q;
55+
g = y.v;
56+
b = p;
57+
break;
58+
case 2:
59+
r = p;
60+
g = y.v;
61+
b = t;
62+
break;
63+
case 3:
64+
r = p;
65+
g = q;
66+
b = y.v;
67+
break;
68+
case 4:
69+
r = t;
70+
g = p;
71+
b = y.v;
72+
break;
73+
case 5:
74+
r = y.v;
75+
g = p;
76+
b = q;
77+
break;
78+
default:
79+
__builtin_trap();
5580
}
5681

5782
a = y.a;
5883
}
5984

60-
Rgb& Rgb::operator=( Hsv hsv ) {
61-
Rgb r{ hsv };
62-
swap( r );
85+
Rgb& Rgb::operator=(const Hsv& hsv) {
86+
Rgb r { hsv };
87+
swap(r);
6388
return *this;
6489
}
6590

66-
Rgb Rgb::operator+( Rgb in ) const {
91+
Rgb Rgb::operator+(const Rgb& in) const {
6792
auto copy = *this;
6893
copy += in;
6994
return copy;
7095
}
7196

72-
Rgb& Rgb::operator+=( Rgb in ) {
97+
Rgb& Rgb::operator+=(const Rgb& in) {
7398
unsigned int red = r + in.r;
74-
r = ( red < 255 ) ? red : 255;
99+
r = (red < 255) ? red : 255;
75100
unsigned int green = g + in.g;
76-
g = ( green < 255 ) ? green : 255;
101+
g = (green < 255) ? green : 255;
77102
unsigned int blue = b + in.b;
78-
b = ( blue < 255 ) ? blue : 255;
103+
b = (blue < 255) ? blue : 255;
79104
return *this;
80105
}
81106

82-
Rgb& Rgb::blend( Rgb in ) {
83-
unsigned int inAlpha = in.a * ( 255 - a );
84-
unsigned int alpha = a + inAlpha;
85-
r = iRgbSqrt( ( ( r * r * a ) + ( in.r * in.r * inAlpha ) ) / alpha );
86-
g = iRgbSqrt( ( ( g * g * a ) + ( in.g * in.g * inAlpha ) ) / alpha );
87-
b = iRgbSqrt( ( ( b * b * a ) + ( in.b * in.b * inAlpha ) ) / alpha );
88-
a = alpha;
107+
Rgb Rgb::operator-(const Rgb& in) const {
108+
auto copy = *this;
109+
copy -= in;
110+
return copy;
111+
}
112+
113+
Rgb& Rgb::operator-=(const Rgb& in) {
114+
r = (in.r > r) ? 0 : r - in.r;
115+
g = (in.g > g) ? 0 : g - in.g;
116+
b = (in.b > b) ? 0 : b - in.b;
89117
return *this;
90118
}
91119

92-
uint8_t IRAM_ATTR Rgb::getGrb( int idx ) {
93-
switch ( idx ) {
94-
case 0: return g;
95-
case 1: return r;
96-
case 2: return b;
97-
}
98-
__builtin_unreachable();
120+
Rgb& Rgb::blend(const Rgb& in) {
121+
unsigned int inAlpha = in.a * (255 - a);
122+
unsigned int alpha = a + inAlpha;
123+
r = iRgbSqrt(((r * r * a) + (in.r * in.r * inAlpha)) / alpha);
124+
g = iRgbSqrt(((g * g * a) + (in.g * in.g * inAlpha)) / alpha);
125+
b = iRgbSqrt(((b * b * a) + (in.b * in.b * inAlpha)) / alpha);
126+
a = alpha;
127+
return *this;
99128
}
100129

101-
Hsv::Hsv( Rgb r ) {
102-
int min = std::min( r.r, std::min( r.g, r.b ) );
103-
int max = std::max( r.r, std::max( r.g, r.b ) );
130+
Hsv::Hsv(const Rgb& r) {
131+
int min = std::min(r.r, std::min(r.g, r.b));
132+
int max = std::max(r.r, std::max(r.g, r.b));
104133
int chroma = max - min;
105134

106135
v = max;
107-
if ( chroma == 0 ) {
136+
if (chroma == 0) {
108137
h = s = 0;
109138
return;
110139
}
111140

112-
s = up( chroma ) / max;
141+
s = up(chroma) / max;
113142
int hh;
114-
if ( max == r.r )
115-
hh = ( up( int( r.g ) - int( r.b ) ) ) / chroma / 6;
116-
else if ( max == r.g )
117-
hh = 255 / 3 + ( up( int( r.b ) - int( r.r ) ) ) / chroma / 6;
143+
if (max == r.r)
144+
hh = (up(int(r.g) - int(r.b))) / chroma / 6;
145+
else if (max == r.g)
146+
hh = 255 / 3 + (up(int(r.b) - int(r.r))) / chroma / 6;
118147
else
119-
hh = 2 * 255 / 3 + ( up( int( r.r ) - int( r.g ) ) ) / chroma / 6;
148+
hh = 2 * 255 / 3 + (up(int(r.r) - int(r.g))) / chroma / 6;
120149

121-
if ( hh < 0 )
150+
if (hh < 0)
122151
hh += 255;
123152
h = hh;
124153

125154
a = r.a;
126155
}
127156

128-
Hsv& Hsv::operator=( Rgb rgb ) {
129-
Hsv h{ rgb };
130-
swap( h );
157+
Hsv& Hsv::operator=(const Rgb& rgb) {
158+
Hsv h { rgb };
159+
swap(h);
131160
return *this;
132161
}

code/components/jomjol_controlGPIO/Color.h

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,92 @@
11
#pragma once
22

3-
#ifndef COLOR_H
4-
#define COLOR_H
5-
6-
#include <cstdint>
73
#include "esp_attr.h"
4+
#include <cstdint>
85
union Hsv;
96

107
union Rgb {
11-
struct __attribute__ ((packed)) {
12-
uint8_t r, g, b, a;
8+
struct __attribute__((packed)) {
9+
uint8_t g, r, b, a;
1310
};
1411
uint32_t value;
1512

16-
Rgb( uint8_t r = 0, uint8_t g = 0, uint8_t b = 0, uint8_t a = 255 ) : r( r ), g( g ), b( b ), a( a ) {}
17-
Rgb( Hsv c );
18-
Rgb& operator=( Rgb rgb ) { swap( rgb ); return *this; }
19-
Rgb& operator=( Hsv hsv );
20-
Rgb operator+( Rgb in ) const;
21-
Rgb& operator+=( Rgb in );
22-
bool operator==( Rgb in ) const { return in.value == value; }
23-
Rgb& blend( Rgb in );
24-
void swap( Rgb& o ) { value = o.value; }
13+
Rgb(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0, uint8_t a = 255)
14+
: g(g)
15+
, r(r)
16+
, b(b)
17+
, a(a) {}
18+
Rgb(const Hsv& c);
19+
Rgb(const Rgb&) = default;
20+
Rgb& operator=(const Rgb& rgb) {
21+
swap(rgb);
22+
return *this;
23+
}
24+
Rgb& operator=(const Hsv& hsv);
25+
Rgb operator+(const Rgb& in) const;
26+
Rgb& operator+=(const Rgb& in);
27+
Rgb operator-(const Rgb& in) const;
28+
Rgb& operator-=(const Rgb& in);
29+
bool operator==(const Rgb& in) const { return in.value == value; }
30+
Rgb& blend(const Rgb& in);
31+
void swap(const Rgb& o) { value = o.value; }
2532
void linearize() {
2633
r = channelGamma(r);
2734
g = channelGamma(g);
2835
b = channelGamma(b);
2936
}
3037

31-
uint8_t IRAM_ATTR getGrb( int idx );
32-
33-
void stretchChannels( uint8_t maxR, uint8_t maxG, uint8_t maxB ) {
34-
r = stretch( r, maxR );
35-
g = stretch( g, maxG );
36-
b = stretch( b, maxB );
38+
inline uint8_t IRAM_ATTR getGrb(int idx) {
39+
switch (idx) {
40+
case 0:
41+
return g;
42+
case 1:
43+
return r;
44+
case 2:
45+
return b;
46+
}
47+
__builtin_unreachable();
3748
}
3849

39-
void stretchChannelsEvenly( uint8_t max ) {
40-
stretchChannels( max, max, max );
50+
void stretchChannels(uint8_t maxR, uint8_t maxG, uint8_t maxB) {
51+
r = stretch(r, maxR);
52+
g = stretch(g, maxG);
53+
b = stretch(b, maxB);
4154
}
4255

56+
void stretchChannelsEvenly(uint8_t max) { stretchChannels(max, max, max); }
57+
4358
private:
44-
uint8_t stretch( int value, uint8_t max ) {
45-
return ( value * max ) >> 8;
46-
}
59+
uint8_t stretch(int value, uint8_t max) { return (value * max) >> 8; }
4760

48-
uint8_t channelGamma( int channel ) {
61+
uint8_t channelGamma(int channel) {
4962
/* The optimal gamma correction is x^2.8. However, this is expensive to
5063
* compute. Therefore, we use x^3 for gamma correction. Also, we add a
5164
* bias as the WS2812 LEDs do not turn on for values less than 4. */
5265
if (channel == 0)
5366
return channel;
5467
channel = channel * channel * channel * 251;
5568
channel >>= 24;
56-
return static_cast< uint8_t >( 4 + channel );
69+
return static_cast<uint8_t>(4 + channel);
5770
}
5871
};
5972

6073
union Hsv {
61-
struct __attribute__ ((packed)) {
74+
struct __attribute__((packed)) {
6275
uint8_t h, s, v, a;
6376
};
6477
uint32_t value;
6578

66-
Hsv( uint8_t h, uint8_t s = 0, uint8_t v = 0, uint8_t a = 255 ) : h( h ), s( s ), v( v ), a( a ) {}
67-
Hsv( Rgb r );
68-
Hsv& operator=( Hsv h ) { swap( h ); return *this; }
69-
Hsv& operator=( Rgb rgb );
70-
bool operator==( Hsv in ) const { return in.value == value; }
71-
void swap( Hsv& o ) { value = o.value; }
79+
Hsv(uint8_t h, uint8_t s = 0, uint8_t v = 0, uint8_t a = 255)
80+
: h(h)
81+
, s(s)
82+
, v(v)
83+
, a(a) {}
84+
Hsv(const Rgb& r);
85+
Hsv& operator=(const Hsv& h) {
86+
swap(h);
87+
return *this;
88+
}
89+
Hsv& operator=(const Rgb& rgb);
90+
bool operator==(const Hsv& in) const { return in.value == value; }
91+
void swap(const Hsv& o) { value = o.value; }
7292
};
73-
74-
#endif //COLOR_H
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <esp_system.h>
4+
#include <stdint.h>
5+
6+
#if defined(ESP_IDF_VERSION)
7+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
8+
#define SMARTLEDS_NEW_RMT_DRIVER 1
9+
#else
10+
#define SMARTLEDS_NEW_RMT_DRIVER 0
11+
#endif
12+
#else
13+
#define SMARTLEDS_NEW_RMT_DRIVER 0
14+
#endif
15+
16+
namespace detail {
17+
18+
struct TimingParams {
19+
uint32_t T0H;
20+
uint32_t T1H;
21+
uint32_t T0L;
22+
uint32_t T1L;
23+
uint32_t TRS;
24+
};
25+
26+
using LedType = TimingParams;
27+
28+
} // namespace detail
29+
30+
#if SMARTLEDS_NEW_RMT_DRIVER
31+
#include "RmtDriver5.h"
32+
#else
33+
#include "RmtDriver4.h"
34+
#endif

0 commit comments

Comments
 (0)