42#ifndef _OS_LINUXPWM_HXX_
43#define _OS_LINUXPWM_HXX_
70 StringPrintf(
"/sys/class/pwm/pwmchip%d/pwm%d/", chip, channel))
85 StringPrintf(
"/sys/class/pwm/pwmchip%d/export",
chip_);
86 if (access(export_file.c_str(), W_OK) < 0)
89 LOG_ERROR(
"Cannot write to %s: %s\n",export_file.c_str(),
196 filename += basename;
198 FILE *fp = fopen(filename.c_str(),
"r");
202 LOG_ERROR(
"Cannot read from %s: %s\n",filename.c_str(),
206 fscanf(fp,
"%d", &value);
217 filename += basename;
218 FILE *fp = fopen(filename.c_str(),
"w");
222 LOG_ERROR(
"Cannot write to %s: %s\n",filename.c_str(),
226 fprintf(fp,
"%d\n", value);
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.
Implementation of a single PWM channel using the linux sysfs API.
static constexpr const char * DUTY_CYCLE_FILE
Basename for sysfs file for setting the duty cycle.
uint32_t get_period_max() override
Get max period supported.
static constexpr const char * ENABLE_FILE
Basename for sysfs file for enabling the channel.
uint32_t get_period_min() override
Get min period supported.
void set_period(uint32_t counts) override
Set PWM period.
void enable()
Enables the output PWM waveform.
void set_sysfs_file_value(const char *basename, uint32_t value)
Writes a single integer into a sysfs file of the channel directory.
uint32_t get_sysfs_file_value(const char *basename)
Returns a single file's content from the channel directory on sysfs.
void disable()
Disables the output PWM waveform.
void export_pin()
Call this once after instantiating the object but before using any of the PWM apis.
void set_duty(uint32_t counts) override
Sets the duty cycle.
static constexpr const char * PERIOD_FILE
Basename for sysfs file for setting the period.
LinuxPWM(int chip, int channel)
Constructor.
bool enabled()
Returns whether the output PWM waveform is enabled or not.
const uint32_t channel_
Number of the channel in the chip.
const uint32_t chip_
Kernel number of the chip.
const string pwmdir_
Diretory path of the channel, with trailing /.
uint32_t get_duty() override
Gets the duty cycle.
uint32_t get_period() override
Get PWM period.
Abstract interface for a PWM driver.
#define LOG_ERROR(message...)
Shorthand for LOG(LEVEL_ERROR, message...). See LOG.