Skip to content

Commit

Permalink
Add pair button in plugin
Browse files Browse the repository at this point in the history
1. Add Pairing button in plugin, enabling pairing via usb connection
2. Add light indicator on Bridge(only support waveshare board for now)
  • Loading branch information
tcfshcrw committed Sep 21, 2024
1 parent f4cabdf commit 41fc766
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 81 deletions.
3 changes: 2 additions & 1 deletion ESP32/include/DiyActivePedal_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct payloadHeader {
struct payloadPedalAction {
uint8_t triggerAbs_u8;
//uint8_t resetPedalPos_u8; //1=reset position, 2=restart ESP
uint8_t system_action_u8; //1=reset position, 2=restart ESP
uint8_t system_action_u8; //1=reset position, 2=restart ESP, 3=OTA Enable, 4=enable pairing
uint8_t startSystemIdentification_u8;
uint8_t returnPedalConfig_u8;
uint8_t RPM_u8;
Expand Down Expand Up @@ -70,6 +70,7 @@ struct payloadPedalState_Extended {
struct payloadBridgeState {
uint8_t Pedal_RSSI;
uint8_t Pedal_availability[3];
uint8_t Bridge_action;//0=none, 1=enable pairing

};
struct payloadPedalConfig {
Expand Down
1 change: 1 addition & 0 deletions ESP32/include/ESPNOW_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uint8_t ESPNow_error_code=0;
bool ESPNow_Pairing_status = false;
bool UpdatePairingToEeprom = false;
bool ESPNow_pairing_action_b = false;
bool software_pairing_action_b = false;



Expand Down
9 changes: 8 additions & 1 deletion ESP32/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,12 @@ void serialCommunicationTask( void * pvParameters )
//OTA_enable_start=true;
ESPNow_OTA_enable=false;
}
//4 Enable pairing
if (dap_actions_st.payloadPedalAction_.system_action_u8==4)
{
Serial.println("Get Pairing command");
software_pairing_action_b=true;
}

// trigger ABS effect
if (dap_actions_st.payloadPedalAction_.triggerAbs_u8)
Expand Down Expand Up @@ -1675,14 +1681,15 @@ void ESPNOW_SyncTask( void * pvParameters )
else
{
#ifdef ESPNow_Pairing_function
if(digitalRead(Pairing_GPIO)==LOW)
if(digitalRead(Pairing_GPIO)==LOW||software_pairing_action_b)
{
Serial.println("Pedal Pairing.....");
delay(1000);
Pairing_state_start=millis();
Pairing_state_last_sending=millis();
ESPNow_pairing_action_b=true;
building_dap_esppairing_lcl=true;
software_pairing_action_b=false;

}
if(ESPNow_pairing_action_b)
Expand Down
2 changes: 1 addition & 1 deletion ESP32_master/include/DiyActivePedal_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct payloadPedalState_Extended {
struct payloadBridgeState {
uint8_t Pedal_RSSI;
uint8_t Pedal_availability[3];

uint8_t Bridge_action;//0=none, 1=enable pairing
};

struct payloadPedalConfig {
Expand Down
1 change: 1 addition & 0 deletions ESP32_master/include/ESPNOW_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uint8_t pedal_status=0;
bool ESPNow_Pairing_status = false;
bool UpdatePairingToEeprom = false;
bool ESPNow_pairing_action_b = false;
bool software_pairing_action_b = false;

bool MacCheck(uint8_t* Mac_A, uint8_t* Mac_B)
{
Expand Down
175 changes: 118 additions & 57 deletions ESP32_master/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ DAP_config_st dap_config_st_Temp;
DAP_ESPPairing_st dap_esppairing_st;//saving
DAP_ESPPairing_st dap_esppairing_lcl;//sending
//DAP_config_st dap_config_st_store[3];
DAP_bridge_state_st dap_bridge_state_lcl;//
#include "CycleTimer.h"


Expand Down Expand Up @@ -127,6 +128,7 @@ DAP_ESPPairing_st dap_esppairing_lcl;//sending
static const crgb_t L_PURPLE = 0x800080;
*/
Adafruit_NeoPixel pixels(LEDS_COUNT, LED_GPIO, NEO_RGB + NEO_KHZ800);
uint8_t LED_Status=0; //0=normal 1= pairing

#endif

Expand Down Expand Up @@ -407,15 +409,16 @@ void ESPNOW_SyncTask( void * pvParameters )
for(;;)
{
#ifdef ESPNow_Pairing_function
if(digitalRead(Pairing_GPIO)==LOW)
if(digitalRead(Pairing_GPIO)==LOW||software_pairing_action_b)
{
Serial.println("Bridge Pairing.....");
delay(1000);
Pairing_state_start=millis();
Pairing_state_last_sending=millis();
ESPNow_pairing_action_b=true;
building_dap_esppairing_lcl=true;

LED_Status=1;
software_pairing_action_b=false;
}
if(ESPNow_pairing_action_b)
{
Expand Down Expand Up @@ -444,6 +447,7 @@ void ESPNOW_SyncTask( void * pvParameters )
{
Serial.println("Reach timeout");
ESPNow_pairing_action_b=false;
LED_Status=0;
if(UpdatePairingToEeprom)
{
EEPROM.put(EEPROM_offset,_ESP_pairing_reg);
Expand Down Expand Up @@ -656,17 +660,55 @@ void Serial_Task( void * pvParameters)
Serial.print(", CRC received: ");
Serial.println(dap_config_st.payloadFooter_.checkSum);
}


// if checks are successfull, overwrite global configuration struct
if (structChecker == true)
{
// if checks are successfull, overwrite global configuration struct
if (structChecker == true)
{
//Serial.println("Updating pedal config");
configUpdateAvailable = true;
configUpdateAvailable = true;
//memcpy(&dap_config_st, &dap_config_st_local, sizeof(dap_config_st));
}
}
break;

case sizeof(DAP_bridge_state_st) :
DAP_bridge_state_st * dap_bridge_state_local_ptr;
dap_bridge_state_local_ptr = &dap_bridge_state_lcl;
Serial.readBytes((char*)dap_bridge_state_local_ptr, sizeof(DAP_bridge_state_st));
// check if data is plausible
if ( dap_bridge_state_lcl.payLoadHeader_.payloadType != DAP_PAYLOAD_TYPE_BRIDGE_STATE )
{
structChecker = false;
Serial.print("Payload type expected: ");
Serial.print(DAP_PAYLOAD_TYPE_BRIDGE_STATE);
Serial.print(", Payload type received: ");
Serial.println(dap_bridge_state_lcl.payLoadHeader_.payloadType);
}
if ( dap_bridge_state_lcl.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_bridge_state_lcl.payLoadHeader_.version);
}
// checksum validation
crc = checksumCalculator((uint8_t*)(&(dap_bridge_state_lcl.payLoadHeader_)), sizeof(dap_bridge_state_lcl.payLoadHeader_) + sizeof(dap_bridge_state_lcl.payloadBridgeState_));
if (crc != dap_bridge_state_lcl.payloadFooter_.checkSum)
{
structChecker = false;
Serial.print("CRC expected: ");
Serial.print(crc);
Serial.print(", CRC received: ");
Serial.println(dap_bridge_state_lcl.payloadFooter_.checkSum);
}
// if checks are successfull, overwrite global configuration struct
if (structChecker == true)
{
if(dap_bridge_state_lcl.payloadBridgeState_.Bridge_action==1)
{
Serial.println("Get action from Simhub:1");
software_pairing_action_b=true;
}
}
break;

default:
Expand Down Expand Up @@ -754,7 +796,7 @@ void Serial_Task( void * pvParameters)
Serial.println(dap_state_basic_st.payloadPedalState_Basic_.error_code_u8);
ESPNow_error_b=false;
}
if(basic_rssi_update)
if(basic_rssi_update)//Bridge action
{
int rssi_filter_value=constrain(rssi_filter.process(rssi_display),-100,0) ;
dap_bridge_state_st.payloadBridgeState_.Pedal_RSSI=(uint8_t)(rssi_filter_value+101);
Expand All @@ -763,6 +805,7 @@ void Serial_Task( void * pvParameters)
dap_bridge_state_st.payLoadHeader_.version=DAP_VERSION_CONFIG;
crc = checksumCalculator((uint8_t*)(&(dap_bridge_state_st.payLoadHeader_)), sizeof(dap_bridge_state_st.payLoadHeader_) + sizeof(dap_bridge_state_st.payloadBridgeState_));
dap_bridge_state_st.payloadFooter_.checkSum=crc;
dap_bridge_state_st.payloadBridgeState_.Bridge_action=0;
DAP_bridge_state_st * dap_bridge_st_local_ptr;
dap_bridge_st_local_ptr = &dap_bridge_state_st;
Serial.write((char*)dap_bridge_st_local_ptr, sizeof(DAP_bridge_state_st));
Expand Down Expand Up @@ -900,57 +943,75 @@ void LED_Task( void * pvParameters)
{
#ifdef LED_ENABLE_WAVESHARE
//LED status update
if(LED_bright_index>30)
{
LED_bright_direction=-1;
}
if(LED_bright_index<2)
if(LED_Status==0)
{
LED_bright_direction=1;
if(LED_bright_index>30)
{
LED_bright_direction=-1;
}
if(LED_bright_index<2)
{
LED_bright_direction=1;
}
LED_bright_index=LED_bright_index+LED_bright_direction;
pixels.setBrightness(LED_bright_index);
uint8_t led_status=dap_bridge_state_st.payloadBridgeState_.Pedal_availability[0]+dap_bridge_state_st.payloadBridgeState_.Pedal_availability[1]*2+dap_bridge_state_st.payloadBridgeState_.Pedal_availability[2]*4;
switch (led_status)
{
case 0:
pixels.setPixelColor(0,0xff,0xff,0xff);
//pixels.setPixelColor(0,0x52,0x00,0xff);//Orange
pixels.show();
break;
case 1:
pixels.setPixelColor(0,0xff,0x00,0x00);//Red
pixels.show();
break;
case 2:
pixels.setPixelColor(0,0xff,0x0f,0x00);//Orange
pixels.show();
break;
case 3:
pixels.setPixelColor(0,0x52,0x00,0xff);//Cyan
pixels.show();
break;
case 4:
pixels.setPixelColor(0,0x5f,0x5f,0x00);//Yellow
pixels.show();
break;
case 5:
pixels.setPixelColor(0,0x00,0x00,0xff);//Blue
pixels.show();
break;
case 6:
pixels.setPixelColor(0,0x00,0xff,0x00);//Green
pixels.show();
break;
case 7:
pixels.setPixelColor(0, 0x80, 0x00, 0x80);//Purple
pixels.show();
break;
default:
break;
}
delay(150);
}
LED_bright_index=LED_bright_index+LED_bright_direction;
pixels.setBrightness(LED_bright_index);
uint8_t led_status=dap_bridge_state_st.payloadBridgeState_.Pedal_availability[0]+dap_bridge_state_st.payloadBridgeState_.Pedal_availability[1]*2+dap_bridge_state_st.payloadBridgeState_.Pedal_availability[2]*4;
switch (led_status)
if(LED_Status==1)//pairing
{
case 0:
pixels.setPixelColor(0,0xff,0xff,0xff);
//pixels.setPixelColor(0,0x52,0x00,0xff);//Orange
pixels.show();
break;
case 1:
pixels.setPixelColor(0,0xff,0x00,0x00);//Red
pixels.show();
break;
case 2:
pixels.setPixelColor(0,0xff,0x0f,0x00);//Orange
pixels.show();
break;
case 3:
pixels.setPixelColor(0,0x52,0x00,0xff);//Cyan
pixels.show();
break;
case 4:
pixels.setPixelColor(0,0x5f,0x5f,0x00);//Yellow
pixels.show();
break;
case 5:
pixels.setPixelColor(0,0x00,0x00,0xff);//Blue
pixels.show();
break;
case 6:
pixels.setPixelColor(0,0x00,0xff,0x00);//Green
pixels.show();
break;
case 7:
pixels.setPixelColor(0, 0x80, 0x00, 0x80);//Purple
pixels.show();
break;
default:
break;

//delay(1000);
pixels.setPixelColor(0,0xff,0x00,0x00);//Red
pixels.setBrightness(25);
pixels.show();
delay(500);
pixels.setPixelColor(0,0x00,0x00,0x00);//fill no color
//pixels.setBrightness(0);
pixels.show();
delay(500);
}
delay(150);

#endif
delay(10);
}
}

Expand Down
12 changes: 12 additions & 0 deletions SimHubPlugin/DataPluginDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public struct payloadBridgeState
public byte Pedal_availability_0;
public byte Pedal_availability_1;
public byte Pedal_availability_2;
public byte Bridge_action;//0=none, 1=enable pairing
};

public struct payloadPedalConfig
Expand Down Expand Up @@ -433,7 +434,18 @@ public byte[] getBytes_Action(DAP_action_st aux)

return myBuffer;
}
public byte[] getBytes_Bridge(DAP_bridge_state_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)
{

Expand Down
Loading

0 comments on commit 41fc766

Please sign in to comment.