-
Notifications
You must be signed in to change notification settings - Fork 0
/
board-explorer.go
199 lines (170 loc) · 5.09 KB
/
board-explorer.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
package main
import (
"context"
"flag"
"fmt"
evdev "github.com/gvalkov/golang-evdev"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/go-ble/ble"
"github.com/go-ble/ble/examples/lib/dev"
"github.com/simulatedsimian/joystick"
)
var (
joystick1, joystick2 bool
)
// readInputEvents reads events from the touch input device and sends formatted messages
func readInputEvents(inputDev *evdev.InputDevice) {
for {
events, err := inputDev.Read()
if err != nil {
log.Printf("Error reading input events: %v", err)
os.Exit(1)
}
for _, event := range events {
fmt.Println(formatEvent(&event))
}
time.Sleep(100 * time.Millisecond)
}
}
// formatEvent formats the input events for logging
func formatEvent(ev *evdev.InputEvent) string {
switch ev.Type {
case evdev.EV_SYN:
if ev.Code == evdev.SYN_MT_REPORT {
return fmt.Sprintf("time %d.%-8d +++++++++ %s ++++++++", ev.Time.Sec, ev.Time.Usec, evdev.SYN[ev.Code])
}
return fmt.Sprintf("time %d.%-8d --------- %s --------", ev.Time.Sec, ev.Time.Usec, evdev.SYN[ev.Code])
case evdev.EV_KEY:
codeName := evdev.KEY[ev.Code]
return fmt.Sprintf("time %d.%-8d type %d (EV_KEY), code %-3d (%s), value %d", ev.Time.Sec, ev.Time.Usec, ev.Type, ev.Code, codeName, ev.Value)
default:
return fmt.Sprintf("time %d.%-8d type %d, code %-3d, value %d", ev.Time.Sec, ev.Time.Usec, ev.Type, ev.Code, ev.Value)
}
}
// readJoystick reads joystick events and sends commands based on joystick input
func readJoystick(js joystick.Joystick, messages chan<- byte) {
jinfo, err := js.Read()
if err != nil {
log.Printf("Error reading joystick: %v", err)
return
}
if jinfo.Buttons&(1<<0) != 0 {
messages <- 0x6f // Button pressed
} else {
messages <- 0x70 // Button released
}
// Handle axis input for directions
switch {
case jinfo.AxisData[0] == 32767:
messages <- 0x7a // UP
case jinfo.AxisData[0] == -32767:
messages <- 0x73 // REVERSE
case jinfo.AxisData[1] == -32767:
messages <- 0x71 // LEFT
case jinfo.AxisData[1] == 32767:
messages <- 0x64 // RIGHT
}
}
func main() {
log.Printf("Welcome to board-explorer!")
// Parse command line flags
microbitName1 := flag.String("microbitName1", "", "Name of microbit 1")
microbitName2 := flag.String("microbitName2", "", "Name of microbit 2 (optional)")
flag.Parse()
if *microbitName1 == "" {
flag.Usage()
os.Exit(1)
}
// Setup signal handling to gracefully terminate
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-sig
cancel()
}()
// Initialize BLE device
device, err := dev.DefaultDevice()
if err != nil {
log.Fatalf("Failed to initialize BLE device: %v", err)
}
ble.SetDefaultDevice(device)
// Connect to microbit 1 via BLE
player1, err := ble.Connect(ctx, func(a ble.Advertisement) bool {
return a.Connectable() && strings.HasPrefix(a.LocalName(), "BBC micro:bit ["+*microbitName1+"]")
})
if err != nil {
log.Fatalf("Failed to connect to microbit 1: %v", err)
}
defer player1.CancelConnection()
// Discover services and characteristics
p1, err := player1.DiscoverProfile(true)
if err != nil {
log.Fatalf("Failed to discover profile: %v", err)
}
characteristic1 := p1.FindCharacteristic(ble.NewCharacteristic(ble.MustParse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")))
// Initialize joystick 1
js1, err := joystick.Open(0)
if err != nil {
log.Printf("Failed to open joystick 1: %v", err)
} else {
joystick1 = true
}
var (
player2 ble.Client
characteristic2 *ble.Characteristic
js2 joystick.Joystick
)
// If microbit 2 is specified, connect to it as well
if *microbitName2 != "" {
player2, err = ble.Connect(ctx, func(a ble.Advertisement) bool {
return a.Connectable() && strings.HasPrefix(a.LocalName(), "BBC micro:bit ["+*microbitName2+"]")
})
if err != nil {
log.Fatalf("Failed to connect to microbit 2: %v", err)
}
defer player2.CancelConnection()
p2, err := player2.DiscoverProfile(true)
if err != nil {
log.Fatalf("Failed to discover profile for player 2: %v", err)
}
characteristic2 = p2.FindCharacteristic(ble.NewCharacteristic(ble.MustParse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")))
// Initialize joystick 2
js2, err = joystick.Open(1)
if err != nil {
log.Printf("Failed to open joystick 2: %v", err)
} else {
joystick2 = true
}
}
ticker := time.NewTicker(40 * time.Millisecond)
defer ticker.Stop()
// Create communication channels
channelBlePlayer1 := make(chan byte)
channelBlePlayer2 := make(chan byte)
for {
select {
case msg := <-channelBlePlayer1:
if err := player1.WriteCharacteristic(characteristic1, []byte{msg, 0x0a}, true); err != nil {
log.Printf("Failed to send data to player 1: %v", err)
}
case msg := <-channelBlePlayer2:
if err := player2.WriteCharacteristic(characteristic2, []byte{msg, 0x0a}, true); err != nil {
log.Printf("Failed to send data to player 2: %v", err)
}
case <-ticker.C:
if joystick1 {
go readJoystick(js1, channelBlePlayer1)
}
if joystick2 {
go readJoystick(js2, channelBlePlayer2)
}
}
}
}