From f2f9019d5c1915adf84ae30b40b5719d969c7574 Mon Sep 17 00:00:00 2001
From: sawinglogz <3787776-sawinglogz@users.noreply.gitlab.com>
Date: Sun, 29 Mar 2020 21:50:19 -0400
Subject: [PATCH] Fix pressure display on welcome page for PRS1
 non-ventilators.

---
 oscar/SleepLib/day.cpp | 26 +++++++++++++++-----------
 oscar/welcome.cpp      | 18 ++++++++++++++++--
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/oscar/SleepLib/day.cpp b/oscar/SleepLib/day.cpp
index fcc90d9f..bb8724df 100644
--- a/oscar/SleepLib/day.cpp
+++ b/oscar/SleepLib/day.cpp
@@ -809,19 +809,23 @@ ChannelID Day::getPressureChannelID() {
     // See the comment in getCPAPModeStr().
     // Determined the preferred pressure channel (CPAP_IPAP or CPAP_Pressure)
     CPAPMode cpapmode = (CPAPMode)(int)settings_max(CPAP_Mode);
-    ChannelID preferredID = CPAP_Pressure;
-    if (cpapmode == MODE_ASV || cpapmode == MODE_ASV_VARIABLE_EPAP || cpapmode == MODE_AVAPS)
-        preferredID = CPAP_IPAP;
 
-    // If preferred channel has data, return it
-    if (channelHasData(preferredID))
-        return preferredID;
+    // TODO: This is also an awful hack. Why would CPAP/APAP have IPAP channels but not Pressure channels?
+    // And why would ASV or AVAPS have Pressure channels?
+    QList<ChannelID> preferredIDs = { CPAP_Pressure, CPAP_PressureSet, CPAP_IPAP, CPAP_IPAPSet };
+    if (cpapmode == MODE_ASV || cpapmode == MODE_ASV_VARIABLE_EPAP || cpapmode == MODE_AVAPS) {
+        preferredIDs = { CPAP_IPAP, CPAP_IPAPSet, CPAP_Pressure, CPAP_PressureSet };
+    }
 
-    // Otherwise return the other pressure channel
-    if (preferredID == CPAP_IPAP)
-        return CPAP_Pressure;
-    else
-        return CPAP_IPAP;
+    for (auto & preferredID : preferredIDs) {
+        // If preferred channel has data, return it
+        if (channelHasData(preferredID)) {
+            //qDebug() << QString("Found pressure channel 0x%1").arg(preferredID, 4, 16, QChar('0'));
+            return preferredID;
+        }
+    }
+    
+    return NoChannel;
 }
 
 bool Day::hasEnabledSessions()
diff --git a/oscar/welcome.cpp b/oscar/welcome.cpp
index d1559132..754d2367 100644
--- a/oscar/welcome.cpp
+++ b/oscar/welcome.cpp
@@ -230,14 +230,28 @@ QString Welcome::GenerateCPAPHTML()
             ChannelID pressChanID = day->getPressureChannelID();     // Get channel id for pressure that we should report
             double perc= p_profile->general->prefCalcPercentile();
 
+            // When CPAP_PressureSet and CPAP_IPAPSet have data (used for percentiles, etc.)
+            // CPAP_Pressure and CPAP_IPAP are their corresponding settings channels.
+            ChannelID pressSettingChanID;
+            if (pressChanID == CPAP_PressureSet) {
+                pressSettingChanID = CPAP_Pressure;
+            } else if (pressChanID == CPAP_IPAPSet) {
+                pressSettingChanID = CPAP_IPAP;
+            } else {
+                pressSettingChanID = pressChanID;
+            }
+
+            if (pressChanID == NoChannel) {
+                qWarning() << "Unable to find pressure channel for welcome summary!";
+            }
             if (cpapmode == MODE_CPAP) {
-                EventDataType pressure = day->settings_max(pressChanID);
+                EventDataType pressure = day->settings_max(pressSettingChanID);
                 html += tr("Your CPAP machine used a constant %1 %2 of air").arg(pressure).arg(schema::channel[pressChanID].units());
             } else if (cpapmode == MODE_APAP) {
                 EventDataType pressure = day->percentile(pressChanID, perc/100.0);
                 html += tr("Your pressure was under %1 %2 for %3% of the time.").arg(pressure).arg(schema::channel[pressChanID].units()).arg(perc);
             } else if (cpapmode == MODE_BILEVEL_FIXED) {
-                EventDataType ipap = day->settings_max(pressChanID);
+                EventDataType ipap = day->settings_max(pressSettingChanID);
                 EventDataType epap = day->settings_min(CPAP_EPAP);
                 html += tr("Your machine used a constant %1-%2 %3 of air.").arg(epap).arg(ipap).arg(schema::channel[pressChanID].units());
             } else if (cpapmode == MODE_BILEVEL_AUTO_FIXED_PS) {