Treehopper C++ API
Pwm.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Treehopper.h"
4 #include "Utility.h"
5 
6 namespace Treehopper {
8  public:
9  virtual ~Pwm() {};
10 
12  virtual double dutyCycle() { return _dutyCycle; }
13 
15  virtual void dutyCycle(double value) {
16  if (Utility::closeTo(_dutyCycle, value))
17  return;
18 
19  _dutyCycle = value;
20  updateDutyCycle();
21  }
22 
23  virtual bool enabled() = 0;
24 
25  virtual void enabled(bool value) = 0;
26 
28  virtual double pulseWidth() {
29  // always calculate from frequency and dc
30  return _dutyCycle * 1000000.0 / _frequency;
31  }
32 
34  virtual void pulseWidth(double value) {
35  if (Utility::closeTo(pulseWidth(), value))
36  return;
37 
38  _dutyCycle = value * _frequency / 1000000.0;
39  updateDutyCycle();
40  }
41 
42  protected:
43  virtual void updateDutyCycle() = 0;
44 
45  double _dutyCycle;
46  double _frequency;
47  };
48 }
virtual void dutyCycle(double value)
Sets the duty cycle.
Definition: Pwm.h:15
virtual double pulseWidth()
Gets the pulse width.
Definition: Pwm.h:28
virtual void pulseWidth(double value)
Sets the pulse width.
Definition: Pwm.h:34
virtual double dutyCycle()
Gets the duty cycle.
Definition: Pwm.h:12
Definition: AdcPin.h:3
virtual ~Pwm()
Definition: Pwm.h:9
static bool closeTo(double a, double b, double error=0.001)
Definition: Pwm.h:7
#define TREEHOPPER_API
Definition: Treehopper.h:17