From eae3eeab17c882162e1b723596c2847cb08d0605 Mon Sep 17 00:00:00 2001
From: John Melton G0ORX <john.d.melton@googlemail.com>
Date: Thu, 19 Mar 2020 12:25:09 +0000
Subject: [PATCH] Fix sample rate change crash. Display Temperature and PA
 current for Hermes Lite 2.

---
 new_protocol.h  |  2 ++
 old_discovery.c |  1 +
 old_protocol.c  | 27 +++++++++++++++++++++++----
 radio.c         | 13 +++++++++++++
 radio.h         |  6 ++++++
 receiver.c      | 12 ++++++------
 rx_panadapter.c | 38 ++++++++++++++++++++------------------
 tx_panadapter.c | 22 +++++++++++++++++++---
 8 files changed, 90 insertions(+), 31 deletions(-)

diff --git a/new_protocol.h b/new_protocol.h
index bdc6ba2..2300386 100644
--- a/new_protocol.h
+++ b/new_protocol.h
@@ -62,9 +62,11 @@ extern long response_sequence;
 // DL1YCF: "response" is global (used in new_protocol_programmer.c)
 extern int response;
 
+/*
 extern unsigned int exciter_power;
 extern unsigned int alex_forward_power;
 extern unsigned int alex_reverse_power;
+*/
 
 /*
 extern int send_high_priority;
diff --git a/old_discovery.c b/old_discovery.c
index 9d53a32..cab68f4 100644
--- a/old_discovery.c
+++ b/old_discovery.c
@@ -445,6 +445,7 @@ fprintf(stderr,"discover_receive_thread\n");
 			    } else {
                               strcpy(discovered[devices].name,"HermesLite V2");		
 			      discovered[devices].device = DEVICE_HERMES_LITE2;
+g_print("discovered HL2: Gateware Major Version=%d Minor Version=%d\n",buffer[9],buffer[15]);
 			    }
                             discovered[devices].frequency_min=0.0;
                             discovered[devices].frequency_max=30720000.0;
diff --git a/old_protocol.c b/old_protocol.c
index 2d9e6ab..498ad7d 100644
--- a/old_protocol.c
+++ b/old_protocol.c
@@ -836,7 +836,6 @@ static void process_ozy_input_buffer(unsigned char  *buffer) {
       g_idle_add(ext_mox_update,(gpointer)(long)(local_ptt));
     }
 
-
     switch((control_in[0]>>3)&0x1F) {
       case 0:
         adc_overload=control_in[1]&0x01;
@@ -869,18 +868,38 @@ static void process_ozy_input_buffer(unsigned char  *buffer) {
 	  // HL2 uses C1/C2 for measuring the temperature
 	  //
           exciter_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Penelope or Hermes
+          temperature=0;
         } else {
           exciter_power=0;
+          temperature+=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // HL2
+          n_temperature++;
+          if(n_temperature==10) {
+            average_temperature=temperature/10;
+            temperature=0;
+            n_temperature=0;
+          }
 	} 
         alex_forward_power=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // from Alex or Apollo
         break;
       case 2:
         alex_reverse_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Alex or Apollo
-        AIN3=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
+        if (device != DEVICE_HERMES_LITE2) {
+          AIN3=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // For Penelope or Hermes
+          current=0;
+        } else {
+          AIN3=0;
+          current+=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // HL2
+          n_current++;
+          if(n_current==10) {
+            average_current=current/10;
+            current=0;
+            n_current=0;
+          }
+        }
         break;
       case 3:
-        AIN4=(control_in[1]<<8)+control_in[2]; // from Pennelope or Hermes
-        AIN6=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
+        AIN4=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // For Penelope or Hermes
+        AIN6=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // For Penelope or Hermes
         break;
     }
 
diff --git a/radio.c b/radio.c
index e4536f7..d173d35 100644
--- a/radio.c
+++ b/radio.c
@@ -234,6 +234,12 @@ int dash;
 int adc_overload;
 int pll_locked;
 unsigned int exciter_power;
+unsigned int temperature;
+unsigned int average_temperature;
+unsigned int n_temperature;
+unsigned int current;
+unsigned int average_current;
+unsigned int n_current;
 unsigned int alex_forward_power;
 unsigned int alex_reverse_power;
 unsigned int AIN3;
@@ -928,6 +934,13 @@ void start_radio() {
   display_toolbar=1;
 #endif
 
+  temperature=0;
+  average_temperature=0;
+  n_temperature=0;
+  current=0;
+  average_current=0;
+  n_current=0;
+
   radioRestoreState();
 
 //
diff --git a/radio.h b/radio.h
index 53aa27a..3b4127b 100644
--- a/radio.h
+++ b/radio.h
@@ -222,6 +222,12 @@ extern int full_tune;
 extern int adc_overload;
 extern int pll_locked;
 extern unsigned int exciter_power;
+extern unsigned int temperature;
+extern unsigned int average_temperature;
+extern unsigned int n_temperature;
+extern unsigned int current;
+extern unsigned int average_current;
+extern unsigned int n_current;
 extern unsigned int alex_forward_power;
 extern unsigned int alex_reverse_power;
 extern unsigned int IO1;
diff --git a/receiver.c b/receiver.c
index 5f9b518..4ebe1d2 100644
--- a/receiver.c
+++ b/receiver.c
@@ -590,7 +590,7 @@ void set_displaying(RECEIVER *rx,int state) {
   rx->displaying=state;
   if(state) {
     if(rx->update_timer_id>=0) g_source_remove(rx->update_timer_id);
-    rx->update_timer_id=g_timeout_add_full(G_PRIORITY_HIGH_IDLE,1000/rx->fps, update_display, rx, NULL);
+    rx->update_timer_id=gdk_threads_add_timeout_full(G_PRIORITY_HIGH_IDLE,1000/rx->fps, update_display, rx, NULL);
   } else {
     rx->update_timer_id=-1;
   }
@@ -879,7 +879,6 @@ fprintf(stderr,"create_receiver: id=%d buffer_size=%d fft_size=%d pixels=%d fps=
   rx->id=id;
   g_mutex_init(&rx->mutex);
   g_mutex_init(&rx->display_mutex);
-fprintf(stderr,"create_receiver: g_mutex_init: %p\n",&rx->mutex);
   switch(id) {
     case 0:
       rx->adc=0;
@@ -1117,7 +1116,6 @@ void receiver_change_sample_rate(RECEIVER *rx,int sample_rate) {
 //
 
   g_mutex_lock(&rx->mutex);
-fprintf(stderr,"receiver_change_sample_rate: g_mutex_lock: %p\n",&rx->mutex);
 
   rx->sample_rate=sample_rate;
   int scale=rx->sample_rate/48000;
@@ -1139,7 +1137,6 @@ g_print("receiver_change_sample_rate: id=%d rate=%d scale=%d buffer_size=%d outp
     init_analyzer(rx);
     fprintf(stderr,"PS FEEDBACK change sample rate:id=%d rate=%d buffer_size=%d output_samples=%d\n",
                    rx->id, rx->sample_rate, rx->buffer_size, rx->output_samples);
-fprintf(stderr,"receiver_change_sample_rate: g_mutex_unlock: %p\n",&rx->mutex);
     g_mutex_unlock(&rx->mutex);
     return;
   }
@@ -1163,7 +1160,6 @@ fprintf(stderr,"receiver_change_sample_rate: g_mutex_unlock: %p\n",&rx->mutex);
 
   SetChannelState(rx->id,1,0);
 
-fprintf(stderr,"receiver_change_sample_rate: g_mutex_unlock: %p\n",&rx->mutex);
   g_mutex_unlock(&rx->mutex);
 
 fprintf(stderr,"receiver_change_sample_rate: id=%d rate=%d buffer_size=%d output_samples=%d\n",rx->id, rx->sample_rate, rx->buffer_size, rx->output_samples);
@@ -1337,6 +1333,8 @@ static void process_rx_buffer(RECEIVER *rx) {
 void full_rx_buffer(RECEIVER *rx) {
   int error;
 
+  g_mutex_lock(&rx->mutex);
+
   // noise blanker works on original IQ samples
   if(rx->nb) {
      xanbEXT (rx->id, rx->iq_input_buffer, rx->iq_input_buffer);
@@ -1359,6 +1357,7 @@ void full_rx_buffer(RECEIVER *rx) {
 
 //g_print("full_rx_buffer: rx=%d buffer_size=%d samples=%d\n",rx->id,rx->buffer_size,rx->samples);
   process_rx_buffer(rx);
+  g_mutex_unlock(&rx->mutex);
 }
 
 static int rx_buffer_seen=0;
@@ -1395,7 +1394,7 @@ void receiver_change_zoom(RECEIVER *rx,double zoom) {
   rx->pixel_samples=g_new(float,rx->pixels);
   rx->hz_per_pixel=(double)rx->sample_rate/(double)rx->pixels;
   rx->zoom=(int)zoom;
-  if(rx->zoom==1) {
+  if(zoom==1) {
     rx->pan=0;
   } else {
     if(vfo[rx->id].ctun) {
@@ -1407,6 +1406,7 @@ void receiver_change_zoom(RECEIVER *rx,double zoom) {
       rx->pan=(rx->pixels/2)-(rx->width/2);
     }
   }
+  rx->zoom=(int)zoom;
   init_analyzer(rx);
 }
 
diff --git a/rx_panadapter.c b/rx_panadapter.c
index 682ac77..f6e41f3 100644
--- a/rx_panadapter.c
+++ b/rx_panadapter.c
@@ -116,6 +116,7 @@ void rx_panadapter_update(RECEIVER *rx) {
   int i;
   int x1,x2;
   float *samples;
+  char text[64];
   cairo_text_extents_t extents;
 
   gboolean active=active_receiver==rx;
@@ -528,25 +529,24 @@ void rx_panadapter_update(RECEIVER *rx) {
   cairo_stroke(cr);
 
 #ifdef GPIO
-  if(active && controller==CONTROLLER1) {
-    char text[64];
+  if(rx->id==0 && controller==CONTROLLER1) {
 
     cairo_set_source_rgb(cr,1.0,1.0,0.0);
     cairo_set_font_size(cr,16);
     if(ENABLE_E2_ENCODER) {
-      cairo_move_to(cr, display_width-200,30);
+      cairo_move_to(cr, display_width-200,70);
       sprintf(text,"%s (%s)",encoder_string[e2_encoder_action],sw_string[e2_sw_action]);
       cairo_show_text(cr, text);
     }
 
     if(ENABLE_E3_ENCODER) {
-      cairo_move_to(cr, display_width-200,50);
+      cairo_move_to(cr, display_width-200,90);
       sprintf(text,"%s (%s)",encoder_string[e3_encoder_action],sw_string[e3_sw_action]);
       cairo_show_text(cr, text);
     }
 
     if(ENABLE_E4_ENCODER) {
-      cairo_move_to(cr, display_width-200,70);
+      cairo_move_to(cr, display_width-200,110);
       sprintf(text,"%s (%s)",encoder_string[e4_encoder_action],sw_string[e4_sw_action]);
       cairo_show_text(cr, text);
     }
@@ -554,29 +554,31 @@ void rx_panadapter_update(RECEIVER *rx) {
 #endif
 
   if(sequence_errors!=0) {
-    cairo_move_to(cr,100,20);
+    cairo_move_to(cr,100.0,50.0);
     cairo_set_source_rgb(cr,1.0,0.0,0.0);
     cairo_set_font_size(cr,12);
     cairo_show_text(cr, "Sequence Error");
     sequence_error_count++;
-    // show for 1 second
+    // show for 2 second
     if(sequence_error_count==2*rx->fps) {
       sequence_errors=0;
       sequence_error_count=0;
     }
   }
 
-  if(rx->fexchange_errors!=0) {
-    cairo_move_to(cr,100,30);
-    cairo_set_source_rgb(cr,1.0,0.0,0.0);
-    cairo_set_font_size(cr,12);
-    cairo_show_text(cr, "fexchange Error");
-    fexchange_error_count++;
-    // show for 1 second
-    if(fexchange_error_count==2*rx->fps) {
-      rx->fexchange_errors=0;
-      fexchange_error_count=0;
-    }
+  if(rx->id==0 && protocol==ORIGINAL_PROTOCOL && device==DEVICE_HERMES_LITE2) {
+    cairo_set_source_rgb(cr,1.0,1.0,0.0);
+    cairo_set_font_size(cr,16);
+
+    double t = (3.26 * ((double)average_temperature / 4096.0) - 0.5) / 0.01;
+    sprintf(text,"%0.1fC",t);
+    cairo_move_to(cr, 100.0, 30.0);  
+    cairo_show_text(cr, text);
+
+    double c = (((3.26 * ((double)average_current / 4096.0)) / 50.0) / 0.04 * 1000 * 1270 / 1000);
+    sprintf(text,"%0.0fmA",c);
+    cairo_move_to(cr, 160.0, 30.0);  
+    cairo_show_text(cr, text);
   }
 
   cairo_destroy (cr);
diff --git a/tx_panadapter.c b/tx_panadapter.c
index fe2fa9f..f4b5a67 100644
--- a/tx_panadapter.c
+++ b/tx_panadapter.c
@@ -357,19 +357,19 @@ void tx_panadapter_update(TRANSMITTER *tx) {
     cairo_set_source_rgb(cr,1.0,1.0,0.0);
     cairo_set_font_size(cr,16);
     if(ENABLE_E2_ENCODER) {
-      cairo_move_to(cr, display_width-200,30);
+      cairo_move_to(cr, display_width-200,70);
       sprintf(text,"%s (%s)",encoder_string[e2_encoder_action],sw_string[e2_sw_action]);
       cairo_show_text(cr, text);
     }
 
     if(ENABLE_E3_ENCODER) {
-      cairo_move_to(cr, display_width-200,50);
+      cairo_move_to(cr, display_width-200,90);
       sprintf(text,"%s (%s)",encoder_string[e3_encoder_action],sw_string[e3_sw_action]);
       cairo_show_text(cr, text);
     }
 
     if(ENABLE_E4_ENCODER) {
-      cairo_move_to(cr, display_width-200,70);
+      cairo_move_to(cr, display_width-200,110);
       sprintf(text,"%s (%s)",encoder_string[e4_encoder_action],sw_string[e4_sw_action]);
       cairo_show_text(cr, text);
     }
@@ -427,6 +427,22 @@ void tx_panadapter_update(TRANSMITTER *tx) {
 */
   }
 
+  if(tx->dialog==NULL && protocol==ORIGINAL_PROTOCOL && device==DEVICE_HERMES_LITE2) {
+    char text[64];
+    cairo_set_source_rgb(cr,1.0,1.0,0.0);
+    cairo_set_font_size(cr,16);
+
+    double t = (3.26 * ((double)average_temperature / 4096.0) - 0.5) / 0.01;
+    sprintf(text,"%0.1fC",t);
+    cairo_move_to(cr, 100.0, 30.0);
+    cairo_show_text(cr, text);
+
+    double c = (((3.26 * ((double)average_current / 4096.0)) / 50.0) / 0.04 * 1000 * 1270 / 1000);
+    sprintf(text,"%0.0fmA",c);
+    cairo_move_to(cr, 160.0, 30.0);
+    cairo_show_text(cr, text);
+  }
+
   cairo_destroy (cr);
   gtk_widget_queue_draw (tx->panadapter);
 
-- 
2.45.2