2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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/binary.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
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/AnalogIn.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
//| .. currentmodule:: analogio
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
|
|
|
//| :class:`AnalogIn` -- read analog voltage
|
|
|
|
//| ============================================
|
|
|
|
//|
|
|
|
|
//| Usage::
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
//| import analogio
|
2016-11-03 18:50:59 -04:00
|
|
|
//| from board import *
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
//| with analogio.AnalogIn(A1) as adc:
|
2016-11-03 18:50:59 -04:00
|
|
|
//| val = adc.value
|
|
|
|
//|
|
|
|
|
|
|
|
|
//| .. class:: AnalogIn(pin)
|
|
|
|
//|
|
2016-12-13 19:09:00 -05:00
|
|
|
//| Use the AnalogIn on the given pin. The reference voltage varies by
|
|
|
|
//| platform so use ``reference_voltage`` to read the configured setting.
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
|
|
|
//| :param ~microcontroller.Pin pin: the pin to read from
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogin_make_new(const mp_obj_type_t *type,
|
2016-11-03 18:50:59 -04:00
|
|
|
mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
|
|
|
// check number of arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
|
|
|
|
|
|
// 1st argument is the pin
|
|
|
|
mp_obj_t pin_obj = args[0];
|
2016-11-29 18:49:50 -05:00
|
|
|
assert_pin(pin_obj, false);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
analogio_analogin_obj_t *self = m_new_obj(analogio_analogin_obj_t);
|
|
|
|
self->base.type = &analogio_analogin_type;
|
2016-11-03 18:50:59 -04:00
|
|
|
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj);
|
2016-12-06 13:31:38 -05:00
|
|
|
assert_pin_free(pin);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_analogio_analogin_construct(self, pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
return (mp_obj_t) self;
|
|
|
|
}
|
|
|
|
|
2016-12-06 13:31:38 -05:00
|
|
|
//| .. method:: deinit()
|
|
|
|
//|
|
|
|
|
//| Turn off the AnalogIn and release the pin for other use.
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogin_deinit(mp_obj_t self_in) {
|
|
|
|
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_analogio_analogin_deinit(self);
|
2016-12-06 13:31:38 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_deinit_obj, analogio_analogin_deinit);
|
2016-12-06 13:31:38 -05:00
|
|
|
|
|
|
|
//| .. method:: __enter__()
|
|
|
|
//|
|
|
|
|
//| 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
|
|
|
|
|
|
|
//| .. method:: __exit__()
|
|
|
|
//|
|
|
|
|
//| Automatically deinitializes the hardware when exiting a context.
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogin___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_analogin_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_analogin___exit___obj, 4, 4, analogio_analogin___exit__);
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
//| .. attribute:: value
|
|
|
|
//|
|
|
|
|
//| Read the value on the analog pin and return it. The returned value
|
|
|
|
//| will be between 0 and 65535 inclusive (16-bit). Even if the underlying
|
|
|
|
//| analog to digital converter (ADC) is lower resolution, the result will
|
|
|
|
//| be scaled to be 16-bit.
|
|
|
|
//|
|
|
|
|
//| :return: the data read
|
|
|
|
//| :rtype: int
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) {
|
|
|
|
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self));
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_property_t analogio_analogin_value_obj = {
|
2016-11-03 18:50:59 -04:00
|
|
|
.base.type = &mp_type_property,
|
2017-04-10 16:32:19 -04:00
|
|
|
.proxy = {(mp_obj_t)&analogio_analogin_get_value_obj,
|
2016-11-03 18:50:59 -04:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2016-12-13 19:09:00 -05:00
|
|
|
//| .. attribute:: reference_voltage
|
|
|
|
//|
|
|
|
|
//| The maximum voltage measurable. Also known as the reference voltage.
|
|
|
|
//|
|
|
|
|
//| :return: the reference voltage
|
|
|
|
//| :rtype: float
|
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) {
|
|
|
|
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self));
|
2016-12-13 19:09:00 -05:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj,
|
|
|
|
analogio_analogin_obj_get_reference_voltage);
|
2016-12-13 19:09:00 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_property_t analogio_analogin_reference_voltage_obj = {
|
2016-12-13 19:09:00 -05:00
|
|
|
.base.type = &mp_type_property,
|
2017-04-10 16:32:19 -04:00
|
|
|
.proxy = {(mp_obj_t)&analogio_analogin_get_reference_voltage_obj,
|
2016-12-13 19:09:00 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t analogio_analogin_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogin_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_analogin___exit___obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&analogio_analogin_value_obj)},
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&analogio_analogin_reference_voltage_obj)},
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(analogio_analogin_locals_dict, analogio_analogin_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_analogin_type = {
|
2016-11-03 18:50:59 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_AnalogIn,
|
2017-04-10 16:32:19 -04:00
|
|
|
.make_new = analogio_analogin_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&analogio_analogin_locals_dict,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|