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"
|
2022-09-24 05:48:47 +00:00
|
|
|
#include "log.h"
|
2021-07-19 20:31:30 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
|
|
|
|
|
|
|
#include <esp/spi.h>
|
2022-09-22 19:48:36 +00:00
|
|
|
#include <ws2812_i2s/ws2812_i2s.h>
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <sysparam.h>
|
|
|
|
}
|
|
|
|
|
2022-09-24 05:48:47 +00:00
|
|
|
const auto htons = [](uint16_t x) -> uint16_t {
|
|
|
|
return ((x) << 8 & 0xFF00) | ((x) >> 8 & 0x00FF);
|
|
|
|
};
|
|
|
|
const auto &ntohs = htons;
|
|
|
|
|
|
|
|
const auto htonl = [](uint32_t x) -> uint32_t {
|
|
|
|
return ((x) << 24 & 0xFF000000UL) | ((x) << 8 & 0x00FF0000UL) | ((x) >> 8 & 0x0000FF00UL) |
|
|
|
|
((x) >> 24 & 0x000000FFUL);
|
|
|
|
};
|
|
|
|
const auto &ntohl = htonl;
|
|
|
|
|
2022-09-22 19:48:36 +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;
|
|
|
|
};
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
volatile rgba_t top_color;
|
|
|
|
volatile rgba_t bottom_color;
|
2023-01-19 06:07:24 +00:00
|
|
|
volatile uint16_t light_value;
|
2022-09-22 19:48:36 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
volatile uint16_t peripheral_version;
|
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
namespace fiatlux {
|
|
|
|
|
|
|
|
struct hal_error_t {
|
|
|
|
constexpr hal_error_t() = default;
|
|
|
|
|
|
|
|
hal_error_t(const char *) {}
|
|
|
|
|
|
|
|
hal_error_t(const char *, hal_error_t *cause) {}
|
|
|
|
};
|
|
|
|
|
2022-09-24 05:48:47 +00:00
|
|
|
constexpr hal_error_t
|
|
|
|
empty_error;
|
2022-09-22 19:48:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
hal_module_t gpio15 = hal_module_t::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace signal {
|
|
|
|
void write_data(bool data) {
|
|
|
|
gpio_write(2, !data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
gpio_enable(2, GPIO_OUTPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace relais {
|
|
|
|
void write_data(bool a, bool b) {
|
|
|
|
gpio_write(4, a);
|
|
|
|
gpio_write(5, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_data(uint8_t data[2]) {
|
|
|
|
write_data((bool) data[0], (bool) data[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
gpio_enable(4, GPIO_OUTPUT);
|
|
|
|
gpio_enable(5, GPIO_OUTPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace spi_dimmer {
|
|
|
|
constexpr int cs0 = 15;
|
|
|
|
|
|
|
|
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));
|
|
|
|
gpio_write(cs0, true);
|
|
|
|
gpio_write(cs0, false);
|
|
|
|
spi_transfer_8(1, ~(0x01 << i));
|
|
|
|
gpio_write(cs0, true);
|
|
|
|
gpio_write(cs0, false);
|
|
|
|
|
|
|
|
spi_transfer_16(1, dac_val);
|
|
|
|
|
|
|
|
spi_transfer_8(1, ~(0x00));
|
|
|
|
gpio_write(cs0, true);
|
|
|
|
gpio_write(cs0, false);
|
|
|
|
spi_transfer_8(1, ~(0x01 << i));
|
|
|
|
gpio_write(cs0, true);
|
|
|
|
gpio_write(cs0, false);
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
void setup() {
|
|
|
|
gpio_enable(cs0, GPIO_OUTPUT);
|
|
|
|
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ws28x {
|
|
|
|
void write_data(ws2812_pixel_t *data) {
|
|
|
|
ws2812_i2s_update(data, PIXEL_RGBW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup(size_t len) {
|
|
|
|
ws2812_i2s_init(len, PIXEL_RGBW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
//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
|
|
|
|
spi_transfer_32(1, 0xFFFFFFFF);
|
|
|
|
spi_transfer_32(1, 0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
ws28x::write_data((ws2812_pixel_t *) data);
|
|
|
|
} 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";
|
|
|
|
}
|
|
|
|
return empty_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}*/
|
|
|
|
return empty_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//ws2812_pixel_t **pixels_ptr;
|
2021-07-19 20:31:30 +00:00
|
|
|
|
|
|
|
extern "C" void signal_led(bool state) {
|
2022-09-22 19:48:36 +00:00
|
|
|
fiatlux::signal::write_data(state);
|
2021-07-19 20:31:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
uint32_t spi_transfer_32_duplex(uint8_t bus, uint32_t val) {
|
|
|
|
uint32_t out = val;
|
|
|
|
uint32_t in;
|
|
|
|
spi_transfer(bus, &out, &in, 1, SPI_32BIT);
|
|
|
|
return in;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val) {
|
|
|
|
uint16_t out = val;
|
|
|
|
uint16_t in;
|
|
|
|
spi_transfer(bus, &out, &in, 1, SPI_16BIT);
|
|
|
|
return in;
|
|
|
|
};
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
void reset_bus() {
|
|
|
|
gpio_write(4, false);
|
|
|
|
for (volatile int k = 0; k < 128; ++k);
|
|
|
|
gpio_write(4, true);
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
extern "C" [[noreturn]] void lux_task(void *pvParameters) {
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
fiatlux::signal::setup();
|
|
|
|
fiatlux::relais::setup();
|
|
|
|
|
|
|
|
gpio_write(4, false);
|
|
|
|
for (volatile int k = 0; k < 512 * 2; ++k);
|
|
|
|
gpio_write(4, true);
|
|
|
|
gpio_enable(4, GPIO_OUT_OPEN_DRAIN);
|
|
|
|
gpio_write(4, true);
|
|
|
|
for (volatile int k = 0; k < 512 * 4; ++k);
|
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
int32_t lux_ws2812_number = 240;
|
|
|
|
auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number);
|
|
|
|
if(ret != SYSPARAM_OK)
|
|
|
|
lux_ws2812_number = 240;
|
|
|
|
|
|
|
|
int32_t lux_apa10xx_number = 40;
|
|
|
|
ret = sysparam_get_int32("lux_apa10xx_number", &lux_apa10xx_number);
|
|
|
|
if(ret != SYSPARAM_OK)
|
|
|
|
lux_apa10xx_number = 40;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
//apa10xx_pixel_t leds[lux_apa10xx_number]; // TODO
|
2022-09-22 19:48:36 +00:00
|
|
|
|
|
|
|
//lux_apa102c_number
|
|
|
|
|
|
|
|
//gpio_enable(9, GPIO_INPUT);
|
|
|
|
//gpio_enable(10, GPIO_INPUT);
|
|
|
|
|
|
|
|
//fiatlux::spi_dimmer::setup();
|
|
|
|
|
2022-09-24 05:48:47 +00:00
|
|
|
//fiatlux::apa10x::setup();
|
2023-01-19 06:07:24 +00:00
|
|
|
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_2M, true, SPI_BIG_ENDIAN, false);
|
2022-09-24 05:48:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
CC48 API:
|
|
|
|
- 12 x 16bit registers
|
|
|
|
- [2*n] = 12bit constant current value
|
|
|
|
- [2*n+1] = 8bit PWM value
|
|
|
|
*/
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
2023-01-19 06:07:24 +00:00
|
|
|
unsigned dat: 12;
|
|
|
|
unsigned addr: 4;
|
2022-09-24 05:48:47 +00:00
|
|
|
} __attribute__((packed));
|
2023-01-19 06:07:24 +00:00
|
|
|
uint16_t _;
|
2022-09-24 05:48:47 +00:00
|
|
|
} frame = {._=0};
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
{
|
|
|
|
frame.addr = 15;
|
|
|
|
frame.dat = 0;
|
|
|
|
spi_transfer_16_duplex(1, frame._);
|
|
|
|
for (volatile int k = 0; k < 512 * 2; ++k);
|
|
|
|
uint16_t reply = spi_transfer_16_duplex(1, 0x0000);
|
|
|
|
peripheral_version = reply;
|
|
|
|
for (volatile int k = 0; k < 512 * 2; ++k);
|
|
|
|
syslog("peripheral_version: ");
|
|
|
|
syslog_i32(peripheral_version);
|
|
|
|
syslog("\n");
|
|
|
|
}
|
2022-09-24 05:48:47 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
light_value = 0xFFF;
|
2023-01-19 06:07:24 +00:00
|
|
|
uint16_t last_light_value = 0xFFFF;
|
2022-09-22 19:48:36 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
fiatlux::signal::write_data(false);
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
if(last_light_value != light_value) {
|
|
|
|
|
|
|
|
syslog("# ");
|
|
|
|
syslog_i32(light_value);
|
|
|
|
syslog("\n");
|
2022-09-22 19:48:36 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
uint16_t last_frame = 0x0000;
|
|
|
|
uint16_t reply = 0x0000;
|
|
|
|
uint8_t error_count = 0;
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
2022-09-24 06:27:44 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
frame.addr = i + 1;
|
2023-01-19 06:07:24 +00:00
|
|
|
frame.dat = light_value;
|
2023-01-19 17:11:13 +00:00
|
|
|
reply = spi_transfer_16_duplex(1, frame._);
|
|
|
|
|
|
|
|
if(reply != last_frame)
|
|
|
|
error_count++;
|
2022-09-24 06:27:44 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
{
|
2023-01-19 06:07:24 +00:00
|
|
|
syslog(" > ");
|
|
|
|
syslog_i32(frame._);
|
2023-01-19 17:11:13 +00:00
|
|
|
syslog(" < ");
|
|
|
|
syslog_i32(reply);
|
|
|
|
syslog(" -- ");
|
|
|
|
syslog_i32(last_frame);
|
2023-01-19 06:07:24 +00:00
|
|
|
syslog("\n");
|
2023-01-19 17:11:13 +00:00
|
|
|
}
|
2022-09-24 06:27:44 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
last_frame = frame._;
|
|
|
|
|
|
|
|
for (volatile int k = 0; k < 512 * 2; ++k);
|
2022-09-24 06:27:44 +00:00
|
|
|
}
|
2022-09-22 19:48:36 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
reply = spi_transfer_16_duplex(1, 0x0000);
|
|
|
|
if(reply != last_frame)
|
|
|
|
error_count++;
|
|
|
|
|
|
|
|
{
|
|
|
|
syslog(" > ");
|
|
|
|
syslog_i32(frame._);
|
|
|
|
syslog(" < ");
|
|
|
|
syslog_i32(reply);
|
|
|
|
syslog(" -- ");
|
|
|
|
syslog_i32(last_frame);
|
|
|
|
syslog("\n");
|
|
|
|
}
|
2023-01-19 06:07:24 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
if(!error_count) {
|
|
|
|
last_light_value = light_value;
|
|
|
|
} else {
|
|
|
|
syslog("reset_bus\n");
|
|
|
|
reset_bus();
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
2022-09-22 19:48:36 +00:00
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
for (int i = 0; i < 120; i++)
|
|
|
|
pixels[i] = {{top_color.r, top_color.g, top_color.b, top_color.a}};
|
2022-09-22 19:48:36 +00:00
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
for (int i = 120; i < 240; i++)
|
|
|
|
pixels[i] = {{bottom_color.r, bottom_color.g, bottom_color.b, bottom_color.a}};
|
|
|
|
|
|
|
|
ws2812_i2s_update(pixels, PIXEL_RGBW);
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
/*//fiatlux::write_channel((uint8_t *) &leds[0], lux_apa10xx_number, 4, fiatlux::hal_module_t::APA10X);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
2022-09-22 19:48:36 +00:00
|
|
|
fiatlux::relais::write_data(true, false);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
fiatlux::relais::write_data(false, true);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
fiatlux::relais::write_data(false, false);
|
|
|
|
fiatlux::signal::write_data(true);
|
2023-01-19 06:07:24 +00:00
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);*/
|
2021-07-19 20:31:30 +00:00
|
|
|
|
2022-09-24 05:48:47 +00:00
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
}
|
2021-07-19 20:31:30 +00:00
|
|
|
}
|