<commit_before>#include <yaml-cpp/yaml.h>

#include <iostream>
#include <fstream>
#include <map>

#include "Config.hh"
#include "Exception.hh"

namespace aegir {

  // PIN default configuration
  pinconfig_t g_pinconfig = pinconfig_t{
    {"swled", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"swon",  PinConfig(PinMode::IN, PinPull::DOWN)},
    {"swoff", PinConfig(PinMode::IN, PinPull::DOWN)},
    {"cs0", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs1", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs2", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs3", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"rimsheat", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"rimspump", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"buzzer", PinConfig(PinMode::OUT, PinPull::NONE)}
  };

  static std::set<std::string> g_tcnames{"RIMS", "MashTun", "HLT", "BK"};

  // SPI ChipSelector string translations
  static std::map<ChipSelectors, std::string> g_spi_cs_to_string{
    {ChipSelectors::DirectSelect, "DirectSelect"}};
  static std::map<std::string, ChipSelectors> g_spi_string_to_cs{
    {"DirectSelect",ChipSelectors::DirectSelect}};
  // ThermoCouple type lookups
  std::map<std::string, MAX31856::TCType> g_string_to_tctype{
    {"B", MAX31856::TCType::B},
      {"E", MAX31856::TCType::E},
	{"J", MAX31856::TCType::J},
	  {"K", MAX31856::TCType::K},
	    {"N", MAX31856::TCType::N},
	      {"R", MAX31856::TCType::R},
		{"S", MAX31856::TCType::S},
		  {"T", MAX31856::TCType::T}
  };
  std::map<MAX31856::TCType, std::string> g_tctype_to_string{
    {MAX31856::TCType::B, "B"},
      {MAX31856::TCType::E, "E"},
	{MAX31856::TCType::J, "J"},
	  {MAX31856::TCType::K, "K"},
	    {MAX31856::TCType::N, "N"},
	      {MAX31856::TCType::R, "R"},
		{MAX31856::TCType::S, "S"},
		  {MAX31856::TCType::T, "T"}
  };
  // Noisefilter lookups
  std::map<std::string, NoiseFilters> g_string_to_noisefilter{
    {"50Hz", NoiseFilters::HZ50},
    {"60Hz", NoiseFilters::HZ60}
  };
  std::map<NoiseFilters, std::string> g_noisefilter_to_string{
    {NoiseFilters::HZ50, "50Hz"},
      {NoiseFilters::HZ60, "60Hz"}
  };

  // The singleton instance holder
  Config *Config::c_instance=0;

  Config::Config(const std::string &_cfgfile, bool _noload): c_cfgfile(_cfgfile) {
    setDefaults();
    if ( !_noload ) load();
  }

  Config::~Config() {
    save();
  }

  Config *Config::instantiate(const std::string &_cfgfile, bool _noload) {
    if ( !c_instance ) c_instance = new Config(_cfgfile, _noload);
    return c_instance;
  }

  // TODO: throw exception when !c_instance
  Config *Config::getInstance() {
    return c_instance;
  }

  void Config::setDefaults() {
    c_device = "/dev/gpioc0";

    // PIN layout
    c_pinlayout.clear();
    c_pinlayout["swled"] = 16;
    c_pinlayout["swon"]  = 20;
    c_pinlayout["swoff"] = 21;
    c_pinlayout["rimsheat"] = 23;
    c_pinlayout["rimspump"] = 24;
    c_pinlayout["buzzer"] = 26;

    // SPI config
    c_spidev = "/dev/spigen0.0";
    c_spi_chipselector = ChipSelectors::DirectSelect;
    c_pinlayout["cs0"] = 7;
    c_pinlayout["cs1"] = 8;
    c_pinlayout["cs2"] = 5;
    c_pinlayout["cs3"] = 6;
    // SPI / MAX31856
    c_spi_max31856_tctype = MAX31856::TCType::T;
    c_spi_max31856_noisefilter = NoiseFilters::HZ50;
    // SPI / chips
    c_spi_dschips = {{0, "cs0"}, {1, "cs1"}, {2, "cs2"}, {3, "cs3"}};

    // thermocouples
    c_thermocouples = {{"MashTun", 1}, {"RIMS", 0}, {"HLT", 2}, {"BK", 3}};

    // thermocouple reading interval
    c_thermoival = 1;

    // the PR ZMQ socket
    c_zmq_pr_port = 42069;

    // the heating element's power
    c_hepower = 3000;

    // pin polling interval
    c_pinival = 100;

    // temperature accuracy
    c_tempaccuracy = 0.3;

    // max heating overhead
    c_heatoverhead = 2.0;

    // heating element cycle time
    c_hecycletime = 3.0;

    // cooling temperature
    c_cooltemp = 24.0f;
  }

  void Config::load() {
    YAML::Node config;
    try {
      config = YAML::LoadFile(c_cfgfile);<commit_msg>Adjusted PIN config for electric HERMS setup<commit_after>#include <yaml-cpp/yaml.h>

#include <iostream>
#include <fstream>
#include <map>

#include "Config.hh"
#include "Exception.hh"

namespace aegir {

  // PIN default configuration
  pinconfig_t g_pinconfig = pinconfig_t{
    {"swled", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"swon",  PinConfig(PinMode::IN, PinPull::DOWN)},
    {"swoff", PinConfig(PinMode::IN, PinPull::DOWN)},
    {"cs0", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs1", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs2", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"cs3", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"rimsheat", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"rimspump", PinConfig(PinMode::OUT, PinPull::NONE)},
    {"buzzer", PinConfig(PinMode::OUT, PinPull::NONE)}
  };

  static std::set<std::string> g_tcnames{"RIMS", "MashTun", "HLT", "BK"};

  // SPI ChipSelector string translations
  static std::map<ChipSelectors, std::string> g_spi_cs_to_string{
    {ChipSelectors::DirectSelect, "DirectSelect"}};
  static std::map<std::string, ChipSelectors> g_spi_string_to_cs{
    {"DirectSelect",ChipSelectors::DirectSelect}};
  // ThermoCouple type lookups
  std::map<std::string, MAX31856::TCType> g_string_to_tctype{
    {"B", MAX31856::TCType::B},
      {"E", MAX31856::TCType::E},
	{"J", MAX31856::TCType::J},
	  {"K", MAX31856::TCType::K},
	    {"N", MAX31856::TCType::N},
	      {"R", MAX31856::TCType::R},
		{"S", MAX31856::TCType::S},
		  {"T", MAX31856::TCType::T}
  };
  std::map<MAX31856::TCType, std::string> g_tctype_to_string{
    {MAX31856::TCType::B, "B"},
      {MAX31856::TCType::E, "E"},
	{MAX31856::TCType::J, "J"},
	  {MAX31856::TCType::K, "K"},
	    {MAX31856::TCType::N, "N"},
	      {MAX31856::TCType::R, "R"},
		{MAX31856::TCType::S, "S"},
		  {MAX31856::TCType::T, "T"}
  };
  // Noisefilter lookups
  std::map<std::string, NoiseFilters> g_string_to_noisefilter{
    {"50Hz", NoiseFilters::HZ50},
    {"60Hz", NoiseFilters::HZ60}
  };
  std::map<NoiseFilters, std::string> g_noisefilter_to_string{
    {NoiseFilters::HZ50, "50Hz"},
      {NoiseFilters::HZ60, "60Hz"}
  };

  // The singleton instance holder
  Config *Config::c_instance=0;

  Config::Config(const std::string &_cfgfile, bool _noload): c_cfgfile(_cfgfile) {
    setDefaults();
    if ( !_noload ) load();
  }

  Config::~Config() {
    save();
  }

  Config *Config::instantiate(const std::string &_cfgfile, bool _noload) {
    if ( !c_instance ) c_instance = new Config(_cfgfile, _noload);
    return c_instance;
  }

  // TODO: throw exception when !c_instance
  Config *Config::getInstance() {
    return c_instance;
  }

  void Config::setDefaults() {
    c_device = "/dev/gpioc0";

    // PIN layout
    c_pinlayout.clear();
    c_pinlayout["mtlevel"] = 16;
    c_pinlayout["mtpump"] = 23;
    c_pinlayout["bkpump"] = 24;
    c_pinlayout["mtheat"] = 25;
    c_pinlayout["buzzer"] = 26;

    // SPI config
    c_spidev = "/dev/spigen0.0";
    c_spi_chipselector = ChipSelectors::DirectSelect;
    c_pinlayout["cs0"] = 7;
    c_pinlayout["cs1"] = 8;
    c_pinlayout["cs2"] = 5;
    c_pinlayout["cs3"] = 6;
    // SPI / MAX31856
    c_spi_max31856_tctype = MAX31856::TCType::T;
    c_spi_max31856_noisefilter = NoiseFilters::HZ50;
    // SPI / chips
    c_spi_dschips = {{0, "cs0"}, {1, "cs1"}, {2, "cs2"}, {3, "cs3"}};

    // thermocouples
    c_thermocouples = {{"MashTun", 1}, {"RIMS", 0}, {"HLT", 2}, {"BK", 3}};

    // thermocouple reading interval
    c_thermoival = 1;

    // the PR ZMQ socket
    c_zmq_pr_port = 42069;

    // the heating element's power
    c_hepower = 9000;

    // pin polling interval
    c_pinival = 100;

    // temperature accuracy
    c_tempaccuracy = 0.3;

    // max heating overhead
    c_heatoverhead = 2.0;

    // heating element cycle time
    c_hecycletime = 3.0;

    // cooling temperature
    c_cooltemp = 24.0f;
  }

  void Config::load() {
    YAML::Node config;
    try {
      config = YAML::LoadFile(c_cfgfile);
{'fraction_non_alphanumeric': 0.18358450616515132, 'fraction_numerical': 0.047079337401918046, 'mean_word_length': 3.935464044253227, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6405024', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3127, 'n_words': 557}