-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.go
More file actions
188 lines (173 loc) · 4.22 KB
/
menu.go
File metadata and controls
188 lines (173 loc) · 4.22 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"image/color"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/shnm"
"github.com/SWITCHSCIENCE/ffb_steering_controller/settings"
"github.com/SWITCHSCIENCE/picossci-ffb-wheel/board"
)
const maxVisibleItems = 3
type Value struct {
Min, Max, Step float32
Value float32
Format string
}
func (v *Value) String() string {
return fmt.Sprintf(v.Format, v.Value)
}
type Item struct {
Name string
Value *Value
}
type Menu struct {
settingMode bool
edit bool
index int
offset int
params []*Item
current settings.Settings
}
func NewMenu() *Menu {
return &Menu{
params: []*Item{
{"Exit without Saving", nil},
{"Save and Exit", nil},
{"Lock2Lock", &Value{Min: 180, Max: 1440, Step: 90, Format: "%.0f"}},
{"NeutralAdjust", &Value{Min: -10, Max: 10, Step: 0.5, Format: "%.1f"}},
{"MaxCenteringForce", &Value{Min: 0, Max: 1000, Step: 100, Format: "%.0f"}},
{"CoggingTorqueCancel", &Value{Min: 0, Max: 256, Step: 1, Format: "%.0f"}},
{"Viscosity", &Value{Min: 0, Max: 1024, Step: 16, Format: "%.0f"}},
{"SoftLockForce", &Value{Min: 0, Max: 16, Step: 1, Format: "%.0f"}},
},
}
}
func (m *Menu) IsSetting() bool {
return m.settingMode
}
func (m *Menu) Up() {
if !m.settingMode {
return
}
if m.edit {
item := m.params[m.index]
if item.Value == nil {
return
}
item.Value.Value -= item.Value.Step
if item.Value.Value < item.Value.Min {
item.Value.Value = item.Value.Min
}
m.ShowEdit(item)
} else {
if m.index > 0 {
m.index--
if m.index < m.offset {
m.offset = m.index
}
m.ShowMenu()
}
return
}
}
func (m *Menu) Down() {
if !m.settingMode {
return
}
if m.edit {
item := m.params[m.index]
if item.Value == nil {
return
}
item.Value.Value += item.Value.Step
if item.Value.Value > item.Value.Max {
item.Value.Value = item.Value.Max
}
m.ShowEdit(item)
} else {
if m.index < len(m.params)-1 {
m.index++
if m.index-m.offset >= maxVisibleItems {
m.offset++
}
m.ShowMenu()
}
return
}
}
func (m *Menu) Enter() {
if !m.settingMode {
m.settingMode = true
m.index = 0
m.offset = 0
m.current = settings.Get()
m.params[2].Value.Value = float32(m.current.Lock2Lock)
m.params[3].Value.Value = m.current.NeutralAdjust
m.params[4].Value.Value = float32(m.current.MaxCenteringForce)
m.params[5].Value.Value = float32(m.current.CoggingTorqueCancel)
m.params[6].Value.Value = float32(m.current.Viscosity)
m.params[7].Value.Value = float32(m.current.SoftLockForceMagnitude)
m.ShowMenu()
return
}
switch m.params[m.index].Name {
case "Save and Exit":
// TODO: persistent params
b := settings.Marshal(m.current)
if err := writeFlashBlock(b); err != nil {
println(err)
}
fallthrough
case "Exit without Saving":
m.settingMode = false
board.LCD.Show(board.Logo)
board.LCD.Display()
return
default:
if m.edit {
m.edit = false
m.ShowMenu()
} else {
m.edit = true
m.ShowEdit(m.params[m.index])
}
}
}
func (m *Menu) ShowEdit(p *Item) {
switch p.Name {
case "Lock2Lock":
m.current.Lock2Lock = int32(p.Value.Value)
case "NeutralAdjust":
m.current.NeutralAdjust = p.Value.Value
case "MaxCenteringForce":
m.current.MaxCenteringForce = int32(p.Value.Value)
case "CoggingTorqueCancel":
m.current.CoggingTorqueCancel = int32(p.Value.Value)
case "Viscosity":
m.current.Viscosity = int32(p.Value.Value)
case "SoftLockForce":
m.current.SoftLockForceMagnitude = int32(p.Value.Value)
}
settings.Update(m.current)
lines := []string{fmt.Sprintf("%s:", p.Name), "", fmt.Sprintf(" %s", p.Value.String()), ""}
board.LCD.Clear()
for i, line := range lines {
tinyfont.WriteLine(board.LCD, &shnm.Shnmk12, 1, int16(12*(i+1)), line, color.RGBA{0, 0, 0, 255})
}
board.LCD.Display()
}
func (m *Menu) ShowMenu() {
lines := []string{fmt.Sprintf("=== Setting(%d/%d) ===", m.index+1, len(m.params))}
for idx, param := range m.params[m.offset : m.offset+maxVisibleItems] {
c := ' '
if m.index == idx+m.offset {
c = '>'
}
lines = append(lines, fmt.Sprintf("%c%s", c, param.Name))
}
board.LCD.Clear()
for i, line := range lines {
tinyfont.WriteLine(board.LCD, &shnm.Shnmk12, 1, int16(12*(i+1)), line, color.RGBA{0, 0, 0, 255})
}
board.LCD.Display()
}