ardupilot/libraries/AP_HAL_Linux/PWM_Sysfs.h
Lucas De Marchi c66c61180a AP_HAL_Linux: rework PWM_Sysfs
This include some minor changes on all methods of PWM_Sysfs:

  - Sort headers
  - Add code inside Linux namespace rather than just use the namespace
  - Declare a union pwm_params, that's only used to calculate at compile
    time the maximum stack space we need in our methods: this is a bit
    safer for future extensions
  - Standardize error messages to include the useful params first and
    then the error message
  - Remove log message from hot path
  - Don't abuse macros for checking error - convert the SNPRINTF_CHECK
    macro into proper code, ignoring errors for not enough space since
    they can't happen
  - Fix call to read_file() passing uint8_t for "%u" in get_period()
  - Fix passing char** instead of char* to write_file() in set_polarity()
  - Use strncmp() instead of strncasecmp() since the kernel API uses
    lowercase.
  - Add comments on the 2 main methods of this class
2015-11-10 17:05:34 +11:00

47 lines
1.1 KiB
C++

#pragma once
#include <AP_HAL/AP_HAL.h>
#include "AP_HAL_Linux.h"
#include "Util.h"
class Linux::PWM_Sysfs {
public:
PWM_Sysfs(uint8_t chip, uint8_t channel);
~PWM_Sysfs();
enum Polarity {
NORMAL = 0,
INVERSE = 1,
};
void enable(bool value);
bool is_enabled();
void set_period(uint32_t nsec_period);
uint32_t get_period();
void set_freq(uint32_t freq);
uint32_t get_freq();
/*
* This is the main method, to be called on hot path. It doesn't log any
* failure so not to risk flooding the log. If logging is necessary, check
* the return value.
*/
bool set_duty_cycle(uint32_t nsec_duty_cycle);
/*
* This is supposed to be called in the hot path, so it returns the cached
* value rather than getting it from hardware.
*/
uint32_t get_duty_cycle();
void set_polarity(PWM_Sysfs::Polarity polarity);
PWM_Sysfs::Polarity get_polarity();
private:
uint32_t _nsec_duty_cycle_value;
int _duty_cycle_fd;
const uint16_t _channel;
const uint8_t _chip;
};