Skip to content

Commit

Permalink
SimHub: Read config from pedal. ESP: Added BLE PID
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrGri committed Nov 23, 2023
1 parent db502a4 commit 0d1c180
Show file tree
Hide file tree
Showing 23 changed files with 561 additions and 153 deletions.
2 changes: 2 additions & 0 deletions Arduino/Esp32/Main/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ static const int16_t JOYSTICK_RANGE = JOYSTICK_MAX_VALUE - JOYSTICK_MIN_VALUE;
bleGamepadConfig.setButtonCount(0);
bleGamepadConfig.setHatSwitchCount(0);
bleGamepadConfig.setAutoReport(false);
bleGamepadConfig.setPid(chip); // product id

bleGamepad.begin(&bleGamepadConfig);
}

Expand Down
5 changes: 5 additions & 0 deletions Arduino/Esp32/Main/Main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ void serialCommunicationTask( void * pvParameters )
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;
Serial.write((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));
break;

default:
Serial.println("Default case:");
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.
1 change: 1 addition & 0 deletions SimHubPlugin/SettingsControlDemo.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<!-- send config buttons -->
<styles:SHButtonPrimary Click="ResetPedalPosition_click" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Width="180" >Reset pedal position</styles:SHButtonPrimary>
<styles:SHButtonPrimary Click="SendConfigToPedal_click" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Width="180" >Send config to pedal</styles:SHButtonPrimary>
<styles:SHButtonPrimary Click="ReadConfigFromPedal_click" HorizontalAlignment="Left" Grid.Row="4" Grid.Column="0" Width="180" >Read config from pedal</styles:SHButtonPrimary>

<!-- read/write config from/to JSON file buttons -->
<!--<styles:SHButtonPrimary Click="SaveStructToJson_click" HorizontalAlignment="Left" Grid.Row="4" Grid.Column="1" Width="180" >Save config to JSON</styles:SHButtonPrimary>-->
Expand Down
77 changes: 76 additions & 1 deletion SimHubPlugin/SettingsControlDemo.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ public byte[] getBytes(DAP_config_st aux)
}


public DAP_config_st getConfigFromBytes(byte[] myBuffer)
{
DAP_config_st aux;

// see https://stackoverflow.com/questions/31045358/how-do-i-copy-bytes-into-a-struct-variable-in-c
int size = Marshal.SizeOf(typeof(DAP_config_st));
IntPtr ptr = Marshal.AllocHGlobal(size);

Marshal.Copy(myBuffer, 0, ptr, size);

aux = (DAP_config_st)Marshal.PtrToStructure(ptr, typeof(DAP_config_st));
Marshal.FreeHGlobal(ptr);

return aux;
}


unsafe private UInt16 checksumCalc(byte* data, int length)
{

Expand Down Expand Up @@ -992,11 +1009,69 @@ unsafe public void SendConfigToPedal_click(object sender, RoutedEventArgs e)
while (Plugin._serialPort[indexOfSelectedPedal_u].BytesToRead > 0)
{
string message = Plugin._serialPort[indexOfSelectedPedal_u].ReadLine();

TextBox_debugOutput.Text += message;

}
}
catch (TimeoutException) { }



}
}




/********************************************************************************************************************/
/* Read config from pedal */
/********************************************************************************************************************/
unsafe public void ReadConfigFromPedal_click(object sender, RoutedEventArgs e)
{
if (Plugin._serialPort[indexOfSelectedPedal_u].IsOpen)
{

// send query command
Plugin._serialPort[indexOfSelectedPedal_u].DiscardInBuffer();
Plugin._serialPort[indexOfSelectedPedal_u].Write("4");

// wait for response
System.Threading.Thread.Sleep(100);

try
{
int length = sizeof(DAP_config_st);
byte[] newBuffer = new byte[length];

if (Plugin._serialPort[indexOfSelectedPedal_u].BytesToRead == length)
{
Plugin._serialPort[indexOfSelectedPedal_u].Read(newBuffer, 0, length);
DAP_config_st pedalConfig_read_st = getConfigFromBytes(newBuffer);

// check CRC
payloadPedalConfig tmp = pedalConfig_read_st.payloadPedalConfig_;
payloadPedalConfig* v = &tmp;
byte* p = (byte*)v;

if (checksumCalc(p, sizeof(payloadPedalConfig)) == pedalConfig_read_st.payloadHeader_.checkSum)
{
this.dap_config_st[indexOfSelectedPedal_u] = pedalConfig_read_st;
updateTheGuiFromConfig();
TextBox_debugOutput.Text = "Read config from pedal successful!";
}
else
{
TextBox_debugOutput.Text = "CRC mismatch!";
}
}
else
{
TextBox_debugOutput.Text = "Data size mismatch!";
}




}
catch (TimeoutException) { }

Expand Down
Binary file modified SimHubPlugin/bin/DiyActivePedal.dll
Binary file not shown.
Binary file modified SimHubPlugin/bin/DiyActivePedal.g.resources
Binary file not shown.
Binary file modified SimHubPlugin/bin/DiyActivePedal.pdb
Binary file not shown.
Binary file modified SimHubPlugin/obj/Release/DesignTimeResolveAssemblyReferences.cache
Binary file not shown.
Binary file modified SimHubPlugin/obj/Release/DiyActivePedal.dll
Binary file not shown.
Binary file modified SimHubPlugin/obj/Release/DiyActivePedal.g.resources
Binary file not shown.
Binary file modified SimHubPlugin/obj/Release/DiyActivePedal.pdb
Binary file not shown.
Binary file modified SimHubPlugin/obj/Release/SettingsControlDemo.baml
Binary file not shown.
Loading

0 comments on commit 0d1c180

Please sign in to comment.