2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2016-11-03 18:50:59 -04: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 <string.h>
|
|
|
|
|
2017-02-19 10:11:33 -05:00
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
|
2016-11-29 18:49:50 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/analogio/AnalogOut.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"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-04-29 17:36:28 -04:00
|
|
|
//| class AnalogOut:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Output analog values (a specific voltage).
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-04-29 17:36:28 -04:00
|
|
|
//| Example usage::
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-04-29 17:36:28 -04:00
|
|
|
//| import analogio
|
2021-03-14 04:15:35 -04:00
|
|
|
//| from board import *
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2021-03-14 04:15:35 -04:00
|
|
|
//| dac = analogio.AnalogOut(A2) # output on pin A2
|
|
|
|
//| dac.value = 32768 # makes A2 1.65V"""
|
2020-04-29 17:36:28 -04:00
|
|
|
//|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self, pin: microcontroller.Pin) -> None:
|
2020-04-29 17:36:28 -04:00
|
|
|
//| """Use the AnalogOut on the given pin.
|
|
|
|
//|
|
|
|
|
//| :param ~microcontroller.Pin pin: the pin to output to"""
|
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2019-01-14 20:26:36 -05:00
|
|
|
STATIC mp_obj_t analogio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2016-11-03 18:50:59 -04:00
|
|
|
// check arguments
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_check_num(n_args, kw_args, 1, 1, false);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-03-05 16:35:31 -05:00
|
|
|
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]);
|
2016-11-29 18:49:50 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
analogio_analogout_obj_t *self = m_new_obj(analogio_analogout_obj_t);
|
|
|
|
self->base.type = &analogio_analogout_type;
|
|
|
|
common_hal_analogio_analogout_construct(self, pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-02-28 23:32:24 -05:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-04-29 17:36:28 -04:00
|
|
|
//| """Turn off the AnalogOut and release the pin for other use."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogout_deinit(mp_obj_t self_in) {
|
|
|
|
analogio_analogout_obj_t *self = self_in;
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_analogio_analogout_deinit(self);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogout_deinit_obj, analogio_analogout_deinit);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> AnalogOut:
|
2020-04-29 17:36:28 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2017-02-19 10:11:33 -05:00
|
|
|
// Provided by context manager helper.
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __exit__(self) -> None:
|
2020-04-29 17:36:28 -04:00
|
|
|
//| """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 analogio_analogout___exit__(size_t n_args, const mp_obj_t *args) {
|
2016-12-06 13:31:38 -05:00
|
|
|
(void)n_args;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_analogio_analogout_deinit(args[0]);
|
2016-12-06 13:31:38 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4, analogio_analogout___exit__);
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| value: int
|
2020-04-29 17:36:28 -04:00
|
|
|
//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (write-only)
|
|
|
|
//|
|
|
|
|
//| Even if the underlying digital to analog converter (DAC) is lower
|
|
|
|
//| resolution, the value is 16-bit."""
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
|
2021-03-15 09:57:36 -04:00
|
|
|
analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (common_hal_analogio_analogout_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
uint32_t v = mp_obj_get_int(value);
|
|
|
|
if (v >= (1 << 16)) {
|
|
|
|
mp_raise_ValueError(translate("AnalogOut is only 16 bits. Value must be less than 65536."));
|
|
|
|
}
|
|
|
|
common_hal_analogio_analogout_set_value(self, v);
|
|
|
|
return mp_const_none;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(analogio_analogout_set_value_obj, analogio_analogout_obj_set_value);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_property_t analogio_analogout_value_obj = {
|
2016-11-03 18:50:59 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&mp_const_none_obj,
|
2017-04-10 16:32:19 -04:00
|
|
|
(mp_obj_t)&analogio_analogout_set_value_obj,
|
2016-11-03 18:50:59 -04:00
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t analogio_analogout_locals_dict_table[] = {
|
2016-11-03 18:50:59 -04:00
|
|
|
// instance methods
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogout_deinit_obj) },
|
2017-02-19 10:11:33 -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(&analogio_analogout___exit___obj) },
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
// Properties
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&analogio_analogout_value_obj },
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(analogio_analogout_locals_dict, analogio_analogout_locals_dict_table);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_type_t analogio_analogout_type = {
|
2016-11-03 18:50:59 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_AnalogOut,
|
2017-04-10 16:32:19 -04:00
|
|
|
.make_new = analogio_analogout_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&analogio_analogout_locals_dict,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|