-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeroconso.go
115 lines (87 loc) · 1.9 KB
/
zeroconso.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"machine"
"time"
"./core"
"./drivers"
"tinygo.org/x/drivers/hd44780i2c"
)
const ()
var (
rt *core.RuntimeData
)
// --------- HW INIT
func hwInit() {
rt.SerialConsole = &machine.UART0
// I2C and LCD
machine.I2C0.Configure(machine.I2CConfig{
Frequency: machine.TWI_FREQ_400KHZ,
})
driver := hd44780i2c.New(machine.I2C0, 0x27) // some modules have address 0x3F
rt.Lcd = &driver
rt.Lcd.Configure(hd44780i2c.Config{
Width: 20, // required
Height: 4, // required
CursorOn: false,
CursorBlink: false,
})
}
// ---------- Key
func handleKeyboard(key byte) {
println("key:", key)
}
func readSerial() {
for {
if rt.SerialConsole.Buffered() > 0 {
data, _ := rt.SerialConsole.ReadByte()
handleKeyboard(data)
}
time.Sleep(10 * time.Millisecond)
}
}
// ---------- MAIN
func main() {
rt = &core.RuntimeData{}
rt.Metrics = &core.MetricsData{}
println("Hello TinyGo")
hwInit()
rt.Lcd.SetCursor(0, 0)
rt.Lcd.Print([]byte("HELLO"))
go readSerial()
// Blink led
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
drivers.AdcInit()
for i := 0; i < 5; i++ {
led.Low()
time.Sleep(time.Millisecond * 100)
led.High()
time.Sleep(time.Millisecond * 100)
}
display := core.NewDisplayManager(rt)
display.ScreenInfos(true)
for {
// Read U1
drivers.AdcSetChannel(0)
vms := drivers.AcAmplitudeMv()
ima := (30000 * vms) / 1000
rt.Metrics.AcVoltageMv = ima
// Read I1
drivers.AdcSetChannel(1)
vms = drivers.AcAmplitudeMv()
ima = (30000 * vms) / 1000
rt.Metrics.AcCurrent1Ma = ima
// Read I2
drivers.AdcSetChannel(2)
vms = drivers.AcAmplitudeMv()
ima = (30000 * vms) / 1000
rt.Metrics.AcCurrent2Ma = ima
// Read I3
drivers.AdcSetChannel(3)
vms = drivers.AcAmplitudeMv()
ima = (30000 * vms) / 1000
rt.Metrics.AcCurrent3Ma = ima
display.ScreenInfos(false)
time.Sleep(time.Second * 5)
}
}