2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-15 06:41:28 +00:00
|
|
|
#include <stdio.h>
|
2014-04-21 18:23:50 +01:00
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2014-04-18 22:38:09 +01:00
|
|
|
#include "extint.h"
|
2014-03-15 06:41:28 +00:00
|
|
|
#include "pin.h"
|
2014-04-18 22:48:59 +01:00
|
|
|
#include "usrsw.h"
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-21 18:31:40 +01:00
|
|
|
#if MICROPY_HW_HAS_SWITCH
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class Switch - switch object
|
|
|
|
///
|
|
|
|
/// A Switch object is used to control a push-button switch.
|
|
|
|
///
|
|
|
|
/// Usage:
|
|
|
|
///
|
|
|
|
/// sw = pyb.Switch() # create a switch object
|
|
|
|
/// sw() # get state (True if pressed, False otherwise)
|
|
|
|
/// sw.callback(f) # register a callback to be called when the
|
|
|
|
/// # switch is pressed down
|
|
|
|
/// sw.callback(None) # remove the callback
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// pyb.Switch().callback(lambda: pyb.LED(1).toggle())
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-03-30 00:00:15 +00:00
|
|
|
// this function inits the switch GPIO so that it can be used
|
|
|
|
void switch_init0(void) {
|
2018-03-28 06:13:21 +01:00
|
|
|
mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, 0);
|
2014-03-30 00:00:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 06:41:28 +00:00
|
|
|
int switch_get(void) {
|
2018-03-28 06:13:21 +01:00
|
|
|
int val = ((MICROPY_HW_USRSW_PIN->gpio->IDR & MICROPY_HW_USRSW_PIN->pin_mask) != 0);
|
2014-03-30 12:30:35 +01:00
|
|
|
return val == MICROPY_HW_USRSW_PRESSED;
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
// MicroPython bindings
|
2014-04-21 18:23:50 +01:00
|
|
|
|
|
|
|
typedef struct _pyb_switch_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} pyb_switch_obj_t;
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
STATIC const pyb_switch_obj_t pyb_switch_obj = {{&pyb_switch_type}};
|
2014-04-21 18:23:50 +01:00
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
void pyb_switch_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
mp_print_str(print, "Switch()");
|
2014-04-21 18:23:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod \constructor()
|
|
|
|
/// Create and return a switch object.
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t pyb_switch_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-04-21 18:23:50 +01:00
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
|
|
|
// No need to clear the callback member: if it's already been set and registered
|
|
|
|
// with extint then we don't want to reset that behaviour. If it hasn't been set,
|
|
|
|
// then no extint will be called until it is set via the callback method.
|
|
|
|
|
|
|
|
// return static switch object
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_FROM_PTR(&pyb_switch_obj);
|
2014-04-21 18:23:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method \call()
|
|
|
|
/// Return the switch state: `True` if pressed down, `False` otherwise.
|
2017-01-04 13:10:42 +00:00
|
|
|
mp_obj_t pyb_switch_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-04-21 18:23:50 +01:00
|
|
|
// get switch state
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
return switch_get() ? mp_const_true : mp_const_false;
|
|
|
|
}
|
|
|
|
|
2017-06-15 08:34:51 +01:00
|
|
|
mp_obj_t pyb_switch_value(mp_obj_t self_in) {
|
|
|
|
(void)self_in;
|
|
|
|
return mp_obj_new_bool(switch_get());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_switch_value_obj, pyb_switch_value);
|
|
|
|
|
2014-04-21 18:23:50 +01:00
|
|
|
STATIC mp_obj_t switch_callback(mp_obj_t line) {
|
2015-01-07 23:38:50 +00:00
|
|
|
if (MP_STATE_PORT(pyb_switch_callback) != mp_const_none) {
|
|
|
|
mp_call_function_0(MP_STATE_PORT(pyb_switch_callback));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
2014-04-21 18:23:50 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method callback(fun)
|
|
|
|
/// Register the given function to be called when the switch is pressed down.
|
|
|
|
/// If `fun` is `None`, then it disables the callback.
|
2014-04-21 18:23:50 +01:00
|
|
|
mp_obj_t pyb_switch_callback(mp_obj_t self_in, mp_obj_t callback) {
|
2015-01-07 23:38:50 +00:00
|
|
|
MP_STATE_PORT(pyb_switch_callback) = callback;
|
2014-04-21 18:23:50 +01:00
|
|
|
// Init the EXTI each time this function is called, since the EXTI
|
|
|
|
// may have been disabled by an exception in the interrupt, or the
|
|
|
|
// user disabling the line explicitly.
|
2018-07-08 14:25:11 +01:00
|
|
|
extint_register(MP_OBJ_FROM_PTR(MICROPY_HW_USRSW_PIN),
|
2014-04-21 18:23:50 +01:00
|
|
|
MICROPY_HW_USRSW_EXTI_MODE,
|
|
|
|
MICROPY_HW_USRSW_PULL,
|
2018-07-08 14:25:11 +01:00
|
|
|
callback == mp_const_none ? mp_const_none : MP_OBJ_FROM_PTR(&switch_callback_obj),
|
2015-01-07 23:38:50 +00:00
|
|
|
true);
|
2014-04-21 18:23:50 +01:00
|
|
|
return mp_const_none;
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
2014-04-21 18:23:50 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_switch_callback_obj, pyb_switch_callback);
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = {
|
2017-06-15 08:34:51 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pyb_switch_value_obj) },
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&pyb_switch_callback_obj) },
|
2014-04-21 18:23:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_switch_type,
|
|
|
|
MP_QSTR_Switch,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 15:31:23 +01:00
|
|
|
make_new, pyb_switch_make_new,
|
2021-07-14 05:38:38 +01:00
|
|
|
print, pyb_switch_print,
|
|
|
|
call, pyb_switch_call,
|
2022-06-24 07:27:46 +01:00
|
|
|
locals_dict, &pyb_switch_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2014-04-21 18:31:40 +01:00
|
|
|
|
2022-07-01 20:48:59 +01:00
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_t pyb_switch_callback);
|
|
|
|
|
2014-04-21 18:31:40 +01:00
|
|
|
#endif // MICROPY_HW_HAS_SWITCH
|