Skip to content

Commit 435c9f4

Browse files
ysoldakdeadprogram
authored andcommitted
example: naive debouncing for pininterrupt example (#2233)
1 parent 3c86848 commit 435c9f4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

builder/sizes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestBinarySize(t *testing.T) {
4444
// microcontrollers
4545
{"hifive1b", "examples/echo", 4600, 280, 0, 2268},
4646
{"microbit", "examples/serial", 2908, 388, 8, 2272},
47-
{"wioterminal", "examples/pininterrupt", 6140, 1484, 116, 6824},
47+
{"wioterminal", "examples/pininterrupt", 7286, 1486, 116, 6912},
4848

4949
// TODO: also check wasm. Right now this is difficult, because
5050
// wasm binaries are run through wasm-opt and therefore the

src/examples/pininterrupt/pininterrupt.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
// This example demonstrates how to use pin change interrupts.
44
//
55
// This is only an example and should not be copied directly in any serious
6-
// circuit, because it lacks an important feature: debouncing.
6+
// circuit, because it only naively implements an important feature: debouncing.
77
// See: https://en.wikipedia.org/wiki/Switch#Contact_bounce
88

99
import (
@@ -15,6 +15,8 @@ const (
1515
led = machine.LED
1616
)
1717

18+
var lastPress time.Time
19+
1820
func main() {
1921

2022
// Configure the LED, defaulting to on (usually setting the pin to low will
@@ -29,6 +31,13 @@ func main() {
2931

3032
// Set an interrupt on this pin.
3133
err := button.SetInterrupt(buttonPinChange, func(machine.Pin) {
34+
35+
// Ignore events that are too close to the last registered press (debouncing)
36+
if time.Since(lastPress) < 100*time.Millisecond {
37+
return
38+
}
39+
lastPress = time.Now()
40+
3241
led.Set(!led.Get())
3342
})
3443
if err != nil {

0 commit comments

Comments
 (0)