Skip to content

Commit 9f6da7b

Browse files
committed
Add more PWMOut examples and rename the duty constructor argument
to duty_cycle so that its consistent with the attribute. Fixes #84
1 parent 1da14b0 commit 9f6da7b

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

shared-bindings/nativeio/PWMOut.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,48 @@
3838
//|
3939
//| PWMOut can be used to output a PWM signal on a given pin.
4040
//|
41-
//| .. class:: PWMOut(pin, duty=0, frequency=500, variable_frequency=False)
41+
//| .. class:: PWMOut(pin, duty_cycle=0, frequency=500, variable_frequency=False)
4242
//|
4343
//| Create a PWM object associated with the given pin. This allows you to
4444
//| write PWM signals out on the given pin. Frequency is fixed after init
4545
//| unless `variable_frequency` is True.
4646
//|
47+
//| .. note:: When ``variable_frequency`` is True, further PWM outputs may be
48+
//| limited because it may take more internal resources to be flexible. So,
49+
//| when outputting both fixed and flexible frequency signals construct the
50+
//| fixed outputs first.
51+
//|
4752
//| :param ~microcontroller.Pin pin: The pin to output to
4853
//| :param int duty: The fraction of each pulse which is high. 16-bit
4954
//| :param int frequency: The target frequency in Hertz (32-bit)
5055
//| :param bool variable_frequency: True if the frequency will change over time
5156
//|
52-
//| Example usage::
57+
//| Simple LED fade::
5358
//|
5459
//| import nativeio
5560
//| import board
5661
//|
5762
//| with nativeio.PWMOut(board.D13) as pwm: # output on D13
58-
//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16)
63+
//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz
64+
//|
65+
//| PWM at specific frequency (servos and motors)::
66+
//|
67+
//| import nativeio
68+
//| import board
69+
//|
70+
//| with nativeio.PWMOut(board.D13, frequency=50) as pwm:
71+
//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz
72+
//|
73+
//| Variable frequency (usually tones)::
74+
//|
75+
//| import nativeio
76+
//| import board
77+
//| import time
78+
//|
79+
//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm:
80+
//| time.sleep(0.2)
81+
//| pwm.frequency = 880
82+
//| time.sleep(0.1)
5983
//|
6084
STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
6185
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
@@ -72,7 +96,7 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg
7296
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
7397
enum { ARG_duty, ARG_frequency, ARG_variable_frequency };
7498
static const mp_arg_t allowed_args[] = {
75-
{ MP_QSTR_duty, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
99+
{ MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
76100
{ MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500} },
77101
{ MP_QSTR_variable_frequency, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
78102
};

0 commit comments

Comments
 (0)