2017-03-10 13:17:54 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-03-13 16:55:11 -04:00
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
2017-03-10 13:17:54 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
|
2017-03-10 13:17:54 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/pulseio/PulseOut.h"
|
|
|
|
#include "shared-bindings/pulseio/PWMOut.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| class PulseOut:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Pulse PWM "carrier" output on and off. This is commonly used in infrared remotes. The
|
|
|
|
//| pulsed signal consists of timed on and off periods. Unlike PWM, there is no set duration
|
|
|
|
//| for on and off pairs."""
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| def __init__(self, carrier: pulseio.PWMOut):
|
|
|
|
//| """Create a PulseOut object associated with the given PWMout object.
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| :param ~pulseio.PWMOut carrier: PWMOut that is set to output on the desired pin.
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Send a short series of pulses::
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| import array
|
|
|
|
//| import pulseio
|
|
|
|
//| import board
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| # 50% duty cycle at 38kHz.
|
|
|
|
//| pwm = pulseio.PWMOut(board.D13, frequency=38000, duty_cycle=32768)
|
|
|
|
//| pulse = pulseio.PulseOut(pwm)
|
|
|
|
//| # on off on off on
|
|
|
|
//| pulses = array.array('H', [65000, 1000, 65000, 65000, 1000])
|
|
|
|
//| pulse.send(pulses)
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| # Modify the array of pulses.
|
|
|
|
//| pulses[0] = 200
|
|
|
|
//| pulse.send(pulses)"""
|
|
|
|
//| ...
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2019-01-14 20:26:36 -05:00
|
|
|
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);
|
2017-03-10 13:17:54 -05:00
|
|
|
mp_obj_t carrier_obj = args[0];
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_TypeError_varg(translate("Expected a %q"), pulseio_pwmout_type.name);
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// create Pulse object from the given pin
|
2017-04-10 16:32:19 -04:00
|
|
|
pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t);
|
|
|
|
self->base.type = &pulseio_pulseout_type;
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj));
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def deinit(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Deinitialises the PulseOut and releases any hardware resources for reuse."""
|
|
|
|
//| ...
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t pulseio_pulseout_deinit(mp_obj_t self_in) {
|
|
|
|
pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_pulseio_pulseout_deinit(self);
|
2017-03-10 13:17:54 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulseout_deinit_obj, pulseio_pulseout_deinit);
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
|
|
|
// Provided by context manager helper.
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __exit__(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t pulseio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
2017-03-10 13:17:54 -05:00
|
|
|
(void)n_args;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_pulseio_pulseout_deinit(args[0]);
|
2017-03-10 13:17:54 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4, pulseio_pulseout_obj___exit__);
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| def send(self, pulses: array.array) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Pulse alternating on and off durations in microseconds starting with on.
|
|
|
|
//| ``pulses`` must be an `array.array` with data type 'H' for unsigned
|
|
|
|
//| halfword (two bytes).
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| This method waits until the whole array of pulses has been sent and
|
|
|
|
//| ensures the signal is off afterwards.
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| :param array.array pulses: pulse durations in microseconds"""
|
|
|
|
//| ...
|
2017-03-10 13:17:54 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t pulseio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) {
|
|
|
|
pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
if (common_hal_pulseio_pulseout_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ);
|
|
|
|
if (bufinfo.typecode != 'H') {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_TypeError(translate("Array must contain halfwords (type 'H')"));
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_pulseio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2);
|
2017-03-10 13:17:54 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulseout_send_obj, pulseio_pulseout_obj_send);
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pulseio_pulseout_locals_dict_table[] = {
|
2017-03-10 13:17:54 -05:00
|
|
|
// Methods
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulseout_deinit_obj) },
|
2017-03-10 13:17:54 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulseout___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&pulseio_pulseout_send_obj) },
|
2017-03-10 13:17:54 -05:00
|
|
|
};
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pulseio_pulseout_locals_dict, pulseio_pulseout_locals_dict_table);
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_type_t pulseio_pulseout_type = {
|
2017-03-10 13:17:54 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_PulseOut,
|
2017-04-10 16:32:19 -04:00
|
|
|
.make_new = pulseio_pulseout_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&pulseio_pulseout_locals_dict,
|
2017-03-10 13:17:54 -05:00
|
|
|
};
|