From 70c7060eaf71e8fa35d63e9279ef8040727ff2c2 Mon Sep 17 00:00:00 2001 From: Starman0620 <28871190+Starman0620@users.noreply.github.com> Date: Tue, 20 Apr 2021 14:26:48 -0400 Subject: [PATCH] Added frequency display to presence --- discord/src/main.cpp | 51 ++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/discord/src/main.cpp b/discord/src/main.cpp index 1066bdc3..e35035e2 100644 --- a/discord/src/main.cpp +++ b/discord/src/main.cpp @@ -4,7 +4,7 @@ #include #include #include - +#include #include SDRPP_MOD_INFO { @@ -15,7 +15,10 @@ SDRPP_MOD_INFO { /* Max instances */ -1 }; +void ready(const DiscordUser *request); static DiscordRichPresence presence; +static time_t lastUpdate = time(0); +static char* freq = new char[24]; class PresenceModule : public ModuleManager::Instance { public: @@ -23,20 +26,7 @@ public: this->name = name; gui::menu.registerEntry(name, menuHandler, this, this); - // Discord initialization - DiscordEventHandlers handlers; - memset(&handlers, 0, sizeof(handlers)); - memset(&presence, 0, sizeof(presence)); - Discord_Initialize("833485588954742864", &handlers, 1, ""); - - // Set the first presence - presence.state = "Loading..."; - presence.details = "Initializing rich presence..."; - presence.partySize = 1; - presence.partyMax = 1; - presence.startTimestamp = time(0); - presence.largeImageKey = "image_large"; - Discord_UpdatePresence(&presence); + startPresence(); } ~PresenceModule() { @@ -44,10 +34,12 @@ public: } void enable() { + startPresence(); enabled = true; } void disable() { + Discord_ClearPresence(); enabled = false; } @@ -64,12 +56,39 @@ private: // GUI ImGui::BeginGroup(); - ImGui::Text("Connecting to Discord..."); + ImGui::Text("SDR++ Rich Presence by Starman0620"); ImGui::EndGroup(); + + // Very basic method of implenting a 10s timer + if (time(0) - lastUpdate > 10) { + updatePresence(); + lastUpdate = time(0); + } if (!_this->enabled) { style::endDisabled(); } } + static void updatePresence() { + presence.details = "Listening"; + sprintf(freq, "%.2fMHz", gui::waterfall.getCenterFrequency()/1000000, 3); + presence.state = freq; + Discord_UpdatePresence(&presence); + } + + static void startPresence() { + // Discord initialization + DiscordEventHandlers handlers; + memset(&handlers, 0, sizeof(handlers)); + memset(&presence, 0, sizeof(presence)); + Discord_Initialize("833485588954742864", &handlers, 1, ""); + + // Set the first presence + presence.details = "Initializing rich presence..."; + presence.startTimestamp = time(0); + presence.largeImageKey = "image_large"; + Discord_UpdatePresence(&presence); + } + std::string name; bool enabled = true; };