LUA: show protocol2 only on receivers with serial1_TX or PWM pins defined (#2999)
[ExpressLRS.git] / src / lib / LED / esp32rgb.cpp
blobbc9562577da2e14594165db79e0ad075f3d89d9b
1 #include "targets.h"
3 #if defined(PLATFORM_ESP32)
5 #include "logging.h"
7 #include <math.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "esp_system.h"
13 #include "driver/i2s.h"
14 #include "esp32rgb.h"
16 #define I2S_NUM i2s_port_t(0)
18 #if defined(CONFIG_IDF_TARGET_ESP32S2)
19 #define SAMPLE_RATE (360000)
20 #define MCLK 48000000
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)
29 #define MCLK 48000000
30 #endif
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,
52 .dma_buf_count = 4,
53 .use_apll = true,
54 .tx_desc_auto_clear = true,
55 .fixed_mclk = MCLK,
58 i2s_pin_config_t pin_config = {
59 .bck_io_num = -1,
60 .ws_io_num = -1,
61 .data_out_num = gpio_pin,
62 .data_in_num = -1,
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);
70 i2s_stop(I2S_NUM);
73 void ESP32S3LedDriver::Show()
75 size_t bytes_written = 0;
76 i2s_stop(I2S_NUM);
77 i2s_write(I2S_NUM, out_buffer, out_buffer_size, &bytes_written, portMAX_DELAY);
78 i2s_start(I2S_NUM);
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};
98 #endif
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;
124 #endif