Skip to content

Commit

Permalink
Refactoring of Berry animate module for WS2812 Leds (#20236)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger committed Dec 14, 2023
1 parent 7b12450 commit c1f8ee5
Show file tree
Hide file tree
Showing 22 changed files with 1,993 additions and 1,877 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added

### Breaking Changed
- Refactoring of Berry `animate` module for WS2812 Leds

### Changed

Expand Down
9 changes: 3 additions & 6 deletions lib/libesp32/berry/default/be_modtab.c
Expand Up @@ -146,7 +146,10 @@ BERRY_LOCAL const bntvmodule_t* const be_module_table[] = {
#ifdef USE_UNISHOX_COMPRESSION
&be_native_module(unishox),
#endif // USE_UNISHOX_COMPRESSION

#ifdef USE_WS2812
&be_native_module(animate),
#endif // USE_WS2812

#ifdef USE_LVGL
&be_native_module(lv),
Expand Down Expand Up @@ -206,10 +209,7 @@ be_extern_native_class(AXP192);
be_extern_native_class(AXP202);
be_extern_native_class(OneWire);
be_extern_native_class(Leds_ntv);
be_extern_native_class(Leds_frame);
be_extern_native_class(Leds);
be_extern_native_class(Leds_animator);
be_extern_native_class(Leds_pulse);
be_extern_native_class(AudioGenerator);
be_extern_native_class(AudioFileSource);
be_extern_native_class(AudioOutputI2S);
Expand Down Expand Up @@ -278,9 +278,6 @@ BERRY_LOCAL bclass_array be_class_table = {
#ifdef USE_WS2812
&be_native_class(Leds_ntv),
&be_native_class(Leds),
&be_native_class(Leds_frame),
&be_native_class(Leds_animator),
&be_native_class(Leds_pulse),
#endif // USE_WS2812
#ifdef USE_ENERGY_SENSOR
&be_native_class(energy_struct),
Expand Down
7 changes: 0 additions & 7 deletions lib/libesp32/berry_tasmota/src/be_animate_lib.c

This file was deleted.

197 changes: 197 additions & 0 deletions lib/libesp32/berry_tasmota/src/be_animate_module.c
@@ -0,0 +1,197 @@
/*
be_animate_module.c - implements the animate module for Leds
Copyright (C) 2023 Stephan Hadinger & Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*******************************************************************\
* `animate` global module
\*******************************************************************/

#include "be_constobj.h"
#include "be_mapping.h"

#include "solidify/solidified_animate_0_core.h"
#include "solidify/solidified_animate_1_animate_effects.h"
#include "solidify/solidified_animate_9_module.h"


/*******************************************************************\
* standard palettes
\*******************************************************************/
static const uint8_t PALETTE_RAINBOW_WHITE[] = {
0x50, 0xFF, 0x00, 0x00, // red
0x30, 0xFF, 0x00, 0x00, // red
0x50, 0xFF, 0xA5, 0x00, // orange
0x30, 0xFF, 0xA5, 0x00, // orange
0x50, 0xFF, 0xFF, 0x00, // yellow
0x30, 0xFF, 0xFF, 0x00, // yellow
0x50, 0x00, 0xFF, 0x00, // green
0x30, 0x00, 0xFF, 0x00, // green
0x50, 0x00, 0x00, 0xFF, // blue
0x30, 0x00, 0x00, 0xFF, // blue
0x50, 0xFF, 0x00, 0xFF, // indigo
0x30, 0xFF, 0x00, 0xFF, // indigo
0x50, 0xFF, 0xFF, 0xFF, // white
0x30, 0xFF, 0xFF, 0xFF, // white
0x00, 0xFF, 0x00, 0x00, // red
};

static const uint8_t PALETTE_STANDARD_TAG[] = {
0x40, 0xFF, 0x00, 0x00, // red
0x40, 0xFF, 0xA5, 0x00, // orange
0x40, 0xFF, 0xFF, 0x00, // yellow
0x40, 0x00, 0xFF, 0x00, // green
0x40, 0x00, 0x00, 0xFF, // blue
0x40, 0xFF, 0x00, 0xFF, // indigo
0x40, 0xFF, 0xFF, 0xFF, // violet
0x00, 0xFF, 0x00, 0x00, // red
};

// Gradient palette "ib_jul01_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/ing/xmas/tn/ib_jul01.png.index.html
static const uint8_t PALETTE_ib_jul01_gp[] = {
0x00, 0xE6, 0x06, 0x11, // rgb(230, 6, 17) 0.000%,
0x5E, 0x25, 0x60, 0x5A, // rgb( 37, 96, 90) 37.010%,
0x85, 0x90, 0xBD, 0x6A, // rgb(144,189,106) 52.000%,
0xFF, 0xBB, 0x03, 0x0D, // rgb(187, 3, 13) 100.000%
};

static const uint8_t PALETTE_STANDARD_VAL[] = {
0x00, 0xFF, 0x00, 0x00, // red
0x24, 0xFF, 0xA5, 0x00, // orange
0x49, 0xFF, 0xFF, 0x00, // yellow
0x6E, 0x00, 0xFF, 0x00, // green
0x92, 0x00, 0x00, 0xFF, // blue
0xB7, 0xFF, 0x00, 0xFF, // indigo
0xDB, 0xFF, 0xFF, 0xFF, // violet
0xFF, 0xFF, 0x00, 0x00, // red
};

static const uint8_t PALETTE_SATURATED_TAG[] = {
0x40, 0xFF, 0x00, 0x00, // red
0x40, 0xFF, 0xA5, 0x00, // orange
0x40, 0xFF, 0xFF, 0x00, // yellow
0x40, 0x00, 0xFF, 0x00, // green
0x40, 0x00, 0x00, 0xFF, // blue
0x40, 0xFF, 0x00, 0xFF, // indigo
0x40, 0xFF, 0xFF, 0xFF, // violet
0x00, 0xFF, 0x00, 0x00, // red
};

extern const bclass be_class_Leds_frame;

#include "be_fixed_animate.h"

/* @const_object_info_begin
module animate (scope: global, strings: weak) {
SAWTOOTH, int(1)
TRIANGLE, int(2)
SQUARE, int(3)
COSINE, int(4)
SINE, int(5)
LASTFORM, int(5)
PALETTE_STANDARD_TAG, comptr(PALETTE_STANDARD_TAG)
PALETTE_RAINBOW_WHITE, comptr(PALETTE_RAINBOW_WHITE)
PALETTE_STANDARD_VAL, comptr(PALETTE_STANDARD_VAL)
PALETTE_SATURATED_TAG, comptr(PALETTE_SATURATED_TAG)
(), class(be_class_Animate_core)
core, class(be_class_Animate_core) // alias
animator, class(be_class_Animate_animator)
frame, class(be_class_Leds_frame)
pulse, class(be_class_Animate_pulse)
palette, class(be_class_Animate_palette)
oscillator, class(be_class_Animate_oscillator)
}
@const_object_info_end */

/* Unit test for palettes
import animate
var p, gradient
p = animate.palette.ptr_to_palette(animate.PALETTE_STANDARD_TAG)
assert(p == bytes('40FF000040FFA50040FFFF004000FF00400000FF40FF00FF40FFFFFF00FF0000'))
gradient = animate.palette.to_css_gradient(p)
assert(gradient == 'background:linear-gradient(to right,#FF0000 0.0%,#FFA500 14.3%,#FFFF00 28.6%,#00FF00 42.9%,#0000FF 57.1%,#FF00FF 71.4%,#FFFFFF 85.7%,#FF0000 100.0%);')
p = animate.palette.ptr_to_palette(animate.PALETTE_STANDARD_VAL)
assert(p == bytes('00FF000024FFA50049FFFF006E00FF00920000FFB7FF00FFDBFFFFFFFFFF0000'))
gradient = animate.palette.to_css_gradient(animate.PALETTE_STANDARD_VAL)
assert(gradient == 'background:linear-gradient(to right,#FF0000 0.0%,#FFA500 14.1%,#FFFF00 28.6%,#00FF00 43.1%,#0000FF 57.3%,#FF00FF 71.8%,#FFFFFF 85.9%,#FF0000 100.0%);')
# unit tests
import animate
var o = animate.oscillator(-1000, 1000, 3000)
o.start(1000)
assert(o.value == -1000)
assert(o.animate(1500) == -667)
assert(o.animate(2500) == 0)
assert(o.animate(3999) == 1000)
assert(o.animate(4000) == -1000)
assert(o.animate(4100) == -933)
o = animate.oscillator(-1000, 1000, 6000, animate.TRIANGLE)
o.start(1000)
assert(o.value == -1000)
assert(o.animate(1500) == -667)
assert(o.animate(2500) == 0)
assert(o.animate(3999) == 1000)
assert(o.animate(4000) == 1000)
assert(o.animate(4100) == 933)
assert(o.animate(6000) == -334)
assert(o.animate(7000) == -1000)
assert(o.animate(7100) == -933)
o = animate.oscillator(-1000, 1000, 6000, animate.SQUARE)
o.start(1000)
assert(o.value == -1000)
assert(o.animate(1500) == -1000)
assert(o.animate(2500) == -1000)
assert(o.animate(3999) == -1000)
assert(o.animate(4000) == 1000)
assert(o.animate(4100) == 1000)
assert(o.animate(6000) == 1000)
assert(o.animate(7000) == -1000)
assert(o.animate(7100) == -1000)
o = animate.oscillator(-1000, 1000, 6000, animate.SINE)
o.start(1000)
assert(o.animate(1000) == 0)
assert(o.animate(1500) == 500)
assert(o.animate(2000) == 867)
assert(o.animate(2500) == 1000)
assert(o.animate(4000) == 0)
assert(o.animate(5500) == -1000)
assert(o.animate(7000) == 0)
o = animate.oscillator(-1000, 1000, 6000, animate.COSINE)
o.start(1000)
assert(o.animate(1000) == -1000)
assert(o.animate(1500) == -867)
assert(o.animate(2000) == -500)
assert(o.animate(2500) == 0)
assert(o.animate(4000) == 1000)
assert(o.animate(5500) == 0)
assert(o.animate(7000) == -1000)
*/
9 changes: 0 additions & 9 deletions lib/libesp32/berry_tasmota/src/be_leds_animator_class.c

This file was deleted.

9 changes: 0 additions & 9 deletions lib/libesp32/berry_tasmota/src/be_leds_pulse_class.c

This file was deleted.

2 changes: 2 additions & 0 deletions lib/libesp32/berry_tasmota/src/be_tasmota_lib.c
Expand Up @@ -35,6 +35,7 @@ extern int l_delay(bvm *vm);
extern int l_delay_microseconds(bvm *vm);
extern int l_scaleuint(bvm *vm);
extern int l_scaleint(bvm *vm);
extern int l_sineint(bvm *vm);
extern int l_logInfo(bvm *vm);
extern int l_loglevel(bvm *vm);
extern int l_save(bvm *vm);
Expand Down Expand Up @@ -120,6 +121,7 @@ class be_class_tasmota (scope: global, name: Tasmota) {
delay_microseconds, func(l_delay_microseconds)
scale_uint, static_func(l_scaleuint)
scale_int, static_func(l_scaleint)
sine_int, static_func(l_sineint)
log, func(l_logInfo)
loglevel, func(l_loglevel)
save, func(l_save)
Expand Down
@@ -1,21 +1,17 @@
# class Leds_animator

#@ solidify:Leds_animator,weak

# for solidification
class Leds_frame end
# class Animate_core

##########################################################################################
#
# class Leds_animator
# class Animate_core
#
# Simple framework to orchestrate all the animations for a led strip or led matrix
#
# You should pass a Leds or Leds_segment to push pixels at each tick
# This version uses `fast_loop` for up to 5ms animation time (200 Hz)
#
##########################################################################################
class Leds_animator
#@ solidify:Animate_core,weak
class Animate_core
var strip # neopixelbus object
var pixel_count # number of pixels in the strip
var bri # brightness of the animation, 0..100, default 50
Expand All @@ -35,6 +31,7 @@ class Leds_animator
var back_color # background color RRGGBB

def init(strip, bri)
import animate
self.strip = strip
if (bri == nil) bri = 50 end
self.bri = bri # percentage of brightness 0..100
Expand All @@ -44,8 +41,8 @@ class Leds_animator
self.painters = []
#
self.clear() # clear all leds first
self.frame = Leds_frame(self.pixel_count)
self.layer = Leds_frame(self.pixel_count)
self.frame = animate.frame(self.pixel_count)
self.layer = animate.frame(self.pixel_count)
#
self.fast_loop_cb = def() self.fast_loop() end
self.back_color = 0x000000
Expand Down

0 comments on commit c1f8ee5

Please sign in to comment.