Skip to content

Commit

Permalink
Moved pedal action triggers to structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrGri committed Dec 14, 2023
1 parent 6ce3540 commit 0035129
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 136 deletions.
15 changes: 15 additions & 0 deletions Arduino/Esp32/Main/DiyActivePedal_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ struct payloadHeader {

};

struct payloadPedalAction {
uint8_t triggerAbs_u8;
uint8_t resetPedalPos_u8;
uint8_t startSystemIdentification_u8;
uint8_t returnPedalConfig_u8;
};

struct payloadPedalConfig {
// configure pedal start and endpoint
// In percent
Expand Down Expand Up @@ -77,6 +84,14 @@ struct payloadFooter {
uint16_t checkSum;
};


struct DAP_actions_st {
payloadHeader payLoadHeader_;
payloadPedalAction payloadPedalAction_;
payloadFooter payloadFooter_;
};


struct DAP_config_st {

payloadHeader payLoadHeader_;
Expand Down
198 changes: 112 additions & 86 deletions Arduino/Esp32/Main/Main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -711,108 +711,134 @@ void serialCommunicationTask( void * pvParameters )
// read serial input
byte n = Serial.available();

/*if (n > 0)
{
Serial.println("In: ");
Serial.println(n);
Serial.println(" Exp: ");
Serial.println(sizeof(DAP_config_st) );
}*/



if (n > 0)
{
switch (n) {

// likely config structure
case sizeof(DAP_config_st):

if(semaphore_updateConfig!=NULL)
{
if(xSemaphoreTake(semaphore_updateConfig, (TickType_t)1)==pdTRUE)
{
DAP_config_st * dap_config_st_local_ptr;
dap_config_st_local_ptr = &dap_config_st_local;
Serial.readBytes((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));



// check if data is plausible
bool structChecker = true;
if ( dap_config_st_local.payLoadHeader_.payloadType != DAP_PAYLOAD_TYPE_CONFIG ){
structChecker = false;
Serial.print("Payload type expected: ");
Serial.print(DAP_PAYLOAD_TYPE_CONFIG);
Serial.print(", Payload type received: ");
Serial.println(dap_config_st_local.payLoadHeader_.payloadType);
}
if ( dap_config_st_local.payLoadHeader_.version != DAP_VERSION_CONFIG ){
structChecker = false;
Serial.print("Config version expected: ");
Serial.print(DAP_VERSION_CONFIG);
Serial.print(", Config version received: ");
Serial.println(dap_config_st_local.payLoadHeader_.version);
}
// checksum validation
crc = checksumCalculator((uint8_t*)(&(dap_config_st_local.payLoadHeader_)), sizeof(dap_config_st_local.payLoadHeader_) + sizeof(dap_config_st_local.payLoadPedalConfig_));
if (crc != dap_config_st_local.payloadFooter_.checkSum){
structChecker = false;
Serial.print("CRC expected: ");
Serial.print(crc);
Serial.print(", CRC received: ");
Serial.println(dap_config_st_local.payloadFooter_.checkSum);
}


// if checks are successfull, overwrite global configuration struct
if (structChecker == true)
{
Serial.println("Updating pedal config");
configUpdateAvailable = true;
}
xSemaphoreGive(semaphore_updateConfig);
}
}
break;

// likely config structure
if ( n == sizeof(DAP_config_st) )
{

if(semaphore_updateConfig!=NULL)
{
if(xSemaphoreTake(semaphore_updateConfig, (TickType_t)1)==pdTRUE)
{
DAP_config_st * dap_config_st_local_ptr;
dap_config_st_local_ptr = &dap_config_st_local;
Serial.readBytes((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));
// likely action structure
case sizeof(DAP_actions_st) :


DAP_actions_st dap_actions_st;
Serial.readBytes((char*)&dap_actions_st, sizeof(DAP_actions_st));

// check if data is plausible
bool structChecker = true;
if ( dap_config_st_local.payLoadHeader_.payloadType != DAP_PAYLOAD_TYPE_CONFIG ){
structChecker = false;
Serial.print("Payload type expected: ");
Serial.print(DAP_PAYLOAD_TYPE_CONFIG);
Serial.print(", Payload type received: ");
Serial.println(dap_config_st_local.payLoadHeader_.payloadType);
}
if ( dap_config_st_local.payLoadHeader_.version != DAP_VERSION_CONFIG ){
structChecker = false;
Serial.print("Config version expected: ");
Serial.print(DAP_VERSION_CONFIG);
Serial.print(", Config version received: ");
Serial.println(dap_config_st_local.payLoadHeader_.version);
}
// checksum validation
crc = checksumCalculator((uint8_t*)(&(dap_config_st_local.payLoadHeader_)), sizeof(dap_config_st_local.payLoadHeader_) + sizeof(dap_config_st_local.payLoadPedalConfig_));
if (crc != dap_config_st_local.payloadFooter_.checkSum){
structChecker = false;
crc = checksumCalculator((uint8_t*)(&(dap_actions_st.payLoadHeader_)), sizeof(dap_actions_st.payLoadHeader_) + sizeof(dap_actions_st.payloadPedalAction_));
if (crc != dap_actions_st.payloadFooter_.checkSum){
Serial.print("CRC expected: ");
Serial.print(crc);
Serial.print(", CRC received: ");
Serial.println(dap_config_st_local.payloadFooter_.checkSum);
Serial.println(dap_actions_st.payloadFooter_.checkSum);
}
else
{

// trigger reset pedal position
if (dap_actions_st.payloadPedalAction_.resetPedalPos_u8)
{
resetPedalPosition = true;
}

// trigger ABS effect
if (dap_actions_st.payloadPedalAction_.triggerAbs_u8)
{
absOscillation.trigger();
}

// trigger system identification
if (dap_actions_st.payloadPedalAction_.startSystemIdentification_u8)
{
systemIdentificationMode_b = true;
}

// trigger return pedal position
if (dap_actions_st.payloadPedalAction_.returnPedalConfig_u8)
{
DAP_config_st * dap_config_st_local_ptr;
dap_config_st_local_ptr = &dap_config_st;
//uint16_t crc = checksumCalculator((uint8_t*)(&(dap_config_st.payLoadHeader_)), sizeof(dap_config_st.payLoadHeader_) + sizeof(dap_config_st.payLoadPedalConfig_));
crc = checksumCalculator((uint8_t*)(&(dap_config_st.payLoadHeader_)), sizeof(dap_config_st.payLoadHeader_) + sizeof(dap_config_st.payLoadPedalConfig_));
dap_config_st_local_ptr->payloadFooter_.checkSum = crc;
Serial.write((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));
}


// if checks are successfull, overwrite global configuration struct
if (structChecker == true)
{
Serial.println("Updating pedal config");
configUpdateAvailable = true;
}
xSemaphoreGive(semaphore_updateConfig);
}
}
}
else
{
if (n!=0)
{
int menuChoice = Serial.parseInt();
switch (menuChoice) {
// resset minimum position
case 1:
Serial.println("Reset position");
resetPedalPosition = true;
break;

// toggle ABS
case 2:
//Serial.print("Second case:");
absOscillation.trigger();
break;
// de-/activate spline debug
case 3:
Serial.println("Start system identification");
systemIdentificationMode_b = true;
break;
case 4:
DAP_config_st * dap_config_st_local_ptr;
dap_config_st_local_ptr = &dap_config_st;
//uint16_t crc = checksumCalculator((uint8_t*)(&(dap_config_st.payLoadHeader_)), sizeof(dap_config_st.payLoadHeader_) + sizeof(dap_config_st.payLoadPedalConfig_));
crc = checksumCalculator((uint8_t*)(&(dap_config_st.payLoadHeader_)), sizeof(dap_config_st.payLoadHeader_) + sizeof(dap_config_st.payLoadPedalConfig_));
dap_config_st_local_ptr->payloadFooter_.checkSum = crc;
Serial.write((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));
break;

default:
Serial.println("Default case:");
break;
}

break;

default:

// flush the input buffer
while (Serial.available()) Serial.read();
//Serial.flush();

Serial.println("\nIn byte size: ");
Serial.println(n);
Serial.println(" Exp config size: ");
Serial.println(sizeof(DAP_config_st) );
Serial.println(" Exp action size: ");
Serial.println(sizeof(DAP_actions_st) );

break;




}
}


// transmit controller output
if (IsControllerReady()) {
if(semaphore_updateJoystick!=NULL)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified SimHubPlugin/.vs/User.PluginSdkDemo/v17/.suo
Binary file not shown.
105 changes: 102 additions & 3 deletions SimHubPlugin/DataPluginDemo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GameReaderCommon;
//using log4net.Plugin;
using SimHub.Plugins;
using SimHub.Plugins.OutputPlugins.Dash.GLCDTemplating;
using System;
Expand Down Expand Up @@ -28,6 +29,16 @@ public struct payloadHeader

public byte storeToEeprom;
}


public struct payloadPedalAction
{
public byte triggerAbs_u8;
public byte resetPedalPos_u8;
public byte startSystemIdentification_u8;
public byte returnPedalConfig_u8;
};

public struct payloadPedalConfig
{
// configure pedal start and endpoint
Expand Down Expand Up @@ -98,6 +109,14 @@ public struct payloadFooter
}



public struct DAP_action_st
{
public payloadHeader payloadHeader_;
public payloadPedalAction payloadPedalAction_;
public payloadFooter payloadFooter_;
}

public struct DAP_config_st
{
public payloadHeader payloadHeader_;
Expand Down Expand Up @@ -163,7 +182,44 @@ public class DataPluginDemo : IPlugin, IDataPlugin, IWPFSettingsV2
/// </summary>
/// <param name="pluginManager"></param>
/// <param name="data">Current game data, including current and previous data frame.</param>
public void DataUpdate(PluginManager pluginManager, ref GameData data)
///
unsafe public UInt16 checksumCalc(byte* data, int length)
{

UInt16 curr_crc = 0x0000;
byte sum1 = (byte)curr_crc;
byte sum2 = (byte)(curr_crc >> 8);
int index;
for (index = 0; index < length; index = index + 1)
{
int v = (sum1 + (*data));
sum1 = (byte)v;
sum1 = (byte)(v % 255);

int w = (sum1 + sum2) % 255;
sum2 = (byte)w;

data++;// = data++;
}

int x = (sum2 << 8) | sum1;
return (UInt16)x;
}

public byte[] getBytes_Action(DAP_action_st aux)
{
int length = Marshal.SizeOf(aux);
IntPtr ptr = Marshal.AllocHGlobal(length);
byte[] myBuffer = new byte[length];

Marshal.StructureToPtr(aux, ptr, true);
Marshal.Copy(ptr, myBuffer, 0, length);
Marshal.FreeHGlobal(ptr);

return myBuffer;
}

unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
{

bool sendAbsSignal_local_b = false;
Expand Down Expand Up @@ -210,12 +266,36 @@ public void DataUpdate(PluginManager pluginManager, ref GameData data)
}




// Send ABS trigger signal via serial
if (sendAbsSignal_local_b)
{
if (_serialPort[1].IsOpen)
{
_serialPort[1].Write("2");
//_serialPort[1].Write("2");

// compute checksum
DAP_action_st tmp;
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);

}
}

Expand All @@ -224,7 +304,26 @@ public void DataUpdate(PluginManager pluginManager, ref GameData data)
{
if (_serialPort[2].IsOpen)
{
_serialPort[2].Write("2");
// compute checksum
DAP_action_st tmp;
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[2].DiscardInBuffer();

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

Expand Down
Loading

0 comments on commit 0035129

Please sign in to comment.