code formatting

This commit is contained in:
Данила Горнушко 2023-09-21 05:04:24 +03:00
parent 5502b04fcb
commit 2176892c3b
3 changed files with 147 additions and 127 deletions

26
.editorconfig Normal file
View file

@ -0,0 +1,26 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
# Set default charset
charset = utf-8
# 4 space indentation
[*.{c, h, cpp}]
indent_style = space
indent_size = 4
# Tab indentation (no size specified)
[Makefile]
indent_style = tab
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

View file

@ -5,8 +5,7 @@ CRGB *ws28xx_pixels;
static int n_of_leds, reset_delay, dma_buf_size;
led_strip_model_t led_model;
static spi_settings_t spi_settings = {
.host = SPI2_HOST,
static spi_settings_t spi_settings = {.host = SPI2_HOST,
.dma_chan = SPI_DMA_CH_AUTO,
.buscfg =
{
@ -25,15 +24,12 @@ static spi_settings_t spi_settings = {
.flags = SPI_DEVICE_TXBIT_LSBFIRST,
}};
esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds,
CRGB **led_buffer_ptr) {
esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, CRGB** led_buffer_ptr) {
esp_err_t err = ESP_OK;
n_of_leds = num_of_leds;
led_model = model;
dma_buf_size =
n_of_leds * 12 +
(reset_delay + 1) *
2; // 12 bytes for each led + bytes for initial zero and reset state
n_of_leds * 12 + (reset_delay + 1) * 2; // 12 bytes for each led + bytes for initial zero and reset state
ws28xx_pixels = malloc(sizeof(CRGB) * n_of_leds);
if (ws28xx_pixels == NULL) {
return ESP_ERR_NO_MEM;
@ -41,21 +37,17 @@ esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds,
*led_buffer_ptr = ws28xx_pixels;
spi_settings.buscfg.mosi_io_num = pin;
spi_settings.buscfg.max_transfer_sz = dma_buf_size;
err = spi_bus_initialize(spi_settings.host, &spi_settings.buscfg,
spi_settings.dma_chan);
err = spi_bus_initialize(spi_settings.host, &spi_settings.buscfg, spi_settings.dma_chan);
if (err != ESP_OK) {
free(ws28xx_pixels);
return err;
}
err = spi_bus_add_device(spi_settings.host, &spi_settings.devcfg,
&spi_settings.spi);
err = spi_bus_add_device(spi_settings.host, &spi_settings.devcfg, &spi_settings.spi);
if (err != ESP_OK) {
free(ws28xx_pixels);
return err;
}
reset_delay = (model == WS2812B)
? 3
: 30; // insrease if something breaks. values are less that
reset_delay = (model == WS2812B) ? 3 : 30; // insrease if something breaks. values are less that
// recommended in datasheets but seem stable
dma_buffer = heap_caps_malloc(dma_buf_size,
MALLOC_CAP_DMA); // Critical to be DMA memory.
@ -73,9 +65,8 @@ void ws28xx_fill_all(CRGB color) {
}
esp_err_t ws28xx_update() {
uint16_t timing_bits[16] = {0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171,
0x1771, 0x7771, 0x1117, 0x7117, 0x1717, 0x7717,
0x1177, 0x7177, 0x1777, 0x7777};
uint16_t timing_bits[16] = {0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771,
0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777};
esp_err_t err;
int n = 0;
memset(dma_buffer, 0, dma_buf_size);
@ -104,8 +95,9 @@ esp_err_t ws28xx_update() {
dma_buffer[n++] = timing_bits[0x0f & (temp >> 20)];
dma_buffer[n++] = timing_bits[0x0f & (temp) >> 16];
}
for (int i = 0; i < reset_delay; i++)
for (int i = 0; i < reset_delay; i++) {
dma_buffer[n++] = 0;
}
err = spi_device_transmit(spi_settings.spi, &(spi_transaction_t){
.length = dma_buf_size * 8,

View file

@ -1,9 +1,9 @@
#ifndef MAIN_ESP_WS28XX_H_
#define MAIN_ESP_WS28XX_H_
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "driver/spi_master.h"
typedef struct {
union {
@ -12,15 +12,18 @@ typedef struct {
uint8_t r;
uint8_t red;
};
union {
uint8_t g;
uint8_t green;
};
union {
uint8_t b;
uint8_t blue;
};
};
uint8_t raw[3];
uint32_t num;
};
@ -39,8 +42,7 @@ typedef enum {
WS2815,
} led_strip_model_t;
esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds,
CRGB **led_buffer_ptr);
esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, CRGB** led_buffer_ptr);
void ws28xx_fill_all(CRGB color);
esp_err_t ws28xx_update();