ESP32Servo
ESP32PWM.h
Go to the documentation of this file.
1 /*
2  * ESP32PWM.h
3  *
4  * Created on: Sep 22, 2018
5  * Author: hephaestus
6  */
7 
8 #ifndef LIBRARIES_ESP32SERVO_SRC_ESP32PWM_H_
9 #define LIBRARIES_ESP32SERVO_SRC_ESP32PWM_H_
10 #include "esp32-hal-ledc.h"
11 #define NUM_PWM 16
12 #define PWM_BASE_INDEX 0
13 #define USABLE_ESP32_PWM (NUM_PWM-PWM_BASE_INDEX)
14 #include <cstdint>
15 
16 #include "Arduino.h"
17 class ESP32PWM {
18 private:
19 
20  void attach(int pin);
21  int pwmChannel = 0; // channel number for this servo
22  bool attachedState = false;
23  int pin;
24  uint8_t resolutionBits;
25  double myFreq;
26  int allocatenext(double freq);
27 
28  static double _ledcSetupTimerFreq(uint8_t chan, double freq,
29  uint8_t bit_num);
30 
32 
33  void adjustFrequencyLocal(double freq, float dutyScaled);
34  static float mapf(float x, float in_min, float in_max, float out_min,
35  float out_max) {
36  if(x>in_max)
37  return out_max;
38  if(x<in_min)
39  return out_min;
40  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
41  }
42 
43  double setup(double freq, uint8_t resolution_bits=10);
44  //channel 0-15 resolution 1-16bits freq limits depend on resolution9
45  void attachPin(uint8_t pin);
46  // pin allocation
47  void deallocate();
48 public:
49  // setup
50  ESP32PWM();
51  virtual ~ESP32PWM();
52 
53 
54  void detachPin(int pin);
55  void attachPin(uint8_t pin, double freq, uint8_t resolution_bits=10);
56  bool attached() {
57  return attachedState;
58  }
59 
60  // write raw duty cycle
61  void write(uint32_t duty);
62  // Write a duty cycle to the PWM using a unit vector from 0.0-1.0
63  void writeScaled(float duty);
64  //Adjust frequency
65  double writeTone(double freq);
66  double writeNote(note_t note, uint8_t octave);
67  void adjustFrequency(double freq, float dutyScaled=-1);
68 
69  // Read pwm data
70  uint32_t read();
71  double readFreq();
72  float getDutyScaled();
73 
74  //Timer data
75  static int timerAndIndexToChannel(int timer, int index);
82  static void allocateTimer(int timerNumber);
84  int getTimer() {
85  return timerNum;
86  }
87  int timerNum = -1;
88  uint32_t myDuty = 0;
89  int getChannel();
90  static int PWMCount; // the total number of attached pwm
91  static int timerCount[4];
92  static ESP32PWM * ChannelUsed[NUM_PWM]; // used to track whether a channel is in service
93  static long timerFreqSet[4];
94 
95  // Helper functions
96  int getPin() {
97  return pin;
98  }
99  static bool hasPwm(int pin) {
100  if ((pin == 2) || //1
101  (pin == 4) || //1
102  (pin == 5) || //1
103  ((pin >= 12) && (pin <= 19)) || //8
104  ((pin >= 21) && (pin <= 23)) || //3
105  ((pin >= 25) && (pin <= 27)) || //3
106  (pin == 32) || (pin == 33)) //2
107  return true;
108  return false;
109  }
110  static int channelsRemaining() {
111  return NUM_PWM - PWMCount;
112  }
113 
114 
115 };
116 
117 ESP32PWM* pwmFactory(int pin);
118 
119 #endif /* LIBRARIES_ESP32SERVO_SRC_ESP32PWM_H_ */
void adjustFrequency(double freq, float dutyScaled=-1)
Definition: ESP32PWM.cpp:180
void adjustFrequencyLocal(double freq, float dutyScaled)
Definition: ESP32PWM.cpp:166
int timerNum
Definition: ESP32PWM.h:87
float getDutyScaled()
Definition: ESP32PWM.cpp:155
double setup(double freq, uint8_t resolution_bits=10)
Definition: ESP32PWM.cpp:143
static void allocateTimer(int timerNumber)
Definition: ESP32PWM.cpp:25
static int channelsRemaining()
Definition: ESP32PWM.h:110
static int timerAndIndexToChannel(int timer, int index)
Definition: ESP32PWM.cpp:61
virtual ~ESP32PWM()
Definition: ESP32PWM.cpp:48
int getPin()
Definition: ESP32PWM.h:96
void detachPin(int pin)
Definition: ESP32PWM.cpp:250
int pwmChannel
Definition: ESP32PWM.h:21
int getChannel()
Definition: ESP32PWM.cpp:136
void attachPin(uint8_t pin)
Definition: ESP32PWM.cpp:231
void deallocate()
Definition: ESP32PWM.cpp:120
static ESP32PWM * ChannelUsed[NUM_PWM]
Definition: ESP32PWM.h:92
static int PWMCount
Definition: ESP32PWM.h:90
int pin
Definition: ESP32PWM.h:23
int freq
Definition: PWMExample.ino:4
bool checkFrequencyForSideEffects(double freq)
Definition: ESP32PWM.cpp:275
void attach(int pin)
Definition: ESP32PWM.cpp:227
static float mapf(float x, float in_min, float in_max, float out_min, float out_max)
Definition: ESP32PWM.h:34
static bool explicateAllocationMode
Definition: ESP32PWM.h:83
static int timerCount[4]
Definition: ESP32PWM.h:91
bool attachedState
Definition: ESP32PWM.h:22
void write(uint32_t duty)
Definition: ESP32PWM.cpp:162
uint32_t read()
Definition: ESP32PWM.cpp:221
int getTimer()
Definition: ESP32PWM.h:84
int allocatenext(double freq)
Definition: ESP32PWM.cpp:73
uint32_t myDuty
Definition: ESP32PWM.h:88
static double _ledcSetupTimerFreq(uint8_t chan, double freq, uint8_t bit_num)
Definition: ESP32PWM.cpp:55
double writeTone(double freq)
Definition: ESP32PWM.cpp:194
double writeNote(note_t note, uint8_t octave)
Definition: ESP32PWM.cpp:208
static long timerFreqSet[4]
Definition: ESP32PWM.h:93
bool attached()
Definition: ESP32PWM.h:56
double readFreq()
Definition: ESP32PWM.cpp:224
#define NUM_PWM
Definition: ESP32PWM.h:11
static bool hasPwm(int pin)
Definition: ESP32PWM.h:99
ESP32PWM * pwmFactory(int pin)
Definition: ESP32PWM.cpp:305
ESP32PWM()
Definition: ESP32PWM.cpp:36
uint8_t resolutionBits
Definition: ESP32PWM.h:24
void writeScaled(float duty)
Definition: ESP32PWM.cpp:159
double myFreq
Definition: ESP32PWM.h:25