Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
M
micropython-as7341
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
Show more breadcrumbs
Rob Hamerling
micropython-as7341
Commits
6209431a
Commit
6209431a
authored
4 months ago
by
Rob Hamerling
Browse files
Options
Downloads
Patches
Plain Diff
Driver for Buttons or Switches
parent
d95c1b88
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
switch.py
+44
-0
44 additions, 0 deletions
switch.py
with
44 additions
and
0 deletions
switch.py
0 → 100644
+
44
−
0
View file @
6209431a
"""
class for mechanical switches (micro switches, push buttons and alike)
with rudimentary support for debouncing.
The Pins for the switches are configured as Pin.IN and Pin.PULL_UP.
The (normally-open) switches are supposed to make ground contact
(falling interrupts are used).
"""
from
machine
import
Pin
from
time
import
ticks_ms
class
Switch
(
object
):
def
__init__
(
self
,
switch
):
"""
<switch> (number) GPIO pin
"""
self
.
_pin
=
switch
# pin number
self
.
_switch
=
Pin
(
switch
,
Pin
.
IN
,
Pin
.
PULL_UP
)
self
.
_switch_interrupt
=
False
# set by switch-ISR, reset by switch() method
self
.
_debounce_ticks_ms
=
ticks_ms
()
# remember last switch interrupt time
try
:
# choose for 'hard' interrupt if supported
self
.
_interrupt
=
self
.
_switch
.
irq
(
trigger
=
Pin
.
IRQ_FALLING
,
handler
=
self
.
_callback
,
hard
=
True
)
except
TypeError
:
self
.
_interrupt
=
self
.
_switch
.
irq
(
trigger
=
Pin
.
IRQ_FALLING
,
handler
=
self
.
_callback
)
def
_callback
(
self
,
switch
):
"""
Pin interrupt service routine
"""
tick_delta
=
ticks_ms
()
-
self
.
_debounce_ticks_ms
#
# print(f"pin {self._pin} interrupt {tick_delta} status {self._switch.value()}")
if
tick_delta
>
200
:
# skip switch bounces
# print(f"pin {self._pin} debounced {tick_delta}")
self
.
_switch_interrupt
=
True
# mark 'interrupt occurred'
self
.
_debounce_ticks_ms
=
ticks_ms
()
# update time of last accepted interrupt
def
switch
(
self
):
"""
Indicator of Pin interrupt
True: interrupt occurred since last poll, False: not!
"""
if
self
.
_switch_interrupt
:
self
.
_switch_interrupt
=
False
# user is aware of interrupt
return
True
# signal switch interrupt
return
False
def
__call__
(
self
):
# make instance callable (for convenience!)
return
self
.
switch
()
# Pin.value()
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