-
Notifications
You must be signed in to change notification settings - Fork 4
/
button.go
38 lines (29 loc) · 835 Bytes
/
button.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
// 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.
package buttons
import (
"github.com/embeddedgo/stm32/hal/gpio"
_ "github.com/embeddedgo/stm32/devboard/devebox-h743/board/system"
)
// Onboard buttons
const (
K1 Button = 0x43 // PE3
K2 Button = 0x25 // PC5
User = K1
)
type Button uint8
func (b Button) prt() int { return int(b) >> 4 }
func (b Button) pin() uint { return uint(b) & 15 }
func (b Button) Read() int {
return int(gpio.P(b.prt()).Load()>>b.pin())&1 ^ 1
}
func (b Button) Pin() gpio.Pin {
return gpio.P(b.prt()).Pin(int(b.pin()))
}
func init() {
gpio.PC().EnableClock(true)
gpio.PE().EnableClock(true)
K1.Pin().Setup(&gpio.Config{Mode: gpio.In})
K2.Pin().Setup(&gpio.Config{Mode: gpio.In})
}