ESP32Tone.cpp 792 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * ESP32Tone.cpp
  3. *
  4. * Created on: Sep 23, 2018
  5. * Author: hephaestus
  6. */
  7. #include "ESP32Tone.h"
  8. void tone(int APin,unsigned int frequency){
  9. ESP32PWM* chan = pwmFactory(APin);
  10. if (chan == NULL) {
  11. chan = new ESP32PWM();
  12. }
  13. if(!chan->attached()){
  14. chan->attachPin(APin,frequency, 10); // This adds the PWM instance to the factory list
  15. //Serial.println("Attaching tone : "+String(APin)+" on PWM "+String(chan->getChannel()));
  16. }
  17. chan->writeTone(frequency);// update the time base of the PWM
  18. }
  19. void tone(int pin, unsigned int frequency, unsigned long duration){
  20. tone(pin,frequency);
  21. delay(duration);
  22. noTone(pin);
  23. }
  24. void noTone(int pin){
  25. ESP32PWM* chan = pwmFactory(pin);
  26. if (chan != NULL) {
  27. if(chan->attached())
  28. {
  29. chan->detachPin(pin);
  30. delete chan;
  31. }
  32. }
  33. }