From 5237de0539f264db3f4a33efc1bc3f85cd63ff00 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 26 Aug 2024 18:49:24 -0400 Subject: [PATCH] minor bug fixes and a new animation --- lib/Animator/Animation.h | 221 +++++++++++++++++++++++++++++++++++++++ lib/Board/Board.h | 2 +- lib/Board/BoardManager.h | 3 +- src/main.cpp | 31 +++++- 4 files changed, 251 insertions(+), 6 deletions(-) create mode 100644 lib/Animator/Animation.h diff --git a/lib/Animator/Animation.h b/lib/Animator/Animation.h new file mode 100644 index 0000000..4adfa02 --- /dev/null +++ b/lib/Animator/Animation.h @@ -0,0 +1,221 @@ +#pragma once + +#include "AnimationTypes.h" +#include "Vector3D.h" +#include "Animator.h" + +using namespace ANIMATION_TYPES; + +namespace AnimationHelpers{ + V3D red{255,0,0}; + V3D green{0,255,0}; + V3D blue{0,0,255}; + V3D cyan{0,255,255}; + V3D magenta{255,0,255}; + + Cell CreateCell(float x_percent, float y_percent, float z_percent, V3D &color){ + float continuousMaxValue{static_cast(std::numeric_limits::max())}; + Cell cell{ + .position = V3D{ + static_cast(continuousMaxValue*x_percent), + static_cast(continuousMaxValue*y_percent), + static_cast(continuousMaxValue*z_percent) + }, + .color = color + }; + + return cell; + } +} + +namespace RotatingCubes{ + using namespace AnimationHelpers; + + AnimationFrame frame1{ + .frame = { + CreateCell(0,0,0,red), + CreateCell(1,0.5,0,green), + CreateCell(0,1,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1000) + }; + + AnimationFrame frame2{ + .frame = { + CreateCell(0,0.5,0,red), + CreateCell(1,0,0,green), + CreateCell(0.5,1,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(500) + }; + + AnimationFrame frame3{ + .frame = { + CreateCell(0,1,0,red), + CreateCell(0.5,0,0,green), + CreateCell(1,1,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1000) + }; + + AnimationFrame frame4{ + .frame = { + CreateCell(0.5,1,0,red), + CreateCell(0,0,0,green), + CreateCell(1,0.5,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(500) + }; + + AnimationFrame frame5{ + .frame = { + CreateCell(1,1,0,red), + CreateCell(0,0.5,0,green), + CreateCell(1,0,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1000) + }; + + AnimationFrame frame6{ + .frame = { + CreateCell(1,0.5,0,red), + CreateCell(0,1,0,green), + CreateCell(0.5,0,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(500) + }; + + AnimationFrame frame7{ + .frame = { + CreateCell(1,0,0,red), + CreateCell(0.5,1,0,green), + CreateCell(0,0,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1000) + }; + + AnimationFrame frame8{ + .frame = { + CreateCell(0.5,0,0,red), + CreateCell(1,1,0,green), + CreateCell(0,0.5,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(500) + }; + + AnimationFrame frame9{ + .frame = { + CreateCell(0,0,0,red), + CreateCell(1,0.5,0,green), + CreateCell(0,1,0,blue) + }, + .fillInterpolation = FillInterpolation::CLOSEST_COLOR, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1) + }; + + std::vector rotating{ + frame1, // 0 + frame2, // 1 + frame3, // 2 + frame4, // 3 + frame5, // 4 + frame6, // 5 + frame7, // 6 + frame8, // 7 + frame9, // 8 + }; +} + +namespace RisingCubes{ + using namespace AnimationHelpers; + + AnimationFrame frame1{ + .frame = { + CreateCell(0,0,0,cyan), + CreateCell(0,1,0.5,green), + CreateCell(1,0,1,blue), + CreateCell(0.5,0.5,0.5,red), + CreateCell(1,1,0,magenta) + }, + .fillInterpolation = FillInterpolation::LINEAR_WEIGHTED_DISTANCE, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(800) + }; + + AnimationFrame frame2{ + .frame = { + CreateCell(0,0,0.5,cyan), + CreateCell(0,1,1,green), + CreateCell(1,0,0.5,blue), + CreateCell(0.5,0.5,0,red), + CreateCell(1,1,0.5,magenta) + }, + .fillInterpolation = FillInterpolation::LINEAR_WEIGHTED_DISTANCE, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(800) + }; + + AnimationFrame frame3{ + .frame = { + CreateCell(0,0,1,cyan), + CreateCell(0,1,0.5,green), + CreateCell(1,0,0,blue), + CreateCell(0.5,0.5,0.5,red), + CreateCell(1,1,1,magenta) + }, + .fillInterpolation = FillInterpolation::LINEAR_WEIGHTED_DISTANCE, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(800) + }; + + AnimationFrame frame4{ + .frame = { + CreateCell(0,0,0.5,cyan), + CreateCell(0,1,0,green), + CreateCell(1,0,0.5,blue), + CreateCell(0.5,0.5,1,red), + CreateCell(1,1,0.5,magenta) + }, + .fillInterpolation = FillInterpolation::LINEAR_WEIGHTED_DISTANCE, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(800) + }; + + AnimationFrame frame5{ + .frame = { + CreateCell(0,0,0,cyan), + CreateCell(0,1,0.5,green), + CreateCell(1,0,1,blue), + CreateCell(0.5,0.5,0.5,red), + CreateCell(1,1,0,magenta) + }, + .fillInterpolation = FillInterpolation::LINEAR_WEIGHTED_DISTANCE, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(1) + }; + + std::vector rising{ + frame1, // 0 + frame2, // 1 + frame3, // 2 + frame4, // 3 + frame5 + }; +} \ No newline at end of file diff --git a/lib/Board/Board.h b/lib/Board/Board.h index 00be54c..b64774d 100644 --- a/lib/Board/Board.h +++ b/lib/Board/Board.h @@ -121,7 +121,7 @@ void Board::ToStackString(String &stringBuffer) const{ stringBuffer += String(linearizedBoard[0]); - for(uint32_t i = 0; i < BOARD_DIMS.x * BOARD_DIMS.y; i++){ + for(uint32_t i = 1; i < BOARD_DIMS.x * BOARD_DIMS.y; i++){ stringBuffer += "," + String(linearizedBoard[i]); } } diff --git a/lib/Board/BoardManager.h b/lib/Board/BoardManager.h index 4710e1b..54624d3 100644 --- a/lib/Board/BoardManager.h +++ b/lib/Board/BoardManager.h @@ -68,6 +68,7 @@ class BoardManager{ void Board2StackString(String& messageBuffer); void FillColor(const V3D &color){this->board.FillColor(color);} + void PrintColorState(){this->board.PrintEntireBoard();} private: BoardDriver &driver; @@ -167,7 +168,7 @@ void BoardManager::SetColumnColors(const V3D &column, cons uint32_t sliceLength{this->board.SliceBoard(column, slicedBoard)}; uint32_t maxIndex{std::min(numColors, columnHeight)}; - for(uint32_t i = 0; i < columnHeight; i++){ + for(uint32_t i = 0; i < maxIndex; i++){ slicedBoard[i]->color = color[i]; } } diff --git a/src/main.cpp b/src/main.cpp index 52fe41e..737824f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include // Static Defines #include "PINOUT.h" @@ -21,6 +22,7 @@ #include "Animator.h" #include "TestFrames.h" +#include "Animation.h" // -------------------------------------------------- // ----------------- VARIABLES ---------------------- @@ -28,6 +30,11 @@ TaskHandle_t updateCommunicaitonTask; TaskHandle_t updateBoardTask; +std::array*, 2> animations = { + &RisingCubes::rising, + &RotatingCubes::rotating, +}; + // BluetoothSerial SerialBT; // BluetoothSerialMessage serialMessageBT(&SerialBT); SerialMessage serialMessage(&Serial); @@ -158,17 +165,33 @@ void UpdateBoard(void * params){ auto boardStateTimer{std::chrono::milliseconds(0)}; auto boardStateMaxUpdatePeriod{std::chrono::milliseconds(34)}; // this is a little slower than 30fps unsigned long accurateTimer{millis()}; + auto changeAnimationTimer{std::chrono::milliseconds(0)}; + uint8_t currentAnimation{0}; + for(;;){ + auto actualTimePassed{std::chrono::milliseconds(millis() - accurateTimer)}; + accurateTimer = millis(); + if(boardStateTimer >= boardStateMaxUpdatePeriod && boardManager.HasBoardChanged()){ printBoardState(); boardManager.ClearBoardChanged(); + boardStateTimer = std::chrono::milliseconds(0); } - animator.RunAnimation(std::chrono::milliseconds(millis() - accurateTimer)); - accurateTimer = millis(); + if(changeAnimationTimer >= std::chrono::duration_cast(std::chrono::minutes(1))){ + changeAnimationTimer = std::chrono::milliseconds(0); + currentAnimation++; + if(currentAnimation >= animations.size()){ + currentAnimation = 0; + } + animator.StartAnimation(animations[currentAnimation]); + } + + animator.RunAnimation(actualTimePassed); boardManager.Update(); - boardStateTimer += updateTickRate; + boardStateTimer += actualTimePassed; + changeAnimationTimer += actualTimePassed; vTaskDelay(updateTickRate.count()); } Serial.println("UpdateBoard task has ended unexpectedly!"); @@ -197,7 +220,7 @@ void setup() { Serial.println("Beginning Board Initializaiton"); boardManager.Init(); animator.SetLoop(true); - animator.StartAnimation(&(TestFrames::testAnimationSequence2)); + animator.StartAnimation(animations[0]); xTaskCreate(UpdateBoard, "UpdateBoard", 10000, NULL, 0, &updateBoardTask); Serial.println("Setup Complete");