You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • It turns out software PWM is much easier than I thought it would be, so I've been experimenting with adding it to Espruino - it's especially useful for the LEDs on the Pico.

    But... how should it be exposed? In my prototype, if you do:

    analogWrite(pin, 0.5);
    

    It'll use a DAC if it's available, followed by hardware PWM, followed by software PWM. But you have no way to tell which one it's using, which could be dangerous - especially if using really high frequencies.

    I guess an option would be to use the optional 3rd argument, like so:

    >analogWrite(LED1, 0.5);
    Pin B2 is not capable of PWM, available pins are:
    ...
    Or use software pwm with analogWrite(pin, val, {soft:true});
    >analogWrite(LED1, 0.5, {soft:true});
    ... now works
    

    But then that's not very beginner friendly. Perhaps it could default to using 50Hz PWM, and if you specified a frequency but not soft:true it'd complain?

    >analogWrite(LED1, 0.5);
    ... works
    >analogWrite(LED1, 0.5, {freq:1000});
    Pin B2 is not capable of Hardware PWM, available pins are:
    ...
    Or use software pwm with analogWrite(pin, val, {soft:true});
    >analogWrite(LED1, 0.5, {freq:1000, soft:true});
    ... now works
    

    Thoughts?

About

Avatar for Gordon @Gordon started