Skip to content

Commit

Permalink
Merge pull request #60 from ExpressLRS/adds-hdzero-vrx-backpack
Browse files Browse the repository at this point in the history
Adds HDZero VRX backpack targets
  • Loading branch information
JyeSmith authored Feb 23, 2022
2 parents 9ab3152 + 907f7e7 commit 97a93d1
Show file tree
Hide file tree
Showing 18 changed files with 387 additions and 57 deletions.
23 changes: 23 additions & 0 deletions lib/MSP/msp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,26 @@ MSP::getTotalPacketSize(mspPacket_t* packet)

return totalSize;
}

bool
MSP::awaitPacket(mspPacket_t* packet, Stream* port, uint32_t timeoutMillis)
{
uint32_t requestTime = millis();

sendPacket(packet, port);

// wait up to <timeoutMillis> milliseconds for a response, then bail out
while(millis() - requestTime < timeoutMillis)
{
while (port->available())
{
uint8_t data = port->read();
if (processReceivedByte(data))
{
return true;
}
}
}
DBGLN("MSP::awaitPacket Exceeded timeout while waiting for packet");
return false;
}
1 change: 1 addition & 0 deletions lib/MSP/msp.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class MSP
bool sendPacket(mspPacket_t* packet, Stream* port);
uint8_t convertToByteArray(mspPacket_t* packet, uint8_t* byteArray);
uint8_t getTotalPacketSize(mspPacket_t* packet);
bool awaitPacket(mspPacket_t* packet, Stream* port, uint32_t timeoutMillis);

private:
mspState_e m_inputState;
Expand Down
16 changes: 16 additions & 0 deletions lib/MSP/msptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@
// CRSF encapsulated msp defines
#define ENCAPSULATED_MSP_PAYLOAD_SIZE 4
#define ENCAPSULATED_MSP_FRAME_LEN 8

// ELRS backpack protocol opcodes
// See: https://docs.google.com/document/d/1u3c7OTiO4sFL2snI-hIo-uRSLfgBK4h16UrbA08Pd6U/edit#heading=h.1xw7en7jmvsj
#define MSP_ELRS_BACKPACK_GET_CHANNEL_INDEX 0x0300
#define MSP_ELRS_BACKPACK_SET_CHANNEL_INDEX 0x0301
#define MSP_ELRS_BACKPACK_GET_FREQUENCY 0x0302
#define MSP_ELRS_BACKPACK_SET_FREQUENCY 0x0303
#define MSP_ELRS_BACKPACK_GET_RECORDING_STATE 0x0304
#define MSP_ELRS_BACKPACK_SET_RECORDING_STATE 0x0305
#define MSP_ELRS_BACKPACK_GET_VRX_MODE 0x0306
#define MSP_ELRS_BACKPACK_SET_VRX_MODE 0x0307
#define MSP_ELRS_BACKPACK_GET_RSSI 0x0308
#define MSP_ELRS_BACKPACK_GET_BATTERY_VOLTAGE 0x0309
#define MSP_ELRS_BACKPACK_GET_FIRMWARE 0x030A
#define MSP_ELRS_BACKPACK_SET_BUZZER 0x030B
#define MSP_ELRS_BACKPACK_SET_OSD_ELEMENT 0x030C
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <Updater.h>
#include <FS.h>

Expand Down
23 changes: 20 additions & 3 deletions lib/WIFI/UpdateWrapper.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#ifdef NAMIMNO_TX_BACKPACK
#include "stmUpdateClass.h"
#endif
Expand All @@ -8,14 +10,21 @@ class UpdateWrapper {
_stmMode = stmMode;
}

#ifdef PLATFORM_ESP8266
bool begin(size_t size) {
_running = true;
#ifdef NAMIMNO_TX_BACKPACK
return Update.begin(size, U_FLASH);
}
#elif NAMIMNO_TX_BACKPACK
bool begin(size_t size) {
if (_stmMode)
return STMUpdate.begin(0); // we don't know the size!
#endif
return Update.begin(size, U_FLASH);
}
#elif PLATFORM_ESP32
bool begin() {
return Update.begin();
}
#endif

size_t write(uint8_t *data, size_t len) {
#ifdef NAMIMNO_TX_BACKPACK
Expand Down Expand Up @@ -51,13 +60,21 @@ class UpdateWrapper {
}

void runAsync(bool async) {
#ifdef PLATFORM_ESP8266
if (!_stmMode) Update.runAsync(async);
#endif
}

bool isRunning() {
return _running;
}

#ifdef PLATFORM_ESP32
void abort() {
if (!_stmMode) Update.abort();
}
#endif

private:
bool _stmMode = false;
bool _running = false;
Expand Down
67 changes: 44 additions & 23 deletions lib/WIFI/devWIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@

#if defined(PLATFORM_ESP8266) || defined(PLATFORM_ESP32)

#if defined(PLATFORM_ESP32)
#include <WiFi.h>
#include <ESPmDNS.h>
#include <Update.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#define wifi_mode_t WiFiMode_t

#endif
#include <DNSServer.h>
#include <ESPAsyncWebServer.h>

#include <set>
#include <StreamString.h>

#include <ESPAsyncWebServer.h>

#include "common.h"
#include "logging.h"
#include "options.h"
Expand Down Expand Up @@ -345,7 +351,7 @@ static void WebUploadDataHandler(AsyncWebServerRequest *request, const String& f
updater.printError(Serial);
}
#else
if (!updater->begin()) { //start with max available size
if (!updater.begin()) { //start with max available size
updater.printError(Serial);
}
#endif
Expand Down Expand Up @@ -390,7 +396,7 @@ static void WebUploadForceUpdateHandler(AsyncWebServerRequest *request) {
WebUploadResponseHandler(request);
} else {
#if defined(PLATFORM_ESP32)
updater->abort();
updater.abort();
#endif
request->send(200, "application/json", "{\"status\": \"ok\", \"msg\": \"Update cancelled\"}");
}
Expand Down Expand Up @@ -452,26 +458,41 @@ static void startMDNS()

String instance = String(myHostname) + "_" + WiFi.macAddress();
instance.replace(":", "");
// We have to do it differently on ESP8266 as setInstanceName has the side-effect of chainging the hostname!
MDNS.setInstanceName(myHostname);
MDNSResponder::hMDNSService service = MDNS.addService(instance.c_str(), "http", "tcp", 80);
MDNS.addServiceTxt(service, "vendor", "elrs");
MDNS.addServiceTxt(service, "target", (const char *)&target_name[4]);
MDNS.addServiceTxt(service, "version", VERSION);
MDNS.addServiceTxt(service, "options", String(FPSTR(compile_options)).c_str());
#if defined(TARGET_VRX_BACKPACK)
MDNS.addServiceTxt(service, "type", "vrx");
#elif defined(TARGET_TX_BACKPACK)
MDNS.addServiceTxt(service, "type", "txbp");
#ifdef PLATFORM_ESP8266
// We have to do it differently on ESP8266 as setInstanceName has the side-effect of chainging the hostname!
MDNS.setInstanceName(myHostname);
MDNSResponder::hMDNSService service = MDNS.addService(instance.c_str(), "http", "tcp", 80);
MDNS.addServiceTxt(service, "vendor", "elrs");
MDNS.addServiceTxt(service, "target", (const char *)&target_name[4]);
MDNS.addServiceTxt(service, "version", VERSION);
MDNS.addServiceTxt(service, "options", String(FPSTR(compile_options)).c_str());
#if defined(TARGET_VRX_BACKPACK)
MDNS.addServiceTxt(service, "type", "vrx");
#elif defined(TARGET_TX_BACKPACK)
MDNS.addServiceTxt(service, "type", "txbp");
#endif
// If the probe result fails because there is another device on the network with the same name
// use our unique instance name as the hostname. A better way to do this would be to use
// MDNSResponder::indexDomain and change wifi_hostname as well.
MDNS.setHostProbeResultCallback([instance](const char* p_pcDomainName, bool p_bProbeResult) {
if (!p_bProbeResult) {
WiFi.hostname(instance);
MDNS.setInstanceName(instance);
}
});
#else
MDNS.setInstanceName(instance);
MDNS.addService("http", "tcp", 80);
MDNS.addServiceTxt("http", "tcp", "vendor", "elrs");
MDNS.addServiceTxt("http", "tcp", "target", (const char *)&target_name[4]);
MDNS.addServiceTxt("http", "tcp", "version", VERSION);
MDNS.addServiceTxt("http", "tcp", "options", String(FPSTR(compile_options)).c_str());
#if defined(TARGET_VRX_BACKPACK)
MDNS.addServiceTxt("http", "tcp", "type", "vrx");
#elif defined(TARGET_TX_BACKPACK)
MDNS.addServiceTxt("http", "tcp", "type", "txbp");
#endif
#endif
// If the probe result fails because there is another device on the network with the same name
// use our unique instance name as the hostname.
MDNS.setHostProbeResultCallback([instance](const char* p_pcDomainName, bool p_bProbeResult) {
if (!p_bProbeResult) {
WiFi.hostname(instance);
MDNS.setInstanceName(instance);
}
});
}

static void startServices()
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ extra_configs =
targets/betafpv_tx.ini
targets/dupletx_tx.ini
targets/radiomaster_tx.ini
targets/hdzero.ini
2 changes: 2 additions & 0 deletions src/Tx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ void ProcessMSPPacketFromTX(mspPacket_t *packet)
RebootIntoWifi();
break;
default:
// transparently forward MSP packets via espnow to any subscribers
sendMSPViaEspnow(packet);
break;
}
}
Expand Down
Loading

0 comments on commit 97a93d1

Please sign in to comment.