38
38
//|
39
39
//| PWMOut can be used to output a PWM signal on a given pin.
40
40
//|
41
- //| .. class:: PWMOut(pin, duty =0, frequency=500, variable_frequency=False)
41
+ //| .. class:: PWMOut(pin, duty_cycle =0, frequency=500, variable_frequency=False)
42
42
//|
43
43
//| Create a PWM object associated with the given pin. This allows you to
44
44
//| write PWM signals out on the given pin. Frequency is fixed after init
45
45
//| unless `variable_frequency` is True.
46
46
//|
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
+ //|
47
52
//| :param ~microcontroller.Pin pin: The pin to output to
48
53
//| :param int duty: The fraction of each pulse which is high. 16-bit
49
54
//| :param int frequency: The target frequency in Hertz (32-bit)
50
55
//| :param bool variable_frequency: True if the frequency will change over time
51
56
//|
52
- //| Example usage ::
57
+ //| Simple LED fade ::
53
58
//|
54
59
//| import nativeio
55
60
//| import board
56
61
//|
57
62
//| 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)
59
83
//|
60
84
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 ) {
61
85
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
72
96
mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
73
97
enum { ARG_duty , ARG_frequency , ARG_variable_frequency };
74
98
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 } },
76
100
{ MP_QSTR_frequency , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 500 } },
77
101
{ MP_QSTR_variable_frequency , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
78
102
};
0 commit comments