ESP32Servo
analogWrite.cpp
Go to the documentation of this file.
1 /*
2  * analogWrite.cpp
3  *
4  * Created on: Sep 17, 2018
5  * Author: Harry-Laptop
6  */
7 
8 #include "analogWrite.h"
9 #include "ESP32PWM.h"
10 
11 void analogWrite(uint8_t APin, uint16_t AValue) {
12  if(APin== 25 ||APin==26){
13  dacWrite(APin, AValue);
14  return;
15  }
16  ESP32PWM* chan = pwmFactory(APin);
17  if (AValue == 0) {
18  if ((chan != NULL) && chan->attached()) {
19  chan->detachPin(APin);
20  delete chan;
21  pinMode(APin, OUTPUT);
22  }
23  digitalWrite(APin, 0);
24  } else if (AValue >= 255) {
25  if ((chan != NULL) && chan->attached()) {
26  chan->detachPin(APin);
27  delete chan;
28  pinMode(APin, OUTPUT);
29  }
30  digitalWrite(APin, 1);
31  } else
32  {
33  if (chan == NULL) {
34  chan = new ESP32PWM();
35  }
36  if(!chan->attached()){
37  chan->attachPin(APin,1000, 8); // This adds the PWM instance to the factory list
38  //Serial.println("Attaching AnalogWrite : "+String(APin)+" on PWM "+String(chan->getChannel()));
39  }
40  chan->write(AValue);
41  // Serial.print( "ledcWrite: " ); Serial.print( CESP32PWMPinMap[ APin ] - 1 ); Serial.print( " " ); Serial.println( AValue );
42  }
43 }
ESP32PWM * pwmFactory(int pin)
Definition: ESP32PWM.cpp:305
void analogWrite(uint8_t APin, uint16_t AValue)
Definition: analogWrite.cpp:11
void detachPin(int pin)
Definition: ESP32PWM.cpp:250
void attachPin(uint8_t pin)
Definition: ESP32PWM.cpp:231
void write(uint32_t duty)
Definition: ESP32PWM.cpp:162
bool attached()
Definition: ESP32PWM.h:56
int APin
Definition: PWMExample.ino:2