aboutsummaryrefslogtreecommitdiff
// -*- mode: c++ -*-
#pragma once

#include <Kaleidoscope-LEDControl.h>

class DakkarColor: public kaleidoscope::plugin::LEDMode {
public:
  typedef const cRGB (*color_function)();

  struct colorSrc {
    enum { VALUE, FUNCTION };
    uint8_t value_or_function;
    union {
      cRGB value;
      color_function function;
    } vf;
  };

  typedef const uint8_t (*color_index_map)[Kaleidoscope.device().matrix_rows * Kaleidoscope.device().matrix_columns];
  typedef const colorSrc *color_src_array;

  DakkarColor(
              const color_index_map _map,
              const color_src_array _colors
              ) : map(_map),
                  color_sources(_colors) {}
private:
  const color_index_map map;
  const color_src_array color_sources;

  cRGB getColor(uint8_t layer, KeyAddr key_addr) {
    uint8_t index = map[layer][key_addr.toInt()];
    colorSrc color_src = color_sources[index];
    if (color_src.value_or_function == colorSrc::VALUE) {
      return color_src.vf.value;
    }
    else {
      return color_src.vf.function();
    }
  }
protected:
  void update(void) final {
    uint8_t layer = Layer.mostRecent();
    for (auto key_addr : KeyAddr::all()) {
      LEDControl.setCrgbAt(key_addr, getColor(layer,key_addr));
    }
  }
  void refreshAt(KeyAddr key_addr) final {
    LEDControl.setCrgbAt(key_addr, getColor(Layer.mostRecent(),key_addr));
  }
};