Read Operating Frequency from IC-7410

freqnow

Now gathering information from IC-7410.

gboolean freq_read( gpointer data ) {
  int outputcount = 6;
  static char command1[6] = {0xfe, 0xfe, 0x80, 0xe0, 0x03, 0xfd}; /* Read Operating Freq */
  unsigned char buf[255];
  int res;
  char string[128];
  double freq;

  write(fd, &command1, outputcount);
  while( (res = read(fd,buf,255)) !=11 ) {
   ;
  }
/* freq data in buf[8]-[5] */
   sprintf(string, "%02x%02x%02x%02x", buf[8],buf[7],buf[6],buf[5]);
   freq = (double) atoi(string) / 1000.0;
   g_print("freq is now %10.3f kHz \n", freq);
  return TRUE; /* MUST return TRUE */
}

I am not checking the response from IC-7410 strictly, just observing if its length is as expected or not.

/* main() */
    g_timeout_add( (guint) 250, (GSourceFunc) freq_read, (gpointer) "test" );

freqdisplay

Getting S-meter level should be likewise. The rest of the problem is how you can generate nice looking displays out of just numbers. (The above figure from grig.)

Radio Buttons (2)

radiofrq

Radio buttons for preset frequencies.

void set_freq (double frq) { /* freq in kHz */
  int outputcount = 11;
  static unsigned char command[11] /* set frequency command */
   = {0xfe, 0xfe, 0x80, 0xe0, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd};
  int ifrq, idigit[8];

  ifrq = frq * 1000; /* freq in Hz */
  for(int i=0; i<8; i++) {
   idigit[i] = ifrq % 10;
   ifrq /= 10;
  }
  command[5] = 16*idigit[1] + idigit[0];
  command[6] = 16*idigit[3] + idigit[2];
  command[7] = 16*idigit[5] + idigit[4];
  command[8] = 16*idigit[7] + idigit[6];
  write(fd, &command1, outputcount);
}

I always wonder if I should use a for loop for the lines like command[5] through [8]. I suppose repeating the lines is more explicit and more readable.

/* main */
    int nfrq = 8;
    char *strfrq[] = {" 3501.000kHz", " 7026.000kHz", "10118.000kHz", "14058.000kHz",
                      "18085.000kHz", "21058.000kHz", "24908.000kHz", "28058.000kHz"};

    for(int i=0;i<nfrq;i++) {
     if(i == 0) {
      button = gtk_radio_button_new_with_label (NULL, strfrq[i]);
     } else {
      button = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (button)), strfrq[i]);
     }
     gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC (callback3), (gpointer) strfrq[i]);
     gtk_box_pack_start (GTK_BOX (box6), button, TRUE, TRUE, 0);
     gtk_widget_show (button);
    }

Radio Buttons

radiobutton

It works, but initially a signal is issued that tells “10 wpm” is toggled. This is strange because the default depressed button is set to “30 wpm”.

void callback2( GtkWidget *widget, gpointer data ) {
 int wpm;
 wpm = atoi( g_strndup((char *) data, 2) );
 set_cw_speed(wpm);
 g_print ("%s was toggled \n", (char *) data);
}

The callback routine is almost the same as before. No need to check the suffix of “wpm” here.

/* main() */
    char *strwpm[] = {"10 wpm", "15 wpm", "20 wpm", "25 wpm", "30 wpm", "35 wpm", "40 wpm"};

    for(int i=0;i<7;i++) {
     if(i == 0) {
      button = gtk_radio_button_new_with_label(NULL, strwpm[i]);
     } else {
      button = gtk_radio_button_new_with_label(gtk_radio_button_group (GTK_RADIO_BUTTON (button)), strwpm[i]);
     }
     if(i == 4) gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
     gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC (callback2), (gpointer) strwpm[i]);
     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
     gtk_widget_show (button);
    }

Initially, the fourth button (30 wpm) is depressed. (Yes, we count from the number zero.)

% ./a.out
10 wpm was toggled <-- before I touch no buttons.

This is not what I expected, but it should not be a big matter, because I will need some initialization for IC-7410 anyway.