erropix / ESP32_AnalogWrite

Provides an analogWrite polyfill for ESP32 using the LEDC functions
MIT License
159 stars 36 forks source link

Fixed mapping between channels and pins by making the map global #12

Closed AdityaNG closed 8 months ago

AdityaNG commented 3 years ago

When analogWrite() is called from different libraries, the mapping between channels and pins would not be correct. Take the example

Lets say that in the main file, we set pin 13 to 100% duty cycle.

// main.ino
void loop() {
    ...
    analogWrite(13, 255);
}

In the above, pin 13 would get mapped to channel 0, but lets say we import another library that happens to use analogWrite()

// other_library.cpp
void init() {
    ...
    analogWrite(14, 125);
}

Since analog_write_channel_t _analog_write_channels[16] is not declared as a globally accessible extern variable, pin 14 will also get mapped to channel 0.

This is fixed by making _analog_write_channels an extern variable.