Skip to content

Commit acd4336

Browse files
committed
Silly (but fun) light controller
1 parent d6a866f commit acd4336

File tree

4 files changed

+174
-20
lines changed

4 files changed

+174
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019 Jon Skeet.
2+
// Licensed under the Apache License Version 2.0.
3+
4+
using MiLight.Net.Api;
5+
using MiLight.Net.Commands;
6+
using System.Net;
7+
using System.Net.Sockets;
8+
9+
namespace Shed.DrumKitLights
10+
{
11+
public sealed class LightingController
12+
{
13+
private readonly IPEndPoint endPoint;
14+
private readonly UdpClient udpClient;
15+
16+
internal LightingController(string host = "192.168.1.255", int port = 8899)
17+
{
18+
endPoint = new IPEndPoint(IPAddress.Parse(host), port);
19+
udpClient = new UdpClient();
20+
}
21+
22+
public void On()
23+
{
24+
Send(Colour.On(Zone.All));
25+
}
26+
27+
public void Off()
28+
{
29+
Send(Colour.Off(Zone.All));
30+
}
31+
32+
public void Brightness(int brightness)
33+
{
34+
Send(Colour.SetBrightness(brightness));
35+
}
36+
37+
public void Hue(int hue)
38+
{
39+
Send(Colour.Hue(hue));
40+
}
41+
42+
public void White()
43+
{
44+
Send(Colour.SetWhite(Zone.All));
45+
}
46+
47+
// There's no MiLight controller package yet, so let's just inline the
48+
// socket code here...
49+
private void Send(byte[] command)
50+
{
51+
udpClient.Send(command, command.Length, endPoint);
52+
}
53+
}
54+
}

Shed/Shed.DrumKitLights/Program.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Sanford.Multimedia.Midi;
2+
using System;
3+
4+
namespace Shed.DrumKitLights
5+
{
6+
class Program
7+
{
8+
private static int[] noteMapping = new int[128];
9+
10+
static void Main(string[] args)
11+
{
12+
for (int i = 0; i < 128; i++)
13+
{
14+
noteMapping[i] = i;
15+
}
16+
17+
// Specific mappings
18+
noteMapping[36] = 0x10; // Kick
19+
noteMapping[38] = 0x20; // Snare head
20+
noteMapping[40] = 0x30; // Snare rim
21+
noteMapping[37] = 0x40; // Snare xstick
22+
noteMapping[48] = 0x50; // Tom 1 head
23+
noteMapping[50] = 0x60; // Tom 1 rim
24+
noteMapping[45] = 0x70; // Tom 2 head
25+
noteMapping[47] = 0x80; // Tom 2 rim
26+
noteMapping[43] = 0x90; // Tom 3 head
27+
noteMapping[58] = 0xa0; // Tom 3 rim
28+
noteMapping[46] = 0xb0; // Hi-hat open bow
29+
noteMapping[26] = 0xc0; // Hi-hat open edge
30+
noteMapping[42] = 0xd0; // Hi-hat closed bow
31+
noteMapping[22] = 0xe0; // Hi-hat closed edge
32+
noteMapping[49] = 0x05; // Crash 1 bow
33+
noteMapping[55] = 0x45; // Crash 1 edge
34+
noteMapping[51] = 0x85; // Ride bow
35+
noteMapping[59] = 0xc5; // Ride edge
36+
37+
// No settings for aux, crash 2, ride bell
38+
39+
var controller = new LightingController();
40+
controller.On();
41+
controller.White();
42+
43+
using (var device = new InputDevice(0))
44+
{
45+
device.ChannelMessageReceived += (sender, e) => ReactToMessage(controller, e.Message);
46+
device.StartRecording();
47+
Console.WriteLine("Hit return to quit.");
48+
Console.ReadLine();
49+
}
50+
}
51+
52+
private static void ReactToMessage(LightingController controller, ChannelMessage message)
53+
{
54+
if (message.Command == ChannelCommand.NoteOn)
55+
{
56+
int note = message.Data1;
57+
int velocity = message.Data2;
58+
controller.Hue(noteMapping[note]);
59+
controller.Brightness(velocity / 5);
60+
Console.WriteLine($"Note: {note}; Velocity: {velocity}");
61+
}
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="MiLight.Net.Commands" Version="1.0.2-CI00029">
9+
<NoWarn>NU1701</NoWarn>
10+
</PackageReference>
11+
<PackageReference Include="Sanford.Multimedia.Midi" Version="6.6.0">
12+
<NoWarn>NU1701</NoWarn>
13+
</PackageReference>
14+
</ItemGroup>
15+
</Project>

Shed/Shed.sln

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26020.0
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29102.190
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shed.Controllers", "Shed.Controllers\Shed.Controllers.csproj", "{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shed.Controllers", "Shed.Controllers\Shed.Controllers.csproj", "{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shed.CommandLine", "Shed.CommandLine\Shed.CommandLine.csproj", "{8EADF212-BE86-45A1-AD7E-903726A85625}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shed.CommandLine", "Shed.CommandLine\Shed.CommandLine.csproj", "{8EADF212-BE86-45A1-AD7E-903726A85625}"
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shed.Uwp", "Shed.Uwp\Shed.Uwp.csproj", "{28FDC18C-24CD-470D-97B4-96D69634B4DC}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shed.DrumKitLights", "Shed.DrumKitLights\Shed.DrumKitLights.csproj", "{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -25,34 +27,34 @@ Global
2527
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|ARM.ActiveCfg = Debug|Any CPU
2729
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|ARM.Build.0 = Debug|Any CPU
28-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x64.ActiveCfg = Debug|x64
29-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x64.Build.0 = Debug|x64
30-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x86.ActiveCfg = Debug|x86
31-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x86.Build.0 = Debug|x86
30+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x64.ActiveCfg = Debug|Any CPU
31+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x64.Build.0 = Debug|Any CPU
32+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x86.ActiveCfg = Debug|Any CPU
33+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Debug|x86.Build.0 = Debug|Any CPU
3234
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
3335
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|Any CPU.Build.0 = Release|Any CPU
3436
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|ARM.ActiveCfg = Release|Any CPU
3537
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|ARM.Build.0 = Release|Any CPU
36-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x64.ActiveCfg = Release|x64
37-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x64.Build.0 = Release|x64
38-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x86.ActiveCfg = Release|x86
39-
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x86.Build.0 = Release|x86
38+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x64.ActiveCfg = Release|Any CPU
39+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x64.Build.0 = Release|Any CPU
40+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x86.ActiveCfg = Release|Any CPU
41+
{B4E7C822-67B1-4331-8E4F-A39E2EB5D1BF}.Release|x86.Build.0 = Release|Any CPU
4042
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4143
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|Any CPU.Build.0 = Debug|Any CPU
4244
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|ARM.ActiveCfg = Debug|Any CPU
4345
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|ARM.Build.0 = Debug|Any CPU
44-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x64.ActiveCfg = Debug|x64
45-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x64.Build.0 = Debug|x64
46-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x86.ActiveCfg = Debug|x86
47-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x86.Build.0 = Debug|x86
46+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x64.ActiveCfg = Debug|Any CPU
47+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x64.Build.0 = Debug|Any CPU
48+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x86.ActiveCfg = Debug|Any CPU
49+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Debug|x86.Build.0 = Debug|Any CPU
4850
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|Any CPU.ActiveCfg = Release|Any CPU
4951
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|Any CPU.Build.0 = Release|Any CPU
5052
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|ARM.ActiveCfg = Release|Any CPU
5153
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|ARM.Build.0 = Release|Any CPU
52-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x64.ActiveCfg = Release|x64
53-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x64.Build.0 = Release|x64
54-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x86.ActiveCfg = Release|x86
55-
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x86.Build.0 = Release|x86
54+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x64.ActiveCfg = Release|Any CPU
55+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x64.Build.0 = Release|Any CPU
56+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x86.ActiveCfg = Release|Any CPU
57+
{8EADF212-BE86-45A1-AD7E-903726A85625}.Release|x86.Build.0 = Release|Any CPU
5658
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Debug|Any CPU.ActiveCfg = Debug|x86
5759
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Debug|ARM.ActiveCfg = Debug|ARM
5860
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Debug|ARM.Build.0 = Debug|ARM
@@ -73,8 +75,27 @@ Global
7375
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Release|x86.ActiveCfg = Release|x86
7476
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Release|x86.Build.0 = Release|x86
7577
{28FDC18C-24CD-470D-97B4-96D69634B4DC}.Release|x86.Deploy.0 = Release|x86
78+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
80+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|ARM.ActiveCfg = Debug|Any CPU
81+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|ARM.Build.0 = Debug|Any CPU
82+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|x64.ActiveCfg = Debug|Any CPU
83+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|x64.Build.0 = Debug|Any CPU
84+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|x86.ActiveCfg = Debug|Any CPU
85+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Debug|x86.Build.0 = Debug|Any CPU
86+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
87+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|ARM.ActiveCfg = Release|Any CPU
89+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|ARM.Build.0 = Release|Any CPU
90+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|x64.ActiveCfg = Release|Any CPU
91+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|x64.Build.0 = Release|Any CPU
92+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|x86.ActiveCfg = Release|Any CPU
93+
{6D4F4682-4039-4BA4-9D9F-80FA0A5045DA}.Release|x86.Build.0 = Release|Any CPU
7694
EndGlobalSection
7795
GlobalSection(SolutionProperties) = preSolution
7896
HideSolutionNode = FALSE
7997
EndGlobalSection
98+
GlobalSection(ExtensibilityGlobals) = postSolution
99+
SolutionGuid = {84CF9285-234B-4452-9EAC-BA183022868B}
100+
EndGlobalSection
80101
EndGlobal

0 commit comments

Comments
 (0)