Skip to content

Commit

Permalink
example: naive debouncing for pininterrupt example
Browse files Browse the repository at this point in the history
  • Loading branch information
ysoldak committed Jun 26, 2023
1 parent 4619896 commit b40356c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion builder/sizes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestBinarySize(t *testing.T) {
// microcontrollers
{"hifive1b", "examples/echo", 4612, 280, 0, 2252},
{"microbit", "examples/serial", 2724, 388, 8, 2256},
{"wioterminal", "examples/pininterrupt", 6039, 1485, 116, 6816},
{"wioterminal", "examples/pininterrupt", 8007, 1485, 116, 6904},

// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
Expand Down
12 changes: 11 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 @@ -16,6 +16,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 @@ -30,6 +32,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 All @@ -41,3 +50,4 @@ func main() {
time.Sleep(time.Hour)
}
}

0 comments on commit b40356c

Please sign in to comment.