2021-07-02 16:45:29 +00:00
|
|
|
//
|
|
|
|
// Created by jedi on 25.06.21.
|
|
|
|
//
|
2021-07-19 20:31:30 +00:00
|
|
|
|
|
|
|
#include "lux.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
|
|
|
|
|
|
|
#include <esp/spi.h>
|
2021-07-19 20:56:57 +00:00
|
|
|
#include <ws2812_i2s/ws2812_i2s.h>
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <sysparam.h>
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
const int signal_led_pin = 2;
|
|
|
|
|
2021-09-11 19:49:53 +00:00
|
|
|
//const int cs0 = 15;
|
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
const int gpio4 = 4;
|
|
|
|
const int gpio5 = 5;
|
|
|
|
|
2021-07-19 20:56:57 +00:00
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
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;
|
|
|
|
};
|
2021-07-19 20:56:57 +00:00
|
|
|
|
|
|
|
static ws2812_pixel_t next_colour(int i) {
|
|
|
|
ws2812_pixel_t colour = {{0, 0, 0, 0}};
|
|
|
|
if(i == 8) {
|
|
|
|
colour.white = 32;
|
|
|
|
} else {
|
|
|
|
colour.red = i & 1 ? 32 : 0;
|
|
|
|
colour.green = i & 2 ? 32 : 0;
|
|
|
|
colour.blue = i & 4 ? 32 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return colour;
|
|
|
|
}
|
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:49:53 +00:00
|
|
|
/*
|
2021-07-19 20:56:57 +00:00
|
|
|
void spi_dac(int id, int val) {
|
|
|
|
int dac_val = (val << 2) & 0x3FFC;
|
|
|
|
|
|
|
|
spi_transfer_8(1, ~(0x00));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
spi_transfer_8(1, ~(0x01 << id));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
|
|
|
|
spi_transfer_16(1, dac_val);
|
|
|
|
|
|
|
|
spi_transfer_8(1, ~(0x00));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
spi_transfer_8(1, ~(0x01 << id));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
}
|
2021-09-11 19:49:53 +00:00
|
|
|
*/
|
2021-09-09 22:09:03 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
extern "C" void signal_led(bool state) {
|
2021-07-19 21:31:20 +00:00
|
|
|
gpio_write(signal_led_pin, !state);
|
2021-07-19 20:31:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 20:56:57 +00:00
|
|
|
|
|
|
|
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
|
|
|
|
*
|
|
|
|
*/
|
2021-07-19 20:31:30 +00:00
|
|
|
extern "C" void lux_task(void *pvParameters) {
|
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
//gpio_enable(9, GPIO_INPUT);
|
|
|
|
//gpio_enable(10, GPIO_INPUT);
|
|
|
|
|
|
|
|
int32_t lux_ws2812_number = 8;
|
|
|
|
auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_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;
|
2021-07-19 20:56:57 +00:00
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
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
|
2021-07-19 20:56:57 +00:00
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
gpio_enable(signal_led_pin, GPIO_OUTPUT);
|
2021-09-11 19:49:53 +00:00
|
|
|
//gpio_enable(cs0, GPIO_OUTPUT);
|
2021-07-19 20:31:30 +00:00
|
|
|
gpio_enable(gpio4, GPIO_OUTPUT);
|
|
|
|
gpio_enable(gpio5, GPIO_OUTPUT);
|
2021-09-09 22:09:03 +00:00
|
|
|
//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
|
2021-07-19 20:56:57 +00:00
|
|
|
while (1) {
|
|
|
|
/*for (int j = 0; j < 64; j++) {
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
spi_dac(i, 64 * j);
|
|
|
|
//printf("> %d\n", 64*j);
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
}*/
|
2021-09-11 19:49:53 +00:00
|
|
|
/*gpio_write(gpio4, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
gpio_write(gpio4, 0);
|
2021-07-19 20:56:57 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
spi_dac(i, 0);
|
|
|
|
|
|
|
|
gpio_write(gpio5, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
2021-09-11 19:49:53 +00:00
|
|
|
gpio_write(gpio5, 0);*/
|
|
|
|
signal_led(0);
|
2021-07-19 20:56:57 +00:00
|
|
|
for (int c = 8; c >= 0; c--) {
|
|
|
|
|
2021-09-09 22:09:03 +00:00
|
|
|
for (auto &pixel: pixels) {
|
|
|
|
pixel = next_colour(c);
|
|
|
|
}
|
|
|
|
for (auto &led: leds) {
|
|
|
|
led = next_color(c);
|
2021-07-19 20:56:57 +00:00
|
|
|
}
|
|
|
|
ws2812_i2s_update(pixels, PIXEL_RGBW);
|
2021-09-09 22:09:03 +00:00
|
|
|
write_leds(leds, lux_apa10xx_number);
|
2021-07-19 20:56:57 +00:00
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
}
|
2021-09-11 19:49:53 +00:00
|
|
|
gpio_write(gpio4, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
gpio_write(gpio4, 0);
|
|
|
|
gpio_write(gpio5, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
gpio_write(gpio5, 0);
|
|
|
|
signal_led(1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2021-07-19 20:56:57 +00:00
|
|
|
}
|
2021-07-19 20:31:30 +00:00
|
|
|
}
|