-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhw_keyboard.cs
146 lines (131 loc) · 3.65 KB
/
hw_keyboard.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace guardian_server {
public class Keyboard_info {
public string name;
public int number_of_zones;
}
public class Keyboard {
Hardware hw;
dynamic kb_server;
public Dictionary<string, dynamic> zones;
public Keyboard_info kb_info;
void set_zone_color(string zone, dynamic effect) {
if (kb_server == null) {
return;
}
int z = int.Parse(zone);
int r = effect["color"][0];
int g = effect["color"][1];
int b = effect["color"][2];
if (z > kb_server.get_number_of_zones()) {
return;
}
kb_server.set_zone_color(z - 1, r, g, b);
}
void set_static_color(dynamic effect) {
string zone = effect["zone"];
if (zone == "all") {
for (int z = 1; z <= kb_info.number_of_zones; z += 1) {
set_zone_color(z.ToString(), effect);
}
} else {
set_zone_color(zone, effect);
}
}
public void reset_zone(string zone) {
hw.server.send_plugin_message("reset_keyboard_effect", zones[zone]);
if (zone == "all") {
set_default_effects();
}
set_static_color(zones[zone]);
}
public void set_keyboard_zones(dynamic effect) {
if (zones == null) {
return;
}
string name = effect["name"];
string zone = effect["zone"];
if (name == "static_color_from_plugin") {
if (zones[zone]["name"] != "static_color") {
set_static_color(effect);
}
return;
}
if (name == "off" || zones[zone]["name"] != "static_color") {
reset_zone(zone);
}
if (name == "static_color") {
set_static_color(effect);
} else if (name != "off") {
hw.server.send_plugin_message("set_keyboard_effect", effect);
}
zones[zone] = effect;
}
public void update_sensors() {
hw.server.sensors["keyboards"] = zones;
}
public void update_devices() {
hw.server.devices["led_keyboard"] = kb_info;
}
public void set_default_effect(string zone) {
var effect = new Dictionary<string, dynamic>();
effect["name"] = "static_color";
effect["zone"] = zone;
effect["color"] = new List<int> { 0, 0, 0 };
effect["beg_color"] = new List<int> { 0, 0, 0 };
effect["end_color"] = new List<int> { 0, 0, 0 };
zones[zone] = effect;
}
public void set_default_effects() {
for (int z = 1; z <= kb_info.number_of_zones; z += 1) {
set_default_effect(z.ToString());
}
set_default_effect("all");
zones["all"]["name"] = "off";
}
public void restore() {
if (zones["all"]["name"] != "off") {
set_keyboard_zones(zones["all"]);
return;
}
for (int z = 1; z <= kb_info.number_of_zones; z += 1) {
set_keyboard_zones(zones[z.ToString()]);
}
}
public Dictionary<string, dynamic> save() {
return zones;
}
public Keyboard(Hardware h) {
Program.log.add("keyboard: ");
hw = h;
kb_info = new Keyboard_info();
kb_info.name = Program.settings.keyboard;
if (kb_info.name == "SSE3") {
kb_server = new SSE(3);
} else if (kb_info.name == "SSE4") {
kb_server = new SSE(4);
} else if (kb_info.name == "SSE5") {
kb_server = new SSE(5);
} else if (kb_info.name == "Clevo") {
kb_server = new Clevo();
} else {
Program.log.add_line(kb_info.name);
return;
}
kb_info.number_of_zones = kb_server.get_number_of_zones();
zones = new Dictionary<string, dynamic>();
set_default_effects();
if (hw.state["led_keyboard"] != null) {
foreach (JProperty x in hw.state["led_keyboard"]) {
zones[x.Name] = x.Value;
}
}
restore();
Program.log.add_line(kb_info.name);
}
}
}