-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_secondary.go
125 lines (110 loc) · 2.6 KB
/
ble_secondary.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
//go:build adv360pro_ble_secondary
package main
import (
"time"
"github.com/bgould/adv360pro-firmware/adv360pro"
"github.com/bgould/adv360pro-firmware/adv360pro/ble"
"github.com/bgould/keyboard-firmware/keyboard"
)
func init() {
board.SetEventReceiver(keyboard.EventReceiverFunc(eventReceiver))
}
func startBLE() {
time.Sleep(time.Second)
if debug {
println("enabling secondary")
}
ble.Default.EnableSecondary()
if debug {
println("enabled secondary device", ble.Default.Mode())
}
// go func() {
// for {
// if receivedEvent {
// sendMatrixPacket()
// receivedEvent = false
// }
// time.Sleep(10 * time.Millisecond)
// }
// }()
}
var (
lastSent time.Time
lastMatrix [adv360pro.MatrixRows]keyboard.Row
)
func bleTask() {
// now := time.Now()
// // if now.Sub(lastSent) > 5*time.Millisecond {
// sendMatrixPacket()
// lastSent = now
// }
}
// var receivedEvent bool
func eventReceiver(ev keyboard.Event) (bool, error) {
sendMatrixPacket()
return true, nil
// receivedEvent = true
// var packet ble.MatrixPacket
// var matrix [adv360pro.MatrixRows]keyboard.Row
// for i := 0; i < adv360pro.MatrixRows; i++ {
// matrix[i] = keyboard.Row(board.GetMatrixRowState(i))
// }
// packet.EncodeFrom(adv360pro.MatrixRows, adv360pro.MatrixCols, matrix[:])
// // for i, n := 0, int(board.MatrixRows()); i < n; i++ {
// // if (i+1)*3 < 20 {
// // state := board.GetMatrixRowState(i)
// // bin[i*3+0] = byte(state >> 0xF)
// // bin[i*3+1] = byte(state >> 0x8)
// // bin[i*3+2] = byte(state >> 0x0)
// // }
// // }
// if debug {
// print("send --> ")
// for i := 0; i < len(packet); i++ {
// print(" ", packet[i])
// }
// }
// println()
// if ok, err := ble.Default.Tx(packet[:]); !ok {
// if err != nil {
// println("tx error:", err.Error())
// } else {
// println("tx not sent")
// }
// }
// return true, nil
}
func getMatrix() (matrix [adv360pro.MatrixRows]keyboard.Row) {
for i := 0; i < adv360pro.MatrixRows; i++ {
matrix[i] = keyboard.Row(board.GetMatrixRowState(i))
}
return
}
func sendMatrixPacket() (bool, error) {
var packet ble.MatrixPacket
matrix := getMatrix()
packet.EncodeFrom(adv360pro.MatrixRows, adv360pro.MatrixCols, matrix[:])
if debug {
print("send --> ")
for i := 0; i < len(packet); i++ {
print(" ", packet[i])
}
println()
}
if ok, err := ble.Default.Tx(packet[:]); !ok {
if err != nil {
if debug {
println("tx error:", err.Error())
}
return false, err
} else {
if debug {
println("tx not sent")
}
return false, nil
}
}
lastMatrix = matrix
lastSent = time.Now()
return true, nil
}