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
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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:
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:
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?Thoughts?