Skip to content

Commit

Permalink
wait for multiple errors before stopping the wifi joystick
Browse files Browse the repository at this point in the history
some transient errors can happen that are recovered after a short moment
  • Loading branch information
schugabe committed Apr 25, 2024
1 parent 96cca3d commit c87c2f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/lib/WIFI/wifiJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ WiFiUDP *WifiJoystick::udp = NULL;
IPAddress WifiJoystick::remoteIP;
uint8_t WifiJoystick::channelCount = JOYSTICK_DEFAULT_CHANNEL_COUNT;
bool WifiJoystick::active = false;
uint8_t WifiJoystick::failedCount = 0;

static inline uint16_t htole16(uint16_t val)
{
Expand Down Expand Up @@ -87,6 +88,7 @@ void WifiJoystick::StartSending(const IPAddress& ip, int32_t updateInterval, uin
channelCount = newChannelCount;

active = true;
failedCount = 0;
}


Expand Down Expand Up @@ -142,7 +144,15 @@ void WifiJoystick::UpdateValues()
udp->write((uint8_t*)&channel, 2);
}

active = udp->endPacket() != 0;
// check if sending failed, don't stop sending after the first error since transient errors can happen
if (udp->endPacket() == 0)
{
failedCount++;
active = failedCount < JOYSTICK_MAX_SEND_ERROR_COUNT;
}
else {
failedCount = 0;
}
}

#endif
2 changes: 2 additions & 0 deletions src/lib/WIFI/wifiJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define JOYSTICK_DEFAULT_UPDATE_INTERVAL 10000
#define JOYSTICK_DEFAULT_CHANNEL_COUNT 8
#define JOYSTICK_VERSION 1
#define JOYSTICK_MAX_SEND_ERROR_COUNT 100

/**
* Class to send stick data via udp
Expand Down Expand Up @@ -64,6 +65,7 @@ class WifiJoystick
static IPAddress remoteIP;
static uint8_t channelCount;
static bool active;
static uint8_t failedCount;
};

#endif

0 comments on commit c87c2f3

Please sign in to comment.