% (c):          Joan Vila Carbó

function writeXPLANE(ssock,ip,port,ailerons,elevators,rudder,throttle,brakes)%,magnetos,mixture,starter)
% % DATA 8 11
%
% % sim/joystick/
% yoke_pitch_ratio	float	900+	yes	[-1..1]	The deflection of the joystick axis controlling pitch. Use override_joystick
% yoke_roll_ratio	float	900+	yes	[-1..1]	The deflection of the joystick axis controlling roll
% yoke_heading_ratio	float	900+	yes	[-1..1]	The deflection of the joystick axis controlling yaw. Use override_artstab
% FC_hdng	float	660+	yes	fc	Flight Control Heading (sum of yoke plus artificial stability)
% FC_ptch	float	660+	yes	fc	Flight Control Pitch (sum of yoke plus artificial stability)
% FC_roll	float	660+	yes	fc	Flight Control Roll (sum of yoke plus artificial stability)
% 
% sim/operation/override/
% override_joystick	int	660+	yes	boolean	Override control of the joystick deflections
% override_artstab	int	660+	yes	boolean	Override control of the artificial stability system
%
% sim/flightmodel/engine/
% > ENGN_thro[0]    / ratio   / Throttle (per engine) as set by user, 0 = idle, 1 = max
% > ENGN_running[0] / boolean / Engine on and using fuel (only reliable in 740 and later)
%
% sim/aircraft/controls/
% ignition_on int[8] / enum/ ignition key position 0 = off, 1 = left, 2 = right, 3 = both
% APU_switch int /enum / APU starter switch 0 = off, 1 = on, 2 = start
% APU_running / boolean/ APU running - 1 = on, 0 = off.
%
% sim/flightmodel/controls/
% ail1_def / degrees / [WING] Deflection Aileron 1
% ail2_def
% elv1_def / degrees / [WING] Deflection Elevators
% elv2_def
% rudd_def / degrees / [WING] Deflection Rudders
% rudd2_def
% fla1_def / degrees / [WING] Deflection Flaps
% fla2_def
%
% sim/flightmodel/position/
% sim/network/

value=uint8(true);
dataref='sim/operation/override/override_joystick';
UDPsendDREFchar(ssock,ip,port,dataref,value);
dataref='sim/operation/override/override_artstab';
UDPsendDREFchar(ssock,ip,port,dataref,value);

dataref='sim/joystick/yoke_roll_ratio';
%dataref='sim/joystick/FC_roll';
value=single(ailerons);
UDPsendDREFfloat(ssock,ip,port,dataref,value);

dataref='sim/joystick/yoke_pitch_ratio';
value=single(elevators);
UDPsendDREFfloat(ssock,ip,port,dataref,value);

dataref='sim/joystick/yoke_heading_ratio';
value=single(rudder);
UDPsendDREFfloat(ssock,ip,port,dataref,value);

dataref='sim/flightmodel/engine/ENGN_thro[0]';
value=single(throttle);
UDPsendDREFfloat(ssock,ip,port,dataref,value);

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function UDPsendDREFfloat(ssock,ip,port,dataref,value)
send_msg=uint8(zeros(1,509));
send_msg(1:4)=uint8('DREF');
send_msg(5)=uint8(0);

value=single(value);
value_bytes=typecast(value,'uint8');
len=6;
for j=1:4
    send_msg(len)=value_bytes(j);
    len=len+1;
end
dataref_bytes=uint8(dataref);
for j=1:length(dataref)
    send_msg(len)=dataref_bytes(j);
    len=len+1;
end
send_pkt = java.net.DatagramPacket(send_msg, size(send_msg,2),java.net.InetAddress.getByName(ip),port);
ssock.send(send_pkt);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function UDPsendDREFchar(ssock,ip,port,dataref,char)
send_msg=uint8(zeros(1,509));
send_msg(1:4)=uint8('DREF');
send_msg(5)=uint8(0);

value_bytes=typecast(char,'uint8');

send_msg(9)=value_bytes;
len=10;
dataref_bytes=uint8(dataref);
for j=1:length(dataref)
    send_msg(len)=dataref_bytes(j);
    len=len+1;
end
send_pkt = java.net.DatagramPacket(send_msg, size(send_msg,2),java.net.InetAddress.getByName(ip),port);
ssock.send(send_pkt);
end