WSLed/src/wsLED.cpp

250 lines
6.7 KiB
C++
Raw Normal View History

2020-04-04 09:52:53 +02:00
// Ovladani signalizacni chytre LED diody
#include "wsLED.h"
2020-09-04 11:21:26 +02:00
#if defined(ARDUINO_ARCH_ESP32)
2020-08-18 13:01:44 +02:00
//# include "esp_task.h"
2020-09-04 11:21:26 +02:00
// https://github.com/JSchaenzle/ESP32-NeoPixel-WS2812-RMT/blob/master/ws2812_control.c
# include <set>
# ifndef LED_RMT_TX_CHANNEL
# define LED_RMT_TX_CHANNEL RMT_CHANNEL_0
# endif
// These values are determined by measuring pulse timing with logic analyzer and adjusting to match datasheet.
# define T0H 14 // 0 bit high time
# define T1H 52 // 1 bit high time
# define TL 52 // low time for either bit
#endif
#if defined(ARDUINO_ARCH_ESP8266)
2020-04-04 09:52:53 +02:00
// due to linker overriding the ICACHE_RAM_ATTR for cpp files, these methods are
// moved into a C file so the attribute will be applied correctly
extern "C" void ICACHE_RAM_ATTR send_pixels_800(uint8_t* pixel, size_t count, uint8_t pin);
2020-09-04 11:21:26 +02:00
#endif
// TODO: Proverit ESP32 s https://github.com/adafruit/Adafruit_NeoPixel/blob/master/esp.c
// ... a s https://github.com/Freenove/Freenove_WS2812_Lib_for_ESP32
2020-04-04 09:52:53 +02:00
#define countof(a) (sizeof(a) / sizeof(a[0]))
static const uint16_t LB[] PROGMEM = {
0x201 , 0x202 , 0x103 , 0x104 , 0x105 , 0x106 , 0x107 , 0x109 , 0x10a , 0x10c , 0x10e , 0x110 , 0x112 , 0x114 , 0x116 , 0x119 , 0x11c , 0x11e , 0x122 , 0x125 , 0x128 , 0x12c , 0x130 , 0x134,
0x139 , 0x13d , 0x142 , 0x147 , 0x14d , 0x152 , 0x158 , 0x15e , 0x165 , 0x16b , 0x172 , 0x179 , 0x180 , 0x187 , 0x18e , 0x196 , 0x19d , 0x1a5 , 0x1ac , 0x1b3 , 0x1bb , 0x1c2 , 0x1c9 , 0x1cf,
0x1d6 , 0x1dc , 0x1e2 , 0x1e7 , 0x1ec , 0x1f0 , 0x1f4 , 0x1f7 , 0x1fa , 0x1fc , 0x1fd , 0x3fe , 0x1fd , 0x1fb , 0x1f8 , 0x1f5 , 0x1f2 , 0x1ed , 0x1e9 , 0x1e4 , 0x1de , 0x1d8 , 0x1d2 , 0x1cb,
0x1c4 , 0x1bd , 0x1b6 , 0x1af , 0x1a7 , 0x1a0 , 0x198 , 0x191 , 0x18a , 0x182 , 0x17b , 0x174 , 0x16d , 0x167 , 0x161 , 0x15a , 0x154 , 0x14f , 0x149 , 0x144 , 0x13f , 0x13a , 0x136 , 0x131,
2021-08-03 08:52:55 +02:00
0x12d , 0x12a , 0x126 , 0x123 , 0x11f , 0x11c , 0x11a , 0x117 , 0x115 , 0x112 , 0x110 , 0x10e , 0x10c , 0x10b , 0x109 , 0x108 , 0x107 , 0x105 , 0x104 , 0x203 , 0x102 , 0x201 , 0x2900
2020-04-04 09:52:53 +02:00
};
2021-08-02 13:24:36 +02:00
static void ICACHE_RAM_ATTR ledTrampoline(wsLED *ptr)
2020-04-04 09:52:53 +02:00
{
2021-08-02 13:24:36 +02:00
ptr->rtLed();
2020-04-04 09:52:53 +02:00
}
void wsLED::show(void)
{
uint8_t color[3];
for (uint8_t i = 0; i < 3; i++) {
// https://github.com/FastLED/FastLED/wiki/FastLED-Color-Correction
uint16_t c = (uint16_t)_color[i] * (1 + (uint16_t)_scale);
color[i] = (uint8_t)(c >> 8);
}
#if defined(ARDUINO_ARCH_ESP32)
2020-09-04 11:21:26 +02:00
uint32_t bits_to_send = ((uint32_t)color[0] << 16) | ((uint32_t)color[1] << 8) | (uint32_t)color[2];
uint32_t mask = 1 << (BITS_PER_LED_CMD - 1);
for (uint32_t bit = 0; bit < BITS_PER_LED_CMD; bit++) {
uint32_t bit_is_set = bits_to_send & mask;
2021-08-03 08:07:41 +02:00
led_data_buffer[bit] = bit_is_set ? (rmt_item32_t) {{{T1H, 1, TL, 0}}} : (rmt_item32_t) {{{T0H, 1, TL, 0}}};
2020-09-04 11:21:26 +02:00
mask >>= 1;
}
ESP_ERROR_CHECK(rmt_write_items(LED_RMT_TX_CHANNEL, led_data_buffer, LED_BUFFER_ITEMS, false));
2021-08-03 08:07:41 +02:00
// ESP_ERROR_CHECK(rmt_wait_tx_done(LED_RMT_TX_CHANNEL, portMAX_DELAY));
2020-04-04 09:52:53 +02:00
#else
noInterrupts();
send_pixels_800(color, 1, _pin);
interrupts();
#endif
}
wsLED::wsLED(int pin, int order) : _pin(pin), _order(order), _ledState(LED_STOP)
2020-04-04 09:52:53 +02:00
{
}
wsLED::wsLED(void) : _pin(NOT_A_PIN), _order(RGB), _ledState(LED_STOP)
{
}
void wsLED::begin(int pin)
{
_pin = pin;
begin();
2020-04-04 09:52:53 +02:00
}
void wsLED::begin(void)
{
if (NOT_A_PIN != _pin) {
2020-09-04 11:21:26 +02:00
#if defined(ARDUINO_ARCH_ESP8266)
pinMode(_pin, OUTPUT);
2020-09-04 11:21:26 +02:00
#endif
#if defined(ARDUINO_ARCH_ESP32)
// TODO: mozna nejak zkusit najit volny RMT kanal?
rmt_config_t config;
config.rmt_mode = RMT_MODE_TX;
config.channel = LED_RMT_TX_CHANNEL;
config.gpio_num = (gpio_num_t)_pin;
config.mem_block_num = 3;
config.tx_config.loop_en = false;
config.tx_config.carrier_en = false;
config.tx_config.idle_output_en = true;
config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
config.clk_div = 2;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
2020-09-04 11:21:26 +02:00
#endif
_color[0] = _color[1] = _color[2] = 0;
show();
}
2020-04-04 09:52:53 +02:00
}
2021-07-16 11:30:25 +02:00
void wsLED::setColor(LEDRGB color)
2020-04-04 09:52:53 +02:00
{
_color[(_order >> 6) & 7] = color.r;
_color[(_order >> 3) & 7] = color.g;
_color[_order & 7] = color.b;
}
void ICACHE_RAM_ATTR wsLED::rtLed(void)
{
2021-08-03 08:07:41 +02:00
uint32_t timing;
2020-04-04 09:52:53 +02:00
2021-08-02 13:24:36 +02:00
_handler.detach();
2020-04-04 09:52:53 +02:00
switch (_ledState) {
case LED_BLINK:
if (_subState) {
setColor(_color2);
} else {
setColor(_color1);
}
2021-08-02 13:24:36 +02:00
_subState = !_subState;
2021-08-03 08:07:41 +02:00
timing = _blinkSpeed;
2020-04-04 09:52:53 +02:00
break;
case LED_BREATH: {
++_subState;
_subState %= countof(LB);
uint16_t val = pgm_read_word(&LB[_subState]);
_scale = (uint8_t)val;
val >>= 8;
2021-08-03 08:07:41 +02:00
timing = 30ul * val;
if (timing < 30) {
timing = 30;
}
timing = 30;
2020-04-04 09:52:53 +02:00
break;
}
case LED_PULSE: {
_ledState = _savedState;
2021-08-02 11:06:13 +02:00
_subState = 0;
setColor(_color1);
2021-08-03 08:07:41 +02:00
timing = 5;
break;
}
2021-08-02 11:06:13 +02:00
case LED_PULSE_START:
setColor(_pulseColor);
_ledState = LED_PULSE;
2021-08-03 08:07:41 +02:00
timing = _pulseDuration;
2021-08-02 11:06:13 +02:00
break;
2020-04-04 09:52:53 +02:00
default:
2021-08-03 08:07:41 +02:00
timing = 5000;
2020-04-04 09:52:53 +02:00
break;
}
2021-08-02 13:24:36 +02:00
show();
2021-08-03 08:07:41 +02:00
_handler.attach_ms(timing, ledTrampoline, this);
2020-04-04 09:52:53 +02:00
}
void wsLED::setColors(LEDRGB color1, LEDRGB color2)
{
_color1 = color1;
_color2 = color2;
if (LED_BREATH == _ledState) {
setColor(_color1);
}
2020-04-04 09:52:53 +02:00
}
2021-08-02 11:06:13 +02:00
void wsLED::blink(uint32_t speed)
2020-04-04 09:52:53 +02:00
{
if (NOT_A_PIN != _pin) {
if (LED_BLINK != _ledState) {
_handler.detach();
_ledState = LED_BLINK;
_scale = 0xff;
_subState = 1;
2021-08-02 11:06:13 +02:00
_blinkSpeed = speed;
2021-08-02 13:24:36 +02:00
_handler.attach_ms(5, ledTrampoline, this);
}
}
2020-04-04 09:52:53 +02:00
}
void wsLED::breath(void)
{
if (NOT_A_PIN != _pin) {
if (LED_BREATH != _ledState) {
_handler.detach();
_ledState = LED_BREATH;
_subState = 0;
setColor(_color1);
2021-08-02 13:24:36 +02:00
_handler.attach_ms(5, ledTrampoline, this);
}
}
2020-04-04 09:52:53 +02:00
}
void wsLED::setOrder(int neworder)
{
_order = neworder;
}
2020-09-04 11:21:26 +02:00
void wsLED::pulse(LEDRGB color, uint32_t duration)
{
if (NOT_A_PIN != _pin) {
_handler.detach();
_scale = 0xff;
2021-08-02 11:06:13 +02:00
_pulseColor = color;
2021-08-03 08:07:41 +02:00
if (LED_BREATH == _ledState || LED_BLINK == _ledState) {
_savedState = _ledState;
}
2021-08-02 11:06:13 +02:00
_pulseDuration = duration;
_ledState = LED_PULSE_START;
2021-08-02 13:24:36 +02:00
_handler.attach_ms(5, ledTrampoline, this);
}
}
ledstate_t wsLED::getState(void)
{
2020-09-04 11:21:26 +02:00
return _ledState;
}
2021-07-16 11:30:25 +02:00
void wsLED::stop(void)
{
if (NOT_A_PIN != _pin) {
_handler.detach();
_ledState = LED_STOP;
_scale = 0xff;
setColor(LEDRGB(0, 0, 0));
show();
}
}