Skip to content

Commit

Permalink
Device control and constants
Browse files Browse the repository at this point in the history
Updated the subdivisions control system.
Updated the device control system.
Updated constants variables.
  • Loading branch information
DavidMolTor committed Sep 20, 2024
1 parent 5cacb50 commit 7b75a23
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 28 deletions.
20 changes: 10 additions & 10 deletions build/MidiControl/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class Constants

//Footswitch parameters
public const int FOOTSWITCH_HOLD_TIME = 2000;
public const int FOOTSWITCH_BLINK_PERIOD = 200;
public const int FOOTSWITCH_BLINK_PERIOD = 500;
public const int FOOTSWITCH_BLINK_COUNT = 4;

/*
Expand Down Expand Up @@ -117,15 +117,15 @@ Note subdivisions models dictionary
*/
public static readonly Dictionary<TimeSubdivisions, int> DICT_SUBDIVISIONS = new Dictionary<TimeSubdivisions, int>()
{
{ TimeSubdivisions.EighthTriplet, 425 },
{ TimeSubdivisions.EighthFull, 500 },
{ TimeSubdivisions.EighthDotted, 750 },
{ TimeSubdivisions.QuarterTriplet, 875 },
{ TimeSubdivisions.QuarterFull, 1000 },
{ TimeSubdivisions.QuarterDotted, 1500 },
{ TimeSubdivisions.HalfTripplet, 1750 },
{ TimeSubdivisions.HalfFull, 2000 },
{ TimeSubdivisions.HalfDotted, 3000 }
{ TimeSubdivisions.EighthTriplet, 12 },
{ TimeSubdivisions.EighthFull, 25 },
{ TimeSubdivisions.EighthDotted, 38 },
{ TimeSubdivisions.QuarterTriplet, 50 },
{ TimeSubdivisions.QuarterFull, 64 },
{ TimeSubdivisions.QuarterDotted, 75 },
{ TimeSubdivisions.HalfTripplet, 89 },
{ TimeSubdivisions.HalfFull, 102 },
{ TimeSubdivisions.HalfDotted, 116 }
};
}
}
84 changes: 66 additions & 18 deletions build/MidiControl/DeviceControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MIDI CONTROL 2023
using System;
using System.Linq;
using System.Timers;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Collections.Generic;
Expand Down Expand Up @@ -52,9 +53,9 @@ public DeviceControl()
SetDevice(IControlConfig.Instance.GetPreset(IControlConfig.Instance.GetSelectedPreset()));

//Initialize the device update timer
Timer timerUpdateDevice = new Timer(Constants.DEVICE_UPDATE_PERIOD);
timerUpdateDevice.Elapsed += TimerUpdateDevice_Elapsed;
timerUpdateDevice.Enabled = true;
System.Timers.Timer timerUpdateDevice = new System.Timers.Timer(Constants.DEVICE_UPDATE_PERIOD);
timerUpdateDevice.Elapsed += TimerUpdateDevice_Elapsed;
timerUpdateDevice.Enabled = true;

//Initialize the tempo blink task
Task.Run(TempoBlinkTask);
Expand All @@ -67,6 +68,11 @@ public DeviceControl()
//Current note subdivision variable
int iCurrentSubdivision = 0;

//Tap footswitch control objects
bool bTapMode = false;
object lockTapMode = new object();
AutoResetEvent eventTapMode = new AutoResetEvent(false);

/*
Sets the given configuration
*/
Expand Down Expand Up @@ -216,13 +222,22 @@ private void TimerUpdateDevice_Elapsed(object source, ElapsedEventArgs e)
}
}

//Send the delay notes command if able
if (configCurrent.iDelayNotes != configPrevious.iDelayNotes && configCurrent.iDelayTime == configPrevious.iDelayTime)
IControlMIDI.Instance.AddCommand(ChannelCommand.Controller, iChannel, (int)SettingsCC.DelayNotes, configCurrent.iDelayNotes);
lock (lockTapMode)
{
//Check the tap mode status
if (bTapMode)
{
bTapMode = Constants.DICT_SUBDIVISIONS.Values.Contains(configCurrent.iDelayTime);
}

//Send the delay time command if able
if (configCurrent.iDelayTime != configPrevious.iDelayTime)
IControlMIDI.Instance.AddCommand(ChannelCommand.Controller, iChannel, (int)SettingsCC.DelayTime, configCurrent.iDelayTime);
//Send the delay notes command if able
if (configCurrent.iDelayNotes != configPrevious.iDelayNotes && bTapMode)
IControlMIDI.Instance.AddCommand(ChannelCommand.Controller, iChannel, (int)SettingsCC.DelayNotes, configCurrent.iDelayNotes);

//Send the delay time command if able
if (configCurrent.iDelayTime != configPrevious.iDelayTime && !bTapMode)
IControlMIDI.Instance.AddCommand(ChannelCommand.Controller, iChannel, (int)SettingsCC.DelayTime, configCurrent.iDelayTime);
}

//Send the delay repeats command if able
if (configCurrent.iDelayRepeats != configPrevious.iDelayRepeats)
Expand Down Expand Up @@ -309,13 +324,26 @@ private void TempoBlinkTask()
{
try
{
//Reset the footswitch blink status
Dispatcher.Invoke(new Action(() => footswitch_TAP.SetStatus(FootswitchStatus.Off)));
System.Threading.Thread.Sleep(Constants.DICT_SUBDIVISIONS[(TimeSubdivisions)iCurrentSubdivision]);
//Get the tap mode value
bool bValue;
lock (lockTapMode)
{
bValue = bTapMode;
}

//Check the tap mode value
if (!bValue)
{
eventTapMode.WaitOne();
}

//Set the footswitch blink status
Dispatcher.Invoke(new Action(() => footswitch_TAP.SetStatus(FootswitchStatus.Red)));
System.Threading.Thread.Sleep(Constants.FOOTSWITCH_BLINK_PERIOD);
Thread.Sleep(Constants.FOOTSWITCH_BLINK_PERIOD / 2);

//Reset the footswitch blink status
Dispatcher.Invoke(new Action(() => footswitch_TAP.SetStatus(FootswitchStatus.Off)));
Thread.Sleep(Constants.FOOTSWITCH_BLINK_PERIOD);
}
catch (KeyNotFoundException)
{
Expand Down Expand Up @@ -414,12 +442,31 @@ private void HandleFootswitchPressed(object sender, EventArgs e)
}
break;
case "TAP":
//Get the next note subdivision setting
List<int> listSubdivisions = Enum.GetValues(typeof(TimeSubdivisions)).Cast<int>().ToList();
int iDelayNotes = listSubdivisions.IndexOf(configDevice.iDelayNotes) + 1;
lock (lockTapMode)
{
//Get the next note subdivision setting
List<int> listSubdivisions = Enum.GetValues(typeof(TimeSubdivisions)).Cast<int>().ToList();
int iDelayNotes = listSubdivisions.IndexOf(configDevice.iDelayNotes) + (bTapMode ? 1 : 0);

//Set the note subdivision variable
iCurrentSubdivision = iDelayNotes < listSubdivisions.Count ? iDelayNotes : 0;

//Set the note subdivision variable
iCurrentSubdivision = iDelayNotes < listSubdivisions.Count ? iDelayNotes : 0;
//Set the subdivisions knob
knobDelayTime.SetKnob(Constants.DICT_SUBDIVISIONS[(TimeSubdivisions)iCurrentSubdivision], Enumerable.Range(0, Constants.MAX_KNOB_VALUES).ToList());

//Check the tap mode value
if (!bTapMode)
{
//Send the delay notes command
IControlMIDI.Instance.AddCommand(ChannelCommand.Controller, iChannel, (int)SettingsCC.DelayNotes, iCurrentSubdivision);

//Set the tap mode value
bTapMode = true;

//Set the tap mode event
eventTapMode.Set();
}
}
break;
case "SET":
//Save the current preset
Expand All @@ -434,6 +481,7 @@ Footswitch hold handler function
*/
private void HandleFootswitchHold(object sender, EventArgs e)
{
//Save the current preset
SaveCurrentPreset();
}

Expand Down

0 comments on commit 7b75a23

Please sign in to comment.