-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
122 lines (99 loc) · 2.75 KB
/
menu.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
116
117
118
119
120
121
122
package plateGenie
import (
"github.com/the-sibyl/goLCD20x4"
"time"
)
type Menu struct {
lcd *goLCD20x4.LCD20x4
firstMenuItem *MenuItem
lastMenuItem *MenuItem
currentMenuItem *MenuItem
}
func CreateMenu(lcd *goLCD20x4.LCD20x4) *Menu {
var m Menu
m.lcd = lcd
return &m
}
func (m *Menu) Button1Pressed() {
m.currentMenuItem = m.currentMenuItem.prev
m.Repaint()
time.Sleep(time.Microsecond * defaultDebounceTime)
}
func (m *Menu) Button2Pressed() {
m.currentMenuItem.action <- 1
time.Sleep(time.Microsecond * defaultDebounceTime)
}
func (m *Menu) Button3Pressed() {
m.currentMenuItem.action <- 2
time.Sleep(time.Microsecond * defaultDebounceTime)
}
func (m *Menu) Button4Pressed() {
m.currentMenuItem = m.currentMenuItem.next
m.Repaint()
time.Sleep(time.Microsecond * defaultDebounceTime)
}
func (m *Menu) Repaint() {
m.lcd.WriteLineCentered(m.currentMenuItem.Name, 1)
m.lcd.WriteLineCentered(m.currentMenuItem.Units, 2)
m.lcd.WriteLineCentered(m.currentMenuItem.Values, 3)
m.lcd.WriteLine(m.currentMenuItem.Adjustments, 4)
}
type MenuItem struct {
Name string
Units string
Values string
Adjustments string
adj1 string
adj2 string
// A channel correpsonding to the soft key pressed (1 or 2)
action chan int
prev *MenuItem
next *MenuItem
}
// Two adjustents per screen
// Seven-character limit per adjustment
func (m *Menu) AddMenuItem(name string, units string, values string, adj1 string, adj2 string) *MenuItem {
var mi MenuItem
mi.Name = name
mi.Units = units
mi.Values = values
// Add a full 7 characters of padding to the end in case the string is empty
adj1 += " "
adj2 += " "
mi.adj1 = adj1[0:7]
mi.adj2 = adj2[0:7]
mi.FormatAdjustmentsString()
// Update the links in the menu
if m.firstMenuItem == nil {
m.firstMenuItem = &mi
}
if m.lastMenuItem == nil {
m.lastMenuItem = &mi
}
if m.currentMenuItem == nil {
m.currentMenuItem = &mi
m.Repaint()
}
// Update links for the previous menu item
m.lastMenuItem.next = &mi
// Update links for this menu item
mi.prev = m.lastMenuItem
m.lastMenuItem = &mi
mi.next = m.firstMenuItem
// Update the links in the first and last menu items
m.firstMenuItem.prev = &mi
m.lastMenuItem.next = m.firstMenuItem
mi.action = make(chan int)
return &mi
}
// Output channels for the two user-defined softkey actions. Return a read-only channel.
func (mi *MenuItem) AddAction() (action <-chan int) {
action = make(<-chan int)
action = mi.action
return action
}
// Helper for the last line which has the adjustment text and previous and next screen arrows
func (mi *MenuItem) FormatAdjustmentsString() {
sc := goLCD20x4.GetSpecialCharacters()
mi.Adjustments = sc.LeftArrow + " " + mi.adj1 + " " + mi.adj2 + " " + sc.RightArrow
}