/*
 * DisplayController.cpp
 *
 *  Created on: Aug 7, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "output/detail/DisplayController.h"

#include "output/DisplayController.h"

#include "utility/config.h"

namespace flippR_driver
{
namespace output
{
namespace detail
{

DisplayController::DisplayController(std::vector<std::shared_ptr<items::OutputDisplay>> displays,
                                     std::unique_ptr<DisplayBoardPinController> pin_controller
)
  : displays(std::move(displays)), pin_controller(std::move(pin_controller)), is_running(true)
{
  this->display_cycle_thread = std::thread(&DisplayController::cycle_displays, this);

  CLOG(INFO, OUTPUT_LOGGER) << "Created DisplayController and started cycling them.";

  activate_displays();
}

DisplayController::~DisplayController()
{
  this->is_running = false;

  this->display_cycle_thread.join();
}

void DisplayController::cycle_displays() const
{
  while (is_running)
  {
    for (auto &display : this->displays)
    {
      pin_controller->write_display(*display);
    }
  }
}

void DisplayController::activate_displays() const
{
  pin_controller->activate_displays();
  CLOG(INFO, OUTPUT_LOGGER) << "Activated all displays!";
}

void DisplayController::deactivate_displays() const
{
  pin_controller->deactivate_displays();
  CLOG(INFO, OUTPUT_LOGGER) << "Deactivated all displays!";
}

}
}
}
