extmod/machine_signal: Implement "signal" abstraction for machine module.
A signal is like a pin, but ca also be inverted (active low). As such, it abstracts properties of various physical devices, like LEDs, buttons, relays, buzzers, etc. To instantiate a Signal: pin = machine.Pin(...) signal = machine.Signal(pin, inverted=True) signal has the same .value() and __call__() methods as a pin.
This commit is contained in:
parent
18b6835a92
commit
7a7516d40d
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Paul Sokolovsky
|
||||
*
|
||||
* 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 "py/mpconfig.h"
|
||||
#if MICROPY_PY_MACHINE
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
#include "extmod/virtpin.h"
|
||||
#include "extmod/machine_signal.h"
|
||||
|
||||
// Signal class
|
||||
|
||||
typedef struct _machine_signal_t {
|
||||
mp_obj_base_t base;
|
||||
mp_obj_t pin;
|
||||
bool inverted;
|
||||
} machine_signal_t;
|
||||
|
||||
STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
enum { ARG_pin, ARG_inverted };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_inverted, MP_ARG_BOOL, {.u_bool = false} },
|
||||
};
|
||||
|
||||
mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)];
|
||||
|
||||
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args);
|
||||
|
||||
machine_signal_t *o = m_new_obj(machine_signal_t);
|
||||
o->base.type = type;
|
||||
o->pin = parsed_args[ARG_pin].u_obj;
|
||||
o->inverted = parsed_args[ARG_inverted].u_bool;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
||||
(void)errcode;
|
||||
machine_signal_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
switch (request) {
|
||||
case MP_PIN_READ: {
|
||||
return mp_virtual_pin_read(self->pin) ^ self->inverted;
|
||||
}
|
||||
case MP_PIN_WRITE: {
|
||||
mp_virtual_pin_write(self->pin, arg ^ self->inverted);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// fast method for getting/setting signal value
|
||||
STATIC mp_obj_t signal_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
if (n_args == 0) {
|
||||
// get pin
|
||||
return MP_OBJ_NEW_SMALL_INT(mp_virtual_pin_read(self_in));
|
||||
} else {
|
||||
// set pin
|
||||
mp_virtual_pin_write(self_in, mp_obj_is_true(args[0]));
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) {
|
||||
return signal_call(args[0], n_args - 1, 0, args + 1);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value);
|
||||
|
||||
STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table);
|
||||
|
||||
STATIC const mp_pin_p_t signal_pin_p = {
|
||||
.ioctl = signal_ioctl,
|
||||
};
|
||||
|
||||
const mp_obj_type_t machine_signal_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Signal,
|
||||
.make_new = signal_make_new,
|
||||
.call = signal_call,
|
||||
.protocol = &signal_pin_p,
|
||||
.locals_dict = (void*)&signal_locals_dict,
|
||||
};
|
||||
|
||||
#endif // MICROPY_PY_MACHINE
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Paul Sokolovsky
|
||||
*
|
||||
* 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_EXTMOD_MACHINE_SIGNAL_H__
|
||||
#define __MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H__
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
extern const mp_obj_type_t machine_signal_type;
|
||||
|
||||
#endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H__
|
Loading…
Reference in New Issue