Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
iotee
firmware
Merge requests
!5
fix broken buttons support / debounce
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
fix broken buttons support / debounce
dev
into
main
Overview
0
Commits
1
Pipelines
0
Changes
4
Merged
Thomas Maier
requested to merge
dev
into
main
1 year ago
Overview
0
Commits
1
Pipelines
0
Changes
4
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
28b0bb66
1 commit,
1 year ago
4 files
+
56
−
48
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
buttons.cpp
+
53
−
26
Options
#include
"buttons.hpp"
#include
"hardware/gpio.h"
#include
"pico/stdlib.h"
#include
<map>
#define BTN_DEBOUNCE_MS
10
0
#define BTN_DEBOUNCE_MS
25
0
DebouncedButton
::
DebouncedButton
(
char
name
)
{
_name
=
name
;
last
=
to_ms_since_boot
(
get_absolute_time
());
}
class
DebouncedButton
{
public:
DebouncedButton
()
{}
char
DebouncedButton
::
name
()
{
return
_name
;
}
DebouncedButton
(
char
name
,
uint8_t
gpio
)
{
_name
=
name
;
_gpio
=
gpio
;
last
=
to_ms_since_boot
(
get_absolute_time
());
}
bool
DebouncedButton
::
debounced
()
{
unsigned
long
now
=
to_ms_since_boot
(
get_absolute_time
());
if
(
last
+
BTN_DEBOUNCE_MS
>
now
)
{
last
=
now
;
return
true
;
char
name
()
{
return
_name
;
}
uint8_t
gpio
()
{
return
_gpio
;
}
return
false
;
}
bool
debounced
()
{
uint32_t
now
=
to_ms_since_boot
(
get_absolute_time
());
if
(
now
-
last
>
BTN_DEBOUNCE_MS
)
{
last
=
now
;
return
true
;
}
return
false
;
}
private
:
char
_name
;
uint8_t
_gpio
;
uint32_t
last
;
};
DebouncedButton
_buttons
[]
=
{
DebouncedButton
(
'A'
,
12
),
DebouncedButton
(
'B'
,
13
),
DebouncedButton
(
'X'
,
14
),
DebouncedButton
(
'Y'
,
15
),
};
void
IoTeeButtons
::
setup
(
gpio_irq_callback_t
cb
)
{
buttons
[
12
]
=
DebouncedButton
(
'A'
);
buttons
[
13
]
=
DebouncedButton
(
'B'
);
buttons
[
14
]
=
DebouncedButton
(
'X'
);
buttons
[
15
]
=
DebouncedButton
(
'Y'
);
for
(
auto
const
&
b
:
buttons
)
{
uint8_t
gpio
=
static_cast
<
uint8_t
>
(
b
.
first
);
gpio_pull_up
(
gpio
);
gpio_set_irq_enabled_with_callback
(
gpio
,
GPIO_IRQ_EDGE_FALL
,
true
,
cb
);
for
(
DebouncedButton
button
:
_buttons
)
{
gpio_pull_up
(
button
.
gpio
());
gpio_set_irq_enabled_with_callback
(
button
.
gpio
(),
GPIO_IRQ_EDGE_FALL
,
true
,
cb
);
}
}
bool
IoTeeButtons
::
debounced
(
uint8_t
gpio
)
{
return
buttons
[
gpio
].
debounced
();
for
(
DebouncedButton
&
button
:
_buttons
)
{
if
(
button
.
gpio
()
==
gpio
)
{
return
button
.
debounced
();
}
}
return
false
;
}
char
IoTeeButtons
::
name
(
uint8_t
gpio
)
{
return
buttons
[
gpio
].
name
();
for
(
DebouncedButton
button
:
_buttons
)
{
if
(
button
.
gpio
()
==
gpio
)
{
return
button
.
name
();
}
}
return
' '
;
}
Loading