-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (47 loc) · 1.08 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
package main
import (
"machine"
"time"
)
var switchState = true
func main() {
encoder := NewRotaryEncoder[int32](machine.A2, machine.A1, 4095, 10, 0, 4095)
encoder.Start()
i2c := machine.I2C1
err := i2c.Configure(machine.I2CConfig{
SCL: machine.D5,
SDA: machine.D4,
})
encoderLED := machine.D7
encoderLED.Configure(machine.PinConfig{Mode: machine.PinOutput})
encoderLED.Low()
encoderSwitch := machine.D8
encoderSwitch.Configure(machine.PinConfig{Mode: machine.PinInput})
encoderSwitch.SetInterrupt(machine.PinRising, func(machine.Pin) {
switchState = !switchState
if switchState {
encoderLED.Low()
} else {
encoderLED.High()
}
})
if err != nil {
println(err.Error())
return
}
println("Start")
for {
println(encoder.Value)
value := encoder.Value
if !switchState {
value = 0
}
// I2C で 12bit の値を DAC に送る
// 0x60: MCP4726 の I2C アドレス
wErr := i2c.WriteRegister(0x60, 0, []byte{0x00, byte((value >> 8) & 0x0F), byte(value & 0xFF)})
if wErr != nil {
println(wErr.Error())
}
time.Sleep(time.Millisecond * 100)
}
}