new static effect with range select, increment, and transition time

This commit is contained in:
Martin Bauer 2024-01-01 18:24:55 +01:00
parent 6128d8706d
commit d5a4bbb7e2
9 changed files with 144 additions and 8 deletions

View File

@ -45,6 +45,7 @@ shelve_led_effect_to_message_id = {
EffectRandomTwoColorInterpolationConfig: 17, EffectRandomTwoColorInterpolationConfig: 17,
EffectSwipeAndChange: 18, EffectSwipeAndChange: 18,
EffectReverseSwipe: 19, EffectReverseSwipe: 19,
EffectStaticDetailedConfig: 20,
} }
mouse_leds_index_ranges = { mouse_leds_index_ranges = {
@ -54,8 +55,8 @@ mouse_leds_index_ranges = {
TouchButton.RIGHT_EAR: (6 + 6 + 16, 6 + 6 + 16 + 17), TouchButton.RIGHT_EAR: (6 + 6 + 16, 6 + 6 + 16 + 17),
} }
PREV_BUTTON_LED_MSG = 20 PREV_BUTTON_LED_MSG = 21
NEXT_BUTTON_LED_MSG = 21 NEXT_BUTTON_LED_MSG = 22
class RfidTokenRead: class RfidTokenRead:

View File

@ -76,6 +76,20 @@ class EffectStaticConfig:
return self.color.as_bytes() + struct.pack("<HH", self.begin, self.end) return self.color.as_bytes() + struct.pack("<HH", self.begin, self.end)
@dataclass
class EffectStaticDetailedConfig:
color: ColorRGBW
increment: int = 1
begin: float = 0.0
end: float = 1.0
transition_time_in_ms : float = 500
def __repr__(self):
return f"EffectStaticDetailedConfig {str(self.color)}, beg: {self.begin}, end {self.end}, incr {self.increment}, transition in ms {self.transition_time_in_ms}"
def as_bytes(self) -> bytes:
return self.color.as_bytes() + struct.pack("<Hfff", self.increment, self.begin, self.end, self.transition_time_in_ms)
@dataclass @dataclass
class EffectAlexaSwipeConfig: class EffectAlexaSwipeConfig:
primary_color_width: float = 20 # in degrees primary_color_width: float = 20 # in degrees

View File

@ -1,4 +1,4 @@
from led_cmds import ColorRGBW, EffectStaticConfig, EffectCircularConfig, EffectRandomTwoColorInterpolationConfig, EffectAlexaSwipeConfig, EffectSwipeAndChange from led_cmds import ColorRGBW, EffectStaticConfig, EffectStaticDetailedConfig, EffectCircularConfig, EffectRandomTwoColorInterpolationConfig, EffectAlexaSwipeConfig, EffectSwipeAndChange
import asyncio import asyncio
import asyncio_mqtt import asyncio_mqtt
import json import json
@ -63,11 +63,19 @@ class ShelveLightMqtt:
def _update_device(self): def _update_device(self):
s = self._state s = self._state
current_color = self._color_from_json(s['color'], brightness=s["brightness"]) current_color = self._color_from_json(s['color'], brightness=s["brightness"])
transition = s.get("transition", 0.0) * 1000
print(f"Effect {s['effect']} Transition {transition}")
if s['state'] == "OFF": if s['state'] == "OFF":
eff = EffectStaticConfig(ColorRGBW(0, 0, 0, 0)) if transition > 0:
eff = EffectStaticDetailedConfig(ColorRGBW(0,0,0,0), transition_tim_in_ms=transition)
else:
eff = EffectStaticConfig(ColorRGBW(0, 0, 0, 0))
elif s['effect'] == 'static': elif s['effect'] == 'static':
eff = EffectStaticConfig(current_color) if transition > 0:
eff = EffectStaticDetailedConfig(current_color, transition_time_in_ms=transition)
else:
eff = EffectStaticConfig(current_color)
elif s['effect'] == 'circular': elif s['effect'] == 'circular':
eff = EffectCircularConfig(speed=180, width=90, color=current_color) eff = EffectCircularConfig(speed=180, width=90, color=current_color)
elif s['effect'] == 'wipeup': elif s['effect'] == 'wipeup':
@ -92,6 +100,14 @@ class ShelveLightMqtt:
eff.hue1_random = True eff.hue1_random = True
eff.hue2_random = True eff.hue2_random = True
eff.start_with_existing = True eff.start_with_existing = True
elif s['effect'] == "side_0.2":
eff = EffectStaticDetailedConfig(current_color, begin=0.9, end=0.1, increment=1, transition_time_in_ms=transition)
elif s['effect'] == "side_0.2_inc4":
eff = EffectStaticDetailedConfig(current_color, begin=0.9, end=0.1, increment=4, transition_time_in_ms=transition)
elif s['effect'] == "side_0.5":
eff = EffectStaticDetailedConfig(current_color, begin=0.75, end=0.25, increment=1, transition_time_in_ms=transition)
elif s['effect'] == "side_0.5_inc4":
eff = EffectStaticDetailedConfig(current_color, begin=0.75, end=0.25, increment=4, transition_time_in_ms=transition)
else: else:
print(f"Unknown effect {s['effect']}") print(f"Unknown effect {s['effect']}")
eff = EffectStaticConfig(ColorRGBW(0, 0, 0, 0)) eff = EffectStaticConfig(ColorRGBW(0, 0, 0, 0))
@ -115,7 +131,8 @@ class ShelveLightMqtt:
# 'model': "SK6812 LED strip", # 'model': "SK6812 LED strip",
#}, #},
'effect': True, 'effect': True,
'effect_list': ['static', 'circular', 'wipeup', 'twocolor', 'twocolorrandom'], 'effect_list': ['static', 'circular', 'wipeup', 'twocolor', 'twocolorrandom',
"side_0.2", "side_0.5", "side_0.2_inc4", "side_0.5_inc4", "top_0.2"],
'supported_color_modes': ['rgbw'], 'supported_color_modes': ['rgbw'],
} }

View File

@ -8,6 +8,7 @@ enum class EffectId
RANDOM_TWO_COLOR_INTERPOLATION, RANDOM_TWO_COLOR_INTERPOLATION,
SWIPE_AND_CHANGE, // combination of ALEXA_SWIPE and RANDOM_TWO_COLOR_INTERPOLATION SWIPE_AND_CHANGE, // combination of ALEXA_SWIPE and RANDOM_TWO_COLOR_INTERPOLATION
REVERSE_SWIPE, REVERSE_SWIPE,
STATIC_DETAILED,
}; };
template <EffectId id> template <EffectId id>

View File

@ -3,6 +3,7 @@
#include "effects/Common.h" #include "effects/Common.h"
#include "helpers/ColorRGBW.h" #include "helpers/ColorRGBW.h"
#pragma pack(push, 1)
struct EffectStaticConfig struct EffectStaticConfig
{ {
EffectStaticConfig(const ColorRGBW &c = ColorRGBW{0, 0, 0, 0}, uint16_t beg = 0, uint16_t en = 0) EffectStaticConfig(const ColorRGBW &c = ColorRGBW{0, 0, 0, 0}, uint16_t beg = 0, uint16_t en = 0)
@ -12,6 +13,7 @@ struct EffectStaticConfig
uint16_t begin = 0; uint16_t begin = 0;
uint16_t end = 0; uint16_t end = 0;
}; };
#pragma pack(pop)
template <typename TLedStrip> template <typename TLedStrip>
class EffectStatic class EffectStatic

View File

@ -0,0 +1,97 @@
#pragma once
#include "effects/Common.h"
#include "helpers/ColorRGBW.h"
#include "helpers/ColorConversions.h"
#pragma pack(push, 1)
struct EffectStaticDetailedConfig
{
EffectStaticDetailedConfig(const ColorRGBW &c = ColorRGBW{0, 0, 0, 0}, uint16_t beg = 0, uint16_t en = 0)
: color(c), begin(beg), end(en) {}
ColorRGBW color;
uint16_t increment = 1;
float begin = 0.0f;
float end = 0.0f;
float transition_time_in_ms = 0.0f;
};
#pragma pack(pop)
template <typename TLedStrip>
class EffectStaticDetailed
{
public:
static constexpr auto NUM_LEDS = numLeds<TLedStrip>();
static constexpr int DELAY_MS = 10;
using ConfigType = EffectStaticDetailedConfig;
EffectStaticDetailed(const EffectStaticDetailedConfig &cfg, TLedStrip &ledStrip)
: config_(cfg),
ledStrip_(ledStrip)
{
for (int i = 0; i < NUM_LEDS; ++i)
state_[i] = getLedRGBW(ledStrip_, i);
beginIdx_ = constrain(static_cast<int>(cfg.begin * NUM_LEDS + 0.5f), 0, NUM_LEDS - 1);
endIdx_ = constrain(static_cast<int>(cfg.end * NUM_LEDS + 0.5f), 0, NUM_LEDS - 1);
}
bool finished() const { return finished_; }
int operator()()
{
if (finished_)
return 1000000;
const float progress = static_cast<float>(DELAY_MS * calls_) / config_.transition_time_in_ms;
// Finished case
if(progress > 1.0) {
finished_ = true;
return 10000000;
}
// In-progress case
clear(ledStrip_);
for(int i = beginIdx_; i != endIdx_; i += config_.increment) {
ColorRGBW newColor = hsv2rgb(interpolate(rgb2hsv(state_[i]), rgb2hsv(config_.color), progress));
newColor.w = config_.color.w * progress + state_[i].w * (1 - progress);
setLedRGBW(ledStrip_, i, newColor);
if(i >= NUM_LEDS)
i = 0;
}
++calls_;
return DELAY_MS;
}
private:
EffectStaticDetailedConfig config_;
TLedStrip &ledStrip_;
ColorRGBW state_[NUM_LEDS];
int beginIdx_;
int endIdx_;
int calls_ = 0;
bool finished_ = false;
};
// Traits
template <>
struct EffectIdToConfig<EffectId::STATIC_DETAILED>
{
using type = EffectStaticDetailedConfig;
};
template <>
struct EffectConfigToId<EffectStaticDetailedConfig>
{
static constexpr auto id = EffectId::STATIC_DETAILED;
};
template <typename TLedStrip>
struct EffectIdToClass<EffectId::STATIC_DETAILED, TLedStrip>
{
using type = EffectStaticDetailed<TLedStrip>;
};

View File

@ -116,9 +116,10 @@ enum class MessageHostToFw : uint8_t
SHELF_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 17, SHELF_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 17,
SHELF_LED_EFFECT_SWIPE_AND_CHANGE = 18, SHELF_LED_EFFECT_SWIPE_AND_CHANGE = 18,
SHELF_LED_EFFECT_REVERSE_SWIPE = 19, SHELF_LED_EFFECT_REVERSE_SWIPE = 19,
SHELF_LED_EFFECT_STATIC_DETAILED = 20,
PREV_BUTTON_LED = 20, PREV_BUTTON_LED = 21,
NEXT_BUTTON_LED = 21, NEXT_BUTTON_LED = 22,
}; };
template <> template <>
@ -227,6 +228,7 @@ inline void handleIncomingMessagesFromHost(LedTask1 *ledTaskCircle, LedTask2 *le
else if (handleLedEffect<EffectRandomTwoColorInterpolationConfig>(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION, msgType, msgBuffer)) {} else if (handleLedEffect<EffectRandomTwoColorInterpolationConfig>(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION, msgType, msgBuffer)) {}
else if (handleLedEffect<EffectSwipeAndChangeConfig >(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_SWIPE_AND_CHANGE, msgType, msgBuffer)) {} else if (handleLedEffect<EffectSwipeAndChangeConfig >(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_SWIPE_AND_CHANGE, msgType, msgBuffer)) {}
else if (handleLedEffect<EffectReverseSwipeConfig >(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_REVERSE_SWIPE, msgType, msgBuffer)) {} else if (handleLedEffect<EffectReverseSwipeConfig >(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_REVERSE_SWIPE, msgType, msgBuffer)) {}
else if (handleLedEffect<EffectStaticDetailedConfig >(ledTaskShelf, MessageHostToFw::SHELF_LED_EFFECT_STATIC_DETAILED, msgType, msgBuffer)) {}
// clang-format on // clang-format on
else if (msgType == MessageHostToFw::PREV_BUTTON_LED) else if (msgType == MessageHostToFw::PREV_BUTTON_LED)

View File

@ -77,6 +77,7 @@ void _led_task_func(void *params)
// clang-format off // clang-format off
if (dispatchEffectId<EffectId::CIRCULAR >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {} if (dispatchEffectId<EffectId::CIRCULAR >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}
else if (dispatchEffectId<EffectId::STATIC >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {} else if (dispatchEffectId<EffectId::STATIC >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}
else if (dispatchEffectId<EffectId::STATIC_DETAILED >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}
else if (dispatchEffectId<EffectId::ALEXA_SWIPE >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {} else if (dispatchEffectId<EffectId::ALEXA_SWIPE >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}
else if (dispatchEffectId<EffectId::RANDOM_TWO_COLOR_INTERPOLATION>(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {} else if (dispatchEffectId<EffectId::RANDOM_TWO_COLOR_INTERPOLATION>(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}
else if (dispatchEffectId<EffectId::SWIPE_AND_CHANGE >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {} else if (dispatchEffectId<EffectId::SWIPE_AND_CHANGE >(id, effectFunction, ledStrip, msgBuffer, effectStorage)) {}

View File

@ -8,6 +8,7 @@
#include "drivers/Esp32DriverRGBW.h" #include "drivers/Esp32DriverRGBW.h"
#include "effects/Circular.h" #include "effects/Circular.h"
#include "effects/Static.h" #include "effects/Static.h"
#include "effects/StaticDetailed.h"
#include "effects/AlexaSwipe.h" #include "effects/AlexaSwipe.h"
#include "effects/RandomTwoColorInterpolation.h" #include "effects/RandomTwoColorInterpolation.h"