fiatlux/firmware/lux.cpp

305 lines
8.7 KiB
C++
Raw Permalink Normal View History

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-09-11 19:49:53 +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}};
2021-09-12 02:32:05 +00:00
colour.red = 0;
colour.green = 0;
colour.blue = 0;
colour.white = 8;
2021-07-19 20:56:57 +00:00
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;
2021-09-12 02:32:05 +00:00
colour.r = 16;
colour.g = 16;
colour.b = 16;
2021-09-09 22:09:03 +00:00
return colour;
}
2021-11-03 00:49:07 +00:00
namespace fiatlux {
2021-11-18 00:45:35 +00:00
struct hal_error_t {
constexpr hal_error_t() = default;
hal_error_t(const char *) {}
hal_error_t(const char *, hal_error_t *cause) {}
};
constexpr hal_error_t empty_error;
enum class hal_module_t {
NONE, SIGNAL, RELAIS, SPI_DIMMER, WS28X, APA10X
};
namespace ports {
hal_module_t spi = hal_module_t::NONE;
hal_module_t uart = hal_module_t::NONE;
hal_module_t gpio2 = hal_module_t::NONE;
hal_module_t gpio4 = hal_module_t::NONE;
hal_module_t gpio5 = hal_module_t::NONE;
2021-11-18 02:31:26 +00:00
hal_module_t gpio15 = hal_module_t::NONE;
2021-11-18 00:45:35 +00:00
}
2021-11-03 00:49:07 +00:00
namespace signal {
void write_data(bool data) {
2021-11-18 00:45:35 +00:00
gpio_write(2, !data);
}
void setup() {
gpio_enable(2, GPIO_OUTPUT);
2021-11-03 00:49:07 +00:00
}
}
namespace relais {
2021-11-18 00:45:35 +00:00
void write_data(bool a, bool b) {
gpio_write(4, a);
gpio_write(5, b);
}
2021-11-03 00:49:07 +00:00
void write_data(uint8_t data[2]) {
2021-11-18 00:45:35 +00:00
write_data((bool) data[0], (bool) data[1]);
}
void setup() {
gpio_enable(4, GPIO_OUTPUT);
gpio_enable(5, GPIO_OUTPUT);
2021-11-03 00:49:07 +00:00
}
}
namespace spi_dimmer {
2021-11-18 02:31:26 +00:00
constexpr int cs0 = 15;
2021-11-03 00:49:07 +00:00
void write_data(uint16_t data[6]) {
for (int i = 0; i < 6; ++i) {
int dac_val = (data[i] << 2) & 0x3FFC;
spi_transfer_8(1, ~(0x00));
2021-11-18 02:31:26 +00:00
gpio_write(cs0, true);
gpio_write(cs0, false);
2021-11-03 00:49:07 +00:00
spi_transfer_8(1, ~(0x01 << i));
2021-11-18 02:31:26 +00:00
gpio_write(cs0, true);
gpio_write(cs0, false);
2021-11-03 00:49:07 +00:00
spi_transfer_16(1, dac_val);
spi_transfer_8(1, ~(0x00));
2021-11-18 02:31:26 +00:00
gpio_write(cs0, true);
gpio_write(cs0, false);
2021-11-03 00:49:07 +00:00
spi_transfer_8(1, ~(0x01 << i));
2021-11-18 02:31:26 +00:00
gpio_write(cs0, true);
gpio_write(cs0, false);
2021-11-03 00:49:07 +00:00
}
}
2021-11-18 00:45:35 +00:00
void setup() {
2021-11-18 02:31:26 +00:00
gpio_enable(cs0, GPIO_OUTPUT);
2021-11-18 00:45:35 +00:00
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
}
2021-11-03 00:49:07 +00:00
}
namespace ws28x {
2021-11-18 02:31:26 +00:00
void write_data(ws2812_pixel_t *data) {
2021-11-03 00:49:07 +00:00
ws2812_i2s_update(data, PIXEL_RGBW);
}
2021-11-18 00:45:35 +00:00
2021-11-18 02:31:26 +00:00
void setup(size_t len) {
ws2812_i2s_init(len, PIXEL_RGBW);
2021-11-18 00:45:35 +00:00
}
2021-11-03 00:49:07 +00:00
}
namespace apa10x {
void write_data(apa10xx_pixel_t *data, size_t len) {
spi_transfer_32(1, 0x00000000);
for (size_t i = 0; i < len; i++)
spi_transfer_32(1, *(uint32_t *) &data[i]);
2021-11-18 02:31:26 +00:00
//spi_transfer_32(1, *(uint32_t *) &data[len - 1]); // dunno maybe this helps
//spi_transfer_32(1, *(uint32_t *) &data[len - 1]); // dunno maybe this helps
2021-11-03 00:49:07 +00:00
spi_transfer_32(1, 0xFFFFFFFF);
spi_transfer_32(1, 0xFFFFFFFF);
}
2021-11-18 00:45:35 +00:00
void setup() {
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
}
}
2021-11-03 00:49:07 +00:00
hal_error_t write_channel(uint8_t *data, size_t count, size_t stride, hal_module_t mod) {
if(mod == hal_module_t::SIGNAL) {
if(count != 1)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
signal::write_data(data[0]);
} else if(mod == hal_module_t::RELAIS) {
if(count != 2)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
relais::write_data(data);
} else if(mod == hal_module_t::SPI_DIMMER) {
if(count != 6)
return "unsupported value for count";
if(stride != 2)
return "unsupported value for stride";
spi_dimmer::write_data((uint16_t *) data);
} else if(mod == hal_module_t::WS28X) {
if(stride != 4)
return "unsupported value for stride";
2021-11-18 02:31:26 +00:00
ws28x::write_data((ws2812_pixel_t *) data);
2021-11-03 00:49:07 +00:00
} else if(mod == hal_module_t::APA10X) {
if(stride != 4)
return "unsupported value for stride";
apa10x::write_data((apa10xx_pixel_t *) data, count);
} else {
return "unsupported module";
}
2021-11-18 00:45:35 +00:00
return empty_error;
2021-11-03 00:49:07 +00:00
}
2021-11-04 00:23:01 +00:00
hal_error_t setup_channel(size_t count, size_t stride, hal_module_t mod) {
/*if(mod == hal_module_t::SIGNAL) {
if(count != 1)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
signal::write_data(data[0]);
} else if(mod == hal_module_t::RELAIS) {
if(count != 2)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
relais::write_data(data);
} else if(mod == hal_module_t::SPI_DIMMER) {
if(count != 6)
return "unsupported value for count";
if(stride != 2)
return "unsupported value for stride";
spi_dimmer::write_data((uint16_t *) data);
} else if(mod == hal_module_t::WS28X) {
if(stride != 4)
return "unsupported value for stride";
ws28x::write_data((ws2812_pixel_t *) data, count);
} else if(mod == hal_module_t::APA10X) {
if(stride != 4)
return "unsupported value for stride";
apa10x::write_data((apa10xx_pixel_t *) data, count);
} else {
return "unsupported module";
2021-11-18 00:45:35 +00:00
}*/
return empty_error;
2021-11-04 00:23:01 +00:00
}
2021-11-03 00:49:07 +00:00
}
2021-09-09 22:09:03 +00:00
2021-11-18 00:45:35 +00:00
//ws2812_pixel_t **pixels_ptr;
2021-09-09 22:09:03 +00:00
2021-07-19 20:31:30 +00:00
extern "C" void signal_led(bool state) {
2021-11-03 00:49:07 +00:00
fiatlux::signal::write_data(state);
2021-07-19 20:31:30 +00:00
}
2021-09-12 02:32:05 +00:00
extern "C" void white_led(bool state) {
gpio_write(signal_led_pin, !state);
}
2021-07-19 20:56:57 +00:00
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
*
*/
2021-11-18 00:45:35 +00:00
extern "C" [[noreturn]] void lux_task(void *pvParameters) {
2021-09-09 22:09:03 +00:00
2021-09-12 02:32:05 +00:00
int32_t lux_ws2812_number = 40;
2021-09-09 22:09:03 +00:00
auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number);
if(ret != SYSPARAM_OK)
2021-09-12 02:32:05 +00:00
lux_ws2812_number = 40;
2021-09-09 22:09:03 +00:00
2021-11-18 02:31:26 +00:00
int32_t lux_apa10xx_number = 40;
2021-09-09 22:09:03 +00:00
ret = sysparam_get_int32("lux_apa10xx_number", &lux_apa10xx_number);
if(ret != SYSPARAM_OK)
2021-11-18 02:31:26 +00:00
lux_apa10xx_number = 40;
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-11-18 00:45:35 +00:00
//gpio_enable(9, GPIO_INPUT);
//gpio_enable(10, GPIO_INPUT);
//fiatlux::spi_dimmer::setup();
fiatlux::signal::setup();
fiatlux::relais::setup();
2021-09-09 22:09:03 +00:00
2021-11-18 00:45:35 +00:00
fiatlux::apa10x::setup();
while (true) {
2021-07-19 20:56:57 +00:00
/*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);*/
2021-11-18 00:45:35 +00:00
fiatlux::signal::write_data(false);
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-11-18 00:45:35 +00:00
fiatlux::write_channel((uint8_t *) &leds[0], lux_apa10xx_number, 4, fiatlux::hal_module_t::APA10X);
2021-07-19 20:56:57 +00:00
vTaskDelay(200 / portTICK_PERIOD_MS);
}
2021-11-18 00:45:35 +00:00
fiatlux::relais::write_data(true, false);
2021-09-11 19:49:53 +00:00
vTaskDelay(200 / portTICK_PERIOD_MS);
2021-11-18 00:45:35 +00:00
fiatlux::relais::write_data(false, true);
2021-09-11 19:49:53 +00:00
vTaskDelay(200 / portTICK_PERIOD_MS);
2021-11-18 00:45:35 +00:00
fiatlux::relais::write_data(false, false);
fiatlux::signal::write_data(true);
2021-09-11 19:49:53 +00:00
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
}