diff --git a/firmware/lux.cpp b/firmware/lux.cpp index d6a1397..874f151 100644 --- a/firmware/lux.cpp +++ b/firmware/lux.cpp @@ -17,12 +17,11 @@ extern "C" { const int signal_led_pin = 2; -//const int cs0 = 15; +const int cs0 = 15; const int gpio4 = 4; const int gpio5 = 5; - struct apa10xx_pixel_t { struct { unsigned int mod: 5, marker: 3; @@ -55,7 +54,6 @@ static apa10xx_pixel_t next_color(int i) { return colour; } -/* void spi_dac(int id, int val) { int dac_val = (val << 2) & 0x3FFC; @@ -75,7 +73,6 @@ void spi_dac(int id, int val) { gpio_write(cs0, 1); gpio_write(cs0, 0); } -*/ void write_leds(apa10xx_pixel_t *arr, size_t len) { spi_transfer_32(1, 0x00000000); @@ -85,11 +82,114 @@ void write_leds(apa10xx_pixel_t *arr, size_t len) { spi_transfer_32(1, 0xFFFFFFFF); } +namespace fiatlux { + + namespace signal { + void write_data(bool data) { + gpio_write(signal_led_pin, !data); + } + } + + namespace relais { + void write_data(uint8_t data[2]) { + gpio_write(gpio4, (bool) data[0]); + gpio_write(gpio5, (bool) data[1]); + } + } + + namespace spi_dimmer { + 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, 1); + gpio_write(cs0, 0); + spi_transfer_8(1, ~(0x01 << i)); + 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 << i)); + gpio_write(cs0, 1); + gpio_write(cs0, 0); + } + } + } + + namespace ws28x { + void write_data(ws2812_pixel_t *data, size_t len) { + ws2812_i2s_init(len, PIXEL_RGBW); + ws2812_i2s_update(data, 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, 0xFFFFFFFF); + spi_transfer_32(1, 0xFFFFFFFF); + } + } + + 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 { + SIGNAL, RELAIS, SPI_DIMMER, WS28X, APA10X + }; + + 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, 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"; + } + } + +} ws2812_pixel_t **pixels_ptr; extern "C" void signal_led(bool state) { - gpio_write(signal_led_pin, !state); + fiatlux::signal::write_data(state); }