From 5d453ce20f01fb1a01d949ec49eae8ed16fa563a Mon Sep 17 00:00:00 2001 From: Bryan Mayland Date: Tue, 10 Oct 2023 13:52:32 -0400 Subject: [PATCH] Convert to more generic UDP service --- src/lib/WIFI/devWIFI.cpp | 17 +++++++++++------ src/lib/WIFI/wifiJoystick.cpp | 12 +++++------- src/lib/WIFI/wifiJoystick.h | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/lib/WIFI/devWIFI.cpp b/src/lib/WIFI/devWIFI.cpp index 2ac062e2fd..f7f2565893 100644 --- a/src/lib/WIFI/devWIFI.cpp +++ b/src/lib/WIFI/devWIFI.cpp @@ -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 @@ -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 } @@ -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)); diff --git a/src/lib/WIFI/wifiJoystick.cpp b/src/lib/WIFI/wifiJoystick.cpp index 21941fd27b..7acab75bfc 100644 --- a/src/lib/WIFI/wifiJoystick.cpp +++ b/src/lib/WIFI/wifiJoystick.cpp @@ -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) @@ -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() @@ -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 diff --git a/src/lib/WIFI/wifiJoystick.h b/src/lib/WIFI/wifiJoystick.h index 4bfe42084f..78b3273e54 100644 --- a/src/lib/WIFI/wifiJoystick.h +++ b/src/lib/WIFI/wifiJoystick.h @@ -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 @@ -29,9 +29,11 @@ * Start udp socket recvfrom(IP, PORT) discovered in Step 1 * * Step 3: - * Send HTTP GET request to device in the form of http:///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:///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:///udpcontrol?action=joystick_begin&interval=10000&channels=8 * * Step 3: * receive frames in the format of: @@ -39,7 +41,12 @@ * 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:///udpcontrol?action=joystick_end */ + class WifiJoystick { public: @@ -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;