godotVmcSharp/VmcMessages/VmcExtOk.cs

172 lines
6.7 KiB
C#
Raw Normal View History

2023-08-16 19:47:14 +02:00
/*
godotVmcSharp
Copyright (C) 2023 Cassandra de la Cruz-Munoz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using godotOscSharp;
using System.Collections.Generic;
2023-08-16 19:47:14 +02:00
namespace godotVmcSharp
{
public class VmcExtOk : VmcMessage
{
2023-09-24 19:06:26 +02:00
public readonly int Loaded;
public readonly int? CalibrationState;
public readonly int? CalibrationMode;
public readonly int? TrackingStatus;
2023-08-16 19:47:14 +02:00
public static VmcExtOk CreateFromParams(int loaded)
2023-08-16 19:47:14 +02:00
{
if (loaded < 0 || loaded > 1)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
}
return new VmcExtOk(loaded);
}
private VmcExtOk(int loaded): base(new OscAddress("/VMC/Ext/OK"))
{
Loaded = loaded;
2023-08-16 19:47:14 +02:00
}
public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode)
2023-08-16 19:47:14 +02:00
{
if (loaded < 0 || loaded > 1)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
}
if (calibrationState < 0 || calibrationState > 3)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
}
if (calibrationMode < 0 || calibrationMode > 2)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
}
return new VmcExtOk(loaded, calibrationState, calibrationMode);
}
private VmcExtOk(int loaded, int calibrationState, int calibrationMode) : base(new OscAddress("/VMC/Ext/OK"))
{
Loaded = loaded;
CalibrationState = calibrationState;
CalibrationMode = calibrationMode;
2023-08-16 19:47:14 +02:00
}
public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode, int trackingStatus)
2023-08-16 19:47:14 +02:00
{
if (loaded < 0 || loaded > 1)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
}
if (calibrationState < 0 || calibrationState > 3)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
}
if (calibrationMode < 0 || calibrationMode > 2)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
}
if (trackingStatus < 0 || trackingStatus > 1)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus);
}
return new VmcExtOk(loaded, calibrationState, calibrationMode, trackingStatus);
}
private VmcExtOk(int loaded, int calibrationState, int calibrationMode, int trackingStatus) : base(new OscAddress("/VMC/Ext/OK"))
{
Loaded = loaded;
CalibrationState = calibrationState;
CalibrationMode = calibrationMode;
TrackingStatus = trackingStatus;
2023-08-16 19:47:14 +02:00
}
public static VmcExtOk CreateFromMessage(OscMessage m)
2023-08-16 19:47:14 +02:00
{
switch (m.Data.Count)
{
case 1:
return new VmcExtOk(getLoaded(m));
2023-08-16 19:47:14 +02:00
case 3:
return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m));
2023-08-16 19:47:14 +02:00
case 4:
var trackingStatus = m.Data[3].GetAsInt32();
if (trackingStatus < 0 || trackingStatus > 1)
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus);
}
return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m), trackingStatus);
2023-08-16 19:47:14 +02:00
default:
throw new MissingArgumentsException(m.Address, "{1, 3, 4}", m.Data.Count);
2023-08-16 19:47:14 +02:00
}
}
private static int getLoaded(OscMessage m)
2023-08-16 19:47:14 +02:00
{
var loaded = m.Data[0].GetAsInt32();
if (loaded < 0 || loaded > 1)
2023-08-16 19:47:14 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
2023-08-16 19:47:14 +02:00
}
return loaded;
2023-08-16 19:47:14 +02:00
}
private static int getCalibrationState(OscMessage m)
2023-08-16 19:47:14 +02:00
{
var calibrationState = m.Data[1].GetAsInt32();
if (calibrationState < 0 || calibrationState > 3)
2023-08-16 19:47:14 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
2023-08-16 19:47:14 +02:00
}
return calibrationState;
2023-08-16 19:47:14 +02:00
}
private static int getCalibrationMode(OscMessage m)
2023-08-16 19:47:14 +02:00
{
var calibrationMode = m.Data[2].GetAsInt32();
if (calibrationMode < 0 || calibrationMode > 2)
2023-08-16 19:47:14 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
2023-08-16 19:47:14 +02:00
}
return calibrationMode;
}
public new OscMessage ToMessage()
{
if (CalibrationState == null)
{
2023-08-18 00:22:27 +02:00
return new OscMessage(Addr, new List<OscArgument>{
OscArgument.CreateFromParams(Loaded, 'i')!
});
}
if (TrackingStatus == null)
{
2023-08-18 00:22:27 +02:00
return new OscMessage(Addr, new List<OscArgument>{
OscArgument.CreateFromParams(Loaded, 'i')!,
OscArgument.CreateFromParams(CalibrationState, 'i')!,
OscArgument.CreateFromParams(CalibrationMode, 'i')!
});
}
2023-08-18 00:22:27 +02:00
return new OscMessage(Addr, new List<OscArgument>{
OscArgument.CreateFromParams(Loaded, 'i')!,
OscArgument.CreateFromParams(CalibrationState, 'i')!,
OscArgument.CreateFromParams(CalibrationMode, 'i')!,
OscArgument.CreateFromParams(TrackingStatus, 'i')!
});
2023-08-16 19:47:14 +02:00
}
}
}