Funkcni i pro ESP32-C3, modifikovane casovani, odstraneny mutex, opravena inicializace promennych

This commit is contained in:
Pavel Brychta 2022-10-25 11:40:05 +02:00
parent db3c9fa429
commit 0908cd9825
4 changed files with 22 additions and 65 deletions

View File

@ -1,4 +1,4 @@
{ {
"name": "WSLed", "name": "WSLed",
"version": "0.0.0+20210128092328" "version": "0.0.2"
} }

View File

@ -24,17 +24,15 @@ License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>. <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/ -------------------------------------------------------------------------*/
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP8266)
#include <Arduino.h> #include <Arduino.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <eagle_soc.h> #include <eagle_soc.h>
#if (ARDUINO_ESP8266_MAJOR < 3) #if (ARDUINO_ESP8266_MAJOR < 3)
# define INRAM ICACHE_RAM_ATTR # define INRAM ICACHE_RAM_ATTR
#else #else
# define INRAM IRAM_ATTR # define INRAM IRAM_ATTR
#endif #endif
#endif
// ESP32 doesn't define ICACHE_RAM_ATTR // ESP32 doesn't define ICACHE_RAM_ATTR
#ifndef INRAM #ifndef INRAM
@ -87,11 +85,7 @@ void INRAM send_pixels_800(uint8_t* pixels, size_t count, uint8_t pin)
} while ((cyclesStart - cyclesNext) < CYCLES_800); } while ((cyclesStart - cyclesNext) < CYCLES_800);
// set high // set high
#if defined(ARDUINO_ARCH_ESP32)
GPIO.out_w1ts = pinRegister;
#else
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister);
#endif
// wait for the LOW // wait for the LOW
do do
@ -100,11 +94,7 @@ void INRAM send_pixels_800(uint8_t* pixels, size_t count, uint8_t pin)
} while ((cyclesNext - cyclesStart) < cyclesBit); } while ((cyclesNext - cyclesStart) < cyclesBit);
// set low // set low
#if defined(ARDUINO_ARCH_ESP32)
GPIO.out_w1tc = pinRegister;
#else
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister);
#endif
} }
} while (pixels < end); } while (pixels < end);
} }

View File

@ -1,5 +1,5 @@
// Ovladani signalizacni chytre LED diody // Ovladani signalizacni chytre LED diody
#include "wsLED.h" #include "wsLED.hpp"
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
//# include "esp_task.h" //# include "esp_task.h"
// https://github.com/JSchaenzle/ESP32-NeoPixel-WS2812-RMT/blob/master/ws2812_control.c // https://github.com/JSchaenzle/ESP32-NeoPixel-WS2812-RMT/blob/master/ws2812_control.c
@ -8,9 +8,10 @@
# define LED_RMT_TX_CHANNEL RMT_CHANNEL_0 # define LED_RMT_TX_CHANNEL RMT_CHANNEL_0
# endif # endif
// These values are determined by measuring pulse timing with logic analyzer and adjusting to match datasheet. // These values are determined by measuring pulse timing with logic analyzer and adjusting to match datasheet.
# define T0H 14 // 0 bit high time # define T0H 16 // 0 bit high time
# define T1H 52 // 1 bit high time # define T1H 32 // 1 bit high time
# define TL 52 // low time for either bit # define T0L 34 // 0 bit low time
# define T1L 18 // 1 bit low time
# define INRAM IRAM_ATTR # define INRAM IRAM_ATTR
#endif #endif
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
@ -65,29 +66,30 @@ void wsLED::show(void)
for (uint32_t bit = 0; bit < BITS_PER_LED_CMD; bit++) { for (uint32_t bit = 0; bit < BITS_PER_LED_CMD; bit++) {
uint32_t bit_is_set = bits_to_send & mask; uint32_t bit_is_set = bits_to_send & mask;
led_data_buffer[bit] = bit_is_set ? (rmt_item32_t) {{{T1H, 1, TL, 0}}} : (rmt_item32_t) {{{T0H, 1, TL, 0}}}; led_data_buffer[bit] = bit_is_set ? (rmt_item32_t) {{{T1H, 1, T1L, 0}}} : (rmt_item32_t) {{{T0H, 1, T0L, 0}}};
mask >>= 1; mask >>= 1;
} }
ESP_ERROR_CHECK(rmt_write_items(LED_RMT_TX_CHANNEL, led_data_buffer, LED_BUFFER_ITEMS, false)); ESP_ERROR_CHECK(rmt_write_items(LED_RMT_TX_CHANNEL, led_data_buffer, LED_BUFFER_ITEMS, false));
// ESP_ERROR_CHECK(rmt_wait_tx_done(LED_RMT_TX_CHANNEL, portMAX_DELAY)); _endtime = micros() + 100; // TODO: unfortunately this time is not valid for ESP32 using RMT peripheral
#else #else
noInterrupts(); noInterrupts();
send_pixels_800(color, 1, _pin); send_pixels_800(color, 1, _pin);
interrupts(); interrupts();
#endif
_endtime = micros(); _endtime = micros();
#endif
} }
wsLED::wsLED(int pin, int order) wsLED::wsLED(int pin, int order)
: _pin(pin) : _pin(pin)
, _order(order) , _order(order)
, _scale(0xff)
, _ledState(LED_STOP) , _ledState(LED_STOP)
{} {}
wsLED::wsLED(void) wsLED::wsLED(void)
: _pin(NOT_A_PIN) : _pin(NOT_A_PIN)
, _order(RGB) , _order(RGB)
, _scale(0xff)
, _ledState(LED_STOP) , _ledState(LED_STOP)
{} {}
@ -121,8 +123,6 @@ void wsLED::begin(void)
ESP_ERROR_CHECK(rmt_config(&config)); ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
_mutex = xSemaphoreCreateMutex();
#endif #endif
_color[0] = _color[1] = _color[2] = 0; _color[0] = _color[1] = _color[2] = 0;
show(); show();
@ -141,9 +141,6 @@ void INRAM wsLED::rtLed(void)
{ {
uint32_t timing; uint32_t timing;
#if defined(ESP32)
xSemaphoreTake(_mutex, portMAX_DELAY);
#endif
_handler.detach(); _handler.detach();
switch (_ledState) { switch (_ledState) {
case LED_BLINK: case LED_BLINK:
@ -156,7 +153,6 @@ void INRAM wsLED::rtLed(void)
timing = _blinkSpeed; timing = _blinkSpeed;
break; break;
case LED_BREATH: { case LED_BREATH: {
++_subState; ++_subState;
_subState %= countof(LB); _subState %= countof(LB);
@ -191,9 +187,6 @@ void INRAM wsLED::rtLed(void)
} }
show(); show();
_handler.attach_ms(timing, ledTrampoline, this); _handler.attach_ms(timing, ledTrampoline, this);
#if defined(ESP32)
xSemaphoreGive(_mutex);
#endif
} }
void wsLED::setColors(LEDRGB color1, LEDRGB color2) void wsLED::setColors(LEDRGB color1, LEDRGB color2)
@ -211,18 +204,12 @@ void wsLED::blink(uint32_t speed)
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
if (LED_BLINK != _ledState) { if (LED_BLINK != _ledState) {
#if defined(ESP32)
xSemaphoreTake(_mutex, portMAX_DELAY);
#endif
_handler.detach(); _handler.detach();
_ledState = LED_BLINK; _ledState = LED_BLINK;
_scale = 0xff; _scale = 0xff;
_subState = 1; _subState = 1;
_blinkSpeed = speed; _blinkSpeed = speed;
_handler.attach_ms(5, ledTrampoline, this); _handler.attach_ms(5, ledTrampoline, this);
#if defined(ESP32)
xSemaphoreGive(_mutex);
#endif
} }
} }
} }
@ -232,17 +219,11 @@ void wsLED::breath(void)
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
if (LED_BREATH != _ledState) { if (LED_BREATH != _ledState) {
#if defined(ESP32)
xSemaphoreTake(_mutex, portMAX_DELAY);
#endif
_handler.detach(); _handler.detach();
_ledState = LED_BREATH; _ledState = LED_BREATH;
_subState = 0; _subState = 0;
setColor(_color1); setColor(_color1);
_handler.attach_ms(5, ledTrampoline, this); _handler.attach_ms(5, ledTrampoline, this);
#if defined(ESP32)
xSemaphoreGive(_mutex);
#endif
} }
} }
} }
@ -257,9 +238,6 @@ void wsLED::pulse(LEDRGB color, uint32_t duration)
{ {
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
#if defined(ESP32)
xSemaphoreTake(_mutex, portMAX_DELAY);
#endif
_handler.detach(); _handler.detach();
_scale = 0xff; _scale = 0xff;
_pulseColor = color; _pulseColor = color;
@ -269,9 +247,6 @@ void wsLED::pulse(LEDRGB color, uint32_t duration)
_pulseDuration = duration; _pulseDuration = duration;
_ledState = LED_PULSE_START; _ledState = LED_PULSE_START;
_handler.attach_ms(5, ledTrampoline, this); _handler.attach_ms(5, ledTrampoline, this);
#if defined(ESP32)
xSemaphoreGive(_mutex);
#endif
} }
} }
@ -281,14 +256,14 @@ ledstate_t wsLED::getState(void)
return _ledState; return _ledState;
} }
void wsLED::stop(void) void wsLED::stop(LEDRGB color)
{ {
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
_handler.detach(); _handler.detach();
_ledState = LED_STOP; _ledState = LED_STOP;
_scale = 0xff; _scale = 0xff;
setColor(LEDRGB(0, 0, 0)); setColor(color);
show(); show();
} }
} }

View File

@ -19,8 +19,7 @@ SOFTWARE.
See more at http://www.xpablo.cz See more at http://www.xpablo.cz
*/ */
#ifndef __WSLED_H__ #pragma once
#define __WSLED_H__
#include <Arduino.h> #include <Arduino.h>
#include <Ticker.h> #include <Ticker.h>
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
@ -146,16 +145,12 @@ protected:
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
int _rmtChannel; int _rmtChannel;
rmt_item32_t led_data_buffer[LED_BUFFER_ITEMS]; rmt_item32_t led_data_buffer[LED_BUFFER_ITEMS];
SemaphoreHandle_t _mutex;
#endif #endif
uint32_t _endtime; uint32_t _endtime;
void show(void); void show(void);
void setColor(LEDRGB color); void setColor(LEDRGB color);
bool canShow(void); bool canShow(void);
#if defined(ARDUINO_ARCH_ESP32)
portMUX_TYPE _mux = portMUX_INITIALIZER_UNLOCKED;
#endif
public: public:
wsLED(int pin, int order = RGB); wsLED(int pin, int order = RGB);
@ -169,8 +164,5 @@ public:
void setOrder(int neworder); void setOrder(int neworder);
ledstate_t getState(void); ledstate_t getState(void);
void pulse(LEDRGB color, uint32_t duration); void pulse(LEDRGB color, uint32_t duration);
void stop(void); void stop(LEDRGB color = LEDRGB(0, 0, 0));
}; };
#endif