From 14ac96988aa7dcd932fc399fa61bc421937e0e49 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 25 Aug 2024 16:11:06 -0400 Subject: [PATCH] Got the animator fades working --- include/BOARD-DEFINITIONS.h | 2 +- include/Vector3D.h | 32 +++++++--- lib/Animator/AnimationTypes.h | 4 +- lib/Animator/Animator.h | 115 +++++++++++++++++----------------- lib/Animator/TestFrames.h | 36 +++++++---- lib/Board/Board.h | 40 ++++++------ lib/Board/BoardDriver.h | 2 +- lib/Board/BoardManager.h | 57 ++++++++--------- lib/Board/BoardTypes.h | 2 +- src/main.cpp | 13 ++-- 10 files changed, 164 insertions(+), 139 deletions(-) diff --git a/include/BOARD-DEFINITIONS.h b/include/BOARD-DEFINITIONS.h index 4dd56da..4ded666 100644 --- a/include/BOARD-DEFINITIONS.h +++ b/include/BOARD-DEFINITIONS.h @@ -20,7 +20,7 @@ static constexpr uint32_t BOARD_WIDTH{3}; static constexpr uint32_t BOARD_LENGTH{3}; static constexpr uint32_t BOARD_HEIGHT{3}; -static constexpr V3D BOARD_DIMENSIONS{BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT}; +static constexpr V3D BOARD_DIMENSIONS{BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT}; // define the number of stacks static constexpr uint32_t NUMBER_STACKS{BOARD_WIDTH * BOARD_LENGTH}; diff --git a/include/Vector3D.h b/include/Vector3D.h index fdcb83d..cb60300 100644 --- a/include/Vector3D.h +++ b/include/Vector3D.h @@ -3,17 +3,31 @@ #include #include +template class V3D{ public: constexpr V3D(const V3D& other): x(other.x), y(other.y), - z(other.z){} + z(other.z){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + } - constexpr V3D(uint32_t x=0, uint32_t y=0, uint32_t z=0): + constexpr V3D(Type x=0, Type y=0, Type z=0): x(x), y(y), - z(z){} + z(z){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + } + + template + constexpr V3D(const V3D other): + x(static_cast(other.x)), + y(static_cast(other.y)), + z(static_cast(other.z)){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + static_assert(std::is_arithmetic::value, "OtherType must be a number"); + } V3D& operator=(const V3D &other){ this->x = other.x; @@ -36,7 +50,7 @@ class V3D{ return *this; } - V3D& operator/=(const uint32_t scalar){ + V3D& operator/=(const Type scalar){ if(scalar == 0){ return *this; } @@ -46,7 +60,7 @@ class V3D{ return *this; } - V3D& operator*=(const uint32_t scalar){ + V3D& operator*=(const Type scalar){ this->x *= scalar; this->y *= scalar; this->z *= scalar; @@ -58,9 +72,9 @@ class V3D{ } float magnitude(){ - return std::sqrt(this->x * this->x + this->y * this->y + this->z * this-> z); + return std::sqrt(static_cast(this->x * this->x + this->y * this->y + this->z * this->z)); } - uint32_t x; - uint32_t y; - uint32_t z; + Type x; + Type y; + Type z; }; \ No newline at end of file diff --git a/lib/Animator/AnimationTypes.h b/lib/Animator/AnimationTypes.h index 482ceeb..d3b29c5 100644 --- a/lib/Animator/AnimationTypes.h +++ b/lib/Animator/AnimationTypes.h @@ -19,8 +19,8 @@ namespace ANIMATION_TYPES{ }; struct Cell{ - V3D position; - V3D color; + V3D position; + V3D color; }; // this contains all of the information to specify exactly how a single frame should look and fade to the next frame diff --git a/lib/Animator/Animator.h b/lib/Animator/Animator.h index 0ee4d98..9c5af47 100644 --- a/lib/Animator/Animator.h +++ b/lib/Animator/Animator.h @@ -13,11 +13,11 @@ using namespace ANIMATION_TYPES; -template +template &BOARD_DIMS> class Animator{ public: -typedef std::array, BOARD_DIMS.y>, BOARD_DIMS.x> Frame; +typedef std::array, BOARD_DIMS.z>, BOARD_DIMS.y>, BOARD_DIMS.x> Frame; void StartAnimation(const std::vector *animationSequence); @@ -26,15 +26,13 @@ void RunAnimation(const std::chrono::milliseconds& timePassed); void SetLoop(bool isLooping); -bool HasInterpolatedFrameChanged(){return this->interpolatedFrameHasChanged;} - Frame &GetInterpolatedFrame(){return this->interpolatedFrame;} bool isEnabled{true}; +bool interpolatedFrameHasChanged{false}; private: bool isLooping{true}; -bool interpolatedFrameHasChanged{false}; // these are the uncompressed frames you get by following the key colors and interpolation instructions of an animation frame Frame startFrame; Frame interpolatedFrame; @@ -52,17 +50,17 @@ void copyFrame(Frame ©From, Frame ©To){ std::memcpy(©To, ©From, sizeof(Frame)); } -V3D getInterpolatedColor(const AnimationFrame &keyFrame, V3D position); +V3D getInterpolatedColor(const AnimationFrame &keyFrame, V3D position); -V3D keyFrame2BoardCoords(const V3D &keyFramePosition); +V3D keyFrame2BoardCoords(const V3D &keyFramePosition); -V3D noFillInterpolate(const AnimationFrame &keyFrame, V3D position); +V3D noFillInterpolate(const AnimationFrame &keyFrame, V3D position); -V3D closestColorInterpolate(const AnimationFrame &keyFrame, V3D position); +V3D closestColorInterpolate(const AnimationFrame &keyFrame, V3D position); -V3D linearInterpolate(const AnimationFrame &keyFrame, V3D position); +V3D linearInterpolate(const AnimationFrame &keyFrame, V3D position); -V3D squareInterpolate(const AnimationFrame &keyFrame, V3D position); +V3D squareInterpolate(const AnimationFrame &keyFrame, V3D position); void PrintUncompressedFrame(){ for(uint32_t x = 0; x < BOARD_DIMS.x; x++){ @@ -78,7 +76,7 @@ void PrintUncompressedFrame(){ }; -template +template &BOARD_DIMS> void Animator::StartAnimation(const std::vector *animationSequence){ this->animationSequence = animationSequence; @@ -99,13 +97,13 @@ void Animator::StartAnimation(const std::vector *ani this->copyFrame(this->startFrame, this->interpolatedFrame); this->uncompressFrame((*this->animationSequence)[1], this->endFrame); } - this->PrintUncompressedFrame(); + this->interpolatedFrameHasChanged = true; } -template +template &BOARD_DIMS> void Animator::RunAnimation(const std::chrono::milliseconds& timePassed){ if(!(this->isEnabled)){ - this->interpolatedFrameHasChanged = false; + return; } auto delayTime = (*this->animationSequence)[this->animationIndex].delay; @@ -118,19 +116,19 @@ void Animator::RunAnimation(const std::chrono::milliseconds& timePas // don't do frame interpolations if we're doing snap fades if((*this->animationSequence)[this->animationIndex].frameInterpolation == FrameInterpolation::SNAP){ - this->interpolatedFrameHasChanged = false; + return; } // linearly interpolate between the two uncompressed frames for(uint32_t x = 0; x < BOARD_DIMS.x; x++){ for(uint32_t y = 0; y < BOARD_DIMS.y; y++){ for(uint32_t z = 0; z < BOARD_DIMS.z; z++){ - V3D startColor{this->startFrame[x][y][z]}; - V3D endColor{this->endFrame[x][y][z]}; - V3D difference{endColor}; + V3D startColor{this->startFrame[x][y][z]}; + V3D endColor{this->endFrame[x][y][z]}; + V3D difference{endColor}; difference -= startColor; - V3D interpolatedColor{difference}; + V3D interpolatedColor{difference}; interpolatedColor *= this->timeElapsed.count(); interpolatedColor /= delayTime.count(); interpolatedColor += startColor; @@ -143,40 +141,40 @@ void Animator::RunAnimation(const std::chrono::milliseconds& timePas this->interpolatedFrameHasChanged = true; } -template +template &BOARD_DIMS> void Animator::SetLoop(bool isLooping){ this->isLooping = isLooping; } -template +template &BOARD_DIMS> void Animator::incrimentAnimationIndex(){ - if(this->animationIndex < this->animationSequence->size() - 1){ + if(this->animationIndex < this->animationSequence->size() - 2){ this->animationIndex++; this->timeElapsed = std::chrono::milliseconds(0); this->uncompressFrame((*this->animationSequence)[this->animationIndex], this->startFrame); + this->copyFrame(this->startFrame, this->interpolatedFrame); this->uncompressFrame((*this->animationSequence)[this->animationIndex + 1], this->endFrame); } - else if(isLooping){ + else{ this->StartAnimation(this->animationSequence); } - Serial.println("Animator::incrimentAnimationIndex" + String(animationIndex)); - this->PrintUncompressedFrame(); + this->interpolatedFrameHasChanged = true; } -template +template &BOARD_DIMS> void Animator::uncompressFrame(const AnimationFrame &keyFrame, Frame &frameBuffer){ for(uint32_t x = 0; x < BOARD_DIMS.x; x++){ for(uint32_t y = 0; y < BOARD_DIMS.y; y++){ for(uint32_t z = 0; z < BOARD_DIMS.z; z++){ - frameBuffer[x][y][z] = getInterpolatedColor(keyFrame, V3D(x, y, z)); + frameBuffer[x][y][z] = getInterpolatedColor(keyFrame, V3D(x, y, z)); } } } } -template -V3D Animator::getInterpolatedColor(const AnimationFrame &keyFrame, V3D position){ +template &BOARD_DIMS> +V3D Animator::getInterpolatedColor(const AnimationFrame &keyFrame, V3D position){ switch(keyFrame.fillInterpolation){ case FillInterpolation::NO_FILL: return noFillInterpolate(keyFrame, position); @@ -187,19 +185,19 @@ V3D Animator::getInterpolatedColor(const AnimationFrame &keyFrame, V case FillInterpolation::SQUARE_WEIGHTED_DISTANCE: return squareInterpolate(keyFrame, position); default: - V3D black{}; + V3D black{}; return black; } } -template -V3D Animator::keyFrame2BoardCoords(const V3D &keyFramePosition){ - V3D returnValue{}; +template &BOARD_DIMS> +V3D Animator::keyFrame2BoardCoords(const V3D &keyFramePosition){ + V3D returnValue{}; float maxValue{static_cast(std::numeric_limits::max())}; // scale the key frame values down to be within board coordinates - float keyFrame_X = static_cast(BOARD_DIMS.x) * static_cast(keyFramePosition.x) / maxValue; - float keyFrame_Y = static_cast(BOARD_DIMS.y) * static_cast(keyFramePosition.y) / maxValue; - float keyFrame_Z = static_cast(BOARD_DIMS.z) * static_cast(keyFramePosition.z) / maxValue; + float keyFrame_X = static_cast(BOARD_DIMS.x - 1) * static_cast(keyFramePosition.x) / maxValue; + float keyFrame_Y = static_cast(BOARD_DIMS.y - 1) * static_cast(keyFramePosition.y) / maxValue; + float keyFrame_Z = static_cast(BOARD_DIMS.z - 1) * static_cast(keyFramePosition.z) / maxValue; // carefully quantize the float values back into ints with a precise rounding operation if(keyFrame_X - std::floor(keyFrame_X) < 0.5f){ @@ -226,9 +224,9 @@ V3D Animator::keyFrame2BoardCoords(const V3D &keyFramePosition){ return returnValue; } -template -V3D Animator::noFillInterpolate(const AnimationFrame &keyFrame, V3D position){ - V3D returnColor{}; +template &BOARD_DIMS> +V3D Animator::noFillInterpolate(const AnimationFrame &keyFrame, V3D position){ + V3D returnColor{}; for(Cell cell : keyFrame.frame){ if(keyFrame2BoardCoords(cell.position) == position){ returnColor = cell.color; @@ -238,34 +236,35 @@ V3D Animator::noFillInterpolate(const AnimationFrame &keyFrame, V3D return returnColor; } -template -V3D Animator::closestColorInterpolate(const AnimationFrame &keyFrame, V3D position){ - V3D returnColor{}; - V3D distance{keyFrame.frame[0].position}; - distance -= position; +template &BOARD_DIMS> +V3D Animator::closestColorInterpolate(const AnimationFrame &keyFrame, V3D cubePosition){ + Serial.print("X:" + String(cubePosition.x) + ",Y:" + String(cubePosition.y) + ",Z:" + String(cubePosition.z)); + V3D returnColor{keyFrame.frame[0].color}; + V3D distance{keyFrame.frame[0].position}; + distance -= cubePosition; float closestDistance = distance.magnitude(); + for(Cell cell : keyFrame.frame){ distance = keyFrame2BoardCoords(cell.position); - distance -= position; + distance -= cubePosition; float euclidDistance = distance.magnitude(); if(euclidDistance < closestDistance){ returnColor = cell.color; closestDistance = euclidDistance; } } - return returnColor; } -template -V3D Animator::linearInterpolate(const AnimationFrame &keyFrame, V3D position){ - V3D returnColor{}; +template &BOARD_DIMS> +V3D Animator::linearInterpolate(const AnimationFrame &keyFrame, V3D position){ + V3D returnColor{}; for(Cell cell : keyFrame.frame){ - V3D vectorDistance{keyFrame2BoardCoords(cell.position)}; + V3D vectorDistance{keyFrame2BoardCoords(cell.position)}; vectorDistance -= position; - uint32_t distance = static_cast(vectorDistance.magnitude()); - if(distance == 0) distance = 1; + float distance = vectorDistance.magnitude(); + if(distance == 0) return cell.color; returnColor += cell.color; returnColor /= distance; } @@ -275,16 +274,16 @@ V3D Animator::linearInterpolate(const AnimationFrame &keyFrame, V3D return returnColor; } -template -V3D Animator::squareInterpolate(const AnimationFrame &keyFrame, V3D position){ - V3D returnColor{}; +template &BOARD_DIMS> +V3D Animator::squareInterpolate(const AnimationFrame &keyFrame, V3D position){ + V3D returnColor{}; for(Cell cell : keyFrame.frame){ - V3D vectorDistance{keyFrame2BoardCoords(cell.position)}; + V3D vectorDistance{keyFrame2BoardCoords(cell.position)}; vectorDistance -= position; uint32_t distance = static_cast(vectorDistance.magnitude()); distance *= distance; - if(distance == 0) distance = 1; + if(distance == 0) return cell.color; returnColor += cell.color; returnColor /= distance; } diff --git a/lib/Animator/TestFrames.h b/lib/Animator/TestFrames.h index 673955c..5312dd7 100644 --- a/lib/Animator/TestFrames.h +++ b/lib/Animator/TestFrames.h @@ -8,15 +8,15 @@ using namespace ANIMATION_TYPES; namespace TestFrames{ - V3D red{255,0,0}; - V3D green{0,255,0}; - V3D blue{0,0,255}; + V3D red{255,0,0}; + V3D green{0,255,0}; + V3D blue{0,0,255}; uint32_t maxValue{std::numeric_limits::max()}; - Cell CreateCell(float x_percent, float y_percent, float z_percent, V3D &color){ + 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{ + .position = V3D{ static_cast(continuousMaxValue*x_percent), static_cast(continuousMaxValue*y_percent), static_cast(continuousMaxValue*z_percent) @@ -126,33 +126,43 @@ namespace TestFrames{ squareFillFadeFrame // 7 }; + AnimationFrame testFrame1{ + .frame = { + CreateCell(0,0,0,red), + // CreateCell(0.5,0.5,0,green), + CreateCell(1,1,0,blue) + }, + .fillInterpolation = FillInterpolation::NO_FILL, + .frameInterpolation = FrameInterpolation::FADE, + .delay = std::chrono::milliseconds(10000) + }; - AnimationFrame noFillFrame2{ + AnimationFrame testFrame2{ .frame = { CreateCell(0,1,0,red), // CreateCell(0.5,0.5,0,green), CreateCell(1,0,0,green) }, .fillInterpolation = FillInterpolation::NO_FILL, - .frameInterpolation = FrameInterpolation::SNAP, + .frameInterpolation = FrameInterpolation::FADE, .delay = std::chrono::milliseconds(10000) }; - AnimationFrame noFillFrame3{ + AnimationFrame testFrame3{ .frame = { CreateCell(0.5,0.5,0,red), // CreateCell(0.5,0.5,0,green), CreateCell(0,1,0,blue) }, .fillInterpolation = FillInterpolation::NO_FILL, - .frameInterpolation = FrameInterpolation::SNAP, + .frameInterpolation = FrameInterpolation::FADE, .delay = std::chrono::milliseconds(10000) }; std::vector testAnimationSequence1{ - noFillFrame, - noFillFrame2, - noFillFrame3, - noFillFrame3 + testFrame1, + testFrame2, + testFrame3, + testFrame1 }; } \ No newline at end of file diff --git a/lib/Board/Board.h b/lib/Board/Board.h index c5496e3..00be54c 100644 --- a/lib/Board/Board.h +++ b/lib/Board/Board.h @@ -12,13 +12,13 @@ #include "BoardTypes.h" #include "Vector3D.h" -template +template &BOARD_DIMS> class Board{ public: Board() = default; ~Board() = default; - constexpr const V3D &GetSize() const{return BOARD_DIMS;} + constexpr const V3D &GetSize() const{return BOARD_DIMS;} constexpr uint32_t GetNumberCubes() const{return BOARD_DIMS.x * BOARD_DIMS.y * BOARD_DIMS.z;} constexpr uint32_t GetMaxDimension(){return std::max(std::max(BOARD_DIMS.x, BOARD_DIMS.y), BOARD_DIMS.z);} @@ -34,7 +34,7 @@ class Board{ * @brief fill the entire board with the given color * @param color the color to fill the board with */ - void FillColor(const V3D &color); + void FillColor(const V3D &color); /** * @brief Set the color of the cube at the given position. @@ -43,7 +43,7 @@ class Board{ * @param position the position of the cube. * @param color the color you want the cube to be */ - void SetCubeColor(const V3D &position, const V3D &color); + void SetCubeColor(const V3D &position, const V3D &color); /** * @brief Set the occupation status of the cube at a given position @@ -52,7 +52,7 @@ class Board{ * @post if the new occupation status of the cube is different than * the old occupation status, this will enable boardStateHasChanged. */ - void SetCubeOccupation(const V3D &position, bool occupation); + void SetCubeOccupation(const V3D &position, bool occupation); /** * @returns true if the board state has changed since this flag was last set to false @@ -69,18 +69,18 @@ class Board{ * @brief Get a column along any axis read into the sliceBuffer * @param column .z specifies the normal direction of the plane (see PLANE_NORMAL), and * the x,y values specify the location of the column in that plane - * to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z) + * to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z) * @param sliceBuffer an array of pointers to the cubes along that column * @returns the number of elements written into the slice buffer * @note That array is stored locally and will be overwritten everytime this function is called. * Also, any unused spots at the end of the array will be nullptrs * @warning allocate the size of the slice buffer using GetMaxDimension if you don't know what you're doing! */ - uint32_t SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer); + uint32_t SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer); void PrintEntireBoard() const; - void UpdateAllColors(const std::array, BOARD_DIMS.y>, BOARD_DIMS.x>& colorFrame){ + void UpdateAllColors(const std::array, BOARD_DIMS.z>, BOARD_DIMS.y>, BOARD_DIMS.x>& colorFrame){ for(uint32_t x = 0; x < BOARD_DIMS.x; x++){ for(uint32_t y = 0; y < BOARD_DIMS.y; y++){ for(uint32_t z = 0; z < BOARD_DIMS.z; z++){ @@ -106,7 +106,7 @@ class Board{ bool boardStateHasChanged; }; -template +template &BOARD_DIMS> void Board::ToStackString(String &stringBuffer) const{ std::array linearizedBoard; for(uint32_t x{0}; x < BOARD_DIMS.x; x++){ @@ -126,8 +126,8 @@ void Board::ToStackString(String &stringBuffer) const{ } } -template -void Board::FillColor(const V3D &color){ +template &BOARD_DIMS> +void Board::FillColor(const V3D &color){ for(uint32_t x{0}; x < BOARD_DIMS.x; x++){ for(uint32_t y{0}; y < BOARD_DIMS.y; y++){ for(uint32_t z{0}; z < BOARD_DIMS.z; z++){ @@ -137,23 +137,23 @@ void Board::FillColor(const V3D &color){ } } -template -void Board::SetCubeColor(const V3D &position, const V3D &color){ +template &BOARD_DIMS> +void Board::SetCubeColor(const V3D &position, const V3D &color){ this->cubes[position.x][position.y][position.z].color = color; } -template -void Board::SetCubeOccupation(const V3D &position, bool occupation){ +template &BOARD_DIMS> +void Board::SetCubeOccupation(const V3D &position, bool occupation){ bool oldOccupation{this->cubes[position.x][position.y][position.z].isOccupied}; this->cubes[position.x][position.y][position.z].isOccupied = occupation; if(occupation != oldOccupation) this->boardStateHasChanged = true; } -template -uint32_t Board::SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer){ +template &BOARD_DIMS> +uint32_t Board::SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer){ uint32_t columnLength{0}; - V3D indexIncrimentVector{}; - V3D indexVector{}; + V3D indexIncrimentVector{}; + V3D indexVector{}; switch(column.z){ case BOARD_TYPES::PLANE_NORMAL::X: @@ -189,7 +189,7 @@ uint32_t Board::SliceBoard(const V3D &column, BOARD_TYPES::Cube ** s return columnLength; } -template +template &BOARD_DIMS> void Board::PrintEntireBoard() const{ for(uint32_t x = 0; x < BOARD_DIMS.x; x++){ for(uint32_t y = 0; y < BOARD_DIMS.y; y++){ diff --git a/lib/Board/BoardDriver.h b/lib/Board/BoardDriver.h index 7be2a35..fa49550 100644 --- a/lib/Board/BoardDriver.h +++ b/lib/Board/BoardDriver.h @@ -129,7 +129,7 @@ void BoardDriver::UpdateStackLEDs( ){ this->pixelController.setPin(this->stacks[stackIndex].ledPin); for(int i = 0; i < numCubes; i++){ - V3D color{cubes[i]->color}; + V3D color{cubes[i]->color}; this->pixelController.setPixelColor(i*2, this->pixelController.Color(color.x, color.y, color.z)); this->pixelController.setPixelColor((i*2 + 1), this->pixelController.Color(color.x, color.y, color.z)); } diff --git a/lib/Board/BoardManager.h b/lib/Board/BoardManager.h index 417117b..4710e1b 100644 --- a/lib/Board/BoardManager.h +++ b/lib/Board/BoardManager.h @@ -9,7 +9,7 @@ #include "Vector3D.h" #include "Animator.h" -template +template &BOARD_DIMS> class BoardManager{ public: BoardManager(BoardDriver &boardDriver, Animator &animator); @@ -31,7 +31,7 @@ class BoardManager{ * @param position the position of the cube * @param color the oclor you want the cube to be */ - void SetCubeColor(const V3D &position, const V3D &color); + void SetCubeColor(const V3D &position, const V3D &color); /** * @brief Set the color of one column of cubes. @@ -40,17 +40,17 @@ class BoardManager{ * @param column the column vector * @param color the color you want the column to be */ - void SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors); + void SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors); /** * @brief Fill a column along any axis with a color * @param column .z specifies the normal direction of the plane (see PLANE_NORMAL), and * the x,y values specify the location of the column in that plane - * to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z) + * to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z) * @param color the color you want to fill the column with */ - void FillColumnColor(const V3D &column, const V3D &color); + void FillColumnColor(const V3D &column, const V3D &color); /** * @returns true if the board has changed state @@ -67,14 +67,14 @@ class BoardManager{ */ void Board2StackString(String& messageBuffer); - void FillColor(const V3D &color){this->board.FillColor(color);} + void FillColor(const V3D &color){this->board.FillColor(color);} private: BoardDriver &driver; Board board{}; Animator &animator; - void updateStackColors(const V3D &column); + void updateStackColors(const V3D &column); uint32_t getColumnHeight(BOARD_TYPES::PLANE_NORMAL normal){ switch(normal){ @@ -95,24 +95,25 @@ class BoardManager{ void updateColorsFromAnimator(); }; -template +template &BOARD_DIMS> void BoardManager::updateColorsFromAnimator(){ - if(this->animator.HasInterpolatedFrameChanged()){ + if(this->animator.interpolatedFrameHasChanged){ this->board.UpdateAllColors(this->animator.GetInterpolatedFrame()); + this->animator.interpolatedFrameHasChanged = false; } } -template +template &BOARD_DIMS> BoardManager::BoardManager(BoardDriver &boardDriver, Animator &animator): driver(boardDriver), animator(animator){} -template +template &BOARD_DIMS> void BoardManager::Init(){ this->driver.Init(); } -template +template &BOARD_DIMS> void BoardManager::Update(){ this->updateColorsFromAnimator(); // update the occupied cubes on the board and the cube colors @@ -121,13 +122,13 @@ void BoardManager::Update(){ uint32_t stackIndex{y * BOARD_DIMS.x + x}; uint32_t numCubes{this->driver.GetNumberCubes(stackIndex)}; for(uint32_t z = 0; z < BOARD_DIMS.z; z++){ - V3D cubePosition{x, y, z}; + V3D cubePosition{x, y, z}; // update the cube's occupation this->board.SetCubeOccupation(cubePosition, z < numCubes); } // create the column vector for the slice direction - V3D sliceVector{x,y,BOARD_TYPES::PLANE_NORMAL::Z}; + V3D sliceVector{x,y,BOARD_TYPES::PLANE_NORMAL::Z}; // create a cube slice array buffer BOARD_TYPES::Cube* sliceBuffer[BOARD_DIMS.z]; // have the board slice get read into our buffer @@ -138,10 +139,10 @@ void BoardManager::Update(){ } } -template -void BoardManager::updateStackColors(const V3D &column){ +template &BOARD_DIMS> +void BoardManager::updateStackColors(const V3D &column){ // the only column type allowed here is z. - V3D sliceVector{column.x, column.y, BOARD_TYPES::Z}; + V3D sliceVector{column.x, column.y, BOARD_TYPES::Z}; // create a buffer for slice board to write the cube slice into BOARD_TYPES::Cube * cubeSlice[BOARD_DIMS.z]; this->board.SliceBoard(column, cubeSlice); @@ -150,15 +151,15 @@ void BoardManager::updateStackColors(const V3D &column){ this->driver.UpdateStackLEDs(BOARD_DIMS.x, cubeSlice, numCubes); } -template -void BoardManager::SetCubeColor(const V3D &position, const V3D &color){ +template &BOARD_DIMS> +void BoardManager::SetCubeColor(const V3D &position, const V3D &color){ this->board.SetCubeColor(position, color); - V3D slice{position.x, position.y, BOARD_TYPES::PLANE_NORMAL::Z}; + V3D slice{position.x, position.y, BOARD_TYPES::PLANE_NORMAL::Z}; this->updateStackColors(slice); } -template -void BoardManager::SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors){ +template &BOARD_DIMS> +void BoardManager::SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors){ uint32_t columnHeight{this->getColumnHeight(static_cast(column.z))}; // create a cube pointer buffer and store a board slice into it @@ -171,11 +172,11 @@ void BoardManager::SetColumnColors(const V3D &column, const V3D *col } } -template -void BoardManager::FillColumnColor(const V3D &column, const V3D &color){ +template &BOARD_DIMS> +void BoardManager::FillColumnColor(const V3D &column, const V3D &color){ uint32_t columnHeight{this->getColumnHeight(column.z)}; - V3D colors[columnHeight]; + V3D colors[columnHeight]; for(uint32_t i = 0; i < columnHeight; i++){ colors[i] = color; } @@ -183,13 +184,13 @@ void BoardManager::FillColumnColor(const V3D &column, const V3D &col this->SetColumnColors(column, colors); } -template +template &BOARD_DIMS> bool BoardManager::HasBoardChanged(){return this->board.BoardStateChanged();} -template +template &BOARD_DIMS> void BoardManager::ClearBoardChanged(){this->board.SetStateChanged(false);} -template +template &BOARD_DIMS> void BoardManager::Board2StackString(String& messageBuffer){ this->board.ToStackString(messageBuffer); } diff --git a/lib/Board/BoardTypes.h b/lib/Board/BoardTypes.h index 812c341..4eba9a5 100644 --- a/lib/Board/BoardTypes.h +++ b/lib/Board/BoardTypes.h @@ -20,7 +20,7 @@ namespace BOARD_TYPES{ }; struct Cube{ - V3D color; + V3D color; bool isOccupied{false}; }; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ccb7ede..c27de7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -83,15 +83,15 @@ void SetStackColor(uint32_t * args, int argsLength){ uint32_t Y_COORD{(stackNum - X_COORD) / BOARD_DIMENSIONS.y}; uint32_t numColors = (argsLength - 2) / 3; - V3D colors[numColors]; + V3D colors[numColors]; for(int i = 0; i < numColors; i++){ uint32_t red = args[2 + (i * 3)]; uint32_t green = args[3 + (i * 3)]; uint32_t blue = args[4 + (i * 3)]; - colors[i] = V3D{red, green, blue}; + colors[i] = V3D{red, green, blue}; } - boardManager.SetColumnColors(V3D{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors, numColors); + boardManager.SetColumnColors(V3D{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors, numColors); } void parseData(Message &message){ @@ -110,7 +110,7 @@ void parseData(Message &message){ case Commands::SetStackColors:{ GlobalPrint::Println("!2;"); animator.isEnabled = false; - V3D black{}; + V3D black{}; boardManager.FillColor(black); SetStackColor(reinterpret_cast(args), argsLength); break; @@ -157,14 +157,15 @@ void UpdateBoard(void * params){ auto updateTickRate{std::chrono::milliseconds(8)}; auto boardStateTimer{std::chrono::milliseconds(0)}; auto boardStateMaxUpdatePeriod{std::chrono::milliseconds(34)}; // this is a little slower than 30fps - + unsigned long accurateTimer{millis()}; for(;;){ if(boardStateTimer >= boardStateMaxUpdatePeriod && boardManager.HasBoardChanged()){ printBoardState(); boardManager.ClearBoardChanged(); } - animator.RunAnimation(updateTickRate); + animator.RunAnimation(std::chrono::milliseconds(millis() - accurateTimer)); + accurateTimer = millis(); boardManager.Update(); boardStateTimer += updateTickRate;