802 lines
35 KiB
C#
802 lines
35 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using System;
|
|
using static AgroBase.Models.Enums;
|
|
using AgroBase.Forms;
|
|
using AgroBase.Models;
|
|
using System.Threading.Tasks;
|
|
using AgroBase.Models.Modules;
|
|
using SharpDX.DirectInput;
|
|
using AgroBase.Services;
|
|
using AgroBase.Forms.IHM;
|
|
using System.Data;
|
|
|
|
namespace AgroBase
|
|
{
|
|
class GeneralJoystick
|
|
{
|
|
public static Joystick JoystickConectado = null;
|
|
public static int MargemAnalogico = 100;
|
|
public static int MaximoAnalogico = 65534;
|
|
|
|
public static List<ComandosDispositivo> Comandos = new List<ComandosDispositivo>() { };
|
|
public static List<DeParaComandos> DeParaComandos = new List<DeParaComandos>()
|
|
{
|
|
new DeParaComandos() { key = Keys.Left, Dispositivo = T_Code.Dir, botaoJoy = BotoesJoystick.SEsquerda, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Left, Dispositivo = T_Code.Dir, botaoJoy = BotoesJoystick.AEEsquerda, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Right, Dispositivo = T_Code.Dir, botaoJoy = BotoesJoystick.SDireita, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Right, Dispositivo = T_Code.Dir, botaoJoy = BotoesJoystick.AEDireita, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.R, Dispositivo = T_Code.Dir, botaoJoy = BotoesJoystick.R3, PararAoSoltar = false },
|
|
new DeParaComandos() { key = Keys.Up, Dispositivo = T_Code.Mov, botaoJoy = BotoesJoystick.A, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Down, Dispositivo = T_Code.Mov, botaoJoy = BotoesJoystick.X, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Space, Dispositivo = T_Code.Mov, botaoJoy = BotoesJoystick.B, PararAoSoltar = true },
|
|
new DeParaComandos() { key = Keys.Subtract, Dispositivo = T_Code.Mov, botaoJoy = BotoesJoystick.L1, PararAoSoltar = false },
|
|
new DeParaComandos() { key = Keys.Add, Dispositivo = T_Code.Mov, botaoJoy = BotoesJoystick.R1, PararAoSoltar = false },
|
|
new DeParaComandos() { key = Keys.D1, Dispositivo = T_Code.Atu, botaoJoy = BotoesJoystick.L2, PararAoSoltar = true, IDcontrolar = "B01" },
|
|
new DeParaComandos() { key = Keys.D2, Dispositivo = T_Code.Atu, botaoJoy = BotoesJoystick.L2, PararAoSoltar = true, IDcontrolar = "B02" },
|
|
new DeParaComandos() { key = Keys.D3, Dispositivo = T_Code.Atu, botaoJoy = BotoesJoystick.R2, PararAoSoltar = true, IDcontrolar = "B03" },
|
|
new DeParaComandos() { key = Keys.D4, Dispositivo = T_Code.Atu, botaoJoy = BotoesJoystick.R2, PararAoSoltar = true, IDcontrolar = "B04" },
|
|
};
|
|
public static List<BotoesJoystick> BotoesJoyPressionados = new List<BotoesJoystick>() { };
|
|
public static List<TipoMovimentoDirecional> MovimentosCombinados = new List<TipoMovimentoDirecional>()
|
|
{
|
|
TipoMovimentoDirecional.MovimentoLateral,
|
|
TipoMovimentoDirecional.RotacionarNoEixo
|
|
};
|
|
|
|
public static Direcao UltimaDirecao = Direcao.Parado;
|
|
public static bool Pressionado = false;
|
|
public static List<IDispositivosService> DispCon;
|
|
|
|
//public static int RPM { get; set; } = 60;
|
|
//public static double Angulo { get; set; } = 20;
|
|
//public static int Velocidade { get; set; } = 50;
|
|
|
|
public static AsyncTaskTimerModel tmrJoystick;
|
|
|
|
public static void IniciarRotinas()
|
|
{
|
|
tmrJoystick = new AsyncTaskTimerModel("tmrJoystick", tmrJoystick_Tick, 100);
|
|
}
|
|
|
|
public static void AtualizaDispositivo()
|
|
{
|
|
Func<bool> funcAtt = new Func<bool>(() =>
|
|
{
|
|
DispCon = Variaveis.DispositivosConectados.Where(x => x.Dados.GetStatusConexao()).ToList();
|
|
frmInstancial.frmJoystick.lblDispositivo.Text = "Dispositivos: " + (DispCon.Count == 0 ? "N/A" : string.Join(", ", DispCon.Select(x => x._Descricao + " (" + Enum.GetName(typeof(T_Code), x.Dispositivo) + ")").ToArray()));
|
|
frmInstancial.frmPrincipal.cmbJoysticks.Items.Clear();
|
|
|
|
InicializarJoysticksConectados();
|
|
|
|
return true;
|
|
});
|
|
FuncoesGlobais.ExecutarMetodoComVerificacaoCrossThread(frmInstancial.frmPrincipal.cmbJoysticks, funcAtt);
|
|
}
|
|
|
|
public static void InicializarJoysticksConectados()
|
|
{
|
|
var directInput = new DirectInput();
|
|
|
|
// Find a Joystick Guid
|
|
var joystickGuid = Guid.Empty;
|
|
|
|
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
|
|
{
|
|
joystickGuid = deviceInstance.InstanceGuid;
|
|
frmInstancial.frmPrincipal.cmbJoysticks.Items.Add(deviceInstance.InstanceName);
|
|
}
|
|
|
|
// If Gamepad not found, look for a Joystick
|
|
if (joystickGuid == Guid.Empty)
|
|
{
|
|
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
|
|
{
|
|
joystickGuid = deviceInstance.InstanceGuid;
|
|
frmInstancial.frmPrincipal.cmbJoysticks.Items.Add(deviceInstance.InstanceName);
|
|
}
|
|
}
|
|
|
|
// If Joystick not found, throws an error
|
|
if (joystickGuid == Guid.Empty)
|
|
{
|
|
JoystickConectado = null;
|
|
tmrJoystick.Stop();
|
|
|
|
return;
|
|
}
|
|
|
|
// Instantiate the joystick
|
|
JoystickConectado = new Joystick(directInput, joystickGuid);
|
|
|
|
// Query all suported ForceFeedback effects
|
|
var allEffects = JoystickConectado.GetEffects();
|
|
foreach (var effectInfo in allEffects)
|
|
{
|
|
Console.WriteLine("Effect available {0}", effectInfo.Name);
|
|
}
|
|
|
|
// Set BufferSize in order to use buffered data.
|
|
JoystickConectado.Properties.BufferSize = 128;
|
|
|
|
// Acquire the joystick
|
|
JoystickConectado.Acquire();
|
|
|
|
|
|
if (JoystickConectado != null)
|
|
{
|
|
tmrJoystick.Start();
|
|
frmInstancial.frmPrincipal.cmbJoysticks.SelectedIndex = 0;
|
|
}
|
|
|
|
}
|
|
|
|
private static bool ComparaPosicaoAnalogico(int Atual, ComparativoAnalogico Comparativo)
|
|
{
|
|
int Meio = MaximoAnalogico / 2;
|
|
bool Pressionado = false;
|
|
|
|
if (Comparativo == ComparativoAnalogico.Igual)
|
|
{
|
|
Pressionado = Atual >= (Meio - MargemAnalogico) && Atual <= (Meio + MargemAnalogico);
|
|
}
|
|
else if (Comparativo == ComparativoAnalogico.Maior)
|
|
{
|
|
Pressionado = Atual >= (Meio + MargemAnalogico);
|
|
}
|
|
else if (Comparativo == ComparativoAnalogico.Menor)
|
|
{
|
|
Pressionado = Atual <= (Meio - MargemAnalogico);
|
|
}
|
|
|
|
return Pressionado;
|
|
}
|
|
|
|
private static double DeParaAnanlogico(double Valor, double DeMin, double DeMax, double ParaMin, double ParaMax)
|
|
{
|
|
if (Valor < DeMin)
|
|
{
|
|
Valor = DeMin;
|
|
}
|
|
else if (Valor > DeMax)
|
|
{
|
|
Valor = DeMax;
|
|
}
|
|
|
|
double scale = (Valor - DeMin) / (DeMax - DeMin);
|
|
double mappedValue = scale * (ParaMax - ParaMin) + ParaMin;
|
|
|
|
return mappedValue;
|
|
}
|
|
|
|
private static async Task tmrJoystick_Tick()
|
|
{
|
|
if (JoystickConectado == null)
|
|
{
|
|
return;
|
|
}
|
|
JoystickUpdate[] datas;
|
|
try
|
|
{
|
|
JoystickConectado.Poll();
|
|
datas = JoystickConectado.GetBufferedData();
|
|
}
|
|
catch
|
|
{
|
|
AtualizaDispositivo();
|
|
return;
|
|
}
|
|
|
|
|
|
List<BotoesPressionadosValores> _botoesPressionados = new List<BotoesPressionadosValores>();
|
|
List<BotoesJoystick> _botoesSoltos = new List<BotoesJoystick>();
|
|
|
|
foreach (var state in datas)
|
|
{
|
|
Console.WriteLine(state);
|
|
if (state.Offset == JoystickOffset.PointOfViewControllers0)
|
|
{
|
|
if (state.Value == -1)
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.SBaixo))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.SBaixo);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.SCima))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.SCima);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.SEsquerda))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.SEsquerda);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.SDireita))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.SDireita);
|
|
}
|
|
}
|
|
else if (state.Value == 0)
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.SCima, valor = state.Value });
|
|
}
|
|
else if (state.Value == 9000)
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.SDireita, valor = state.Value });
|
|
}
|
|
else if (state.Value == 18000)
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.SBaixo, valor = state.Value });
|
|
}
|
|
else if (state.Value == 27000)
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.SEsquerda, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Z)
|
|
{
|
|
if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Igual))
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.L2))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.L2);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.R2))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.R2);
|
|
}
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Maior))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.L2, valor = state.Value });
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Menor))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.R2, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.X)
|
|
{
|
|
if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Igual))
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.AEEsquerda))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.AEEsquerda);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.AEDireita))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.AEDireita);
|
|
}
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Maior))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.AEDireita, valor = state.Value });
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Menor))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.AEEsquerda, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Y)
|
|
{
|
|
if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Igual))
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.AECima))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.AECima);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.AEBaixo))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.AEBaixo);
|
|
}
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Maior))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.AEBaixo, valor = state.Value });
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Menor))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.AECima, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.RotationX)
|
|
{
|
|
if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Igual))
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.ADEsquerda))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.ADEsquerda);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.ADDireita))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.ADDireita);
|
|
}
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Maior))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.ADDireita, valor = state.Value });
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Menor))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.ADEsquerda, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.RotationY)
|
|
{
|
|
if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Igual))
|
|
{
|
|
if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.ADCima))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.ADCima);
|
|
}
|
|
else if (BotoesJoyPressionados.Any(x => x == BotoesJoystick.ADBaixo))
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.ADBaixo);
|
|
}
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Maior))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.ADCima, valor = state.Value });
|
|
}
|
|
else if (ComparaPosicaoAnalogico(state.Value, ComparativoAnalogico.Menor))
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.ADBaixo, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons0)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.A);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.A, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons1)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.B);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.B, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons2)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.X);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.X, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons3)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.Y);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.Y, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons4)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.L1);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.L1, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons5)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.R1);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.R1, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons6)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.Select);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.Select, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons7)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.Start);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.Start, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons8)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.L3);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.L3, valor = state.Value });
|
|
}
|
|
}
|
|
else if (state.Offset == JoystickOffset.Buttons9)
|
|
{
|
|
if (state.Value == 0)
|
|
{
|
|
_botoesSoltos.Add(BotoesJoystick.R3);
|
|
}
|
|
else
|
|
{
|
|
_botoesPressionados.Add(new BotoesPressionadosValores() { botao = BotoesJoystick.R3, valor = state.Value });
|
|
}
|
|
}
|
|
}
|
|
|
|
_botoesPressionados.ForEach(botao =>
|
|
{
|
|
BotoesJoyPressionados.Add(botao.botao);
|
|
if (DeParaComandos.Any(x => x.botaoJoy == botao.botao))
|
|
{
|
|
EnviaComandoMotor(DeParaComandos.FirstOrDefault(x => x.botaoJoy == botao.botao).key);
|
|
}
|
|
});
|
|
_botoesSoltos.ForEach(botao =>
|
|
{
|
|
BotoesJoyPressionados.Remove(botao);
|
|
if (DeParaComandos.Any(x => x.botaoJoy == botao && x.PararAoSoltar))
|
|
{
|
|
EnviaComandoMotor(Keys.Escape, DeParaComandos.FirstOrDefault(x => x.botaoJoy == botao).Dispositivo);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void EnviaComandoMotor(Keys Tecla, T_Code Disp = T_Code.Vzo, bool ForcarComando = false)
|
|
{
|
|
// Verifica se deve interromper o fluxo
|
|
bool deveInterromper =
|
|
!frmDiagnosticos.Diagnosticando && !ForcarComando &&
|
|
(
|
|
MKS057DService.Referenciando ||
|
|
!Variaveis.OperacaoEmAndamento.OperacaoLiberada ||
|
|
!Variaveis.OperacaoEmAndamento.Iniciado ||
|
|
Variaveis.OperacaoEmAndamento.Modo != ModoOperacao.Manual ||
|
|
!VerificaComandoValidoSonar(Tecla)
|
|
);
|
|
|
|
if (deveInterromper)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ComandosDispositivo Comando = new ComandosDispositivo()
|
|
{
|
|
Momento = DateTime.Now,
|
|
Comando = Tecla,
|
|
IDcontrolar = DeParaComandos.FirstOrDefault(x => x.key == Tecla)?.IDcontrolar ?? "",
|
|
};
|
|
|
|
Direcao DirecaoAtual = Direcao.Parado;
|
|
|
|
// Cancelar Comando Anterior
|
|
if (Comando.Comando == Keys.Escape)
|
|
{
|
|
var ComandosDisp = Disp == T_Code.Vzo ? Comandos : Comandos.Where(x => x.Dispositivo == Disp).ToList();
|
|
if (ComandosDisp.Any())
|
|
{
|
|
Comando.Dispositivo = ComandosDisp[ComandosDisp.Count - 1].Dispositivo;
|
|
Comando.IDcontrolar = ComandosDisp[ComandosDisp.Count - 1].IDcontrolar;
|
|
DirecaoAtual = Direcao.Parado;
|
|
Pressionado = true;
|
|
}
|
|
}
|
|
// Controle de Velocidade (MOV RPM SP)
|
|
else if (Comando.Comando == Keys.Add)
|
|
{
|
|
Comando.Dispositivo = T_Code.Mov;
|
|
DirecaoAtual = MovimentacaoUnificadaModel.UltimaDirecao;
|
|
Variaveis.OperacaoEmAndamento.Controle.PercentualVelocidadeSP++;
|
|
Pressionado = true;
|
|
}
|
|
else if (Comando.Comando == Keys.Subtract)
|
|
{
|
|
Comando.Dispositivo = T_Code.Mov;
|
|
DirecaoAtual = MovimentacaoUnificadaModel.UltimaDirecao;
|
|
Variaveis.OperacaoEmAndamento.Controle.PercentualVelocidadeSP--;
|
|
Pressionado = true;
|
|
}
|
|
// Controle Direcao dos Motores BLDC (MOV)
|
|
else if (Comando.Comando == Keys.Up && UltimaDirecao != Direcao.Frente)
|
|
{
|
|
Comando.Dispositivo = T_Code.Mov;
|
|
DirecaoAtual = Direcao.Frente;
|
|
Pressionado = true;
|
|
}
|
|
else if (Comando.Comando == Keys.Down && UltimaDirecao != Direcao.Tras)
|
|
{
|
|
Comando.Dispositivo = T_Code.Mov;
|
|
DirecaoAtual = Direcao.Tras;
|
|
Pressionado = true;
|
|
}
|
|
else if (Comando.Comando == Keys.Space && UltimaDirecao != Direcao.EmFreio)
|
|
{
|
|
Comando.Dispositivo = T_Code.Mov;
|
|
DirecaoAtual = Direcao.EmFreio;
|
|
Pressionado = true;
|
|
}
|
|
// Controle de Rotação dos Motores de Passo (DIR)
|
|
else if (Comando.Comando == Keys.Left && UltimaDirecao != Direcao.Esquerda)
|
|
{
|
|
Comando.Dispositivo = T_Code.Dir;
|
|
DirecaoAtual = Direcao.Esquerda;
|
|
Pressionado = true;
|
|
}
|
|
else if (Comando.Comando == Keys.Right && UltimaDirecao != Direcao.Direita)
|
|
{
|
|
Comando.Dispositivo = T_Code.Dir;
|
|
DirecaoAtual = Direcao.Direita;
|
|
Pressionado = true;
|
|
}
|
|
else if (Comando.Comando == Keys.R)
|
|
{
|
|
Comando.Dispositivo = T_Code.Dir;
|
|
DirecaoAtual = Direcao.Referenciar;
|
|
Pressionado = true;
|
|
}
|
|
// Controle dos Bicos Pulverizadores (ATU)
|
|
else if ((Comando.Comando == Keys.D1 || Comando.Comando == Keys.D2 || Comando.Comando == Keys.D3 || Comando.Comando == Keys.D4) && Variaveis.OperacaoEmAndamento.DispAtu.Dados.BicosPulverizadores.Any(x => x.ID == Comando.IDcontrolar && !x.ComandoAtuar))
|
|
{
|
|
Comando.Dispositivo = T_Code.Atu;
|
|
Pressionado = true;
|
|
}
|
|
// Soltar o Botão
|
|
else
|
|
{
|
|
Pressionado = false;
|
|
}
|
|
|
|
if (Pressionado)
|
|
{
|
|
Comandos.Add(Comando);
|
|
|
|
/*for (int i = 0; i < 1; i++)
|
|
{
|
|
var Dispositivo = DispCon.FirstOrDefault(x => x.Dispositivo == Comando.Dispositivo);
|
|
if (Dispositivo != null)
|
|
{
|
|
var Protocolos =
|
|
Dispositivo.Dados is MovimentacaoModel ? ((MovimentacaoModel)Dispositivo.Dados).Motores.Select(x => x.ProtocoloComando(DirecaoAtual)) :
|
|
Dispositivo.Dados is DirecionalModel ? ((DirecionalModel)Dispositivo.Dados).Motores.Select(x => x.ProtocoloComando(DirecaoAtual)) :
|
|
null;
|
|
foreach (var ProtocoloMotor in Protocolos)
|
|
{
|
|
Dispositivo.EnviarDadosSerial(ProtocoloMotor);
|
|
Task.Delay(10);
|
|
}
|
|
}
|
|
|
|
Task.Delay(100);
|
|
}*/
|
|
|
|
for (int i = 0; i < 1; i++)
|
|
{
|
|
switch (Comando.Dispositivo)
|
|
{
|
|
case T_Code.Mov:
|
|
{
|
|
Variaveis.OperacaoEmAndamento.DispMvd.Dados.Modulos.ForEach(x => x.MovMotor.EnviarComandoControle(DirecaoAtual));
|
|
break;
|
|
}
|
|
case T_Code.Dir:
|
|
{
|
|
if (MovimentosCombinados.Contains(Variaveis.OperacaoEmAndamento.Controle.TipoMovimento))
|
|
{
|
|
Task.Run(async () => await Variaveis.OperacaoEmAndamento.DispMvd.Dados.ExecutarMovimentoDirecionalCombinado(DirecaoAtual));
|
|
}
|
|
else if (DirecaoAtual == Direcao.Referenciar)
|
|
{
|
|
Task.Run(async () => await Variaveis.OperacaoEmAndamento.DispMvd.Dados.RealizarReferenciamentoDirecional());
|
|
}
|
|
else
|
|
{
|
|
Variaveis.OperacaoEmAndamento.DispMvd.Dados.Modulos.ForEach(x => x.DirMotor.EnviarComandoControle(DirecaoAtual));
|
|
/*Task.Run(async () =>
|
|
{
|
|
foreach (var mod in Variaveis.OperacaoEmAndamento.DispMvd.Dados.Modulos)
|
|
{
|
|
mod.DirMotor.EnviarComandoControle(DirecaoAtual);
|
|
};
|
|
});*/
|
|
}
|
|
break;
|
|
}
|
|
case T_Code.Atu:
|
|
{
|
|
EnviarComandoAtuador(S_Code.sBIC, Comando.Comando == Keys.Escape ? false : true, Comando.IDcontrolar);
|
|
break;
|
|
}
|
|
}
|
|
|
|
var Ctrl = Variaveis.OperacaoEmAndamento.Controle.TiposControle.FirstOrDefault(x => x.Tipo == Comando.Dispositivo);
|
|
if (Ctrl != null)
|
|
{
|
|
Ctrl.UltimaDirecao = DirecaoAtual;
|
|
}
|
|
}
|
|
|
|
UltimaDirecao = DirecaoAtual;
|
|
|
|
AtualizaConsole(Enum.GetName(typeof(Direcao), UltimaDirecao));
|
|
}
|
|
}
|
|
|
|
public static void EnviarComandoAtuador(S_Code Componente, bool Status, string ID)
|
|
{
|
|
if (!Variaveis.OperacaoEmAndamento.Iniciado && !frmDiagnosticos.Diagnosticando)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var DispAtu = Variaveis.OperacaoEmAndamento.DispAtu;
|
|
|
|
switch (Componente)
|
|
{
|
|
case S_Code.sBMB:
|
|
var Bomba = DispAtu?.Dados?.BombasPressurizadoras?.FirstOrDefault(x => x.ID == ID);
|
|
if (Bomba != null)
|
|
{
|
|
Bomba.ComandoAtuar = Status;
|
|
string ProtocoloBomba = Bomba.ProtocoloComando();
|
|
DispAtu.AdicionarMensagemFila(DispAtu.Dados.Modulo_ID, ProtocoloBomba, true, true);
|
|
}
|
|
break;
|
|
case S_Code.sBIC:
|
|
var Bico = DispAtu.Dados.BicosPulverizadores.FirstOrDefault(x => x.ID == ID);
|
|
if (Bico != null)
|
|
{
|
|
Bico.ComandoAtuar = Status;
|
|
string ProtocoloBico = Bico.ProtocoloComando();
|
|
DispAtu.AdicionarMensagemFila(DispAtu.Dados.Modulo_ID, ProtocoloBico, true, false);
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
public static void EnviarComandoSensoriamento(S_Code Componente, string ID, dynamic Status)
|
|
{
|
|
var DispSen = Variaveis.OperacaoEmAndamento.DispSen;
|
|
|
|
switch (Componente)
|
|
{
|
|
case S_Code.sLED:
|
|
var Sinaleiro = DispSen?.Dados?.Sinaleiros?.FirstOrDefault(x => x.ID == ID && x.Inicializado);
|
|
if (Sinaleiro != null)
|
|
{
|
|
Sinaleiro.Comportamento = (SensorSinaleiroComportamentoModel)Status;
|
|
string ProtocoloSinaleiro = Sinaleiro.ProtocoloComando();
|
|
DispSen.AdicionarMensagemFila(DispSen.Dados.Modulo_ID, ProtocoloSinaleiro, true, false);
|
|
}
|
|
break;
|
|
case S_Code.sRLE:
|
|
var Rele = DispSen?.Dados?.Reles?.FirstOrDefault(x => x.ID == ID && x.Inicializado);
|
|
if (Rele != null)
|
|
{
|
|
Rele.StatusControle = (Estado)Status;
|
|
string ProtocoloRele = Rele.ProtocoloComando();
|
|
DispSen.AdicionarMensagemFila(DispSen.Dados.Modulo_ID, ProtocoloRele, true, false);
|
|
}
|
|
break;
|
|
case S_Code.sFRO:
|
|
var ServoFreio = DispSen?.Dados?.Servos?.FirstOrDefault(x => x.ID == ID && x.Inicializado);
|
|
if (ServoFreio != null)
|
|
{
|
|
ServoFreio._AnguloControle = (int)Status;
|
|
string ProtocoloFreio = ServoFreio.ProtocoloComando();
|
|
DispSen.AdicionarMensagemFila(DispSen.Dados.Modulo_ID, ProtocoloFreio, true, false);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static void AtualizaConsole(string Mensagem)
|
|
{
|
|
if (frmInstancial.frmJoystick.lblConsole.InvokeRequired)
|
|
{
|
|
frmInstancial.frmJoystick.lblConsole.Invoke((MethodInvoker)delegate
|
|
{
|
|
frmInstancial.frmJoystick.lblConsole.Text = Mensagem;
|
|
});
|
|
}
|
|
else
|
|
{
|
|
frmInstancial.frmJoystick.lblConsole.Text = Mensagem;
|
|
}
|
|
}
|
|
|
|
private static bool VerificaComandoValidoSonar(Keys Comando)
|
|
{
|
|
List<Keys> ComandosImpedidos = new List<Keys>();
|
|
|
|
var Sonar = Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal_Kinect;
|
|
if ((Sonar?.Iniciado ?? false) && Variaveis.OperacaoEmAndamento.Controle.SonarAtivado && (Sonar?.DesvioNecessario ?? false))
|
|
{
|
|
switch (Sonar.DirecaoDesvio)
|
|
{
|
|
case Direcao.Parado:
|
|
ComandosImpedidos.Add(Keys.Up);
|
|
break;
|
|
case Direcao.Esquerda:
|
|
ComandosImpedidos.Add(Keys.Right);
|
|
break;
|
|
case Direcao.Direita:
|
|
ComandosImpedidos.Add(Keys.Left);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return !ComandosImpedidos.Any(x => x == Comando);
|
|
}
|
|
|
|
public static T_Code GetDispositivoComando(Keys Tecla, BotoesJoystick botao = BotoesJoystick.Vazio)
|
|
{
|
|
T_Code disp = DeParaComandos.FirstOrDefault(x => x.key == Tecla || x.botaoJoy == botao)?.Dispositivo ?? T_Code.Vzo;
|
|
return disp;
|
|
}
|
|
|
|
}
|
|
|
|
public class ComandosDispositivo
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public Keys Comando { get; set; }
|
|
public T_Code Dispositivo { get; set; }
|
|
public string IDcontrolar { get; set; }
|
|
}
|
|
|
|
public class DeParaComandos
|
|
{
|
|
public BotoesJoystick botaoJoy { get; set; }
|
|
public Keys key { get; set; }
|
|
public T_Code Dispositivo { get; set; }
|
|
public bool PararAoSoltar { get; set; }
|
|
public string IDcontrolar { get; set; }
|
|
}
|
|
|
|
public enum ComparativoAnalogico
|
|
{
|
|
Igual,
|
|
Maior,
|
|
Menor
|
|
}
|
|
|
|
public class BotoesPressionadosValores
|
|
{
|
|
public BotoesJoystick botao { get; set; }
|
|
public int valor { get; set; }
|
|
}
|
|
|
|
}
|