Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Tx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "options.h"
#include "helpers.h"

#include "time.h"
#include <sys/time.h>

#include "device.h"
#include "devWIFI.h"
#include "devButton.h"
Expand All @@ -39,6 +42,7 @@ unsigned long rebootTime = 0;

bool cacheFull = false;
bool sendCached = false;
bool sendRTC = false;

device_t *ui_devices[] = {
#ifdef PIN_LED
Expand Down Expand Up @@ -98,6 +102,8 @@ void ProcessMSPPacketFromPeer(mspPacket_t *packet)
{
sendCached = true;
}
// the vrx-backpack just booted, so send it the time as well
sendRTC = true;
break;
}
case MSP_ELRS_BACKPACK_SET_PTR: {
Expand Down Expand Up @@ -240,6 +246,25 @@ void ProcessMSPPacketFromTX(mspPacket_t *packet)
HandleConfigMsg(packet);
break;

case MSP_ELRS_BACKPACK_SET_RTC:
DBGLN("Processing MSP_ELRS_BACKPACK_SET_RTC...");
// Seed our local clock from the payload, then forward the
// time on to the VRX backpack
if (packet->payloadSize >= 6)
{
tm timeData = {};
timeData.tm_year = packet->payload[0];
timeData.tm_mon = packet->payload[1];
timeData.tm_mday = packet->payload[2];
timeData.tm_hour = packet->payload[3];
timeData.tm_min = packet->payload[4];
timeData.tm_sec = packet->payload[5];
timeval tv = {mktime(&timeData), 0};
settimeofday(&tv, NULL);
sendMSPViaEspnow(packet);
}
break;

case MSP_ELRS_BIND:
DBG("MSP_ELRS_BIND = ");
for (int i = 0; i < 6; i++)
Expand Down Expand Up @@ -307,6 +332,31 @@ void sendMSPViaWiFiUDP(mspPacket_t *packet)
SendTxBackpackTelemetryViaUDP(dataOutput, packetSize);
}

void SendRTCViaEspnow()
{
time_t now = time(NULL);
if (now < 1704067200) // 1 Jan 2024 - the local clock has not been set
{
return;
}

tm timeData;
localtime_r(&now, &timeData);

mspPacket_t packet;
packet.reset();
packet.makeCommand();
packet.function = MSP_ELRS_BACKPACK_SET_RTC;
packet.addByte(timeData.tm_year);
packet.addByte(timeData.tm_mon);
packet.addByte(timeData.tm_mday);
packet.addByte(timeData.tm_hour);
packet.addByte(timeData.tm_min);
packet.addByte(timeData.tm_sec);

sendMSPViaEspnow(&packet);
}

void SendCachedMSP()
{
if (!cacheFull)
Expand Down Expand Up @@ -488,4 +538,11 @@ void loop()
SendCachedMSP();
sendCached = false;
}

// Send the time to the VRX backpack when it requests it at boot
if (connectionState == running && sendRTC)
{
sendRTC = false;
SendRTCViaEspnow();
}
}
31 changes: 26 additions & 5 deletions src/Vrx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "config.h"
#include "crsf_protocol.h"

#include "time.h"
#include <sys/time.h>

#include "device.h"
#include "devWIFI.h"
#include "devButton.h"
Expand Down Expand Up @@ -245,6 +248,24 @@ void ProcessMSPPacket(mspPacket_t *packet)
DBGLN("Processing MSP_ELRS_SET_OSD...");
vrxModule.SetOSD(packet);
break;
case MSP_ELRS_BACKPACK_SET_RTC:
DBGLN("Processing MSP_ELRS_BACKPACK_SET_RTC...");
if (packet->payloadSize >= 6)
{
// Set our local clock from the payload, then flag the time
// to be sent on to the goggles from the main loop
tm timeData = {};
timeData.tm_year = packet->readByte();
timeData.tm_mon = packet->readByte();
timeData.tm_mday = packet->readByte();
timeData.tm_hour = packet->readByte();
timeData.tm_min = packet->readByte();
timeData.tm_sec = packet->readByte();
timeval tv = {mktime(&timeData), 0};
settimeofday(&tv, NULL);
sendRTCChangesToVrx = true;
}
break;
case MSP_ELRS_BACKPACK_SET_HEAD_TRACKING:
DBGLN("Processing MSP_ELRS_BACKPACK_SET_HEAD_TRACKING...");
headTrackingEnabled = packet->readByte();
Expand Down Expand Up @@ -524,6 +545,11 @@ void loop()
sendHeadTrackingChangesToVrx = false;
vrxModule.SendHeadTrackingEnableCmd(headTrackingEnabled);
}
if (sendRTCChangesToVrx)
{
sendRTCChangesToVrx = false;
vrxModule.SetRTC();
}

#if !defined(NO_AUTOBIND)
// Power cycle must be done within 30s. Long timeout to allow goggles to boot and shutdown correctly e.g. Orqa.
Expand All @@ -536,11 +562,6 @@ void loop()

if (connectionState == wifiUpdate)
{
if (sendRTCChangesToVrx)
{
sendRTCChangesToVrx = false;
vrxModule.SetRTC();
}
return;
}

Expand Down