1
0

ESP32PWM.h 2.8 KB

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