Page 1 of 1

analogWrite() -> pwm()

Posted: Thu Apr 13, 2017 9:30 pm
by aster
I know it won't be very well seen for code compatibility, history, habits, ecc

But i think that here everyone know what is a pwm pulse, so why don t use its real name? I always found it confusing to call it analogWrite!

Last but not least pwm() is shorter to write :lol:

Re: analogWrite() -> pwm()

Posted: Fri Apr 14, 2017 7:22 am
by racemaniac
we could add it as an alias for analogWrite, but the goal of this project is obviously to keep arduino compatibility, so the analogWrite will remain (even though the name is terrible >_<)

Re: analogWrite() -> pwm()

Posted: Fri Apr 14, 2017 7:44 am
by Pito
analogWrite() has got fixed frequency while it changes duty cycle only.
pwm() usually gets 2 parameters - duty cycle and frequency.

Re: analogWrite() -> pwm()

Posted: Fri Apr 14, 2017 7:45 am
by edogaldo
aster wrote:I know it won't be very well seen for code compatibility, history, habits, ecc

But i think that here everyone know what is a pwm pulse, so why don t use its real name? I always found it confusing to call it analogWrite!

Last but not least pwm() is shorter to write :lol:
The core already provides pwmWrite() (ref. pwm.h/pwm.cpp), it's just 5 letters longer.. ;)

Re: analogWrite() -> pwm()

Posted: Sat Apr 15, 2017 4:24 pm
by aster
edogaldo wrote: The core already provides pwmWrite() (ref. pwm.h/pwm.cpp), it's just 5 letters longer.. ;)
it is great

Code: Select all

void analogWrite(uint8 pin, int duty_cycle8)
{
	pinMode(pin,PWM);
	pwmWrite(pin,duty_cycle8 * 257);// 257 maps 255 to 65535 (i.e 255*257 = 65535)
}

void pwmWrite(uint8 pin, uint16 duty_cycle) {
    if (pin >= BOARD_NR_GPIO_PINS) {
        return;
    }
    timer_dev *dev = PIN_MAP[pin].timer_device;
    uint8 cc_channel = PIN_MAP[pin].timer_channel;
    ASSERT(dev && cc_channel);
    timer_set_compare(dev, cc_channel, duty_cycle);
}
so what @racemaniac suggested had already been did, great
Pito wrote:analogWrite() has got fixed frequency while it changes duty cycle only.
pwm() usually gets 2 parameters - duty cycle and frequency.
this would be very good: the possibility to choose the frequency and duty cycle but it would need some effort to make it in arduino style API

Re: analogWrite() -> pwm()

Posted: Sat Apr 15, 2017 5:55 pm
by Pito
this would be very good: the possibility to choose the frequency and duty cycle but it would need some effort to make it in arduino style API
Everything is available, you can set period/frequency and duty cycle as you wish (with some limitations), just read the libmaple documentation, also discussed few times here, for example:
viewtopic.php?f=3&t=1041&hilit=pito+pwm&start=10