2017-03-25 08:04:49 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/busio/OneWire.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| class OneWire:
|
|
|
|
//| """.. currentmodule:: busio
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol
|
|
|
|
//| ================================================================="""
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def __init__(self, pin: microcontroller.Pin):
|
|
|
|
//| """(formerly Dallas Semi) OneWire protocol.
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| .. class:: OneWire(pin)
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| Create a OneWire object associated with the given pin. The object
|
|
|
|
//| implements the lowest level timing-sensitive bits of the protocol.
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| :param ~microcontroller.Pin pin: Pin connected to the OneWire bus
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| Read a short series of pulses::
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| import busio
|
|
|
|
//| import board
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| onewire = busio.OneWire(board.D7)
|
|
|
|
//| onewire.reset()
|
|
|
|
//| onewire.write_bit(True)
|
|
|
|
//| onewire.write_bit(False)
|
|
|
|
//| print(onewire.read_bit())"""
|
|
|
|
//| ...
|
2019-01-14 20:26:36 -05:00
|
|
|
STATIC mp_obj_t busio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2017-03-25 08:04:49 -04:00
|
|
|
enum { ARG_pin };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-03-05 16:35:31 -05:00
|
|
|
const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
busio_onewire_obj_t *self = m_new_obj(busio_onewire_obj_t);
|
|
|
|
self->base.type = &busio_onewire_type;
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_busio_onewire_construct(self, pin);
|
2017-03-25 08:04:49 -04:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def deinit(self, ) -> Any:
|
|
|
|
//| """Deinitialize the OneWire bus and release any hardware resources for reuse."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_onewire_deinit(mp_obj_t self_in) {
|
|
|
|
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_busio_onewire_deinit(self);
|
2017-03-25 08:04:49 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_deinit_obj, busio_onewire_deinit);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(busio_onewire_obj_t *self) {
|
|
|
|
if (common_hal_busio_onewire_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def __enter__(self, ) -> Any:
|
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2017-03-25 08:04:49 -04:00
|
|
|
// Provided by context manager helper.
|
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def __exit__(self, ) -> Any:
|
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
2017-03-25 08:04:49 -04:00
|
|
|
(void)n_args;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_busio_onewire_deinit(args[0]);
|
2017-03-25 08:04:49 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, busio_onewire_obj___exit__);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def reset(self, ) -> Any:
|
|
|
|
//| """Reset the OneWire bus and read presence
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| :returns: False when at least one device is present
|
|
|
|
//| :rtype: bool"""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) {
|
|
|
|
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
return mp_obj_new_bool(common_hal_busio_onewire_reset(self));
|
2017-03-25 08:04:49 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def read_bit(self, ) -> Any:
|
|
|
|
//| """Read in a bit
|
2017-03-25 08:04:49 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| :returns: bit state read
|
|
|
|
//| :rtype: bool"""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_onewire_obj_read_bit(mp_obj_t self_in) {
|
|
|
|
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self));
|
2017-03-25 08:04:49 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_onewire_obj_read_bit);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| def write_bit(self, value: Any) -> Any:
|
|
|
|
//| """Write out a bit based on value."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) {
|
|
|
|
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_busio_onewire_write_bit(self, mp_obj_is_true(bool_obj));
|
2017-03-25 08:04:49 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(busio_onewire_write_bit_obj, busio_onewire_obj_write_bit);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t busio_onewire_locals_dict_table[] = {
|
2017-03-25 08:04:49 -04:00
|
|
|
// Methods
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_onewire_deinit_obj) },
|
2017-03-25 08:04:49 -04: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(&busio_onewire___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&busio_onewire_reset_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&busio_onewire_read_bit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&busio_onewire_write_bit_obj) },
|
2017-03-25 08:04:49 -04:00
|
|
|
};
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(busio_onewire_locals_dict, busio_onewire_locals_dict_table);
|
2017-03-25 08:04:49 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_type_t busio_onewire_type = {
|
2017-03-25 08:04:49 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_OneWire,
|
2017-04-10 16:32:19 -04:00
|
|
|
.make_new = busio_onewire_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&busio_onewire_locals_dict,
|
2017-03-25 08:04:49 -04:00
|
|
|
};
|