-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMadLed.cs
727 lines (578 loc) · 22 KB
/
MadLed.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using HidSharp;
using MarkdownUI.WPF;
using SimpleLed;
using DeviceTypes = SimpleLed.DeviceTypes;
using Timer = System.Timers.Timer;
namespace Driver.MadLed
{
public class MadLed : ISimpleLedWithConfig
{
public List<CustomDeviceSpecification> GetCustomDeviceSpecifications()
{
return new List<CustomDeviceSpecification>
{
};
}
public static int PacketSize = 64;
public void Dispose()
{
}
private MadLedDevice.MadLedConfig config;
public event Events.DeviceChangeEventHandler DeviceAdded;
public event Events.DeviceChangeEventHandler DeviceRemoved;
private List<USBDevice> SupportedDevices = new List<USBDevice>
{
new USBDevice{VID = 0x1b4f, HID = 0x9207},
new USBDevice{VID = 0x1b4f, HID = 0x9204},
new USBDevice{VID = 0x2341, HID = 0x8036},
new USBDevice{VID = 0x1B1C, HID = 0x0C0B},
new USBDevice{VID = 0x1B4F, HID = 0x9206}
};
public void Configure(DriverDetails driverDetails)
{
config = new MadLedDevice.MadLedConfig();
config.PinConfigs = new MadLedDevice.PinConfig[8];
var connected = SimpleLed.SLSManager.GetSupportedDevices(SupportedDevices);
foreach (USBDevice supportedDevice in connected)
{
InterestedUSBChange(supportedDevice.VID, supportedDevice.HID.Value, true);
}
}
private float globalBrightness = 0.5f;
public void Push(ControlDevice controlDevice)
{
MadLedControlDevice mlcd = (MadLedControlDevice)controlDevice;
byte[] leds = new byte[mlcd.LEDs.Length * 3];
for (int i = 0; i < mlcd.LEDs.Length; i++)
{
leds[(i * 3) + 0] = (byte)(mlcd.LEDs[i].Color.Blue * globalBrightness);
leds[(i * 3) + 1] = (byte)(mlcd.LEDs[i].Color.Green * globalBrightness);
leds[(i * 3) + 2] = (byte)(mlcd.LEDs[i].Color.Red * globalBrightness);
}
mlcd.MadLedDevice.SendLeds(mlcd.MadLedDevice.stream, leds, (byte)mlcd.Pin);
}
public void Pull(ControlDevice controlDevice)
{
}
public DriverProperties GetProperties()
{
return new DriverProperties
{
SupportsPull = false,
SupportsPush = true,
IsSource = false,
SupportsCustomConfig = true,
ProductId = Guid.Parse("5dcf474c-4bd0-4516-b871-f98ca885dbb0"),
Author = "MadNinja",
Blurb = "Driver for controlling MadLed controllers.",
CurrentVersion = new ReleaseNumber(1, 0, 1, AutoRevision.BuildRev.BuildRevision),
GitHubLink = "https://github.com/SimpleLed/Driver.MadLedProtocol",
IsPublicRelease = false,
SupportedDevices = this.SupportedDevices,
SetDeviceOverride = SetDeviceOverride,
DeviceSpecifications = GetCustomDeviceSpecifications(),
GetMappers = GetMappers
};
}
private List<Type> GetMappers()
{
return new List<Type>();
}
public T GetConfig<T>() where T : SLSConfigData
{
MadLedDevice.MadLedConfig data = this.config;
SLSConfigData proxy = data;
return (T)proxy;
}
public void SetColorProfile(ColorProfile value)
{
}
public void PutConfig<T>(T config) where T : SLSConfigData
{
this.config = config as MadLedDevice.MadLedConfig;
}
public string Name()
{
return "MadLed";
}
public static List<string> ConnectedMadLedUnits = new List<string>();
public static List<MadLedDevice> MadLedDevices = new List<MadLedDevice>();
public void InterestedUSBChange(int VID, int PID, bool connected)
{
if (connected)
{
var madLedDevices = MadLedDevice.GetMadLedDevices(VID, PID, this);
foreach (var m in madLedDevices)
{
var remove = MadLedDevices.Where(x => x.Serial == m.Serial);
foreach (MadLedDevice madLedDevice in remove.ToList())
{
MadLedDevices.Remove(madLedDevice);
}
ConnectedMadLedUnits.Add(m.Serial);
MadLedDevices.Add(m);
}
}
else
{
}
}
public void SetDeviceOverride(ControlDevice controlDevice, CustomDeviceSpecification deviceSpec)
{
controlDevice.LEDs = new ControlDevice.LedUnit[deviceSpec.LedCount];
for (int p = 0; p < deviceSpec.LedCount; p++)
{
controlDevice.LEDs[p] = new ControlDevice.LedUnit
{
Data = new ControlDevice.LEDData
{
LEDNumber = p
},
LEDName = "LED " + (p + 1),
Color = new LEDColor(0, 0, 0)
};
}
controlDevice.CustomDeviceSpecification = deviceSpec;
}
public static string[] deviceTypes = new[]
{
"Fan",
"LedStrip",
"GPU",
"MotherBoard",
"Keyboard",
"Keypad",
"Mouse",
"MousePad",
"Headset",
"HeadsetStand",
"PSU",
"Cooler",
"Memory",
"Speaker",
"Bulb",
"Other",
"AIO",
"WaterBlock"
};
public class MadLedControlDevice : ControlDevice
{
public MadLedDevice MadLedDevice { get; set; }
public int Pin { get; set; }
}
public class MadLedDevice
{
public List<MadLedControlDevice> ControlDevices = new List<MadLedControlDevice>();
public HidStream stream = null;
public int VID;
public int PID;
public string Serial;
public byte[] SupportedPins;
public MadLed Driver { get; set; }
private T HandleIncoming<T>(byte[] data)
{
switch (data[2])
{
case (127):
byte pin = data[3];
break;
case (100):
SupportedPins = new byte[data.Length - 3];
int ii = 3;
List<byte> supTempt = new List<byte>();
while (ii < 64)
{
byte sp = data[ii];
if (sp == 0)
{
break;
}
supTempt.Add(sp);
ii++;
}
return (T)((object)supTempt.ToArray());
case (134):
var pc = new PinConfig();
pc.Pin = data[3];
pc.LedCount = data[4];
pc.DeviceClass = data[5];
for (int i = 6; i < 6 + 16; i++)
{
byte t = data[i];
if (t == 13 || t == 0)
{
break;
}
else
{
pc.Name = pc.Name + (char)t;
}
}
return (T)((object)pc);
case (2):
string r = "";
for (int i = 0; i < 16; i++)
{
byte t = data[i + 3];
if (t == 13 || t == 0)
{
break;
}
else
{
r = r + (char)t;
}
}
//Debug.WriteLine("----------------------------------------------");
//Debug.WriteLine(r);
//Debug.WriteLine("----------------------------------------------");
if (!r.StartsWith("MLG4"))
{
return default;
}
return (T)((object)r.Substring(4).Trim());
default:
Debug.WriteLine(data[2]);
break;
}
return default;
}
private MadLedControlDevice SetUpPin(PinConfig pc)
{
if (pc.LedCount > 0 && pc.Name != null)
{
string dt = "";
if (pc.DeviceClass >= 0 && pc.DeviceClass < deviceTypes.Length)
{
dt = deviceTypes[pc.DeviceClass];
MadLedControlDevice mlcd = new MadLedControlDevice
{
ChannelUniqueId = "Pin " + pc.Pin,
DeviceType = dt,
Name = pc.Name.Trim(),
MadLedDevice = this,
LEDs = new ControlDevice.LedUnit[pc.LedCount],
Driver = this.Driver,
Pin = pc.Pin,
OverrideSupport = OverrideSupport.All,
CustomDeviceSpecification = new GenericCooler()
};
for (int p = 0; p < pc.LedCount; p++)
{
mlcd.LEDs[p] = new ControlDevice.LedUnit
{
Data = new ControlDevice.LEDData { LEDNumber = p },
LEDName = "LED " + (p + 1),
Color = new LEDColor(0, 0, 0)
};
}
return mlcd;
}
}
return null;
}
public MadLedDevice()
{
}
public static List<MadLedDevice> GetMadLedDevices(int vid, int pid, MadLed madLed)
{
List<MadLedDevice> results = new List<MadLedDevice>();
var terp = new OpenConfiguration();
terp.SetOption(OpenOption.Transient, true);
var loader = new HidDeviceLoader();
HidDevice device = null;
HidSharp.HidDevice[] devices = null;
HidSharp.HidDevice[] tempdevices = loader.GetDevices(vid).ToArray();
if (tempdevices.Length > 0)
{
devices = tempdevices;
}
int attempts = 0;
if (devices != null)
{
int channelCount = 0;
foreach (HidDevice hidDevice in devices)
{
try
{
device = hidDevice;
byte[] t = device.GetRawReportDescriptor();
var dddd = (device.GetFriendlyName());
MadLedDevice nmld = new MadLedDevice();
nmld.Driver = madLed;
nmld.stream = device.Open(terp);
nmld.stream.ReadTimeout = 1000;
nmld.stream.WriteTimeout = 10000;
string serial = nmld.GetSerial(nmld.stream);
channelCount++;
ControlChannel controlChannel = new ControlChannel()
{
Name = "MadLed " + channelCount + " (" + dddd + ")",
Serial = serial
};
if (!string.IsNullOrWhiteSpace(serial))
{
nmld.Serial = serial;
nmld.DeviceName = dddd;
var pins = nmld.GetSupportedPins();
nmld.SupportedPins = pins;
foreach (byte pin in pins)
{
var pc = nmld.GetConfigFromPin(pin, nmld.stream);
//nmld.SendPacket(nmld.stream, nmld.SetConfigCmd(pin,pc));
var mlcd = nmld.SetUpPin(pc);
if (mlcd != null)
{
mlcd.ControlChannel = controlChannel;
nmld.ControlDevices.Add(mlcd);
nmld.Driver.InvokeAdded(mlcd);
}
}
nmld.VID = vid;
nmld.PID = pid;
results.Add(nmld);
}
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
attempts++;
device = null;
Thread.Sleep(100);
}
}
}
return results;
}
public string DeviceName { get; set; }
public T ReadUsb<T>()
{
isTryingToReadUSB = true;
try
{
if (stream != null)
{
if (stream.CanRead)
{
byte[] temp = new byte[64];
var t = stream.Read(temp);
if (t > 0)
{
int cmd = temp[2];
return (T)HandleIncoming<T>(temp);
}
}
}
}
catch
{
}
isTryingToReadUSB = false;
return default;
}
private bool isTryingToReadUSB = false;
public T SendPacket<T>(HidStream stream, byte[] packet, bool expandTo64Bytes = true)
{
SendPacket(stream, packet, expandTo64Bytes);
return ReadUsb<T>();
}
public void SendPacket(HidStream stream, byte[] packet, bool expandTo64Bytes = true)
{
try
{
while (isSending)
{
Thread.Sleep(0);
}
if (packet.Length < 64)
{
byte[] tmp = new byte[64];
for (int i = 0; i < packet.Length; i++)
{
tmp[i] = packet[i];
}
packet = tmp;
}
isSending = true;
stream.Write(packet);
stream.Flush();
}
catch { }
isSending = false;
}
public bool isSending = false;
short[] Get16BitColorAsShortArray(byte r, byte g, byte b)
{
return new short[]
{
(short)(((r / 8) << 11) | ((g / 4) << 5) | (b / 8))
};
}
byte[] Get16BitColorAsBytes(byte r, byte g, byte b)
{
byte[] output = new byte[2];
Buffer.BlockCopy(Get16BitColorAsShortArray(r, g, b), 0, output, 0, 2);
return output;
}
public void SendLeds(HidStream stream, byte[] leds, byte pin)
{
// Debug.WriteLine("Sending leds for "+pin);
byte[] buf = new byte[PacketSize];
buf[0] = 0;
buf[1] = 0;
buf[2] = pin;
byte page = 0;
int ld = 0;
int nmOfLds = leds.Length / 3;
while (ld < nmOfLds)
{
buf[3] = page;
int ps = 4;
int clc = 0;
while (ps < 60 && ld < nmOfLds && clc < 26)
{
int ldp = ld * 3;
byte r = leds[ldp + 0];
byte g = leds[ldp + 1];
byte b = leds[ldp + 2];
var smallColor = Get16BitColorAsBytes(r, g, b);
buf[ps] = smallColor[0];
buf[ps + 1] = smallColor[1];
ps = ps + 2;
ld = ld + 1;
clc++;
}
SendPacket(stream, buf);
page++;
}
}
public byte[] SetSupportedPinsCmd()
{
byte[] buf = new byte[3];
buf[1] = 99;
return buf;
}
public byte[] SetConfigCmd(int pin, PinConfig pc)
{
byte[] buf = new byte[PacketSize];
for (int i = 0; i < PacketSize; i++)
{
buf[i] = 0;
}
buf[0] = 0; //iunno
buf[1] = 1; //cmd
buf[2] = (byte)pin; //pin
buf[3] = (byte)pc.LedCount; //leds
buf[4] = (byte)pc.DeviceClass; //class
for (int c = 0; c < 16; c++)
{
if (c < pc.Name.Length)
{
buf[5 + c] = (byte)pc.Name.ToCharArray()[c];
}
}
return buf;
}
public PinConfig GetConfigFromPin(int pin, HidStream stream)
{
byte[] buf = new byte[PacketSize];
buf[0] = 3;
buf[1] = 3;
buf[2] = (byte)pin;
return SendPacket<PinConfig>(stream, buf);
}
public byte[] GetSupportedPins() => SendPacket<byte[]>(stream, SetSupportedPinsCmd());
public string GetSerial(HidStream stream)
{
byte[] buf = new byte[PacketSize];
buf[0] = 5;
buf[1] = 5;
buf[2] = 5;
return SendPacket<string>(stream, buf);
}
public void DebugReport(byte[] ret)
{
Console.WriteLine("");
for (int y = 0; y < 8; y++)
{
string hx = "";
string ascii = "";
string numbers = "";
for (int x = 0; x < 8; x++)
{
int db = 0;
if (1 + x + (y * 8) < ret.Length)
{
db = ret[1 + x + (y * 8)];
}
hx = hx + $"{db:X2} ";
ascii = ascii + (char)(db);
numbers = numbers + $"{db:D2} ";
}
Console.WriteLine(hx + " - " + numbers + " - " + ascii);
}
Console.WriteLine("");
}
public class PinConfig
{
public int LedCount { get; set; }
public int DeviceClass { get; set; }
public string Name { get; set; }
public int Pin { get; set; }
}
public class MadLedConfig : SLSConfigData
{
public PinConfig[] PinConfigs = new PinConfig[8];
}
}
public MadLedMDUIViewModel ViewModel;
public MarkdownUIBundle GetCustomConfig(ControlDevice controlDevice)
{
if (ViewModel == null)
{
ViewModel = new MadLedMDUIViewModel();
}
ViewModel.MadLedViewDevices = new List<MadLedMDUIViewModel.MadLedViewDevice>();
foreach (var p in MadLedDevices)
{
foreach (var e in p.ControlDevices)
{
ViewModel.MadLedViewDevices.Add(new MadLedMDUIViewModel.MadLedViewDevice
{
LedCount = e.LEDs.ToList().Count.ToString(),
Name = e.Name,
Serial = p.Serial
});
}
}
string md = MarkdownReader.GetText(Assembly.GetExecutingAssembly(), "MadLedConfig.md");
return new MarkdownUIBundle
{
ViewModel = ViewModel,
Markdown = md
};
//return new MadLedConfigPage(this);
}
public bool GetIsDirty()
{
return false;
}
public void SetIsDirty(bool val)
{
}
public void InvokeAdded(MadLedControlDevice mlcd)
{
DeviceAdded?.Invoke(this, new Events.DeviceChangeEventArgs(mlcd));
}
}
}