Skip to content
Snippets Groups Projects
device_main.cpp 2.01 KiB
#include <qthing>

#include "esp_log.h"
#include "driver/gpio.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"



using qthing::Animation;
using qthing::Animator;
using qthing::RGBW8;
using qthing::HSV;


// TODO: Move somewhere else!
constexpr uint8_t channelW = 27;
constexpr uint8_t channelX = 23;
constexpr uint8_t channelY = 22;
constexpr uint8_t channelZ = 21;


class FunctionTestAnimation : public Animation<HSV> {

  public:
    FunctionTestAnimation(uint8_t size) : size((double)size) {}

    void step() override final {

      const float lowerBound = 0.01;
      const float upperBound = 0.2;

      this->time = qthing::millis();
      this->v = (sin(this->time/1500.l)/2+0.5)*(upperBound-lowerBound)+lowerBound;
    }

    HSV render(uint16_t address) {
      float h = fmod(this->time/6400.l + address/size, 1);

      return HSV(h, 1.0f, 1.0f);
    }

  private:
    float v;
    long time;
    double size;

};


class FlashAnimation : public Animation<HSV> {

  public:
    FlashAnimation() {}

    void step() override final {

      f++;

      uint8_t period = 60 / frq;
      period /= 2;

      if (f >= period) {
        f = 0;
        state ^= true;
      }

    }
    HSV render(uint16_t address) {
      return HSV(0.0f, 0.0f, state * 1.0f);
    }

  private:

    uint8_t frq = 10;

    bool state = false;
    uint8_t f = 0;

};




void device_main() {

  qthing::Config cfg;

  auto ledW = cfg.add<qthing::leds::WS2812B_V2>((gpio_num_t)channelW, 8);
  auto ledX = cfg.add<qthing::leds::WS2812B_V2>((gpio_num_t)channelX, 50);
  auto ledY = cfg.add<qthing::leds::WS2812B_V2>((gpio_num_t)channelY, 50);
  auto ledZ = cfg.add<qthing::leds::WS2812B_V2>((gpio_num_t)channelZ, 50);

  cfg.add<Animator<RGBW8>>(ledW)->setNewAnimation<FunctionTestAnimation>(8);
  cfg.add<Animator<RGBW8>>(ledX)->setNewAnimation<FunctionTestAnimation>(50);
  cfg.add<Animator<RGBW8>>(ledY)->setNewAnimation<FunctionTestAnimation>(50);
  cfg.add<Animator<RGBW8>>(ledZ)->setNewAnimation<FunctionTestAnimation>(50);
  
  cfg.apply();

}