From 834e6be4449eeeb091988204b5aacf241d19d022 Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Mon, 26 Jul 2021 15:18:38 +0200
Subject: [PATCH] Merged-in the Penelope "safety belt" suggested by VK4XV

---
 hpsdrsim.c     |  8 ++++++--
 old_protocol.c | 10 ++++++++++
 radio.c        | 26 +++++++++++++++++++++++++-
 radio_menu.c   |  8 ++++++++
 transmitter.c  | 28 ++++++++++++++++++++++------
 5 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/hpsdrsim.c b/hpsdrsim.c
index e925efa..7f77864 100644
--- a/hpsdrsim.c
+++ b/hpsdrsim.c
@@ -1059,7 +1059,11 @@ void process_ep2(uint8_t *frame)
 
 	case 18:
 	case 19:
-	   chk_data(frame[1],txdrive,"TX DRIVE");
+           if (OLDDEVICE == DEVICE_METIS) {
+             txdrive=255;   // penelope's cannnot adjust TX amplitude 
+           } else {
+	     chk_data(frame[1],txdrive,"TX DRIVE");
+           }
 	   chk_data(frame[2] & 0x3F,hermes_config,"HERMES CONFIG");
 	   chk_data((frame[2] >> 6) & 0x01, alex_manual,"ALEX manual HPF/LPF");
 	   chk_data((frame[2] >> 7) & 0x01, vna     ,"VNA mode");
@@ -1213,7 +1217,7 @@ void *handler_ep6(void *arg)
 	uint8_t header[40] =
 	{
 //                             C0  C1  C2  C3  C4
-		127, 127, 127,  0,  0, 33, 17, 21,
+		127, 127, 127,  0,  0, 33, 18, 21,
 		127, 127, 127,  8,  0,  0,  0,  0,
 		127, 127, 127, 16,  0,  0,  0,  0,
 		127, 127, 127, 24,  0,  0,  0,  0,
diff --git a/old_protocol.c b/old_protocol.c
index bc18040..91c9332 100644
--- a/old_protocol.c
+++ b/old_protocol.c
@@ -872,6 +872,16 @@ static void process_control_bytes() {
           penelope_software_version=control_in[3];
           g_print("  Penelope Software version: %d (0x%0X)\n",penelope_software_version,penelope_software_version);
         }
+        //
+        // Set atlas_penelope flag to "penelope" if three conditions are met:
+        // a) it's a METIS device (no HERMES, ORION, etc.)
+        // b) penelope software version is 18
+        // c) atlas_penelope flag is "unknown"
+        //
+        if (device == DEVICE_METIS && penelope_software_version == 18 && atlas_penelope == 2) {
+          g_print("Adjusted settings for PENELOPE\n");
+	  atlas_penelope=1;
+        }
       }
       //
       //DEBUG code to monitor HL2 TX-FIFO filling and 
diff --git a/radio.c b/radio.c
index 69ca71b..3be79cf 100644
--- a/radio.c
+++ b/radio.c
@@ -150,7 +150,7 @@ int PS_RX_FEEDBACK;
 int buffer_size=1024; // 64, 128, 256, 512, 1024, 2048
 int fft_size=2048; // 1024, 2048, 4096, 8192, 16384
 
-int atlas_penelope=0;
+int atlas_penelope=0;  // 0: no penelope, 1: penelope, 2: unknown
 int atlas_clock_source_10mhz=0;
 int atlas_clock_source_128mhz=0;
 int atlas_config=0;
@@ -686,6 +686,30 @@ void start_radio() {
   protocol=radio->protocol;
   device=radio->device;
 
+  // init atlas_penelop flag
+  atlas_penelope = 0;  // default: no penelope
+  if (protocol == ORIGINAL_PROTOCOL && device == DEVICE_METIS) {
+    //
+    // VK4XV (Bob) suggested
+    //   to have some "protection" for penelope systems which
+    //   start from a virgin props file, namely to auto-detect a penelope
+    //   and set the atlas_penelope flag so that the IQ samples are properly
+    //   scaled (otherwise full PA output power results).
+    //
+    // So my implementation works as follows:
+    //   To guard against "false penelope detects", the atlas_penelope flag
+    //   is initialized with 2 ("unknown").
+    //
+    //   This has no effect if a props file is already there and the flag is
+    //   read from the props file. The value 2 "unknown" can be changed
+    //   to either 0 or 1 in two situations:
+    //     - P1 analyzing the control flags and finding the penelope software version
+    //       is 18: change "unknown" to "penelope"
+    //     - radio menu is opened and penelope checkbox is to be displayed: change
+    //       "unknown" to "no penelope"
+    //
+    atlas_penelope=2;
+  }
   // set the default power output and max drive value
   drive_max=100.0;
   switch(protocol) {
diff --git a/radio_menu.c b/radio_menu.c
index 90d1e01..752e5d2 100644
--- a/radio_menu.c
+++ b/radio_menu.c
@@ -765,6 +765,14 @@ void radio_menu(GtkWidget *parent) {
     g_signal_connect(mic_src_b,"toggled",G_CALLBACK(micsource_cb),NULL);
     row++;
 
+    //
+    // If we arrive here and atlas_penelope is still "unknown", 
+    // set it to "no penelope" (from now on, it may only take
+    // the values 0 and 1).
+    //
+    if (atlas_penelope == 2) {
+      atlas_penelope = 0;
+    }
     GtkWidget *pene_tx_b=gtk_check_button_new_with_label("Penelope TX");
     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (pene_tx_b), atlas_penelope);
     gtk_grid_attach(GTK_GRID(grid),pene_tx_b,col,row,1,1);
diff --git a/transmitter.c b/transmitter.c
index 6d4dcd5..55dfae5 100644
--- a/transmitter.c
+++ b/transmitter.c
@@ -1214,13 +1214,29 @@ static void full_tx_buffer(TRANSMITTER *tx) {
 
     if(  (    (protocol == NEW_PROTOCOL && radio->device==NEW_DEVICE_ATLAS) 
            || (protocol==ORIGINAL_PROTOCOL && radio->device==DEVICE_METIS)
-         ) && atlas_penelope) {
+         ) && atlas_penelope == 1) {
       //
-      // On these boards, drive level changes are performed by
-      // scaling the TX IQ samples. In the other cases, DriveLevel
-      // as sent in the C&C frames becomes effective and the IQ
-      // samples are sent with full amplitude.
-      // DL1YCF: include factor 0.00392 since DriveLevel == 255 means full amplitude
+      // Note that the atlas_penelope flag can have three values, namely
+      //   0 "no penelope"  : no scaling
+      //   1 "penelope"     : scale
+      //   2 "unknown"      : no scaling
+      //
+      // Note "unknown" will be changed to "penelope" if P1 HPSDR packets are
+      // received and the penelope software version is 1.8. However, if it is a
+      // METIS system with a penelope software version different from 1.8, then
+      // the flag will remain in the "unknown" state.
+      //
+      // Note further, in the moment the radio menu is opened, "unknown" will be
+      // changed to "no penelope".
+      //
+      // This is so because some RedPitaya-based HPSDR servers identify as a
+      // METIS with penelope software version = 1.7
+      //
+      // On Penelope boards, the TX drive level as reported by the P1 protocol has
+      // no effect, and TX drive level changes are instead realized by
+      // scaling the TX IQ samples.
+      //
+      // "The magic factor" 0.00392 is slightly less than 1/255.
       //
       if(tune && !tx->tune_use_drive) {
         double fac=sqrt((double)tx->tune_percent * 0.01);
-- 
2.45.2