Skip to content

Commit

Permalink
Convert to more generic UDP service
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Oct 10, 2023
1 parent 4e8238a commit 5d453ce
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/lib/WIFI/devWIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,20 @@ static void WebUploadForceUpdateHandler(AsyncWebServerRequest *request) {
}

#ifdef HAS_WIFI_JOYSTICK
static void WebUStartWifiJoystick(AsyncWebServerRequest *request)
static void WebUdpControl(AsyncWebServerRequest *request)
{
if(request->hasArg("start"))
const String &action = request->arg("action");
if (action.equals("joystick_begin"))
{
int32_t updateInterval = request->hasArg("updateInterval") ? request->arg("updateInterval").toInt() : JOYSTICK_DEFAULT_UPDATE_INTERVAL;
int32_t updateInterval = request->hasArg("interval") ? request->arg("interval").toInt() : JOYSTICK_DEFAULT_UPDATE_INTERVAL;
uint32_t channelCount = request->hasArg("channels") ? request->arg("channels").toInt() : JOYSTICK_DEFAULT_CHANNEL_COUNT;
WifiJoystick::StartSending(request->client()->remoteIP(), updateInterval, channelCount);
request->send(200, "text/plain", "ok");
}
else if (action.equals("joystick_end"))
{
WifiJoystick::StopSending();
}
}
#endif

Expand Down Expand Up @@ -934,8 +939,8 @@ static void startMDNS()
#endif

#ifdef HAS_WIFI_JOYSTICK
MDNS.addService("elrs-joystick", "udp", JOYSTICK_PORT);
MDNS.addServiceTxt("elrs-joystick", "udp", "version", String(JOYSTICK_VERSION).c_str());
MDNS.addService("elrs", "udp", JOYSTICK_PORT);
MDNS.addServiceTxt("elrs", "udp", "version", String(JOYSTICK_VERSION).c_str());
#endif
}

Expand Down Expand Up @@ -993,7 +998,7 @@ static void startServices()
server.on("/reboot", HandleReboot);
server.on("/reset", HandleReset);
#ifdef HAS_WIFI_JOYSTICK
server.on("/wifi_joystick", WebUStartWifiJoystick);
server.on("/udpcontrol", HTTP_POST, WebUdpControl);
#endif

server.addHandler(new AsyncCallbackJsonWebHandler("/config", UpdateConfiguration));
Expand Down
12 changes: 5 additions & 7 deletions src/lib/WIFI/wifiJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ void WifiJoystick::StopJoystickService()

void WifiJoystick::StartSending(const IPAddress& ip, int32_t updateInterval, uint8_t newChannelCount)
{
if (!udp)
remoteIP = ip;
if (!udp || active)
{
return;
}
remoteIP = ip;

hwTimer::updateInterval(updateInterval);
CRSF::setSyncParams(updateInterval);
POWERMGNT::setPower(MinPower);
Radio.End();

CRSF::RCdataCallback = UpdateValues;
CRSF::RCdataCallback = &UpdateValues;
channelCount = newChannelCount;

if (channelCount > 16)
Expand Down Expand Up @@ -101,9 +101,6 @@ void WifiJoystick::Loop(unsigned long now)
udp->write((uint8_t*)device_name, eua.name_len);
udp->endPacket();
}

// Free any received data
udp->flush();
}

void WifiJoystick::UpdateValues()
Expand All @@ -123,7 +120,8 @@ void WifiJoystick::UpdateValues()
CRSF_CHANNEL_VALUE_MIN, CRSF_CHANNEL_VALUE_MAX, 0, 0xffff));
udp->write((uint8_t*)&channel, 2);
}
udp->endPacket();

active = udp->endPacket() != 0;
}

#endif
16 changes: 12 additions & 4 deletions src/lib/WIFI/wifiJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Usage for simulator or driver on PC:
*
* Step 1: Find device. There are two options:
* Recommended: Use MDNS to query for a service called elrs-joystick to find out IP+Port, Protocol version can be found in service txt version
* Recommended: Use MDNS to query for a UDP service called elrs to find out IP+Port, Protocol version can be found in service txt version
* Alternative: Listen for UDP broadcasts that contains a frame in the structure of
* 4 bytes: ['E', 'L', 'R', 'S']
* 1 byte: Protocol Version
Expand All @@ -29,17 +29,24 @@
* Start udp socket recvfrom(IP, PORT) discovered in Step 1
*
* Step 3:
* Send HTTP GET request to device in the form of http://<IP>/wifi_joystick?start=1&updateInterval=10000&channels=8
* updateInterval is in us
* channels defines how many channels are sent in each frame
* Send HTTP POST request to device URL http://<IP>/udpcontrol
* Param: "action" must be "joystick_begin"
* Param: "interval" in us to send updates, or 0 for default (10ms)
* Param: "channels" number of channels to send in each frame, or 0 for default (8)
* e.g. http://<IP>/udpcontrol?action=joystick_begin&interval=10000&channels=8
*
* Step 3:
* receive frames in the format of:
* 1 byte: Frame type (WifiJoystickFrameType_e)CHANNELS
* 1 byte: Number of channels that follow
* 2 bytes unsigned * channel count: Channel data in range 0 to 0xffff, network byte order
*
* Step 4:
* To end joystick data being sent, POST to the control URL
* Param: "action" must be "joystick_end"
* e.g http://<IP>/udpcontrol?action=joystick_end
*/

class WifiJoystick
{
public:
Expand All @@ -50,6 +57,7 @@ class WifiJoystick
static void StopJoystickService();
static void UpdateValues();
static void StartSending(const IPAddress& ip, int32_t updateInterval, uint8_t newChannelCount);
static void StopSending() { active = false; }
static void Loop(unsigned long now);
private:
static WiFiUDP *udp;
Expand Down

0 comments on commit 5d453ce

Please sign in to comment.