|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#include "lib/utils/context_manager_helpers.h" |
| 30 | +#include "py/objproperty.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "py/runtime0.h" |
| 33 | +#include "shared-bindings/microcontroller/Pin.h" |
| 34 | +#include "shared-bindings/bitbangio/OneWire.h" |
| 35 | + |
| 36 | +//| .. currentmodule:: bitbangio |
| 37 | +//| |
| 38 | +//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol |
| 39 | +//| =============================================================== |
| 40 | +//| |
| 41 | +//| :class:`~bitbangio.OneWire` implements the timing-sensitive foundation of |
| 42 | +//| the Maxim (formerly Dallas Semi) OneWire protocol. |
| 43 | +//| |
| 44 | +//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 |
| 45 | +//| |
| 46 | +//| .. class:: OneWire(pin) |
| 47 | +//| |
| 48 | +//| Create a OneWire object associated with the given pin. The object |
| 49 | +//| implements the lowest level timing-sensitive bits of the protocol. |
| 50 | +//| |
| 51 | +//| :param ~microcontroller.Pin pin: Pin to read pulses from. |
| 52 | +//| |
| 53 | +//| Read a short series of pulses:: |
| 54 | +//| |
| 55 | +//| import bitbangio |
| 56 | +//| import board |
| 57 | +//| |
| 58 | +//| with bitbangio.OneWire(board.D7) as onewire: |
| 59 | +//| onewire.reset() |
| 60 | +//| onewire.write_bit(True) |
| 61 | +//| onewire.write_bit(False) |
| 62 | +//| print(onewire.read_bit()) |
| 63 | +//| |
| 64 | +STATIC mp_obj_t bitbangio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { |
| 65 | + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 66 | + mp_map_t kw_args; |
| 67 | + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
| 68 | + enum { ARG_pin }; |
| 69 | + static const mp_arg_t allowed_args[] = { |
| 70 | + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 71 | + }; |
| 72 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 73 | + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 74 | + assert_pin(args[ARG_pin].u_obj, false); |
| 75 | + const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); |
| 76 | + assert_pin_free(pin); |
| 77 | + |
| 78 | + bitbangio_onewire_obj_t *self = m_new_obj(bitbangio_onewire_obj_t); |
| 79 | + self->base.type = &bitbangio_onewire_type; |
| 80 | + |
| 81 | + shared_module_bitbangio_onewire_construct(self, pin); |
| 82 | + return MP_OBJ_FROM_PTR(self); |
| 83 | +} |
| 84 | + |
| 85 | +//| .. method:: deinit() |
| 86 | +//| |
| 87 | +//| Deinitialize the OneWire bus and release any hardware resources for reuse. |
| 88 | +//| |
| 89 | +STATIC mp_obj_t bitbangio_onewire_deinit(mp_obj_t self_in) { |
| 90 | + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 91 | + shared_module_bitbangio_onewire_deinit(self); |
| 92 | + return mp_const_none; |
| 93 | +} |
| 94 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_deinit_obj, bitbangio_onewire_deinit); |
| 95 | + |
| 96 | +//| .. method:: __enter__() |
| 97 | +//| |
| 98 | +//| No-op used by Context Managers. |
| 99 | +//| |
| 100 | +// Provided by context manager helper. |
| 101 | + |
| 102 | +//| .. method:: __exit__() |
| 103 | +//| |
| 104 | +//| Automatically deinitializes the hardware when exiting a context. |
| 105 | +//| |
| 106 | +STATIC mp_obj_t bitbangio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 107 | + (void)n_args; |
| 108 | + shared_module_bitbangio_onewire_deinit(args[0]); |
| 109 | + return mp_const_none; |
| 110 | +} |
| 111 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_onewire___exit___obj, 4, 4, bitbangio_onewire_obj___exit__); |
| 112 | + |
| 113 | +//| .. method:: reset() |
| 114 | +//| |
| 115 | +//| Reset the OneWire bus |
| 116 | +//| |
| 117 | +STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) { |
| 118 | + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 119 | + |
| 120 | + return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self)); |
| 121 | +} |
| 122 | +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_reset_obj, bitbangio_onewire_obj_reset); |
| 123 | + |
| 124 | +//| .. method:: read_bit() |
| 125 | +//| |
| 126 | +//| Read in a bit |
| 127 | +//| |
| 128 | +//| :returns: bit state read |
| 129 | +//| :rtype: bool |
| 130 | +//| |
| 131 | +STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) { |
| 132 | + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 133 | + |
| 134 | + return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self)); |
| 135 | +} |
| 136 | +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_read_bit_obj, bitbangio_onewire_obj_read_bit); |
| 137 | + |
| 138 | +//| .. method:: write_bit(value) |
| 139 | +//| |
| 140 | +//| Write out a bit based on value. |
| 141 | +//| |
| 142 | +STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { |
| 143 | + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 144 | + |
| 145 | + shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); |
| 146 | + return mp_const_none; |
| 147 | +} |
| 148 | +MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_onewire_write_bit_obj, bitbangio_onewire_obj_write_bit); |
| 149 | + |
| 150 | +STATIC const mp_rom_map_elem_t bitbangio_onewire_locals_dict_table[] = { |
| 151 | + // Methods |
| 152 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_onewire_deinit_obj) }, |
| 153 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 154 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_onewire___exit___obj) }, |
| 155 | + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&bitbangio_onewire_reset_obj) }, |
| 156 | + { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&bitbangio_onewire_read_bit_obj) }, |
| 157 | + { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&bitbangio_onewire_write_bit_obj) }, |
| 158 | +}; |
| 159 | +STATIC MP_DEFINE_CONST_DICT(bitbangio_onewire_locals_dict, bitbangio_onewire_locals_dict_table); |
| 160 | + |
| 161 | +const mp_obj_type_t bitbangio_onewire_type = { |
| 162 | + { &mp_type_type }, |
| 163 | + .name = MP_QSTR_OneWire, |
| 164 | + .make_new = bitbangio_onewire_make_new, |
| 165 | + .locals_dict = (mp_obj_dict_t*)&bitbangio_onewire_locals_dict, |
| 166 | +}; |
0 commit comments