added cablights

This commit is contained in:
2024-07-10 17:04:27 +02:00
parent 6be67b5165
commit cb6e65a8b4

View File

@@ -7,42 +7,43 @@ uint16_t joymax = 0;
uint16_t trgmax = 0; uint16_t trgmax = 0;
#define deadband 20000 #define deadband 20000
#define trgdeadband 200 #define trgdeadband 200
bool cablights = false;
direction getdirectionJoy(int setpoint) direction getdirectionJoy(int setpoint)
{ {
if(setpoint > (joymax/2 +deadband)) if (setpoint > (joymax / 2 + deadband))
{ {
return direction::right; return direction::right;
} }
else if(setpoint < (joymax/2 -deadband)) else if (setpoint < (joymax / 2 - deadband))
{ {
return direction::left; return direction::left;
} }
//else // else
return direction::stop; return direction::stop;
} }
direction getdirecion2Button(uint8_t button0, uint8_t button1) direction getdirecion2Button(uint8_t button0, uint8_t button1)
{ {
if(button0 & !button1) if (button0 & !button1)
{ {
return direction::left; return direction::left;
} }
if(!button0 & button1) if (!button0 & button1)
{ {
return direction::right; return direction::right;
} }
//else // else
return direction::stop; return direction::stop;
} }
direction getTrackDirection(int trigger, uint8_t shoulder) direction getTrackDirection(int trigger, uint8_t shoulder)
{ {
if(trigger > (trgmax/2 + trgdeadband) & !shoulder) if (trigger > (trgmax / 2 + trgdeadband) & !shoulder)
{ {
return direction::right; return direction::right;
} }
if((trigger == 0) & shoulder) if ((trigger == 0) & shoulder)
{ {
return direction::left; return direction::left;
} }
@@ -58,8 +59,24 @@ void setup_controller()
Serial.printf("Waiting for controller"); Serial.printf("Waiting for controller");
} }
void loop_controller() void handleCabLights(uint8_t button)
{ {
if(!button) return;
if (cablights)
{
digitalWrite(cabLights, HIGH);
cablights = true;
}
else
{
digitalWrite(cabLights, LOW);
cablights = false;
}
}
void loop_controller()
{
xboxController.onLoop(); xboxController.onLoop();
if (xboxController.isConnected()) if (xboxController.isConnected())
{ {
@@ -81,7 +98,7 @@ void loop_controller()
// log_i("joyRHori rate: %u", xboxController.xboxNotif.trigLT); // log_i("joyRHori rate: %u", xboxController.xboxNotif.trigLT);
// log_i("joyRVert rate: %u", xboxController.xboxNotif.trigRT); // log_i("joyRVert rate: %u", xboxController.xboxNotif.trigRT);
log_i("battery %d %", xboxController.battery); log_i("battery %d %", xboxController.battery);
//log_v("joymax:%d",joymax ); // log_v("joymax:%d",joymax );
lastcontrollernotify = millis(); lastcontrollernotify = millis();
} }
controlMotor("Pivot", getdirectionJoy(xboxController.xboxNotif.joyLHori)); controlMotor("Pivot", getdirectionJoy(xboxController.xboxNotif.joyLHori));
@@ -93,21 +110,21 @@ void loop_controller()
controlMotor("TrackLeft", getTrackDirection(xboxController.xboxNotif.trigLT, xboxController.xboxNotif.btnLB)); controlMotor("TrackLeft", getTrackDirection(xboxController.xboxNotif.trigLT, xboxController.xboxNotif.btnLB));
controlMotor("TrackRight", getTrackDirection(xboxController.xboxNotif.trigRT, xboxController.xboxNotif.btnRB)); controlMotor("TrackRight", getTrackDirection(xboxController.xboxNotif.trigRT, xboxController.xboxNotif.btnRB));
handleCabLights(xboxController.xboxNotif.btnShare);
} }
} }
else else
{ {
if (millis() - lastcontrollernotify > CONTROLLERNOTIFY) if (millis() - lastcontrollernotify > CONTROLLERNOTIFY)
{ {
log_w("not connected"); log_w("not connected");
lastcontrollernotify = millis(); lastcontrollernotify = millis();
} handleCabLights(1);
}
if (xboxController.getCountFailedConnection() > 10) if (xboxController.getCountFailedConnection() > 10)
{ {
log_e("restarting"); log_e("restarting");
ESP.restart(); ESP.restart();
} }
} }
} }