This commit is contained in:
j3d1 2021-09-10 00:09:03 +02:00
parent e6d6757982
commit 78934b85c2
2 changed files with 62 additions and 13 deletions

View file

@ -11,6 +11,10 @@
#include <esp/spi.h> #include <esp/spi.h>
#include <ws2812_i2s/ws2812_i2s.h> #include <ws2812_i2s/ws2812_i2s.h>
extern "C" {
#include <sysparam.h>
}
const int signal_led_pin = 2; const int signal_led_pin = 2;
const int cs0 = 15; const int cs0 = 15;
@ -18,7 +22,14 @@ const int gpio4 = 4;
const int gpio5 = 5; const int gpio5 = 5;
ws2812_pixel_t **pixels_ptr; struct apa10xx_pixel_t {
struct {
unsigned int mod: 5, marker: 3;
} __attribute__((packed)) global = {0x1F, 0x7};
uint8_t b = 0;
uint8_t g = 0;
uint8_t r = 0;
};
static ws2812_pixel_t next_colour(int i) { static ws2812_pixel_t next_colour(int i) {
ws2812_pixel_t colour = {{0, 0, 0, 0}}; ws2812_pixel_t colour = {{0, 0, 0, 0}};
@ -33,6 +44,16 @@ static ws2812_pixel_t next_colour(int i) {
return colour; return colour;
} }
static apa10xx_pixel_t next_color(int i) {
apa10xx_pixel_t colour;
colour.global.mod = 8;
colour.r = i & 1 ? 32 : 0;
colour.g = i & 2 ? 32 : 0;
colour.b = i & 4 ? 32 : 0;
return colour;
}
void spi_dac(int id, int val) { void spi_dac(int id, int val) {
int dac_val = (val << 2) & 0x3FFC; int dac_val = (val << 2) & 0x3FFC;
@ -53,6 +74,18 @@ void spi_dac(int id, int val) {
gpio_write(cs0, 0); gpio_write(cs0, 0);
} }
void write_leds(apa10xx_pixel_t *arr, size_t len) {
spi_transfer_32(1, 0x00000000);
for (size_t i = 0; i < len; i++)
spi_transfer_32(1, *(uint32_t *) &arr[i]);
spi_transfer_32(1, 0xFFFFFFFF);
spi_transfer_32(1, 0xFFFFFFFF);
}
ws2812_pixel_t **pixels_ptr;
extern "C" void signal_led(bool state) { extern "C" void signal_led(bool state) {
gpio_write(signal_led_pin, !state); gpio_write(signal_led_pin, !state);
} }
@ -63,18 +96,34 @@ extern "C" void signal_led(bool state) {
*/ */
extern "C" void lux_task(void *pvParameters) { extern "C" void lux_task(void *pvParameters) {
gpio_enable(9, GPIO_INPUT); //gpio_enable(9, GPIO_INPUT);
gpio_enable(10, GPIO_INPUT); //gpio_enable(10, GPIO_INPUT);
ws2812_pixel_t pixels[led_number]; int32_t lux_ws2812_number = 8;
ws2812_i2s_init(led_number, PIXEL_RGBW); auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number);
memset(pixels, 0, sizeof(ws2812_pixel_t) * led_number); if(ret != SYSPARAM_OK)
lux_ws2812_number = 8;
int32_t lux_apa10xx_number = 8;
ret = sysparam_get_int32("lux_apa10xx_number", &lux_apa10xx_number);
if(ret != SYSPARAM_OK)
lux_apa10xx_number = 8;
ws2812_pixel_t pixels[lux_ws2812_number];
ws2812_i2s_init(lux_ws2812_number, PIXEL_RGBW);
memset(pixels, 0, sizeof(ws2812_pixel_t) * lux_ws2812_number);
apa10xx_pixel_t leds[lux_apa10xx_number];
//lux_apa102c_number
gpio_enable(signal_led_pin, GPIO_OUTPUT); gpio_enable(signal_led_pin, GPIO_OUTPUT);
gpio_enable(cs0, GPIO_OUTPUT); gpio_enable(cs0, GPIO_OUTPUT);
gpio_enable(gpio4, GPIO_OUTPUT); gpio_enable(gpio4, GPIO_OUTPUT);
gpio_enable(gpio5, GPIO_OUTPUT); gpio_enable(gpio5, GPIO_OUTPUT);
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1); //spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1); //for spi dimmer
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false); //for apa10xx
while (1) { while (1) {
gpio_write(gpio4, 1); gpio_write(gpio4, 1);
vTaskDelay(200 / portTICK_PERIOD_MS); vTaskDelay(200 / portTICK_PERIOD_MS);
@ -94,10 +143,14 @@ extern "C" void lux_task(void *pvParameters) {
for (int c = 8; c >= 0; c--) { for (int c = 8; c >= 0; c--) {
for (int i = 0; i < led_number; i++) { for (auto &pixel: pixels) {
pixels[i] = next_colour(c); pixel = next_colour(c);
}
for (auto &led: leds) {
led = next_color(c);
} }
ws2812_i2s_update(pixels, PIXEL_RGBW); ws2812_i2s_update(pixels, PIXEL_RGBW);
write_leds(leds, lux_apa10xx_number);
vTaskDelay(200 / portTICK_PERIOD_MS); vTaskDelay(200 / portTICK_PERIOD_MS);
} }

View file

@ -5,10 +5,6 @@
#ifndef FIRMWARE_LUX_H #ifndef FIRMWARE_LUX_H
#define FIRMWARE_LUX_H #define FIRMWARE_LUX_H
#ifdef __cplusplus
constexpr int led_number = 8;
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif