-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
255 lines (218 loc) · 5.86 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"image/color"
"machine"
"strconv"
"time"
pio "github.com/tinygo-org/pio/rp2-pio"
"github.com/tinygo-org/pio/rp2-pio/piolib"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/ssd1306"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/shnm"
)
// WS2812B LEDコントローラー構造体
type WS2812B struct {
Pin machine.Pin
ws *piolib.WS2812B
}
func NewWS2812B(pin machine.Pin) *WS2812B {
s, _ := pio.PIO0.ClaimStateMachine()
ws, _ := piolib.NewWS2812B(s, pin)
ws.EnableDMA(true)
return &WS2812B{
ws: ws,
}
}
func (ws *WS2812B) PutColor(c color.Color) {
ws.ws.PutColor(c)
}
func (ws *WS2812B) WriteRaw(rawGRB []uint32) error {
return ws.ws.WriteRaw(rawGRB)
}
// 定数定義
const (
testDuration = 10 * time.Second
ROWS = 3
COLS = 4
debounceTime = 50 * time.Millisecond
displayUpdateInterval = 50 * time.Millisecond
)
// キーの状態を保持する構造体
type KeyState struct {
isPressed bool
lastPress time.Time
}
// ディスプレイの状態を管理する構造体
type DisplayState struct {
display *ssd1306.Device // ポインタ型
lastCount int
lastTime int
needUpdate bool
}
func NewDisplayState(display *ssd1306.Device) *DisplayState {
return &DisplayState{
display: display,
lastCount: -1,
lastTime: -1,
needUpdate: false,
}
}
// ディスプレイ更新を最適化する関数
func (ds *DisplayState) updateDisplay(count int, remainingTime int, testStatus string) {
if count != ds.lastCount || remainingTime != ds.lastTime || ds.needUpdate {
ds.display.ClearBuffer()
white := color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
switch testStatus {
case "waiting":
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 12, "赤いキーを押す!!", white)
case "testing":
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 12, "残り時間: "+strconv.Itoa(remainingTime)+"秒", white)
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 30, "打鍵数: "+strconv.Itoa(count), white)
case "result":
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 12, "テスト終了", white)
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 30, "合計打鍵数: "+strconv.Itoa(count), white)
speed := float64(count) / 10.0
speedStr := strconv.FormatFloat(speed, 'f', 1, 64)
tinyfont.WriteLine(ds.display, &shnm.Shnmk12, 5, 48, "速度: "+speedStr+"回/秒", white)
}
ds.display.Display()
ds.lastCount = count
ds.lastTime = remainingTime
ds.needUpdate = false
}
}
func waitForSW12Key(colPins []machine.Pin, rowPins []machine.Pin) {
wasPressed := false
for {
colPins[3].High()
time.Sleep(time.Millisecond)
if rowPins[2].Get() {
if !wasPressed {
wasPressed = true
}
} else if wasPressed {
break
}
colPins[3].Low()
time.Sleep(10 * time.Millisecond)
}
time.Sleep(100 * time.Millisecond)
}
func scanKeys(colPins []machine.Pin, rowPins []machine.Pin, keyStates *[][]KeyState) int {
keyCount := 0
now := time.Now()
for col := 0; col < len(colPins); col++ {
// 現在の列をアクティブに
for i, pin := range colPins {
if i == col {
pin.High()
} else {
pin.Low()
}
}
time.Sleep(time.Millisecond)
// 行をスキャン
for row := 0; row < len(rowPins); row++ {
currentState := rowPins[row].Get()
if currentState {
if !(*keyStates)[row][col].isPressed {
(*keyStates)[row][col].isPressed = true
(*keyStates)[row][col].lastPress = now
keyCount++
}
} else {
if (*keyStates)[row][col].isPressed {
(*keyStates)[row][col].isPressed = false
if now.Sub((*keyStates)[row][col].lastPress) > debounceTime {
// デバウンス処理済み
}
}
}
}
}
return keyCount
}
func main() {
// WS2812B LED初期化
ws := NewWS2812B(machine.GPIO1)
leds := make([]uint32, 12)
leds[11] = 0x00FF0000 // 赤色LED点灯
ws.WriteRaw(leds)
// I2C初期化
machine.I2C0.Configure(machine.I2CConfig{
Frequency: 2.8 * machine.MHz,
SDA: machine.GPIO12,
SCL: machine.GPIO13,
})
// ディスプレイ初期化 - ここを修正
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{
Address: 0x3C,
Width: 128,
Height: 64,
})
display.SetRotation(drivers.Rotation180)
// ディスプレイ状態管理の初期化 - ポインタとして渡す
displayState := NewDisplayState(&display)
// キーマトリックスピン設定
colPins := []machine.Pin{
machine.GPIO5,
machine.GPIO6,
machine.GPIO7,
machine.GPIO8,
}
rowPins := []machine.Pin{
machine.GPIO9,
machine.GPIO10,
machine.GPIO11,
}
// ピンの初期化
for _, c := range colPins {
c.Configure(machine.PinConfig{Mode: machine.PinOutput})
c.Low()
}
for _, c := range rowPins {
c.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
}
// キー状態の初期化
keyStates := make([][]KeyState, ROWS)
for i := range keyStates {
keyStates[i] = make([]KeyState, COLS)
}
for {
// 待機画面表示
displayState.needUpdate = true
displayState.updateDisplay(0, 0, "waiting")
// キー待機
waitForSW12Key(colPins, rowPins)
// テスト開始
keyCount := 0
startTime := time.Now()
endTime := startTime.Add(testDuration)
// キー状態をリセット
for i := range keyStates {
for j := range keyStates[i] {
keyStates[i][j].isPressed = false
}
}
// 定期更新用のティッカー設定
ticker := time.NewTicker(displayUpdateInterval)
defer ticker.Stop()
// メインテストループ
for time.Now().Before(endTime) {
keyCount += scanKeys(colPins, rowPins, &keyStates)
remainingTime := int(endTime.Sub(time.Now()).Seconds())
select {
case <-ticker.C:
displayState.updateDisplay(keyCount, remainingTime, "testing")
default:
// 継続
}
}
// 結果表示
displayState.needUpdate = true
displayState.updateDisplay(keyCount, 0, "result")
time.Sleep(3 * time.Second)
}
}