Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
HardwarePWM.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HardwarePWM.h
8  *
9  * Original Author: https://github.com/hrsavla
10  *
11  * This HW_PWM library enables Sming framework user to use ESP SDK PWM API
12  * Period of PWM is fixed to 1000ms / Frequency = 1khz
13  * Duty at 100% = 22222. Duty at 0% = 0
14  * You can use function setPeriod() to change frequency/period.
15  * Calculate the Duty as per the formulae give in ESP8266 SDK
16  * Duty = (Period *1000)/45
17  *
18  * PWM can be generated on upto 8 pins (ie All pins except pin 16)
19  * Created on August 17, 2015, 2:27 PM
20  *
21  ****/
22 
28 #ifndef _SMING_CORE_HARDWARE_PWM_H_
29 #define _SMING_CORE_HARDWARE_PWM_H_
30 
31 #include "ESP8266EX.h"
32 #include "WiringFrameworkDependencies.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 #include <pwm.h>
38 #ifdef __cplusplus
39 }
40 #endif
41 
42 #define PWM_BAD_CHANNEL 0xff
43 
44 class HardwarePWM
46 {
47 public:
52  HardwarePWM(uint8* pins, uint8 no_of_pins);
53  virtual ~HardwarePWM();
54 
61  bool analogWrite(uint8 pin, uint32 duty)
62  {
63  return setDuty(pin, duty);
64  }
65 
72  bool setDutyChan(uint8 chan, uint32 duty, bool update = true);
73 
82  bool setDuty(uint8 pin, uint32 duty, bool update = true)
83  {
84  uint8 chan = getChannel(pin);
85  return setDutyChan(chan, duty, update);
86  }
87 
92  uint32 getDutyChan(uint8 chan);
93 
98  uint32 getDuty(uint8 pin)
99  {
100  uint8 chan = getChannel(pin);
101  return getDutyChan(chan);
102  }
103 
108  void setPeriod(uint32 period);
109 
113  uint32 getPeriod(void);
114 
119  uint8 getChannel(uint8 pin);
120 
125  uint32 getMaxDuty()
126  {
127  return maxduty;
128  }
129 
132  void update();
133 
134 private:
135  uint8 channel_count;
136  uint8 channels[PWM_CHANNEL_NUM_MAX];
137  uint32 maxduty;
138 };
139 
141 #endif /* _SMING_CORE_HARDWARE_PWM_H_ */
bool setDutyChan(uint8 chan, uint32 duty, bool update=true)
Set PWM duty cycle for a channel.
uint32 getDutyChan(uint8 chan)
Get PWM duty cycle.
uint32 getDuty(uint8 pin)
Get PWM duty cycle.
Definition: HardwarePWM.h:98
bool analogWrite(uint8 pin, uint32 duty)
Set PWM duty cycle.
Definition: HardwarePWM.h:61
Hardware pulse width modulation.
Definition: HardwarePWM.h:45
uint32 getMaxDuty()
Get the maximum duty cycle value.
Definition: HardwarePWM.h:125
HardwarePWM(uint8 *pins, uint8 no_of_pins)
Instantiate hardware PWM object.
bool setDuty(uint8 pin, uint32 duty, bool update=true)
Set PWM duty cycle.
Definition: HardwarePWM.h:82
uint32 getPeriod(void)
Get PWM period.
uint8 getChannel(uint8 pin)
Get channel number for a pin.
void setPeriod(uint32 period)
Set PWM period.
void update()
This function is used to actually update the PWM.