3 #if defined(PLATFORM_ESP32)
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "esp_system.h"
13 #include "driver/i2s.h"
16 #define I2S_NUM i2s_port_t(0)
18 #if defined(CONFIG_IDF_TARGET_ESP32S2)
19 #define SAMPLE_RATE (360000)
21 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
22 #define SAMPLE_RATE (800000)
23 #define MCLK 160000000
24 #elif defined(CONFIG_IDF_TARGET_ESP32C3)
25 #define SAMPLE_RATE (800000)
26 #define MCLK 160000000
27 #elif defined(CONFIG_IDF_TARGET_ESP32)
28 #define SAMPLE_RATE (360000)
32 ESP32S3LedDriver::ESP32S3LedDriver(int count
, int pin
) : num_leds(count
), gpio_pin(pin
)
34 out_buffer_size
= num_leds
* 24 * sizeof(uint16_t);
35 out_buffer
= (uint16_t *)heap_caps_malloc(out_buffer_size
, MALLOC_CAP_8BIT
);
38 ESP32S3LedDriver::~ESP32S3LedDriver()
40 heap_caps_free(out_buffer
);
43 void ESP32S3LedDriver::Begin()
45 i2s_config_t i2s_config
= {
46 .mode
= i2s_mode_t(I2S_MODE_MASTER
| I2S_MODE_TX
),
47 .sample_rate
= SAMPLE_RATE
,
48 .bits_per_sample
= I2S_BITS_PER_SAMPLE_16BIT
,
49 .channel_format
= I2S_CHANNEL_FMT_RIGHT_LEFT
,
50 .communication_format
= I2S_COMM_FORMAT_STAND_I2S
,
51 .intr_alloc_flags
= 0,
54 .tx_desc_auto_clear
= true,
58 i2s_pin_config_t pin_config
= {
61 .data_out_num
= gpio_pin
,
65 i2s_config
.dma_buf_len
= out_buffer_size
;
66 i2s_driver_install(I2S_NUM
, &i2s_config
, 0, NULL
);
67 delay(1); // without this it fails to boot and gets stuck!
68 i2s_set_pin(I2S_NUM
, &pin_config
);
69 i2s_zero_dma_buffer(I2S_NUM
);
73 void ESP32S3LedDriver::Show()
75 size_t bytes_written
= 0;
77 i2s_write(I2S_NUM
, out_buffer
, out_buffer_size
, &bytes_written
, portMAX_DELAY
);
81 void ESP32S3LedDriver::ClearTo(RgbColor color
, uint16_t first
, uint16_t last
)
83 for (uint16_t i
=first
; i
<=last
; i
++)
85 SetPixelColor(i
, color
);
90 #if defined(CONFIG_IDF_TARGET_ESP32S2)
91 static const int bitorder
[] = {0x40, 0x80, 0x10, 0x20, 0x04, 0x08, 0x01, 0x02};
92 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
93 static const int bitorder
[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
94 #elif defined(CONFIG_IDF_TARGET_ESP32C3)
95 static const int bitorder
[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
96 #elif defined(CONFIG_IDF_TARGET_ESP32)
97 static const int bitorder
[] = {0x40, 0x80, 0x10, 0x20, 0x04, 0x08, 0x01, 0x02};
100 void ESP32S3LedDriverGRB::SetPixelColor(uint16_t indexPixel
, RgbColor color
)
102 int loc
= indexPixel
* 24;
103 for(int bitpos
= 0 ; bitpos
< 8 ; bitpos
++)
105 int bit
= bitorder
[bitpos
];
106 out_buffer
[loc
+ bitpos
+ 0] = (color
.G
& bit
) ? 0xFFE0 : 0xF000;
107 out_buffer
[loc
+ bitpos
+ 8] = (color
.R
& bit
) ? 0xFFE0 : 0xF000;
108 out_buffer
[loc
+ bitpos
+ 16] = (color
.B
& bit
) ? 0xFFE0 : 0xF000;
112 void ESP32S3LedDriverRGB::SetPixelColor(uint16_t indexPixel
, RgbColor color
)
114 int loc
= indexPixel
* 24;
115 for(int bitpos
= 0 ; bitpos
< 8 ; bitpos
++)
117 int bit
= bitorder
[bitpos
];
118 out_buffer
[loc
+ bitpos
+ 0] = (color
.R
& bit
) ? 0xFFE0 : 0xF000;
119 out_buffer
[loc
+ bitpos
+ 8] = (color
.G
& bit
) ? 0xFFE0 : 0xF000;
120 out_buffer
[loc
+ bitpos
+ 16] = (color
.B
& bit
) ? 0xFFE0 : 0xF000;