-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
80 lines (63 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2019 Michal Derkacz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This example shows how to handle the push-button with interrupts and with
// the software debouncing done right.
package main
import (
"embedded/rtos"
"time"
"github.com/embeddedgo/stm32/hal/exti"
"github.com/embeddedgo/stm32/hal/irq"
"github.com/embeddedgo/stm32/devboard/f4-discovery/board/buttons"
"github.com/embeddedgo/stm32/devboard/f4-discovery/board/leds"
)
func main() {
pin := buttons.User.Pin() // PA0
line := exti.Lines(1 << pin.Num())
line.Connect(pin.Port())
line.EnableRiseTrig()
line.EnableFallTrig()
irq.EXTI0.Enable(rtos.IntPrioLow, 0)
for {
leds.Green.SetOff()
leds.Orange.SetOn()
waitBtn(0)
waitBtn(1)
leds.Orange.SetOff()
leds.Red.SetOn()
waitBtn(0)
waitBtn(1)
leds.Red.SetOff()
leds.Blue.SetOn()
waitBtn(0)
waitBtn(1)
leds.Blue.SetOff()
leds.Green.SetOn()
waitBtn(0)
waitBtn(1)
}
}
var note rtos.Note
func waitBtn(state int) {
line := exti.Lines(1 << buttons.User.Pin().Num())
for {
note.Clear()
line.EnableIRQ()
wait := time.Duration(-1)
if buttons.User.Read() == state {
wait = 50 * time.Millisecond // we want 50 ms of stable state
}
if !note.Sleep(wait) {
line.DisableIRQ()
return
}
}
}
//go:interrupthandler
func EXTI0_Handler() {
line := exti.Lines(1 << buttons.User.Pin().Num())
line.DisableIRQ()
line.ClearPending()
note.Wakeup()
}