usrsw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "py/runtime.h"
  28. #include "py/mphal.h"
  29. #include "extint.h"
  30. #include "pin.h"
  31. #include "usrsw.h"
  32. #if MICROPY_HW_HAS_SWITCH
  33. /// \moduleref pyb
  34. /// \class Switch - switch object
  35. ///
  36. /// A Switch object is used to control a push-button switch.
  37. ///
  38. /// Usage:
  39. ///
  40. /// sw = pyb.Switch() # create a switch object
  41. /// sw() # get state (True if pressed, False otherwise)
  42. /// sw.callback(f) # register a callback to be called when the
  43. /// # switch is pressed down
  44. /// sw.callback(None) # remove the callback
  45. ///
  46. /// Example:
  47. ///
  48. /// pyb.Switch().callback(lambda: pyb.LED(1).toggle())
  49. // this function inits the switch GPIO so that it can be used
  50. void switch_init0(void) {
  51. mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, 0);
  52. }
  53. int switch_get(void) {
  54. int val = ((MICROPY_HW_USRSW_PIN->gpio->IDR & MICROPY_HW_USRSW_PIN->pin_mask) != 0);
  55. return val == MICROPY_HW_USRSW_PRESSED;
  56. }
  57. /******************************************************************************/
  58. // MicroPython bindings
  59. typedef struct _pyb_switch_obj_t {
  60. mp_obj_base_t base;
  61. } pyb_switch_obj_t;
  62. STATIC const pyb_switch_obj_t pyb_switch_obj = {{&pyb_switch_type}};
  63. void pyb_switch_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  64. mp_print_str(print, "Switch()");
  65. }
  66. /// \classmethod \constructor()
  67. /// Create and return a switch object.
  68. 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) {
  69. // check arguments
  70. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  71. // No need to clear the callback member: if it's already been set and registered
  72. // with extint then we don't want to reset that behaviour. If it hasn't been set,
  73. // then no extint will be called until it is set via the callback method.
  74. // return static switch object
  75. return MP_OBJ_FROM_PTR(&pyb_switch_obj);
  76. }
  77. /// \method \call()
  78. /// Return the switch state: `True` if pressed down, `False` otherwise.
  79. mp_obj_t pyb_switch_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  80. // get switch state
  81. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  82. return switch_get() ? mp_const_true : mp_const_false;
  83. }
  84. mp_obj_t pyb_switch_value(mp_obj_t self_in) {
  85. (void)self_in;
  86. return mp_obj_new_bool(switch_get());
  87. }
  88. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_switch_value_obj, pyb_switch_value);
  89. STATIC mp_obj_t switch_callback(mp_obj_t line) {
  90. if (MP_STATE_PORT(pyb_switch_callback) != mp_const_none) {
  91. mp_call_function_0(MP_STATE_PORT(pyb_switch_callback));
  92. }
  93. return mp_const_none;
  94. }
  95. STATIC MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
  96. /// \method callback(fun)
  97. /// Register the given function to be called when the switch is pressed down.
  98. /// If `fun` is `None`, then it disables the callback.
  99. mp_obj_t pyb_switch_callback(mp_obj_t self_in, mp_obj_t callback) {
  100. MP_STATE_PORT(pyb_switch_callback) = callback;
  101. // Init the EXTI each time this function is called, since the EXTI
  102. // may have been disabled by an exception in the interrupt, or the
  103. // user disabling the line explicitly.
  104. extint_register(MP_OBJ_FROM_PTR(MICROPY_HW_USRSW_PIN),
  105. MICROPY_HW_USRSW_EXTI_MODE,
  106. MICROPY_HW_USRSW_PULL,
  107. callback == mp_const_none ? mp_const_none : MP_OBJ_FROM_PTR(&switch_callback_obj),
  108. true);
  109. return mp_const_none;
  110. }
  111. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_switch_callback_obj, pyb_switch_callback);
  112. STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = {
  113. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pyb_switch_value_obj) },
  114. { MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&pyb_switch_callback_obj) },
  115. };
  116. STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table);
  117. const mp_obj_type_t pyb_switch_type = {
  118. { &mp_type_type },
  119. .name = MP_QSTR_Switch,
  120. .print = pyb_switch_print,
  121. .make_new = pyb_switch_make_new,
  122. .call = pyb_switch_call,
  123. .locals_dict = (mp_obj_dict_t*)&pyb_switch_locals_dict,
  124. };
  125. #endif // MICROPY_HW_HAS_SWITCH