From 50fb58e494e75593a325a3bbc497e3d17b16bd8e Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 21:22:56 +0200 Subject: [PATCH] animation_test: fixed build; test new animation --- animation_test/CMakeLists.txt | 6 +- .../Animation/RgbwPsychedelicAnimation.cpp | 87 ++++++++++++++ .../src/Animation/RgbwPsychedelicAnimation.h | 46 ++++++++ animation_test/src/Fader.cpp | 2 +- animation_test/src/fasttrigon.cpp | 111 ++++++++++++++++++ animation_test/src/fasttrigon.h | 34 ++++++ animation_test/src/main.cpp | 30 +---- 7 files changed, 287 insertions(+), 29 deletions(-) create mode 100644 animation_test/src/Animation/RgbwPsychedelicAnimation.cpp create mode 100644 animation_test/src/Animation/RgbwPsychedelicAnimation.h create mode 100644 animation_test/src/fasttrigon.cpp create mode 100644 animation_test/src/fasttrigon.h diff --git a/animation_test/CMakeLists.txt b/animation_test/CMakeLists.txt index 7c6eb63..9acbc0c 100644 --- a/animation_test/CMakeLists.txt +++ b/animation_test/CMakeLists.txt @@ -3,12 +3,14 @@ cmake_minimum_required(VERSION 3.5) set(SOURCES src/Fader.cpp - src/Animation/ImageScrollerAnimation.cpp + src/Animation/RgbwPsychedelicAnimation.cpp + src/fasttrigon.cpp src/main.cpp src/Animation/Animation.h src/Fader.h - src/Animation/ImageScrollerAnimation.h + src/Animation/RgbwPsychedelicAnimation.h + src/fasttrigon.h ) add_executable(animation_test ${SOURCES}) diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp new file mode 100644 index 0000000..e4046dd --- /dev/null +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp @@ -0,0 +1,87 @@ +#include + +#include "Animation/RgbwPsychedelicAnimation.h" + +RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds, + const rgbw_val_array &tilt_speeds, + int32_t tilt_maxperiods, + uint32_t move_speed_divider, + uint32_t tilt_speed_divider, + uint16_t brightness_scale) + : Animation(fader), + m_move_speeds(move_speeds), + m_tilt_speeds(move_speeds), + m_moveSpeedDivider(move_speed_divider), + m_tiltSpeedDivider(tilt_speed_divider), + m_brightnessScale(brightness_scale), + m_tilt_maxperiods(tilt_maxperiods), + m_move_phi{0,0,0,0}, + m_tilt_phi{100,200,300,400}, + m_moveOverflowInterval(fasttrigon::LUT_SIZE * move_speed_divider), + m_tiltOverflowInterval(fasttrigon::LUT_SIZE * tilt_speed_divider) +{ + reset(); +} + +void RgbwPsychedelicAnimation::loop(uint64_t frame) +{ + std::size_t nled = m_fader->modules_per_strip(); + std::size_t nstrip = m_fader->strips(); + + if(!m_stopping) { + for(unsigned i = 0; i < m_move_speeds.size(); i++) { + m_move_phi[i] += m_move_speeds[i]; + + if(m_move_phi[i] > m_moveOverflowInterval) { + m_move_phi[i] -= m_moveOverflowInterval; + } + } + + rgbw_val_array tilt_nperiods; + + for(unsigned i = 0; i < m_tilt_speeds.size(); i++) { + m_tilt_phi[i] += m_tilt_speeds[i]; + + if(m_tilt_phi[i] > m_tiltOverflowInterval) { + m_tilt_phi[i] -= m_tiltOverflowInterval; + } + + tilt_nperiods[i] = m_tilt_maxperiods * fasttrigon::fastsin(m_tilt_phi[i] / m_tiltSpeedDivider); + } + + + for(std::size_t led = 0; led < nled; led++) { + for(std::size_t strip = 0; strip < nstrip; strip++) { + int32_t x = fasttrigon::LUT_SIZE * led / nled; + int32_t y = fasttrigon::LUT_SIZE * strip / nstrip; + + rgbw_val_array rgbw { + (127 + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> 7) + y)) + }; + + Fader::Color color{ + static_cast(((rgbw[0] * rgbw[0] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[1] * rgbw[1] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[2] * rgbw[2] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[3] * rgbw[3] >> 7) * m_brightnessScale) >> 10)}; // white is too bright otherwise + + m_fader->set_color(strip, led, color); + } + } + } else { + m_fader->fade_color(Fader::Color{0, 0, 0, 0}); + } + + m_fader->update(); + m_running = !m_stopping || m_fader->something_changed(); +} + +void RgbwPsychedelicAnimation::reset(void) +{ + m_stopping = false; + m_running = true; +} diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.h b/animation_test/src/Animation/RgbwPsychedelicAnimation.h new file mode 100644 index 0000000..251f7f6 --- /dev/null +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.h @@ -0,0 +1,46 @@ +#pragma once + +#include + +#include "Animation.h" + +#include "fasttrigon.h" + +class RgbwPsychedelicAnimation : public Animation +{ + public: + typedef std::array rgbw_val_array; + RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds = {2273, 2281, 2287, 2293}, + const rgbw_val_array &tilt_speeds = {2297, 2309, 2311, 2333}, + int32_t tilt_maxperiods = 3, + uint32_t move_speed_divider = 1024, + uint32_t tilt_speed_divider = 8192, + uint16_t brightness_scale = 64); // max: 255 + + void loop(uint64_t frame) override; + + void stop(void) override + { + m_stopping = true; + } + + void reset(void) override; + + private: + bool m_stopping; + + rgbw_val_array m_move_speeds; + rgbw_val_array m_tilt_speeds; + uint32_t m_moveSpeedDivider; + uint32_t m_tiltSpeedDivider; + uint16_t m_brightnessScale; + + int32_t m_tilt_maxperiods; + + rgbw_val_array m_move_phi; + rgbw_val_array m_tilt_phi; + + int32_t m_moveOverflowInterval; + int32_t m_tiltOverflowInterval; +}; diff --git a/animation_test/src/Fader.cpp b/animation_test/src/Fader.cpp index 424ce79..70dafc4 100644 --- a/animation_test/src/Fader.cpp +++ b/animation_test/src/Fader.cpp @@ -104,7 +104,7 @@ void Fader::update(void) target_addr.sin_family = AF_INET; target_addr.sin_port = htons(2703); - target_addr.sin_addr.s_addr = inet_addr("10.42.7.145"); + target_addr.sin_addr.s_addr = inet_addr("192.168.2.145"); int ret = sendto(m_fd, packet.data(), packet.size(), 0, reinterpret_cast(&target_addr), sizeof(target_addr)); if(ret == -1) { diff --git a/animation_test/src/fasttrigon.cpp b/animation_test/src/fasttrigon.cpp new file mode 100644 index 0000000..b0da8e3 --- /dev/null +++ b/animation_test/src/fasttrigon.cpp @@ -0,0 +1,111 @@ +#include + +#include "fasttrigon.h" + +namespace fasttrigon { + static const std::array LUT { + 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, + 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, + 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, + 42, 43, 44, 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, + 54, 55, 56, 56, 57, 58, 58, 59, 60, 61, 61, 62, 63, 63, 64, 65, 65, + 66, 67, 67, 68, 69, 69, 70, 71, 71, 72, 72, 73, 74, 74, 75, 76, 76, + 77, 78, 78, 79, 79, 80, 81, 81, 82, 82, 83, 84, 84, 85, 85, 86, 86, + 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 93, 93, 94, 94, 95, 95, 96, + 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 102, 103, + 103, 104, 104, 105, 105, 106, 106, 106, 107, 107, 108, 108, 109, 109, + 109, 110, 110, 111, 111, 111, 112, 112, 112, 113, 113, 113, 114, 114, + 114, 115, 115, 115, 116, 116, 116, 117, 117, 117, 118, 118, 118, 118, + 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, + 122, 122, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 125, + 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, + 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, + 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, + 127, 127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, + 126, 125, 125, 125, 125, 125, 125, 125, 124, 124, 124, 124, 124, 124, + 123, 123, 123, 123, 123, 122, 122, 122, 122, 122, 121, 121, 121, 121, + 120, 120, 120, 120, 119, 119, 119, 118, 118, 118, 118, 117, 117, 117, + 116, 116, 116, 115, 115, 115, 114, 114, 114, 113, 113, 113, 112, 112, + 112, 111, 111, 111, 110, 110, 109, 109, 109, 108, 108, 107, 107, 106, + 106, 106, 105, 105, 104, 104, 103, 103, 102, 102, 102, 101, 101, 100, + 100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 91, + 91, 90, 90, 89, 89, 88, 88, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, + 81, 81, 80, 79, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, + 71, 70, 69, 69, 68, 67, 67, 66, 65, 65, 64, 63, 63, 62, 61, 61, 60, + 59, 58, 58, 57, 56, 56, 55, 54, 54, 53, 52, 51, 51, 50, 49, 49, 48, + 47, 46, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38, 37, 36, 35, + 35, 34, 33, 32, 32, 31, 30, 29, 29, 28, 27, 26, 26, 25, 24, 23, 22, + 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 14, 13, 12, 12, 11, 10, 9, 9, + 8, 7, 6, 5, 5, 4, 3, 2, 2, 1, 0, -1, -2, -2, -3, -4, -5, -5, -6, -7, + -8, -9, -9, -10, -11, -12, -12, -13, -14, -15, -16, -16, -17, -18, + -19, -19, -20, -21, -22, -22, -23, -24, -25, -26, -26, -27, -28, -29, + -29, -30, -31, -32, -32, -33, -34, -35, -35, -36, -37, -38, -38, -39, + -40, -41, -41, -42, -43, -44, -44, -45, -46, -46, -47, -48, -49, -49, + -50, -51, -51, -52, -53, -54, -54, -55, -56, -56, -57, -58, -58, -59, + -60, -61, -61, -62, -63, -63, -64, -65, -65, -66, -67, -67, -68, -69, + -69, -70, -71, -71, -72, -72, -73, -74, -74, -75, -76, -76, -77, -78, + -78, -79, -79, -80, -81, -81, -82, -82, -83, -84, -84, -85, -85, -86, + -86, -87, -88, -88, -89, -89, -90, -90, -91, -91, -92, -93, -93, -94, + -94, -95, -95, -96, -96, -97, -97, -98, -98, -99, -99, -100, -100, + -101, -101, -102, -102, -102, -103, -103, -104, -104, -105, -105, + -106, -106, -106, -107, -107, -108, -108, -109, -109, -109, -110, + -110, -111, -111, -111, -112, -112, -112, -113, -113, -113, -114, + -114, -114, -115, -115, -115, -116, -116, -116, -117, -117, -117, + -118, -118, -118, -118, -119, -119, -119, -120, -120, -120, -120, + -121, -121, -121, -121, -122, -122, -122, -122, -122, -123, -123, + -123, -123, -123, -124, -124, -124, -124, -124, -124, -125, -125, + -125, -125, -125, -125, -125, -126, -126, -126, -126, -126, -126, + -126, -126, -126, -126, -126, -127, -127, -127, -127, -127, -127, + -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, + -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, + -127, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126, + -126, -125, -125, -125, -125, -125, -125, -125, -124, -124, -124, + -124, -124, -124, -123, -123, -123, -123, -123, -122, -122, -122, + -122, -122, -121, -121, -121, -121, -120, -120, -120, -120, -119, + -119, -119, -118, -118, -118, -118, -117, -117, -117, -116, -116, + -116, -115, -115, -115, -114, -114, -114, -113, -113, -113, -112, + -112, -112, -111, -111, -111, -110, -110, -109, -109, -109, -108, + -108, -107, -107, -106, -106, -106, -105, -105, -104, -104, -103, + -103, -102, -102, -102, -101, -101, -100, -100, -99, -99, -98, -98, + -97, -97, -96, -96, -95, -95, -94, -94, -93, -93, -92, -91, -91, -90, + -90, -89, -89, -88, -88, -87, -86, -86, -85, -85, -84, -84, -83, -82, + -82, -81, -81, -80, -79, -79, -78, -78, -77, -76, -76, -75, -74, -74, + -73, -72, -72, -71, -71, -70, -69, -69, -68, -67, -67, -66, -65, -65, + -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, + -54, -54, -53, -52, -51, -51, -50, -49, -49, -48, -47, -46, -46, -45, + -44, -44, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -35, -35, + -34, -33, -32, -32, -31, -30, -29, -29, -28, -27, -26, -26, -25, -24, + -23, -22, -22, -21, -20, -19, -19, -18, -17, -16, -16, -15, -14, -13, + -12, -12, -11, -10, -9, -9, -8, -7, -6, -5, -5, -4, -3, -2, -2, -1 + }; + + int32_t fastsin(int32_t arg) + { + bool neg = arg < 0; + uint32_t idx; + if(neg) { + idx = (-arg) % LUT_SIZE; + return -LUT[idx]; + } else { + idx = arg % LUT_SIZE; + return LUT[idx]; + } + } + + int32_t fastcos(int32_t arg) + { + return fastsin(arg + LUT_SIZE/4); + } + + int32_t fasttan(int32_t arg) + { + int32_t denom = fastcos(arg); + if(denom == 0) { + return 0x7FFFFFFF; + } + + int32_t numer = fastsin(arg); + + return numer * 127 / denom; + } +} diff --git a/animation_test/src/fasttrigon.h b/animation_test/src/fasttrigon.h new file mode 100644 index 0000000..694418a --- /dev/null +++ b/animation_test/src/fasttrigon.h @@ -0,0 +1,34 @@ +/*! + * \file + * + * This file contains function prototypes for very fast lookup-table based + * trigonometric functions. + * + * The functions take an argument that is dependent on the lookup-table size + * (LUT_SIZE). LUT_SIZE corresponds to 2π. Values outside the range + * [0..LUT_SIZE] are supported and interpreted normally, so just replace 2π + * with LUT_SIZE and you're done. + */ + +#pragma once + +#include + +namespace fasttrigon { + static const uint32_t LUT_SIZE = 1024; + + /*! + * \returns 127 * sin(2π * arg / LUT_SIZE) + */ + int32_t fastsin(int32_t arg); + + /*! + * \returns 127 * cos(2π * arg / LUT_SIZE) + */ + int32_t fastcos(int32_t arg); + + /*! + * \returns 127 * tan(2π * arg / LUT_SIZE) + */ + int32_t fasttan(int32_t arg); +} diff --git a/animation_test/src/main.cpp b/animation_test/src/main.cpp index d19ca0d..4ac915d 100644 --- a/animation_test/src/main.cpp +++ b/animation_test/src/main.cpp @@ -3,35 +3,13 @@ #include #include "Fader.h" -#include "Animation/ImageScrollerAnimation.h" +#include "Animation/RgbwPsychedelicAnimation.h" int main(void) { Fader fader(8, 16); - ImageScrollerAnimation::Image img(12, 3); - img.pixel( 0 + 1, 0).r = 48; - img.pixel( 0 + 2, 1).r = 48; - img.pixel( 0 + 0, 2).r = 48; - img.pixel( 0 + 1, 2).r = 48; - img.pixel( 0 + 2, 2).r = 48; - img.pixel( 3 + 1, 0).g = 48; - img.pixel( 3 + 2, 1).g = 48; - img.pixel( 3 + 0, 2).g = 48; - img.pixel( 3 + 1, 2).g = 48; - img.pixel( 3 + 2, 2).g = 48; - img.pixel( 6 + 1, 0).b = 48; - img.pixel( 6 + 2, 1).b = 48; - img.pixel( 6 + 0, 2).b = 48; - img.pixel( 6 + 1, 2).b = 48; - img.pixel( 6 + 2, 2).b = 48; - img.pixel( 9 + 1, 0).w = 48; - img.pixel( 9 + 2, 1).w = 48; - img.pixel( 9 + 0, 2).w = 48; - img.pixel( 9 + 1, 2).w = 48; - img.pixel( 9 + 2, 2).w = 48; - - Animation *animation = new ImageScrollerAnimation(&fader, &img); + Animation *animation = new RgbwPsychedelicAnimation(&fader); for(uint64_t frame = 0; !animation->finished(); frame++) { animation->loop(frame); @@ -40,11 +18,11 @@ int main(void) fader.update(); } - if(frame == 3000) { + if(frame == 60 * 900) { animation->stop(); } - usleep(100000); + usleep(16667); }; delete animation;