Skip to content

Commit 32ba625

Browse files
authored
Merge pull request #16 from Qbort/rpi-fixes-improvements
Raspberry pi fixes and other small improvements
2 parents 39d859a + 0f09c23 commit 32ba625

9 files changed

Lines changed: 100 additions & 85 deletions

File tree

UI24RBridgeTest/Program.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
67
using UI24RController;
78
using UI24RController.MIDIController;
89

@@ -26,7 +27,7 @@ static void Main(string[] args)
2627
var controllersSetting = new List<UI24RController.ControllerSettings>();
2728
configuration.GetSection("MidiControllers").Bind(controllersSetting);
2829

29-
30+
3031
var address = configuration["UI24R-Url"];
3132
var midiInputDevice = configuration["MIDI-Input-Name"];
3233
var midiOutputDevice = configuration["MIDI-Output-Name"];
@@ -55,7 +56,7 @@ static void Main(string[] args)
5556
{
5657
controllerSecond = MIDIControllerFactory.GetMidiController(protocol);
5758
}
58-
59+
5960
if (args.Length > 0)
6061
WriteMIDIDeviceNames(controller);
6162
else
@@ -156,7 +157,7 @@ static void Main(string[] args)
156157
Console.WriteLine("Start bridge...");
157158

158159
BridgeSettings settings = new BridgeSettings(address, messageWriter);
159-
160+
160161
if (syncID != null)
161162
{
162163
settings.SyncID = syncID;
@@ -251,27 +252,42 @@ static void Main(string[] args)
251252
}
252253
}
253254
}
254-
255+
255256
}
256257
}
257258
}
259+
private static string PromptDeviceChoice(string promptText, IEnumerable<string> deviceNames)
260+
{
261+
var nameList = deviceNames.ToList();
262+
263+
AnsiConsole.WriteLine(promptText);
264+
for (int i = 0; i < nameList.Count; i++)
265+
AnsiConsole.MarkupLine($" [blue][[{i + 1}]][/]: {nameList[i]}");
266+
267+
int chosen = AnsiConsole.Prompt(
268+
new TextPrompt<int>("Enter number:")
269+
.DefaultValue(1)
270+
.Validate(n => n >= 1 && n <= nameList.Count
271+
? ValidationResult.Success()
272+
: ValidationResult.Error($"Please enter a number between 1 and {nameList.Count}")));
273+
274+
return nameList[chosen - 1];
275+
}
258276

259277
private static void CreateAppsettings(string fileName)
260278
{
261279
var controller = MIDIControllerFactory.GetMidiController("MC");
262280
var inputDevicenames = controller.GetInputDeviceNames();
263281
var outputDevicenames = controller.GetOutputDeviceNames();
264-
282+
265283
AnsiConsole.WriteLine("appsettings.json is not found.");
266284
AnsiConsole.WriteLine("Creating of the configuration file is starting.");
267285
var address = AnsiConsole.Ask<string>(@"Please write the mixer address (eg: ws:\\192.168.3.12): "); //"UI24R-Url"
268-
var midiInputDevice = AnsiConsole.Prompt(
269-
new TextPrompt<string>("Choose primary input device. (It is case sensitive.)")
270-
.AddChoices(inputDevicenames)); //configuration["MIDI-Input-Name"];
286+
var midiInputDevice = PromptDeviceChoice(
287+
"Choose primary input device:", inputDevicenames); //configuration["MIDI-Input-Name"];
271288

272-
var midiOutputDevice = AnsiConsole.Prompt(
273-
new TextPrompt<string>("Choose primary output device. (It is case sensitive.)")
274-
.AddChoices(outputDevicenames)); //configuration["MIDI-Output-Name"];
289+
var midiOutputDevice = PromptDeviceChoice(
290+
"Choose primary output device:", outputDevicenames); //configuration["MIDI-Output-Name"];
275291

276292
//var primaryIsExtender = configuration["PrimaryIsExtender"] == "true";
277293
string primaryIsExtender = "false";
@@ -290,7 +306,7 @@ private static void CreateAppsettings(string fileName)
290306
new TextPrompt<string>("Primary controller offset (show 1-8ch: 0 9-16ch: 1")
291307
.AddChoices(["0", "1"])
292308
.DefaultValue("0"));
293-
309+
294310
var isAddSecondaryDevice = AnsiConsole.Prompt(
295311
new TextPrompt<bool>("Do you want to add secondary device?")
296312
.AddChoice(true)
@@ -304,17 +320,15 @@ private static void CreateAppsettings(string fileName)
304320
string secondaryIsExtender = "false";
305321
string secondaryChannelStart = "1";
306322
//var secondaryMidiInputDevice = configuration["MIDI-Input-Name-Second"];
307-
//var secondaryMidiOutputDevice = configuration["MIDI-Output-Name-Second"];
323+
//var secondaryMidiOutputDevice = configuration["MIDI-Output-Name-Second"];
308324
//var secondaryIsExtender = configuration["SecondaryIsExtender"] == "true";
309325
if (isAddSecondaryDevice)
310326
{
311-
secondaryMidiInputDevice = AnsiConsole.Prompt(
312-
new TextPrompt<string>("Choose secondary input device. (It is case sensitive.)")
313-
.AddChoices(inputDevicenames)); //configuration["MIDI-Input-Name"];
327+
secondaryMidiInputDevice = PromptDeviceChoice(
328+
"Choose secondary input device:", inputDevicenames); //configuration["MIDI-Input-Name"];
314329

315-
secondaryMidiOutputDevice = AnsiConsole.Prompt(
316-
new TextPrompt<string>("Choose secondary output device. (It is case sensitive.)")
317-
.AddChoices(outputDevicenames)); //configuration["MIDI-Output-Name"];
330+
secondaryMidiOutputDevice = PromptDeviceChoice(
331+
"Choose secondary output device:", outputDevicenames); //configuration["MIDI-Output-Name"];
318332

319333
if (AnsiConsole.Prompt(
320334
new TextPrompt<bool>("Is secondary device an extender?")
@@ -370,7 +384,7 @@ private static void CreateAppsettings(string fileName)
370384
";
371385

372386
File.WriteAllText(fileName, settingsContent);
373-
387+
374388

375389
}
376390

UI24RBridgeTest/UI24RBridgeTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<RuntimeIdentifiers>win-x64;win-x86;linux-x64</RuntimeIdentifiers>
77
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
88
</PropertyGroup>

UI24RController/MIDIController/BehringerUniversalMIDI.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ public void Dispose()
8686
}
8787
}
8888

89-
public bool ConnectInputDevice(string deviceName)
89+
public async Task<bool> ConnectInputDevice(string deviceName)
9090
{
9191
var access = MidiAccessManager.Default;
9292
var deviceNumber = access.Inputs.Where(i => i.Name == deviceName).FirstOrDefault();
9393
if (deviceNumber != null)
9494
{
95-
_input = access.OpenInputAsync(deviceNumber.Id).Result;
95+
_input = await access.OpenInputAsync(deviceNumber.Id);
9696
_input.MessageReceived += (obj, e) =>
9797
{
9898
if (e.Data.Length>2)
@@ -104,7 +104,7 @@ public bool ConnectInputDevice(string deviceName)
104104
return true;
105105
}
106106
else
107-
return false;
107+
return false;
108108
}
109109

110110
private void ProcessMidiMessage(MidiReceivedEventArgs e)
@@ -122,13 +122,13 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
122122
}
123123
}
124124

125-
public bool ConnectOutputDevice(string deviceName)
125+
public async Task<bool> ConnectOutputDevice(string deviceName)
126126
{
127127
var access = MidiAccessManager.Default;
128128
var deviceNumber = access.Outputs.Where(i => i.Name == deviceName).FirstOrDefault();
129129
if (deviceNumber != null)
130130
{
131-
_output = access.OpenOutputAsync(deviceNumber.Id).Result;
131+
_output = await access.OpenOutputAsync(deviceNumber.Id);
132132
return true;
133133
}
134134
return false;
@@ -207,7 +207,7 @@ public void WriteTextToLCD(string text, int delay)
207207
throw new NotImplementedException();
208208
}
209209

210-
public bool ReConnectDevice()
210+
public async Task<bool> ReConnectDevice()
211211
{
212212
throw new NotImplementedException();
213213
}

UI24RController/MIDIController/IMIDIController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
using System.Collections.Generic;
33
using UI24RController;
44
using UI24RController.MIDIController;
5+
using System.Threading.Tasks;
56

67
public interface IMIDIController
78
{
89
#region Connection
910
string[] GetInputDeviceNames();
1011
string[] GetOutputDeviceNames();
11-
bool ConnectInputDevice(string deviceName);
12-
bool ConnectOutputDevice(string deviceName);
12+
Task<bool> ConnectInputDevice(string deviceName);
13+
Task<bool> ConnectOutputDevice(string deviceName);
1314

14-
bool ReConnectDevice();
15+
Task<bool> ReConnectDevice();
1516

1617
bool IsConnectionErrorOccured { get; }
1718
bool IsConnected { get; }

UI24RController/MIDIController/MC.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Commons.Music.Midi;
55
using System.Linq;
66
using System.Threading.Tasks;
7-
using Commons.Music.Midi.RtMidi;
87
using System.Threading;
98
using System.Collections.Concurrent;
109
using System.Text.RegularExpressions;
@@ -38,7 +37,7 @@ public FaderState(double value)
3837
/// Store every fader setted value of the faders, key is the channel number (z in the message)
3938
/// </summary>
4039
protected ConcurrentDictionary<byte, FaderState> faderValues = new ConcurrentDictionary<byte, FaderState>();
41-
40+
4241
IMidiInput _input = null;
4342
protected string _inputDeviceNumber;
4443
protected string _inputDeviceName;
@@ -421,10 +420,10 @@ public void Dispose()
421420
_output.Dispose();
422421
_output = null;
423422
}
424-
423+
425424
}
426425

427-
public bool ConnectInputDevice(string deviceName)
426+
public async Task<bool> ConnectInputDevice(string deviceName)
428427
{
429428
try
430429
{
@@ -433,8 +432,8 @@ public bool ConnectInputDevice(string deviceName)
433432
var deviceNumber = access.Inputs.Where(i => i.Name.ToUpper() == deviceName.ToUpper()).FirstOrDefault();
434433
if (deviceNumber != null)
435434
{
436-
437-
var input = access.OpenInputAsync(deviceNumber.Id).Result;
435+
var input = await access.OpenInputAsync(deviceNumber.Id);
436+
438437
_input = input;
439438
_inputDeviceNumber = deviceNumber.Id;
440439
_input.MessageReceived += (obj, e) =>
@@ -459,7 +458,7 @@ public bool ConnectInputDevice(string deviceName)
459458
}
460459
return false;
461460
}
462-
public bool ConnectOutputDevice(string deviceName)
461+
public async Task<bool> ConnectOutputDevice(string deviceName)
463462
{
464463
try
465464
{
@@ -468,7 +467,7 @@ public bool ConnectOutputDevice(string deviceName)
468467
var deviceNumber = access.Outputs.Where(i => i.Name.ToUpper() == deviceName.ToUpper()).FirstOrDefault();
469468
if (deviceNumber != null)
470469
{
471-
var output = access.OpenOutputAsync(deviceNumber.Id).Result;
470+
var output = await access.OpenOutputAsync(deviceNumber.Id);
472471
_outputDeviceNumber = deviceNumber.Id;
473472
_output = output;
474473
_isConnected = true;
@@ -504,10 +503,11 @@ private void StartPingThread()
504503
_pingThread.Start();
505504

506505
}
507-
public bool ReConnectDevice()
506+
public async Task<bool> ReConnectDevice()
508507
{
509-
return ConnectInputDevice(_inputDeviceName) &&
510-
ConnectOutputDevice(_outputDeviceName);
508+
var inputOk = await ConnectInputDevice(_inputDeviceName);
509+
var outputOk = await ConnectOutputDevice(_outputDeviceName);
510+
return inputOk && outputOk;
511511
}
512512

513513
public string[] GetInputDeviceNames()
@@ -531,7 +531,7 @@ protected void Send(byte[] mevent, int offset, int length, long timestamp)
531531

532532
}
533533
}
534-
catch
534+
catch
535535
{
536536
OnConnectionErrorEvent();
537537
}
@@ -540,7 +540,7 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
540540
{
541541
var message = e.Data;
542542

543-
if (message[0] == 0x90) //button pressed, released, fader released
543+
if (message[0] == 0x90) //button pressed, released, fader released
544544
{
545545
if (message.MIDIEqual(0x90, 0x00, 0x00, 0xff, 0x00, 0xff) && (message[1] >= 0x68) && (message[1] <= 0x70)) //release fader (0x90 [0x68-0x70] 0x00)
546546
{
@@ -795,7 +795,7 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
795795
int lower = message[1]; // lower 7 bit
796796

797797
var faderValue = (upper + lower) / 16383.0;
798-
798+
799799
faderValues[channelNumber].Value = faderValue;
800800
OnFaderEvent(channelNumber, faderValue);
801801
if (!faderValues[channelNumber].IsTouched)

0 commit comments

Comments
 (0)