-
Notifications
You must be signed in to change notification settings - Fork 4
/
led.go
63 lines (53 loc) · 1.25 KB
/
led.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
// 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 leds
import (
"github.com/embeddedgo/stm32/hal/gpio"
_ "github.com/embeddedgo/stm32/devboard/emw3162/board/system"
)
// Onboard LEDs
const (
D1 LED = 0x10 // PB0
D2 LED = 0x11 // PB1
Green = D1
Red = D2
User = D1
)
type LED uint8
func (d LED) prt() int { return int(d) >> 4 }
func (d LED) pin() uint { return uint(d) & 15 }
func (d LED) SetOn() {
if d&1 == 0 {
gpio.P(d.prt()).SetPins(1 << d.pin())
} else {
gpio.P(d.prt()).ClearPins(1 << d.pin())
}
}
func (d LED) SetOff() {
if d&1 == 0 {
gpio.P(d.prt()).ClearPins(1 << d.pin())
} else {
gpio.P(d.prt()).SetPins(1 << d.pin())
}
}
func (d LED) Set(on int) {
gpio.P(d.prt()).StorePins(1<<d.pin(), gpio.Pins((on&1^int(d&1))<<d.pin()))
}
func (d LED) Get() int {
return int(gpio.P(d.prt()).LoadOut()>>d.pin())&1 ^ int(d&1)
}
func (d LED) Toggle() {
d.Set(d.Get() ^ 1)
}
func (d LED) Pin() gpio.Pin {
return gpio.P(d.prt()).Pin(int(d.pin()))
}
func init() {
gpio.PB().EnableClock(true)
cfg := &gpio.Config{Mode: gpio.Out, Speed: gpio.Low}
D1.Pin().Setup(cfg)
cfg.Driver = gpio.OpenDrain
D2.SetOff()
D2.Pin().Setup(cfg)
}