Skip to content

Commit

Permalink
Normalized RPM value
Browse files Browse the repository at this point in the history
1. Normalized RPM value
2. Add update flag to reduce the frenquency of update
  • Loading branch information
tcfshcrw authored Dec 31, 2023
1 parent 1065971 commit 0598931
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 67 deletions.
129 changes: 62 additions & 67 deletions SimHubPlugin/DataPluginDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)

bool sendAbsSignal_local_b = false;
bool sendTcSignal_local_b = false;
Byte RPM_value =0;
double RPM_value =0;
double RPM_MAX = 0;


// Send ABS signal when triggered by the game
Expand All @@ -275,8 +276,15 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
{
sendTcSignal_local_b = true;
}

RPM_value = (Byte)(data.NewData.Rpms / 1000);
if (data.NewData.CarSettings_MaxRPM == 0)
{
RPM_MAX = 10000;
}
else
{
RPM_MAX = data.NewData.CarSettings_MaxRPM;
}
RPM_value = (data.NewData.Rpms / RPM_MAX*100);
game_running_index = 1;

}
Expand Down Expand Up @@ -309,6 +317,7 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)



bool update_flag = false;

if (data.GameRunning)
{
Expand All @@ -320,68 +329,27 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
tmp.payloadHeader_.version = (byte)Constants.pedalConfigPayload_version;
tmp.payloadHeader_.payloadType = (byte)Constants.pedalActionPayload_type;
tmp.payloadPedalAction_.triggerAbs_u8 = 0;
tmp.payloadPedalAction_.RPM_u8 = rpm_last_value;
if (RPM_value != rpm_last_value)
tmp.payloadPedalAction_.RPM_u8 = (Byte)rpm_last_value;
if ((RPM_value - rpm_last_value>10) || (RPM_value-rpm_last_value<-10))
{
tmp.payloadPedalAction_.RPM_u8 = RPM_value;
DAP_action_st* v = &tmp;
byte* p = (byte*)v;
tmp.payloadFooter_.checkSum = checksumCalc(p, sizeof(payloadHeader) + sizeof(payloadPedalAction));


int length = sizeof(DAP_action_st);
byte[] newBuffer = new byte[length];
newBuffer = getBytes_Action(tmp);


// clear inbuffer
_serialPort[1].DiscardInBuffer();

// send query command
_serialPort[1].Write(newBuffer, 0, newBuffer.Length);
tmp.payloadPedalAction_.RPM_u8 = (Byte)RPM_value;
update_flag = true;
rpm_last_value = (Byte)RPM_value;
}

rpm_last_value = RPM_value;

}
if (sendAbsSignal_local_b)
{
//_serialPort[1].Write("2");

// compute checksum
tmp.payloadPedalAction_.triggerAbs_u8 = 1;
DAP_action_st* v = &tmp;
byte* p = (byte*)v;
tmp.payloadFooter_.checkSum = checksumCalc(p, sizeof(payloadHeader) + sizeof(payloadPedalAction));


int length = sizeof(DAP_action_st);
byte[] newBuffer = new byte[length];
newBuffer = getBytes_Action(tmp);


// clear inbuffer
_serialPort[1].DiscardInBuffer();

// send query command
_serialPort[1].Write(newBuffer, 0, newBuffer.Length);

}
if (sendAbsSignal_local_b)
{
//_serialPort[1].Write("2");

// compute checksum
tmp.payloadPedalAction_.triggerAbs_u8 = 1;
update_flag = true;

}

// Send TC trigger signal via serial
if (_serialPort[2].IsOpen)
{
DAP_action_st tmp;
}

tmp.payloadHeader_.version = (byte)Constants.pedalConfigPayload_version;
tmp.payloadHeader_.payloadType = (byte)Constants.pedalActionPayload_type;
tmp.payloadPedalAction_.triggerAbs_u8 = 0;
tmp.payloadPedalAction_.RPM_u8 = rpm_last_value;
if (RPM_value != rpm_last_value)
if (update_flag)
{
tmp.payloadPedalAction_.RPM_u8 = RPM_value;
DAP_action_st* v = &tmp;
byte* p = (byte*)v;
tmp.payloadFooter_.checkSum = checksumCalc(p, sizeof(payloadHeader) + sizeof(payloadPedalAction));
Expand All @@ -393,18 +361,45 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)


// clear inbuffer
_serialPort[2].DiscardInBuffer();
_serialPort[1].DiscardInBuffer();

// send query command
_serialPort[2].Write(newBuffer, 0, newBuffer.Length);
rpm_last_value = RPM_value;
_serialPort[1].Write(newBuffer, 0, newBuffer.Length);


}
if (sendTcSignal_local_b)
{
// compute checksum





}

// Send TC trigger signal via serial
if (_serialPort[2].IsOpen)
{
DAP_action_st tmp;

tmp.payloadHeader_.version = (byte)Constants.pedalConfigPayload_version;
tmp.payloadHeader_.payloadType = (byte)Constants.pedalActionPayload_type;
tmp.payloadPedalAction_.triggerAbs_u8 = 0;
tmp.payloadPedalAction_.RPM_u8 = rpm_last_value;

if ((RPM_value - rpm_last_value > 10) || (RPM_value - rpm_last_value < -10))
{
tmp.payloadPedalAction_.RPM_u8 = (Byte)RPM_value;
update_flag = true;
rpm_last_value = (Byte)RPM_value;
}
if (sendTcSignal_local_b)
{
// compute checksum

tmp.payloadPedalAction_.triggerAbs_u8 = 1;
update_flag = true;

}
if (update_flag)
{
DAP_action_st* v = &tmp;
byte* p = (byte*)v;
tmp.payloadFooter_.checkSum = checksumCalc(p, sizeof(payloadHeader) + sizeof(payloadPedalAction));
Expand All @@ -420,9 +415,9 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)

// send query command
_serialPort[2].Write(newBuffer, 0, newBuffer.Length);
rpm_last_value = RPM_value;

rpm_last_value = (Byte)RPM_value;
}



}
Expand Down
Binary file modified SimHubPlugin/bin/DiyActivePedal.dll
Binary file not shown.

0 comments on commit 0598931

Please sign in to comment.