Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example: naive debouncing for pininterrupt example #2233

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/sizes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestBinarySize(t *testing.T) {
// microcontrollers
{"hifive1b", "examples/echo", 4600, 280, 0, 2268},
{"microbit", "examples/serial", 2908, 388, 8, 2272},
{"wioterminal", "examples/pininterrupt", 6140, 1484, 116, 6824},
{"wioterminal", "examples/pininterrupt", 7286, 1486, 116, 6912},

// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
Expand Down
11 changes: 10 additions & 1 deletion src/examples/pininterrupt/pininterrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
// This example demonstrates how to use pin change interrupts.
//
// This is only an example and should not be copied directly in any serious
// circuit, because it lacks an important feature: debouncing.
// circuit, because it only naively implements an important feature: debouncing.
// See: https://en.wikipedia.org/wiki/Switch#Contact_bounce

import (
Expand All @@ -15,6 +15,8 @@ const (
led = machine.LED
)

var lastPress time.Time

func main() {

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

// Set an interrupt on this pin.
err := button.SetInterrupt(buttonPinChange, func(machine.Pin) {

// Ignore events that are too close to the last registered press (debouncing)
if time.Since(lastPress) < 100*time.Millisecond {
return
}
lastPress = time.Now()

led.Set(!led.Get())
})
if err != nil {
Expand Down
Loading