diff options
Diffstat (limited to 'shared-bindings/pulseio')
| -rw-r--r-- | shared-bindings/pulseio/PWMOut.c | 221 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PWMOut.h | 45 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseIn.c | 285 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseIn.h | 46 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.c | 150 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.h | 42 | ||||
| -rw-r--r-- | shared-bindings/pulseio/__init__.c | 87 | ||||
| -rw-r--r-- | shared-bindings/pulseio/__init__.h | 34 |
8 files changed, 910 insertions, 0 deletions
diff --git a/shared-bindings/pulseio/PWMOut.c b/shared-bindings/pulseio/PWMOut.c new file mode 100644 index 000000000..73f3bfd3e --- /dev/null +++ b/shared-bindings/pulseio/PWMOut.c @@ -0,0 +1,221 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * 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" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/PWMOut.h" + +//| .. currentmodule:: pulseio +//| +//| :class:`PWMOut` -- Output a Pulse Width Modulated signal +//| ======================================================== +//| +//| PWMOut can be used to output a PWM signal on a given pin. +//| +//| .. class:: PWMOut(pin, \*, duty_cycle=0, frequency=500, variable_frequency=False) +//| +//| Create a PWM object associated with the given pin. This allows you to +//| write PWM signals out on the given pin. Frequency is fixed after init +//| unless ``variable_frequency`` is True. +//| +//| .. note:: When ``variable_frequency`` is True, further PWM outputs may be +//| limited because it may take more internal resources to be flexible. So, +//| when outputting both fixed and flexible frequency signals construct the +//| fixed outputs first. +//| +//| :param ~microcontroller.Pin pin: The pin to output to +//| :param int duty: The fraction of each pulse which is high. 16-bit +//| :param int frequency: The target frequency in Hertz (32-bit) +//| :param bool variable_frequency: True if the frequency will change over time +//| +//| Simple LED fade:: +//| +//| import pulseio +//| import board +//| +//| with pulseio.PWMOut(board.D13) as pwm: # output on D13 +//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz +//| +//| PWM at specific frequency (servos and motors):: +//| +//| import pulseio +//| import board +//| +//| with pulseio.PWMOut(board.D13, frequency=50) as pwm: +//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz +//| +//| Variable frequency (usually tones):: +//| +//| import pulseio +//| import board +//| import time +//| +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: +//| time.sleep(0.2) +//| pwm.frequency = 880 +//| time.sleep(0.1) +//| +STATIC mp_obj_t pulseio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + mp_obj_t pin_obj = args[0]; + assert_pin(pin_obj, false); + const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); + assert_pin_free(pin); + + // create PWM object from the given pin + pulseio_pwmout_obj_t *self = m_new_obj(pulseio_pwmout_obj_t); + self->base.type = &pulseio_pwmout_type; + + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + enum { ARG_duty_cycle, ARG_frequency, ARG_variable_frequency }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500} }, + { MP_QSTR_variable_frequency, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, + }; + mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, args + 1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); + uint16_t duty_cycle = parsed_args[ARG_duty_cycle].u_int; + uint32_t frequency = parsed_args[ARG_frequency].u_int; + bool variable_frequency = parsed_args[ARG_variable_frequency].u_int; + + common_hal_pulseio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialises the PWMOut and releases any hardware resources for reuse. +//| +STATIC mp_obj_t pulseio_pwmout_deinit(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pwmout_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_deinit_obj, pulseio_pwmout_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t pulseio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_pulseio_pwmout_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pulseio_pwmout_obj___exit__); + +//| .. attribute:: duty_cycle +//| +//| 16 bit value that dictates how much of one cycle is high (1) versus low +//| (0). 0xffff will always be high, 0 will always be low and 0x7fff will +//| be half high and then half low. +STATIC mp_obj_t pulseio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle); + +STATIC mp_obj_t pulseio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_int_t duty = mp_obj_get_int(duty_cycle); + if (duty < 0 || duty > 0xffff) { + mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)"); + } + common_hal_pulseio_pwmout_set_duty_cycle(self, duty); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_duty_cycle_obj, pulseio_pwmout_obj_set_duty_cycle); + +const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_duty_cycle_obj, + (mp_obj_t)&pulseio_pwmout_set_duty_cycle_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: frequency +//| +//| 32 bit value that dictates the PWM frequency in Hertz (cycles per +//| second). Only writeable when constructed with ``variable_frequency=True``. +//| +STATIC mp_obj_t pulseio_pwmout_obj_get_frequency(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency); + +STATIC mp_obj_t pulseio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) { + mp_raise_AttributeError( + "PWM frequency not writeable when variable_frequency is False on " + "construction."); + } + common_hal_pulseio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_frequency_obj, pulseio_pwmout_obj_set_frequency); + +const mp_obj_property_t pulseio_pwmout_frequency_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_frequency_obj, + (mp_obj_t)&pulseio_pwmout_set_frequency_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t pulseio_pwmout_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pwmout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pwmout___exit___obj) }, + + // Properties + { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&pulseio_pwmout_duty_cycle_obj) }, + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&pulseio_pwmout_frequency_obj) }, + // TODO(tannewt): Add enabled to determine whether the signal is output + // without giving up the resources. Useful for IR output. +}; +STATIC MP_DEFINE_CONST_DICT(pulseio_pwmout_locals_dict, pulseio_pwmout_locals_dict_table); + +const mp_obj_type_t pulseio_pwmout_type = { + { &mp_type_type }, + .name = MP_QSTR_PWMOut, + .make_new = pulseio_pwmout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pwmout_locals_dict, +}; diff --git a/shared-bindings/pulseio/PWMOut.h b/shared-bindings/pulseio/PWMOut.h new file mode 100644 index 000000000..f7a0bff22 --- /dev/null +++ b/shared-bindings/pulseio/PWMOut.h @@ -0,0 +1,45 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/pulseio/PWMOut.h" + +extern const mp_obj_type_t pulseio_pwmout_type; + +extern void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, + bool variable_frequency); +extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty); +extern uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency); +extern uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self); +extern bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ diff --git a/shared-bindings/pulseio/PulseIn.c b/shared-bindings/pulseio/PulseIn.c new file mode 100644 index 000000000..e87020cd9 --- /dev/null +++ b/shared-bindings/pulseio/PulseIn.c @@ -0,0 +1,285 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * 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" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/PulseIn.h" + +//| .. currentmodule:: pulseio +//| +//| :class:`PulseIn` -- Read a series of pulse durations +//| ======================================================== +//| +//| PulseIn is used to measure a series of active and idle pulses. This is +//| commonly used in infrared receivers and low cost temperature sensors (DHT). +//| The pulsed signal consists of timed active and idle periods. Unlike PWM, +//| there is no set duration for active and idle pairs. +//| +//| .. class:: PulseIn(pin, maxlen=2, \*, idle_state=False) +//| +//| Create a PulseIn object associated with the given pin. The object acts as +//| a read-only sequence of pulse lengths with a given max length. When it is +//| active, new pulse lengths are added to the end of the list. When there is +//| no more room (len() == `maxlen`) the oldest pulse length is removed to +//| make room. +//| +//| :param ~microcontroller.Pin pin: Pin to read pulses from. +//| :param int maxlen: Maximum number of pulse durations to store at once +//| :param bool idle_state: Idle state of the pin. At start and after `resume` +//| the first recorded pulse will the opposite state from idle. +//| +//| Read a short series of pulses:: +//| +//| import pulseio +//| import board +//| +//| with pulseio.PulseIn(board.D7) as pulses: +//| # Wait for an active pulse +//| while len(pulses) == 0: +//| pass +//| # Pause while we do something with the pulses +//| pulses.pause() +//| +//| # Print the pulses. pulses[0] is an active pulse unless the length +//| # reached max length and idle pulses are recorded. +//| print(pulses) +//| +//| # Clear the rest +//| pulse_in.clear() +//| +//| # Resume with an 80 microsecond active pulse +//| pulse_in.resume(80) +//| +STATIC mp_obj_t pulseio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_pin, ARG_maxlen, ARG_idle_state }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_maxlen, MP_ARG_INT, {.u_int = 2} }, + { MP_QSTR_idle_state, MP_ARG_BOOL, {.u_bool = false} }, + }; + 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); + assert_pin(args[ARG_pin].u_obj, false); + const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); + assert_pin_free(pin); + + pulseio_pulsein_obj_t *self = m_new_obj(pulseio_pulsein_obj_t); + self->base.type = &pulseio_pulsein_type; + + common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, + args[ARG_idle_state].u_bool); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialises the PulseIn and releases any hardware resources for reuse. +//| +STATIC mp_obj_t pulseio_pulsein_deinit(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pulsein_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_deinit_obj, pulseio_pulsein_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t pulseio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_pulseio_pulsein_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulsein___exit___obj, 4, 4, pulseio_pulsein_obj___exit__); + +//| .. method:: pause() +//| +//| Pause pulse capture +//| +STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_pulseio_pulsein_pause(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_pause_obj, pulseio_pulsein_obj_pause); + +//| .. method:: resume(trigger_duration=0) +//| +//| Resumes pulse capture after an optional trigger pulse. +//| +//| .. warning:: Using trigger pulse with a device that drives both high and +//| low signals risks a short. Make sure your device is open drain (only +//| drives low) when using a trigger pulse. You most likely added a +//| "pull-up" resistor to your circuit to do this. +//| +//| :param int trigger_duration: trigger pulse duration in microseconds +//| +STATIC mp_obj_t pulseio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t trigger_duration = 0; + if (MP_OBJ_IS_SMALL_INT(duration_obj)) { + trigger_duration = MP_OBJ_SMALL_INT_VALUE(duration_obj); + } else if (duration_obj != mp_const_none) { + mp_raise_TypeError("trigger_duration must be int"); + } + + common_hal_pulseio_pulsein_resume(self, trigger_duration); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulsein_resume_obj, pulseio_pulsein_obj_resume); + +//| .. method:: clear() +//| +//| Clears all captured pulses +//| +STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_pulseio_pulsein_clear(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_clear_obj, pulseio_pulsein_obj_clear); + +//| .. method:: popleft() +//| +//| Removes and returns the oldest read pulse. +//| +STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft); + +//| .. attribute:: maxlen +//| +//| Returns the maximum length of the PulseIn. When len() is equal to maxlen, +//| it is unclear which pulses are active and which are idle. +//| +STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen); + +const mp_obj_property_t pulseio_pulsein_maxlen_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&pulseio_pulsein_get_maxlen_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: __len__() +//| +//| Returns the current pulse length +//| +//| This allows you to:: +//| +//| pulses = pulseio.PulseIn(pin) +//| print(len(pulses)) +//| +STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t len = common_hal_pulseio_pulsein_get_len(self); + switch (op) { + case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); + case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); + default: return MP_OBJ_NULL; // op not supported + } +} + +//| .. method:: __get__(index) +//| +//| Returns the value at the given index or values in slice. +//| +//| This allows you to:: +//| +//| pulses = pulseio.PulseIn(pin) +//| print(pulses[0]) +//| +STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { + if (value == mp_const_none) { + // delete item + mp_raise_AttributeError("Cannot delete values"); + } else { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { + mp_raise_NotImplementedError("Slices not supported"); + } else { + uint16_t index = 0; + if (MP_OBJ_IS_SMALL_INT(index_obj)) { + index = MP_OBJ_SMALL_INT_VALUE(index_obj); + } else { + mp_raise_TypeError("index must be int"); + } + if (value == MP_OBJ_SENTINEL) { + // load + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_item(self, index)); + } else { + mp_raise_AttributeError("Read-only"); + } + } + } + return mp_const_none; +} + +STATIC const mp_rom_map_elem_t pulseio_pulsein_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulsein_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulsein___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&pulseio_pulsein_pause_obj) }, + { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&pulseio_pulsein_resume_obj) }, + { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&pulseio_pulsein_clear_obj) }, + { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&pulseio_pulsein_popleft_obj) }, + { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&pulseio_pulsein_maxlen_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(pulseio_pulsein_locals_dict, pulseio_pulsein_locals_dict_table); + +const mp_obj_type_t pulseio_pulsein_type = { + { &mp_type_type }, + .name = MP_QSTR_PulseIn, + .make_new = pulseio_pulsein_make_new, + .subscr = pulsein_subscr, + .unary_op = pulsein_unary_op, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulsein_locals_dict, +}; diff --git a/shared-bindings/pulseio/PulseIn.h b/shared-bindings/pulseio/PulseIn.h new file mode 100644 index 000000000..5ffab1d67 --- /dev/null +++ b/shared-bindings/pulseio/PulseIn.h @@ -0,0 +1,46 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/pulseio/PulseIn.h" + +extern const mp_obj_type_t pulseio_pulsein_type; + +extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); +extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self); +extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self); +extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration); +extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c new file mode 100644 index 000000000..20e04e8fb --- /dev/null +++ b/shared-bindings/pulseio/PulseOut.c @@ -0,0 +1,150 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * 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" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" + +//| .. currentmodule:: pulseio +//| +//| :class:`PulseOut` -- Output a pulse train +//| ======================================================== +//| +//| PulseOut is used to 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. +//| +//| .. class:: PulseOut(carrier) +//| +//| Create a PulseOut object associated with the given PWM out experience. +//| +//| :param ~pulseio.PWMOut carrier: PWMOut that is set to output on the desired pin. +//| +//| Send a short series of pulses:: +//| +//| import array +//| import pulseio +//| import board +//| +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: +//| pulse = pulseio.PulseOut(pwm) +//| # on off on off on +//| pulses = array.array('h', [65000, 1000, 65000, 65000, 1000]) +//| pulse.send(pulses) +//| +//| # Modify the array of pulses. +//| pulses[0] = 200 +//| pulse.send(pulses) +//| +STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, 1, true); + mp_obj_t carrier_obj = args[0]; + + if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", pulseio_pwmout_type.name)); + } + + // create Pulse object from the given pin + pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); + self->base.type = &pulseio_pulseout_type; + + common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialises the PulseOut and releases any hardware resources for reuse. +//| +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); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulseout_deinit_obj, pulseio_pulseout_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t pulseio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_pulseio_pulseout_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4, pulseio_pulseout_obj___exit__); + +//| .. method:: send(pulses) +//| +//| 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). +//| +//| This method waits until the whole array of pulses has been sent and +//| ensures the signal is off afterwards. +//| +//| :param array.array pulses: pulse durations in microseconds +//| +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); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); + if (bufinfo.typecode != 'H') { + mp_raise_TypeError("Array must contain halfwords (type 'H')"); + } + common_hal_pulseio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulseout_send_obj, pulseio_pulseout_obj_send); + +STATIC const mp_rom_map_elem_t pulseio_pulseout_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulseout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { 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) }, +}; +STATIC MP_DEFINE_CONST_DICT(pulseio_pulseout_locals_dict, pulseio_pulseout_locals_dict_table); + +const mp_obj_type_t pulseio_pulseout_type = { + { &mp_type_type }, + .name = MP_QSTR_PulseOut, + .make_new = pulseio_pulseout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulseout_locals_dict, +}; diff --git a/shared-bindings/pulseio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h new file mode 100644 index 000000000..5365fd1f5 --- /dev/null +++ b/shared-bindings/pulseio/PulseOut.h @@ -0,0 +1,42 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" + +extern const mp_obj_type_t pulseio_pulseout_type; + +extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier); +extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); +extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, + uint16_t* pulses, uint16_t len); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ diff --git a/shared-bindings/pulseio/__init__.c b/shared-bindings/pulseio/__init__.c new file mode 100644 index 000000000..27d978ec5 --- /dev/null +++ b/shared-bindings/pulseio/__init__.c @@ -0,0 +1,87 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * 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 "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/__init__.h" +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" + +//| :mod:`pulseio` --- Support for pulse based protocols +//| ================================================= +//| +//| .. module:: pulseio +//| :synopsis: Support for pulse based protocols +//| :platform: SAMD21, ESP8266 +//| +//| The `pulseio` module contains classes to provide access to basic pulse IO. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| PulseIn +//| PulseOut +//| PWMOut +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import pulseio +//| import time +//| from board import * +//| +//| with pulseio.PWMOut(D13) as pin: +//| pin.duty_cycle = 2 ** 15 +//| time.sleep(0.1) +//| +//| This example will initialize the the device, set +//| :py:data:`~pulseio.PWMOut.duty_cycle`, sleep 0.1 seconds and then +//| :py:meth:`~pulseio.PWMOut.deinit` the hardware. +//| + +STATIC const mp_rom_map_elem_t pulseio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pulseio) }, + { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&pulseio_pulsein_type) }, + { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&pulseio_pulseout_type) }, + { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&pulseio_pwmout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(pulseio_module_globals, pulseio_module_globals_table); + +const mp_obj_module_t pulseio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&pulseio_module_globals, +}; diff --git a/shared-bindings/pulseio/__init__.h b/shared-bindings/pulseio/__init__.h new file mode 100644 index 000000000..26bb9f7b4 --- /dev/null +++ b/shared-bindings/pulseio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ |
