/*
 * PinController.cpp
 *
 *  Created on: Jun 15, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "PinController.h"

#include "utility/config.h"
#ifndef NO_WIRING_PI
#include <wiringPi.h>
#include <mcp23017.h>
#else
#include "utility/wiringPiTesting.hpp"
#endif

#include "json/json.hpp"

namespace flippR_driver
{

std::once_flag PinController::GPIO_LIB_INITIALIZED;

PinController::PinController()
{
  std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
}

void PinController::initialize_input_pin(uint8_t address)
{
  pinMode(address, INPUT);
}

void PinController::initialize_output_pin(const uint8_t address)
{
  pinMode(address, OUTPUT);
}

void PinController::write_pin(uint8_t address, bool value)
{
  digitalWrite(address, value);
}

bool PinController::read_pin(uint8_t address)
{
  return PULLDOWN == digitalRead(address);
}

void PinController::initialize_port_expander(const uint8_t i2c_address, const uint8_t pin_base)
{
  mcp23017Setup(pin_base, i2c_address);
}


void PinController::initialize_pins_output(const uint8_t pin_base, std::map<std::string, uint8_t>::iterator begin, std::map<std::string, uint8_t>::iterator end)
{
  for(; begin != end; begin++)
  {
    initialize_output_pin(pin_base + begin->second);
  }
}

}
