HardwarePWM.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/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 up to 8 pins (ie All pins except pin 16)
19  * Created on August 17, 2015, 2:27 PM
20  *
21  ****/
22 
28 #pragma once
29 
30 #include <cstdint>
31 #include <driver/pwm.h>
32 
33 #define PWM_BAD_CHANNEL 0xff
34 
37 {
38 public:
43  HardwarePWM(uint8_t* pins, uint8_t no_of_pins);
44  virtual ~HardwarePWM();
45 
52  bool analogWrite(uint8_t pin, uint32_t duty)
53  {
54  return setDuty(pin, duty);
55  }
56 
63  bool setDutyChan(uint8_t chan, uint32_t duty, bool update = true);
64 
73  bool setDuty(uint8_t pin, uint32_t duty, bool update = true)
74  {
75  uint8_t chan = getChannel(pin);
76  return setDutyChan(chan, duty, update);
77  }
78 
83  uint32_t getDutyChan(uint8_t chan);
84 
89  uint32_t getDuty(uint8_t pin)
90  {
91  uint8_t chan = getChannel(pin);
92  return getDutyChan(chan);
93  }
94 
99  void setPeriod(uint32_t period);
100 
104  uint32_t getPeriod();
105 
110  uint8_t getChannel(uint8_t pin);
111 
116  uint32_t getMaxDuty()
117  {
118  return maxduty;
119  }
120 
123  void update();
124 
129  uint32_t getFrequency(uint8_t pin);
130 
131 private:
132  uint8_t channel_count;
133  uint8_t channels[PWM_CHANNEL_NUM_MAX];
134  uint32_t maxduty;
135 };
136 
#define PWM_CHANNEL_NUM_MAX
Definition: Esp32/Components/driver/include/driver/pwm.h:17
Hardware pulse width modulation.
Definition: HardwarePWM.h:37
bool analogWrite(uint8_t pin, uint32_t duty)
Set PWM duty cycle.
Definition: HardwarePWM.h:52
uint32_t getPeriod()
Get PWM period.
uint32_t getMaxDuty()
Get the maximum duty cycle value.
Definition: HardwarePWM.h:116
uint8_t getChannel(uint8_t pin)
Get channel number for a pin.
void setPeriod(uint32_t period)
Set PWM period.
void update()
This function is used to actually update the PWM.
uint32_t getDutyChan(uint8_t chan)
Get PWM duty cycle.
bool setDuty(uint8_t pin, uint32_t duty, bool update=true)
Set PWM duty cycle.
Definition: HardwarePWM.h:73
uint32_t getDuty(uint8_t pin)
Get PWM duty cycle.
Definition: HardwarePWM.h:89
HardwarePWM(uint8_t *pins, uint8_t no_of_pins)
Instantiate hardware PWM object.
uint32_t getFrequency(uint8_t pin)
Get PWM Frequency.
virtual ~HardwarePWM()
bool setDutyChan(uint8_t chan, uint32_t duty, bool update=true)
Set PWM duty cycle for a channel.