diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2020-08-21 11:13:53 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2020-08-21 11:13:53 -0700 |
| commit | a5b01f73612f23407d059879664834b40441f34e (patch) | |
| tree | fbb1bde1cb5967de27a7bcecb755b1b19f5f36fd /shared-bindings | |
| parent | 6857f984269149b46d1cd4283506420f341b2601 (diff) | |
| parent | 988e5caafc0866de6593d99632622a8fe62a9a9c (diff) | |
Merge remote-tracking branch 'adafruit/main' into add_pwmio
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.c | 30 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.h | 6 |
2 files changed, 24 insertions, 12 deletions
diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 024ee613c..2b787df4f 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -65,20 +65,28 @@ //| pulse.send(pulses)""" //| ... //| -STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { - mp_arg_check_num(n_args, kw_args, 1, 1, false); - mp_obj_t carrier_obj = args[0]; - - if (!MP_OBJ_IS_TYPE(carrier_obj, &pwmio_pwmout_type)) { - mp_raise_TypeError_varg(translate("Expected a %q"), pwmio_pwmout_type.name); - } - - // create Pulse object from the given pin +STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); self->base.type = &pulseio_pulseout_type; - common_hal_pulseio_pulseout_construct(self, (pwmio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); - + mp_obj_t carrier_obj = pos_args[0]; + if (MP_OBJ_IS_TYPE(carrier_obj, &pwmio_pwmout_type)) { + // Use a PWMOut Carrier + mp_arg_check_num(n_args, kw_args, 1, 1, false); + common_hal_pulseio_pulseout_construct(self, (pwmio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj), NULL, 0, 0); + } else { + // Use a Pin, frequency, and duty cycle + enum { ARG_pin, ARG_frequency}; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 38000} }, + { MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1<<15} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + common_hal_pulseio_pulseout_construct(self, NULL, pin, args[ARG_frequency].u_int, args[ARG_frequency].u_int); + } return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/pulseio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h index 80dd0e92f..49dc555fe 100644 --- a/shared-bindings/pulseio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -34,7 +34,11 @@ extern const mp_obj_type_t pulseio_pulseout_type; extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pwmio_pwmout_obj_t* carrier); + const pwmio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle); + extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); extern bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self); extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, |
