-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.go
78 lines (70 loc) · 1.79 KB
/
mapping.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
package main
//DMXAddress stroes inforamtion about an dmx address with universe information
type DMXAddress struct {
Universe uint16
Channel uint16 //has to be between 0 and 511
}
//Key stores the information about a key. the keyboard id and the keycode
type Key struct {
KeyboardID int
Key uint16
}
//KeyMap stores the connection between a key and the corresponding DMXaddress
var keyMap = make(map[Key]DMXAddress)
func initMapping(conf config) {
//convert the slice of mapTypes to key and dmx addresses
for _, val := range conf.KeyMap {
setMapping(val.Universe, val.Channel, val.Keycode, val.KeyboardID)
}
}
func sendViaMap(event KeyEvent) {
key := Key{
KeyboardID: event.KeyboardID,
Key: event.KeyCode,
}
addr := keyMap[key]
if event.Value == 1 {
sendData(addr.Universe, addr.Channel, 255)
} else if event.Value == 0 {
sendData(addr.Universe, addr.Channel, 0)
}
}
//setMapping sets the mapping of the key on the keyboard to the desired values
func setMapping(universe uint16, channel uint16, keycode uint16, keyboard int) mapType {
key := Key{
Key: keycode,
KeyboardID: keyboard,
}
dmx := DMXAddress{
Universe: universe,
Channel: channel,
}
keyMap[key] = dmx
return convertToMapType(key, dmx)
}
func deleteMapping(keycode uint16, keyboardID int) bool {
key := Key{
Key: keycode,
KeyboardID: keyboardID,
}
if _, ok := keyMap[key]; !ok {
return false
}
delete(keyMap, key)
return true
}
func convertToMapType(key Key, dmx DMXAddress) mapType {
return mapType{
Universe: dmx.Universe,
Channel: dmx.Channel,
Keycode: key.Key,
KeyboardID: key.KeyboardID,
}
}
func getKeyMapAsMapType() []mapType {
list := make([]mapType, 0)
for key, dmx := range keyMap {
list = append(list, convertToMapType(key, dmx))
}
return list
}