//
// sysfs PWM access
//
// Copyright 2026, Jakob Østergaard Hegelund <joe@unthought.net>
// -----------------------------------------------------------------------
// This file is part of FanMan.
//
// FanMan is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FanMan is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FanMan. If not, see <https://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------
//
#ifndef _PWM_HH
#define _PWM_HH

#include <string>
#include <fstream>

class PWMChannel {
public:
  // Set up PWM channel as specified and make sure it is disabled
  //
  // chip: PWM chip number
  // channel: PWM channel number on chip
  // period: PWM period in nanoseconds
  PWMChannel(size_t chip, size_t channel, size_t period);

  // Enable the PWM at 0% DC
  void enable(bool);

  // Adjust duty cycle
  void set_dc(double);

private:
  const size_t m_chip;
  const size_t m_channel;
  const size_t m_period;
  bool m_inversed = false;
  std::ofstream m_last_file;
  std::string m_last_fname;

  std::string chip_prefix();
  std::string chip_file(std::string);
  std::string channel_prefix();
  std::string channel_file(std::string);

  void write_channel_file(std::string f, size_t);
};


#endif
