include "target.tmh"
int APENGcount;
int AxisOut()
{
// When axis position is negative
if (Axis[DX_SLIDER_AXIS].pos <= -10000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 30);
}
if (Axis[DX_SLIDER_AXIS].pos > -10000 & Axis[DX_SLIDER_AXIS].pos <= -6000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 20);
}
if (Axis[DX_SLIDER_AXIS].pos > -6000 & Axis[DX_SLIDER_AXIS].pos <= -3000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 12);
}
if (Axis[DX_SLIDER_AXIS].pos > -3000 & Axis[DX_SLIDER_AXIS].pos <= -200)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 6);
}
if (Axis[DX_SLIDER_AXIS].pos > -200 & Axis[DX_SLIDER_AXIS].pos < 0)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 1);
}
// When axis position is positive
if (Axis[DX_SLIDER_AXIS].pos >= 10000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 30);
}
if (Axis[DX_SLIDER_AXIS].pos < 10000 & Axis[DX_SLIDER_AXIS].pos >= 6000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 20);
}
if (Axis[DX_SLIDER_AXIS].pos < 6000 & Axis[DX_SLIDER_AXIS].pos >= 3000)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 12);
}
if (Axis[DX_SLIDER_AXIS].pos < 3000 & Axis[DX_SLIDER_AXIS].pos >= 200)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 6);
}
if (Axis[DX_SLIDER_AXIS].pos < 200 & Axis[DX_SLIDER_AXIS].pos > 0)
{
DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 1);
}
}
int AxisZero() // When axis recenter is done
{
StopAutoRepeat(1);
StopAutoRepeat(2);
APENGcount = 0;
}
int CenterAxis()
{
if (Axis[DX_SLIDER_AXIS].pos < 0 & APENGcount < 1 | Axis[DX_SLIDER_AXIS].pos > 0 & APENGcount < 1) // Execute only if APENGcount < 1 to avoid multiple press of the button
{
LockAxis(&Throttle, THR_FC, 1); // Lock the THR_FC lever from change the axis position while auto-centering
ActKey(CHAIN(
KEYON+REXEC(1, 1, "if (Axis[DX_SLIDER_AXIS].pos != 0) AxisOut(); else AxisZero();",RNOSTOP),
D(1), // Need delay to ensure the REXEC below to execute last
KEYON+REXEC(2, 1, "if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] == 0) LockAxis(&Throttle, THR_FC, 0);",RNOSTOP))); // If THR_FC lever is centered while the axis is scrolling,
// regain control when the axis reach zero
// (otherwise THR_FC lever need to be centered 2 times)
//KEYON+REXEC(2, 1, "if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] > -256 & Throttle[THR_FC] < 256) LockAxis(&Throttle, THR_FC, 0);",RNOSTOP))); // Alternative line with THR_FC center tolerance
}
}
int main()
{
if(Init(&EventHandle)) return 1;
MapAxis(&Throttle, THR_FC, DX_SLIDER_AXIS); // Map axis to THR_FC
MapKey(&Throttle, APENG, CHAIN(
EXEC("CenterAxis();"),
EXEC("if (Throttle[APENG] & Axis[DX_SLIDER_AXIS].pos < 0 | Throttle[APENG] & Axis[DX_SLIDER_AXIS].pos > 0) APENGcount = 1;"))); // Set APENGcount = 1
}
int EventHandle(int type, alias o, int x)
{
DefaultMapping(&o, x);
if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] == 0) LockAxis(&Throttle, THR_FC, 0); // After the axis has been auto-centered, THR_FC need to be centered to regain control of the axis
}