Skip to content

Commit 44ae4a7

Browse files
committed
led strip
1 parent 868aafa commit 44ae4a7

File tree

6 files changed

+65
-48
lines changed

6 files changed

+65
-48
lines changed

lib/Espfc/src/Connect/Cli.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ const Cli::Param * Cli::initialize(ModelConfig& c)
356356
static const char* currentSourceChoices[] = { PSTR("NONE"), PSTR("ADC"), NULL };
357357
static const char* blackboxDevChoices[] = { PSTR("NONE"), PSTR("FLASH"), PSTR("SD_CARD"), PSTR("SERIAL"), NULL };
358358
static const char* blackboxModeChoices[] = { PSTR("NORMAL"), PSTR("TEST"), PSTR("ALWAYS"), NULL };
359+
static const char* ledTypeChoices[] = { PSTR("SIMPLE"), PSTR("STRIP"), NULL };
359360

360361
size_t i = 0;
361362
static const Param params[] = {
@@ -677,6 +678,7 @@ const Cli::Param * Cli::initialize(ModelConfig& c)
677678
#endif
678679
Param(PSTR("pin_buzzer_invert"), &c.buzzer.inverted),
679680
Param(PSTR("pin_led_invert"), &c.led.invert),
681+
Param(PSTR("pin_led_type"), &c.led.type, ledTypeChoices),
680682

681683
#ifdef ESPFC_I2C_0
682684
Param(PSTR("i2c_speed"), &c.i2cSpeed),

lib/Espfc/src/Connect/StatusLed.cpp

+45-38
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
#ifdef ESPFC_LED_WS2812
66
#include "driver/i2s.h"
77

8+
// https://docs.espressif.com/projects/esp-idf/en/v4.4.4/esp32/api-reference/peripherals/i2s.html
9+
// https://github.com/vunam/esp32-i2s-ws2812/blob/master/ws2812.c
10+
811
static constexpr size_t LED_NUMBER = 1;
9-
static constexpr size_t PIXEL_SIZE = 12; // each colour takes 4 bytes
12+
static constexpr size_t PIXEL_SIZE = 12; // each colour takes 4 bytes in buffer
13+
static constexpr size_t ZERO_BUFFER = 32;
14+
static constexpr size_t SIZE_BUFFER = LED_NUMBER * PIXEL_SIZE + ZERO_BUFFER;
1015
static constexpr uint32_t SAMPLE_RATE = 93750;
11-
static constexpr size_t ZERO_BUFFER = 48;
1216
static constexpr i2s_port_t I2S_NUM = I2S_NUM_0;
1317

1418
typedef struct {
15-
uint8_t green;
16-
uint8_t red;
17-
uint8_t blue;
19+
uint8_t g;
20+
uint8_t r;
21+
uint8_t b;
1822
} ws2812_pixel_t;
1923

2024
static i2s_config_t i2s_config = {
@@ -24,8 +28,8 @@ static i2s_config_t i2s_config = {
2428
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
2529
.communication_format = I2S_COMM_FORMAT_STAND_MSB,
2630
.intr_alloc_flags = 0,
27-
.dma_buf_count = 4,
28-
.dma_buf_len = LED_NUMBER * PIXEL_SIZE,
31+
.dma_buf_count = 2,
32+
.dma_buf_len = SIZE_BUFFER / 2,
2933
.use_apll = false,
3034
.tx_desc_auto_clear = false,
3135
.fixed_mclk = 0,
@@ -40,9 +44,7 @@ static i2s_pin_config_t pin_config = {
4044
.data_in_num = -1
4145
};
4246

43-
static uint8_t out_buffer[LED_NUMBER * PIXEL_SIZE] = {0};
44-
static uint8_t off_buffer[ZERO_BUFFER] = {0};
45-
static uint16_t size_buffer = LED_NUMBER * PIXEL_SIZE;
47+
static uint8_t out_buffer[SIZE_BUFFER] = {0};
4648

4749
static const uint16_t bitpatterns[4] = {0x88, 0x8e, 0xe8, 0xee};
4850

@@ -51,44 +53,45 @@ static void ws2812_init(int8_t pin)
5153
pin_config.data_out_num = pin;
5254
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
5355
i2s_set_pin(I2S_NUM, &pin_config);
56+
i2s_zero_dma_buffer(I2S_NUM);
57+
std::fill_n(out_buffer, SIZE_BUFFER, 0);
58+
}
59+
60+
static void ws2812_write_pixel(uint8_t * buffer, const ws2812_pixel_t& pixel)
61+
{
62+
*buffer++ = bitpatterns[pixel.g >> 6 & 0x03];
63+
*buffer++ = bitpatterns[pixel.g >> 4 & 0x03];
64+
*buffer++ = bitpatterns[pixel.g >> 2 & 0x03];
65+
*buffer++ = bitpatterns[pixel.g >> 0 & 0x03];
66+
67+
*buffer++ = bitpatterns[pixel.r >> 6 & 0x03];
68+
*buffer++ = bitpatterns[pixel.r >> 4 & 0x03];
69+
*buffer++ = bitpatterns[pixel.r >> 2 & 0x03];
70+
*buffer++ = bitpatterns[pixel.r >> 0 & 0x03];
71+
72+
*buffer++ = bitpatterns[pixel.b >> 6 & 0x03];
73+
*buffer++ = bitpatterns[pixel.b >> 4 & 0x03];
74+
*buffer++ = bitpatterns[pixel.b >> 2 & 0x03];
75+
*buffer++ = bitpatterns[pixel.b >> 0 & 0x03];
5476
}
5577

5678
static void ws2812_update(const ws2812_pixel_t * pixels)
5779
{
5880
size_t bytes_written = 0;
59-
60-
for (uint16_t i = 0; i < LED_NUMBER; i++) {
61-
int loc = i * PIXEL_SIZE;
62-
63-
out_buffer[loc] = bitpatterns[pixels[i].green >> 6 & 0x03];
64-
out_buffer[loc + 1] = bitpatterns[pixels[i].green >> 4 & 0x03];
65-
out_buffer[loc + 2] = bitpatterns[pixels[i].green >> 2 & 0x03];
66-
out_buffer[loc + 3] = bitpatterns[pixels[i].green & 0x03];
67-
68-
out_buffer[loc + 4] = bitpatterns[pixels[i].red >> 6 & 0x03];
69-
out_buffer[loc + 5] = bitpatterns[pixels[i].red >> 4 & 0x03];
70-
out_buffer[loc + 6] = bitpatterns[pixels[i].red >> 2 & 0x03];
71-
out_buffer[loc + 7] = bitpatterns[pixels[i].red & 0x03];
72-
73-
out_buffer[loc + 8] = bitpatterns[pixels[i].blue >> 6 & 0x03];
74-
out_buffer[loc + 9] = bitpatterns[pixels[i].blue >> 4 & 0x03];
75-
out_buffer[loc + 10] = bitpatterns[pixels[i].blue >> 2 & 0x03];
76-
out_buffer[loc + 11] = bitpatterns[pixels[i].blue & 0x03];
81+
for (size_t i = 0; i < LED_NUMBER; i++)
82+
{
83+
size_t loc = i * PIXEL_SIZE;
84+
ws2812_write_pixel(out_buffer + loc, pixels[i]);
7785
}
78-
79-
i2s_write(I2S_NUM, out_buffer, size_buffer, &bytes_written, portMAX_DELAY);
80-
i2s_write(I2S_NUM, off_buffer, ZERO_BUFFER, &bytes_written, portMAX_DELAY);
81-
delay(1); // was 10
8286
i2s_zero_dma_buffer(I2S_NUM);
87+
i2s_write(I2S_NUM, out_buffer, SIZE_BUFFER, &bytes_written, portMAX_DELAY);
8388
}
8489

85-
static const ws2812_pixel_t PIXEL_ON[] = {{127, 0, 0}};
90+
static const ws2812_pixel_t PIXEL_ON[] = {{0x40, 0x40, 0x80}};
8691
static const ws2812_pixel_t PIXEL_OFF[] = {{0, 0, 0}};
8792

8893
#endif
8994

90-
// https://github.com/vunam/esp32-i2s-ws2812/blob/master/ws2812.c
91-
9295
namespace Espfc::Connect
9396
{
9497

@@ -99,14 +102,17 @@ static int LED_ON_PATTERN[] = {100, 0};
99102

100103
StatusLed::StatusLed() : _pin(-1), _invert(0), _status(LED_OFF), _next(0), _state(LOW), _step(0), _pattern(LED_OFF_PATTERN) {}
101104

102-
void StatusLed::begin(int8_t pin, uint8_t invert)
105+
void StatusLed::begin(int8_t pin, uint8_t type, uint8_t invert)
103106
{
104107
if(pin == -1) return;
108+
105109
_pin = pin;
110+
_type = type;
106111
_invert = invert;
107112

108113
#ifdef ESPFC_LED_WS2812
109-
ws2812_init(_pin);
114+
if(_type == LED_STRIP) ws2812_init(_pin);
115+
if(_type == LED_SIMPLE) pinMode(_pin, OUTPUT);
110116
#else
111117
pinMode(_pin, OUTPUT);
112118
#endif
@@ -168,7 +174,8 @@ void StatusLed::update()
168174
void StatusLed::_write(uint8_t val)
169175
{
170176
#ifdef ESPFC_LED_WS2812
171-
ws2812_update(val ? PIXEL_ON : PIXEL_OFF);
177+
if(_type == LED_STRIP) ws2812_update(val ? PIXEL_ON : PIXEL_OFF);
178+
if(_type == LED_SIMPLE) digitalWrite(_pin, val ^ _invert);
172179
#else
173180
digitalWrite(_pin, val ^ _invert);
174181
#endif

lib/Espfc/src/Connect/StatusLed.hpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,34 @@
55
namespace Espfc::Connect
66
{
77

8+
enum LedType
9+
{
10+
LED_SIMPLE,
11+
LED_STRIP,
12+
};
13+
814
enum LedStatus
915
{
1016
LED_OFF,
1117
LED_OK,
1218
LED_ERROR,
13-
LED_ON
19+
LED_ON,
1420
};
1521

1622
class StatusLed
1723
{
1824

1925
public:
2026
StatusLed();
21-
void begin(int8_t pin, uint8_t invert);
27+
void begin(int8_t pin, uint8_t type, uint8_t invert);
2228
void update();
2329
void setStatus(LedStatus newStatus, bool force = false);
2430

2531
private:
2632
void _write(uint8_t val);
2733
int8_t _pin;
28-
int8_t _invert;
34+
uint8_t _type;
35+
uint8_t _invert;
2936
LedStatus _status;
3037
uint32_t _next;
3138
bool _state;

lib/Espfc/src/Espfc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int Espfc::load()
1919

2020
int Espfc::begin()
2121
{
22-
_model.state.led.begin(_model.config.pin[PIN_LED_BLINK], _model.config.led.invert);
22+
_model.state.led.begin(_model.config.pin[PIN_LED_BLINK], _model.config.led.type, _model.config.led.invert);
2323

2424
_serial.begin(); // requires _model.load()
2525
//_model.logStorageResult();

lib/Espfc/src/ModelConfig.h

+1
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ struct GpsConfig
668668
struct LedConfig
669669
{
670670
uint8_t invert = 0;
671+
int8_t type = 0;
671672
};
672673

673674
// persistent data

lib/Espfc/src/Target/TargetESP32s2.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#define ESPFC_SERIAL_1
2525
#define ESPFC_SERIAL_1_DEV Serial1
2626
#define ESPFC_SERIAL_1_DEV_T HardwareSerial
27-
#define ESPFC_SERIAL_1_TX 16
28-
#define ESPFC_SERIAL_1_RX 15
27+
#define ESPFC_SERIAL_1_TX 17
28+
#define ESPFC_SERIAL_1_RX 16
2929
#define ESPFC_SERIAL_1_FN (SERIAL_FUNCTION_RX_SERIAL)
3030
#define ESPFC_SERIAL_1_BAUD (SERIAL_SPEED_115200)
3131
#define ESPFC_SERIAL_1_BBAUD (SERIAL_SPEED_NONE)
@@ -48,17 +48,17 @@
4848
#define ESPFC_SPI_0_MOSI 11
4949
#define ESPFC_SPI_0_MISO 13
5050

51-
#define ESPFC_SPI_CS_GYRO 8
51+
#define ESPFC_SPI_CS_GYRO 10
5252
#define ESPFC_SPI_CS_BARO 7
5353

5454
#define ESPFC_I2C_0
55-
#define ESPFC_I2C_0_SCL 10
56-
#define ESPFC_I2C_0_SDA 9
55+
#define ESPFC_I2C_0_SCL 9
56+
#define ESPFC_I2C_0_SDA 8
5757
#define ESPFC_I2C_0_SOFT
5858

5959
#define ESPFC_BUZZER_PIN 5
6060
#define ESPFC_BUTTON_PIN -1
61-
#define ESPFC_LED_PIN -1
61+
#define ESPFC_LED_PIN 15
6262

6363
#define ESPFC_ADC_0
6464
#define ESPFC_ADC_0_PIN 1

0 commit comments

Comments
 (0)