Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
fd860dc5
Commit
fd860dc5
authored
Jun 15, 2017
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
stmhal: Add .value() method to Switch object, to mirror Pin and Signal.
parent
4abe3731
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
docs/library/pyb.Switch.rst
+6
-1
6 additions, 1 deletion
docs/library/pyb.Switch.rst
docs/pyboard/tutorial/switch.rst
+7
-1
7 additions, 1 deletion
docs/pyboard/tutorial/switch.rst
stmhal/usrsw.c
+7
-0
7 additions, 0 deletions
stmhal/usrsw.c
with
20 additions
and
2 deletions
docs/library/pyb.Switch.rst
+
6
−
1
View file @
fd860dc5
...
@@ -8,7 +8,8 @@ A Switch object is used to control a push-button switch.
...
@@ -8,7 +8,8 @@ A Switch object is used to control a push-button switch.
Usage::
Usage::
sw = pyb.Switch() # create a switch object
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
sw.callback(f) # register a callback to be called when the
# switch is pressed down
# switch is pressed down
sw.callback(None) # remove the callback
sw.callback(None) # remove the callback
...
@@ -34,6 +35,10 @@ Methods
...
@@ -34,6 +35,10 @@ Methods
Call switch object directly to get its state: ``True`` if pressed down,
Call switch object directly to get its state: ``True`` if pressed down,
``False`` otherwise.
``False`` otherwise.
.. method:: Switch.value()
Get the switch state. Returns `True` if pressed down, otherwise `False`.
.. method:: Switch.callback(fun)
.. method:: Switch.callback(fun)
Register the given function to be called when the switch is pressed down.
Register the given function to be called when the switch is pressed down.
...
...
This diff is collapsed.
Click to expand it.
docs/pyboard/tutorial/switch.rst
+
7
−
1
View file @
fd860dc5
...
@@ -15,12 +15,18 @@ the name ``pyb`` does not exist.
...
@@ -15,12 +15,18 @@ the name ``pyb`` does not exist.
With the switch object you can get its status::
With the switch object you can get its status::
>>> sw()
>>> sw
.value
()
False
False
This will print ``False`` if the switch is not held, or ``True`` if it is held.
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.
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
Switch callbacks
----------------
----------------
...
...
This diff is collapsed.
Click to expand it.
stmhal/usrsw.c
+
7
−
0
View file @
fd860dc5
...
@@ -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_
...
@@ -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
;
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
)
{
STATIC
mp_obj_t
switch_callback
(
mp_obj_t
line
)
{
if
(
MP_STATE_PORT
(
pyb_switch_callback
)
!=
mp_const_none
)
{
if
(
MP_STATE_PORT
(
pyb_switch_callback
)
!=
mp_const_none
)
{
mp_call_function_0
(
MP_STATE_PORT
(
pyb_switch_callback
));
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) {
...
@@ -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
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
[]
=
{
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
)
},
{
MP_ROM_QSTR
(
MP_QSTR_callback
),
MP_ROM_PTR
(
&
pyb_switch_callback_obj
)
},
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment