Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (driver) implement our own LED driver #141

Merged
merged 9 commits into from
Feb 5, 2023
Merged
2 changes: 1 addition & 1 deletion mcu/include/SystemInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ namespace TL

struct TLInformation
{
float rps;
float fps;
uint16_t ledCount;
uint16_t hiddenLedCount;
};

static void begin();
Expand Down
2 changes: 0 additions & 2 deletions mcu/include/TesLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class TesLight
static unsigned long lightSensorInterval;
static unsigned long motionSensorInterval;
static unsigned long audioUnitInterval;
static unsigned long renderTimer;
static unsigned long frameTimer;
static unsigned long lightSensorTimer;
static unsigned long motionSensorTimer;
Expand All @@ -81,7 +80,6 @@ class TesLight
static unsigned long webServerTimer;

// Counter
static uint16_t renderCounter;
static uint16_t frameCounter;
static float ledPowerCounter;

Expand Down
2 changes: 1 addition & 1 deletion mcu/include/configuration/SystemConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#endif
#define LED_DEFAULT_COUNTS {2, 2, 2, 2, 2, 2, 2, 2} // Default number of LEDs for each channel
#define LED_DEFAULT_CHANNEL_CURRENT 16 // Default current per LED channel in mA
#define LED_MAX_COUNT_PER_ZONE 250 // Maximum number of LEDs per channel
#define ANIMATOR_NUM_ANIMATION_SETTINGS 25 // Number of custom fields in the LED configuration
#define ANIMATOR_DEFAULT_TYPE 0 // Default animation type
#define ANIMATOR_DEFAULT_DATA_SOURCE 0 // Default data source of the animation
Expand Down Expand Up @@ -158,7 +159,6 @@
#define WEB_SERVER_STATIC_CONTENT "/ui/" // Static content location for the UI

// Timer configuration
#define RENDER_INTERVAL 16666 // Interval for rendering the pixels in µs
#define FRAME_INTERVAL 16666 // Interval for outputting to the LEDs in µs
#define FAN_INTERVAL 500000 // Interval for running the fan controll in µs
#define LIGHT_SENSOR_INTERVAL 40000 // Interval for the light sensor in µs
Expand Down
23 changes: 10 additions & 13 deletions mcu/include/led/LedManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
#include "configuration/SystemConfiguration.h"
#include "configuration/Configuration.h"

#include "util/FileUtil.h"
#include "FastLED.h"

#include "led/driver/LedDriver.h"
#include "led/animator/RainbowAnimator.h"
#include "led/animator/GradientAnimator.h"
#include "led/animator/StaticColorAnimator.h"
Expand All @@ -41,6 +39,7 @@
#include "led/animator/GradientAnimatorMotion.h"
#include "led/animator/SparkleAnimator.h"

#include "util/FileUtil.h"
#include "sensor/MotionSensor.h"
#include "hardware/AudioUnit.h"

Expand All @@ -53,10 +52,11 @@ namespace TL
{
OK, // No error
ERROR_CONFIG_UNAVAILABLE, // The configuration is not available
ERROR_CREATE_LED_DATA, // Failed to create LED pixel data
ERROR_INIT_LED_DRIVER, // Failed to initialize the LED driver
ERROR_DRIVER_NOT_READY, // The LED driver is not ready to send new LED data
ERROR_UNKNOWN_ANIMATOR_TYPE, // The animator type is unknown
ERROR_INVALID_FSEQ, // When a custom animation was set but the fseq file is invalid
ERROR_FILE_NOT_FOUND, // The animation file was not found
ERROR_INVALID_FSEQ, // When a custom animation was set but the fseq file is invalid
ERROR_INVALID_LED_CONFIGURATION // The current LED configuration does not match the custom animation
};

Expand All @@ -69,9 +69,6 @@ namespace TL

static void setAmbientBrightness(const float ambientBrightness);

static void setRenderInterval(const uint32_t renderInterval);
static uint32_t getRenderInterval();

static void setFrameInterval(const uint32_t frameInterval);
static uint32_t getFrameInterval();

Expand All @@ -82,31 +79,31 @@ namespace TL

static float getLedPowerDraw();
static size_t getLedCount();
static size_t getHiddenLedCount();

static void render();
static void show();
static TL::LedManager::Error waitShow(const TickType_t timeout);
static TL::LedManager::Error show(const TickType_t timeout);

private:
LedManager();

static bool initialized;
static std::vector<std::vector<CRGB>> ledData;
static std::unique_ptr<TL::LedBuffer> ledBuffer;
static std::vector<std::unique_ptr<TL::LedAnimator>> ledAnimator;
static std::unique_ptr<TL::FseqLoader> fseqLoader;

static uint32_t renderInterval;
static uint32_t frameInterval;
static float regulatorTemperature;

static TL::LedManager::Error createLedData();
static TL::LedManager::Error initLedDriver();
static TL::LedManager::Error createAnimators();
static TL::LedManager::Error loadCalculatedAnimations();
static TL::LedManager::Error loadCustomAnimation(const String &fileName);

static void calculateRegulatorPowerDraw(float regulatorPower[REGULATOR_COUNT]);
static void limitPowerConsumption();
static void limitRegulatorTemperature();

static uint8_t getRegulatorIndexFromPin(const uint8_t pin);
};
}
Expand Down
10 changes: 5 additions & 5 deletions mcu/include/led/animator/ColorBarAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ namespace TL
};

ColorBarAnimator();
ColorBarAnimator(const TL::ColorBarAnimator::ColorBarMode colorBarMode, const CRGB color1, const CRGB color2);
ColorBarAnimator(const TL::ColorBarAnimator::ColorBarMode colorBarMode, const TL::Pixel color1, const TL::Pixel color2);
~ColorBarAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

void setColorBarMode(const TL::ColorBarAnimator::ColorBarMode colorBarMode);
void setColor(const CRGB color1, const CRGB color2);
void setColor(const TL::Pixel color1, const TL::Pixel color2);

private:
float angle;
TL::ColorBarAnimator::ColorBarMode colorBarMode;
CRGB color[2];
TL::Pixel color[2];
};
}

Expand Down
4 changes: 2 additions & 2 deletions mcu/include/led/animator/FseqAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace TL
FseqAnimator(TL::FseqLoader *fseqLoader, const bool loop = false);
~FseqAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
TL::FseqLoader *fseqLoader;
Expand Down
8 changes: 4 additions & 4 deletions mcu/include/led/animator/GradientAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ namespace TL
GRADIENT_CENTER = 1
};

GradientAnimator(const TL::GradientAnimator::GradientMode gradientMode, const CRGB color1, const CRGB color2);
GradientAnimator(const TL::GradientAnimator::GradientMode gradientMode, const TL::Pixel color1, const TL::Pixel color2);
~GradientAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
TL::GradientAnimator::GradientMode gradientMode;
CRGB color[2];
TL::Pixel color[2];
};
}

Expand Down
8 changes: 4 additions & 4 deletions mcu/include/led/animator/GradientAnimatorMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ namespace TL
GRADIENT_CENTER = 1
};

GradientAnimatorMotion(const TL::GradientAnimatorMotion::GradientMode gradientMode, const CRGB color1, const CRGB color2);
GradientAnimatorMotion(const TL::GradientAnimatorMotion::GradientMode gradientMode, const TL::Pixel color1, const TL::Pixel color2);
~GradientAnimatorMotion();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
TL::GradientAnimatorMotion::GradientMode gradientMode;
CRGB color[2];
TL::Pixel color[2];

float getMotionOffset();
};
Expand Down
10 changes: 5 additions & 5 deletions mcu/include/led/animator/LedAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <math.h>
#include <vector>

#include "FastLED.h"
#include "led/driver/LedStrip.h"
#include "sensor/MotionSensor.h"
#include "hardware/AudioUnit.h"

Expand Down Expand Up @@ -91,8 +91,8 @@ namespace TL
void setAudioAnalysis(const TL::AudioUnit::AudioAnalysis &audioAnalysis);
TL::AudioUnit::AudioAnalysis &getAudioAnalysis();

virtual void init(std::vector<CRGB> &pixels) = 0;
virtual void render(std::vector<CRGB> &pixels) = 0;
virtual void init(TL::LedStrip &ledStrip) = 0;
virtual void render(TL::LedStrip &ledStrip) = 0;

protected:
TL::LedAnimator::DataSource dataSource;
Expand All @@ -107,8 +107,8 @@ namespace TL
TL::MotionSensor::MotionSensorData motionSensorData;
TL::AudioUnit::AudioAnalysis audioAnalysis;

void reversePixels(std::vector<CRGB> &pixels);
void applyBrightness(std::vector<CRGB> &pixels);
void reversePixels(TL::LedStrip &ledStrip);
void applyBrightness(TL::LedStrip &ledStrip);
static int32_t random(const int32_t min, const int32_t max);
static float trapezoid(float angle);
static float trapezoid2(float angle);
Expand Down
4 changes: 2 additions & 2 deletions mcu/include/led/animator/RainbowAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace TL
RainbowAnimator(const TL::RainbowAnimator::RainbowMode rainbowMode);
~RainbowAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
float angle;
Expand Down
4 changes: 2 additions & 2 deletions mcu/include/led/animator/RainbowAnimatorMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace TL
RainbowAnimatorMotion(const TL::RainbowAnimatorMotion::RainbowMode rainbowMode);
~RainbowAnimatorMotion();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
float angle;
Expand Down
16 changes: 8 additions & 8 deletions mcu/include/led/animator/SparkleAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ namespace TL
float lastPosition; // Previous position of a spark
float speed; // Speed of a spark
float friction; // Friction of a spark
CRGB color; // Color of a spark
TL::Pixel color; // Color of a spark
float brightness; // Brightness of a spark
float fading; // Fafing speed of a spark
};

SparkleAnimator(const TL::SparkleAnimator::SpawnPosition spawnPosition, const uint8_t sparkCount, const CRGB color,
SparkleAnimator(const TL::SparkleAnimator::SpawnPosition spawnPosition, const uint8_t sparkCount, const TL::Pixel color,
const float sparkFriction, const float sparkFading, const float sparkTail, const float birthRate,
const float spawnVariance, const float speedVariance, const float brightnessVariance, const float frictionVariance,
const float fadingVariance, const bool bounceAtCorner, const uint8_t frequencyBandMask);
~SparkleAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
std::vector<CRGB> pixelBuffer;
std::vector<TL::Pixel> pixelBuffer;
std::vector<bool> pixelMask;
std::vector<Spark> sparks;

TL::SparkleAnimator::SpawnPosition spawnPosition;
CRGB color;
TL::Pixel color;
float sparkFriction;
float sparkFading;
float sparkTail;
Expand All @@ -81,8 +81,8 @@ namespace TL
float colorAngle;
uint8_t audioSequence;

void spawnSparks(std::vector<CRGB> &pixels);
void runSparks(std::vector<CRGB> &pixels);
void spawnSparks(TL::LedStrip &ledStrip);
void runSparks(TL::LedStrip &ledStrip);

template <typename T>
void limit(const T min, const T max, T &value)
Expand Down
8 changes: 4 additions & 4 deletions mcu/include/led/animator/StaticColorAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ namespace TL
class StaticColorAnimator : public LedAnimator
{
public:
StaticColorAnimator(const CRGB color);
StaticColorAnimator(const TL::Pixel color);
~StaticColorAnimator();

void init(std::vector<CRGB> &pixels);
void render(std::vector<CRGB> &pixels);
void init(TL::LedStrip &ledStrip);
void render(TL::LedStrip &ledStrip);

private:
CRGB color;
TL::Pixel color;
};
}

Expand Down
61 changes: 61 additions & 0 deletions mcu/include/led/driver/LedBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @file LedBuffer.h
* @author TheRealKasumi
* @brief Contains a class to manage the LED buffer for multiple LED strips.
*
* @copyright Copyright (c) 2022-2023 TheRealKasumi
*
* This project, including hardware and software, is provided "as is". There is no warranty
* of any kind, express or implied, including but not limited to the warranties of fitness
* for a particular purpose and noninfringement. TheRealKasumi (https://github.com/TheRealKasumi)
* is holding ownership of this project. You are free to use, modify, distribute and contribute
* to this project for private, non-commercial purposes. It is granted to include this hardware
* and software into private, non-commercial projects. However, the source code of any project,
* software and hardware that is including this project must be public and free to use for private
* persons. Any commercial use is hereby strictly prohibited without agreement from the owner.
* By contributing to the project, you agree that the ownership of your work is transferred to
* the project owner and that you lose any claim to your contribute work. This copyright and
* license note applies to all files of this project and must not be removed without agreement
* from the owner.
*
*/
#ifndef LED_BUFFER_H
#define LED_BUFFER_H

#include <stdint.h>
#include <stddef.h>
#include <vector>

#include "LedStrip.h"

namespace TL
{
class LedBuffer
{
public:
LedBuffer(const std::vector<TL::LedStrip> &ledStrips);
~LedBuffer();

size_t getBufferSize();
uint8_t *getBuffer();

size_t getTotalLedCount();
size_t getMaxLedCount();

size_t getTotalHiddenLedCount();
size_t getMaxHiddenLedCount();

size_t getLedStripCount();
TL::LedStrip &getLedStrip(const size_t index);

private:
std::vector<TL::LedStrip> ledStrips;
size_t totalLedCount;
size_t maxLedCount;
size_t totalHiddenLedCount;
size_t maxHiddenLedCount;
uint8_t *buffer;
};
}

#endif
Loading