stmhal: Add .value() method to Switch object, to mirror Pin and Signal.
This commit is contained in:
parent
4abe3731e3
commit
fd860dc552
|
@ -8,7 +8,8 @@ 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.value() # get state (True if pressed, False otherwise)
|
||||
sw() # shorthand notation to get the switch state
|
||||
sw.callback(f) # register a callback to be called when the
|
||||
# switch is pressed down
|
||||
sw.callback(None) # remove the callback
|
||||
|
@ -34,6 +35,10 @@ Methods
|
|||
Call switch object directly to get its state: ``True`` if pressed down,
|
||||
``False`` otherwise.
|
||||
|
||||
.. method:: Switch.value()
|
||||
|
||||
Get the switch state. Returns `True` if pressed down, otherwise `False`.
|
||||
|
||||
.. method:: Switch.callback(fun)
|
||||
|
||||
Register the given function to be called when the switch is pressed down.
|
||||
|
|
|
@ -15,12 +15,18 @@ the name ``pyb`` does not exist.
|
|||
|
||||
With the switch object you can get its status::
|
||||
|
||||
>>> sw()
|
||||
>>> sw.value()
|
||||
False
|
||||
|
||||
This will print ``False`` if the switch is not held, or ``True`` if it is held.
|
||||
Try holding the USR switch down while running the above command.
|
||||
|
||||
There is also a shorthand notation to get the switch status, by "calling" the
|
||||
switch object::
|
||||
|
||||
>>> sw()
|
||||
False
|
||||
|
||||
Switch callbacks
|
||||
----------------
|
||||
|
||||
|
|
|
@ -97,6 +97,12 @@ mp_obj_t pyb_switch_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_
|
|||
return switch_get() ? mp_const_true : mp_const_false;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
STATIC mp_obj_t switch_callback(mp_obj_t line) {
|
||||
if (MP_STATE_PORT(pyb_switch_callback) != mp_const_none) {
|
||||
mp_call_function_0(MP_STATE_PORT(pyb_switch_callback));
|
||||
|
@ -123,6 +129,7 @@ mp_obj_t pyb_switch_callback(mp_obj_t self_in, mp_obj_t callback) {
|
|||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_switch_callback_obj, pyb_switch_callback);
|
||||
|
||||
STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pyb_switch_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&pyb_switch_callback_obj) },
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue