/[xlog]/xlog/src/hamlib-utils.c
ViewVC logotype

Diff of /xlog/src/hamlib-utils.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by pa4tu, Mon Mar 3 15:34:43 2003 UTC revision 1.6 by pa4tu, Wed May 21 19:53:13 2003 UTC
# Line 27  Line 27 
27  #include "types.h"  #include "types.h"
28  #include "utils.h"  #include "utils.h"
29  #include "support.h"  #include "support.h"
30    #include "string.h"
31    
32  extern preferencestype preferences;  extern preferencestype preferences;
33  extern statetype state;  extern statetype state;
34  extern GtkWidget *mainwindow;  extern GtkWidget *mainwindow;
 extern GList *riglist;  
 extern RIG *myrig;  
35  GList *rigs = NULL;  GList *rigs = NULL;
36  gint svalue[10];  gint svalue[10];
37    GList *riglist = NULL;
38    gint hamlibtimer = -1;
39    RIG *myrig;
40    
41    /* stop timer if used and close rig */
42    void stop_hamlib (void)
43    {
44            if (hamlibtimer != -1) g_source_remove (hamlibtimer);
45      hamlibtimer = -1;
46            rig_close ((RIG *) myrig);
47            rig_cleanup ((RIG *) myrig);
48    }
49    
50    /* open rig and report errors, start timer when polling */
51    gboolean start_hamlib (gchar *radio, gchar *device, gint debugmode,
52      gint timervalue)
53    {
54      gint rigid, retcode;
55      gchar *temp;
56    
57      rig_set_debug (debugmode);
58      /* need this to set popdown strings in prefs dialog */
59      riglist = riglist_get_list ();
60      rigid = get_rigid (radio);
61      if (rigid != -1)
62              {
63                myrig = rig_init (rigid);
64                strncpy (myrig->state.rigport.pathname, device, FILPATHLEN);
65                retcode = rig_open (myrig);
66                if (retcode != RIG_OK)
67                {
68                  temp = g_strdup_printf (_("An error occured while opening port %s: %s"),
69                                      myrig->state.rigport.pathname, rigerror (retcode));
70                  update_statusbar (temp);
71            return FALSE;
72                }
73                else
74                {
75            if (timervalue == 0)
76              hamlibtimer = -1;
77            else
78                    hamlibtimer =   gtk_timeout_add (timervalue,
79                (GtkFunction) poll_riginfo, NULL);
80                }
81        }
82      else
83        {
84                update_statusbar (_("Hamlib error: get_rigid failed"));
85          return FALSE;
86        }
87      return TRUE;
88    }
89    
90  /* Append a new entry in the driver list. It is called by rig_list_foreach */  /* Append a new entry in the driver list. It is called by rig_list_foreach */
91  static gint  static gint
# Line 133  getrigmode (gint mode) Line 184  getrigmode (gint mode)
184   * when this function is called. This is needed to let hamlib recover from errors.   * when this function is called. This is needed to let hamlib recover from errors.
185   */   */
186  gint  gint
187  get_riginfo (void)  poll_riginfo (void)
188  {  {
189    gint retcode, strength, spoint, i, status, smax = 1;    gint retcode, strength, spoint, i, status, smax = 1;
190    freq_t freq;    freq_t freq;
# Line 145  get_riginfo (void) Line 196  get_riginfo (void)
196    GtkWidget *frequencylabel;    GtkWidget *frequencylabel;
197    gchar *message;    gchar *message;
198    
199      /* get freq */
200    if (state.hlcounter == 0)    if (state.hlcounter == 0)
201      {      {
202        retcode = rig_get_freq (myrig, RIG_VFO_CURR, &freq);        retcode = rig_get_freq (myrig, RIG_VFO_CURR, &freq);
203        state.hlcounter++;        state.hlcounter++;
204        if (retcode == RIG_OK)        if (retcode == RIG_OK)
205                {
206                  state.rigfrequency = freq;
207                  if ((preferences.hamlib == 2) || (preferences.hamlib == 4))
208                    {
209                      if (preferences.round == 0)
210                              g_string_printf (digits, "%Ld", freq);
211                      else
212                              g_string_printf (digits, "%Ld", (long long) rintl (freq /
213                                                         pow (10, preferences. round)));
214                      g_string_insert_c (digits, (digits->len) - 6 + preferences.round,
215                  '.');
216                      g_strstrip (digits->str);
217                      g_strdelimit (digits->str, " ", '0');
218                      digits = g_string_append (digits, " MHz");
219                      frequencylabel = lookup_widget (mainwindow, "frequencylabel");
220                      gtk_label_set_text (GTK_LABEL (frequencylabel), digits->str);
221                      g_string_free (digits, TRUE);
222                    }
223                }
224          else if (retcode != -RIG_ENAVAIL)
225                {
226                  message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
227                                rigerror (retcode));
228                  update_statusbar (message);
229                  g_free (message);
230                  return 1;
231                }
232        }
233    
234      /* get mode */
235      if (state.hlcounter == 1)
236        {
237          retcode = rig_get_mode (myrig, RIG_VFO_CURR, &rmode, &width);
238          state.hlcounter++;
239          if (retcode == RIG_OK)
240                {
241                  state.rigmode = rmode;
242                }
243          else if (retcode != -RIG_ENAVAIL)
244                {
245                  message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
246                                rigerror (retcode));
247                  update_statusbar (message);
248                  g_free (message);
249                  return 1;
250                }
251        }
252    
253      /* are we transmitting or receiving ? */
254      if (state.hlcounter == 2)
255        {
256          retcode = rig_get_ptt (myrig, RIG_VFO_CURR, &ptt);
257          state.hlcounter++;
258          if (retcode == RIG_OK)
259                {
260                  if (ptt == RIG_PTT_OFF)
261                    state.tx = FALSE;
262                  else
263                    state.tx = TRUE;
264                }
265          else if (retcode != -RIG_ENAVAIL)
266                {
267                  message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
268                                rigerror (retcode));
269                  update_statusbar (message);
270                  g_free (message);
271                  return 1;
272                }
273        }
274    
275      /* get s-meter value */
276      if (state.hlcounter == 3)
277        {
278          if (!state.tx)
279                {
280                  retcode = rig_get_strength (myrig, RIG_VFO_CURR, &strength);
281                  state.hlcounter++;
282                  if (retcode == RIG_OK)
283                  {
284                    if (strength >= 0)
285                            spoint = 9;
286                    else if (strength < -60)
287                            spoint = 0;
288                    else
289                            spoint = (gint) ceil ((strength + 60) / 6);
290                    svalue[state.scounter] = spoint;
291                    state.scounter++;
292                    if (state.scounter == 10)
293                            state.scounter = 0;
294    
295                  /* find maximum of s-meter during last 3 seconds */
296                    for (i = 0; i < 10; i++)
297                          if (svalue[i] > smax) smax = svalue[i];
298    
299                    if ((preferences.hamlib == 3) || (preferences.hamlib == 4))
300                            draw_smeter (smax);
301    
302                    if (smax == 0) smax = 1;
303                    if ((state.rigmode == RIG_MODE_CW)
304                || (state.rigmode == RIG_MODE_RTTY))
305                            state.rigrst = g_strdup_printf ("5%d9", smax);
306                    else
307                            state.rigrst = g_strdup_printf ("5%d", smax);
308                  }
309                  else if (retcode != -RIG_ENAVAIL)
310                  {
311                    message =       g_strdup_printf (_("Hamlib error %d: %s"), retcode,
312                                      rigerror (retcode));
313                    update_statusbar (message);
314                    g_free (message);
315                    return 1;
316                  }
317                }
318          else
319                {
320                  state.hlcounter++;
321                  if ((preferences.hamlib == 3) || (preferences.hamlib == 4))
322                    draw_smeter (0);
323                }
324        }
325    
326      /* get power level */
327      if (state.hlcounter == 4)
328        {
329          state.hlcounter = 0;
330          retcode = rig_get_level (myrig, RIG_VFO_CURR, RIG_LEVEL_RFPOWER, &val);
331          if (retcode == RIG_OK)
332                {
333                  status = rig_power2mW (myrig, &state.rigpower, val.f,
334              state.rigfrequency, state.rigmode);
335                }
336          else if (retcode != -RIG_ENAVAIL)
337                {
338                  message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
339                                rigerror (retcode));
340                  update_statusbar (message);
341                  g_free (message);
342                  return 1;
343                }
344        }
345      return 1;
346    }
347    
348    gint
349    get_riginfo (void)
350    {
351      gint retcode, strength, spoint, status;
352      freq_t freq;
353      rmode_t rmode;
354      pbwidth_t width;
355      ptt_t ptt;
356      value_t val;
357      GString *digits = g_string_new ("");
358      GtkWidget *frequencylabel;
359      gchar *message;
360    
361      /* get frequency */
362      retcode = rig_get_freq (myrig, RIG_VFO_CURR, &freq);
363      if (retcode == RIG_OK)
364          {          {
365            state.rigfrequency = freq;            state.rigfrequency = freq;
366            if ((preferences.hamlib == 2) || (preferences.hamlib == 4))            if ((preferences.hamlib == 2) || (preferences.hamlib == 4))
367              {              {
368                if (preferences.round == 0)                if (preferences.round == 0)
369                  g_string_printf (digits, "%Ld", freq);                        g_string_printf (digits, "%Ld", freq);
370                else                else
371                  g_string_printf (digits, "%Ld",                        g_string_printf (digits, "%Ld", (long long) rintl (freq /
372                                    (long long) rintl (freq /                                                  pow (10, preferences. round)));
373                                                       pow (10,                g_string_insert_c (digits, (digits->len) - 6 + preferences.round, '.');
                                                           preferences.  
                                                           round)));  
               g_string_insert_c (digits,  
                                  (digits->len) - 6 + preferences.round, '.');  
374                g_strstrip (digits->str);                g_strstrip (digits->str);
375                g_strdelimit (digits->str, " ", '0');                g_strdelimit (digits->str, " ", '0');
376                digits = g_string_append (digits, " MHz");                digits = g_string_append (digits, " MHz");
# Line 172  get_riginfo (void) Line 379  get_riginfo (void)
379                g_string_free (digits, TRUE);                g_string_free (digits, TRUE);
380              }              }
381          }          }
382        else if (retcode != -RIG_ENAVAIL)    else if (retcode != -RIG_ENAVAIL)
383          {          {
384            message =            message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
385              g_strdup_printf (_("Hamlib error %d: %s"), retcode,        rigerror (retcode));
                              rigerror (retcode));  
386            update_statusbar (message);            update_statusbar (message);
387            g_free (message);            g_free (message);
388            return 1;            return 1;
389          }          }
     }  
390    
391    if (state.hlcounter == 1)    /* get mode */
392      {    retcode = rig_get_mode (myrig, RIG_VFO_CURR, &rmode, &width);
393        retcode = rig_get_mode (myrig, RIG_VFO_CURR, &rmode, &width);    if (retcode == RIG_OK)
       state.hlcounter++;  
       if (retcode == RIG_OK)  
394          {          {
395            state.rigmode = rmode;            state.rigmode = rmode;
396          }          }
397        else if (retcode != -RIG_ENAVAIL)    else if (retcode != -RIG_ENAVAIL)
398          {          {
399            message =            message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
400              g_strdup_printf (_("Hamlib error %d: %s"), retcode,        rigerror (retcode));
                              rigerror (retcode));  
401            update_statusbar (message);            update_statusbar (message);
402            g_free (message);            g_free (message);
403            return 1;            return 1;
404          }          }
     }  
405    
406    /* are we transmitting or receiving ? */    /* are we transmitting or receiving ? */
407    if (state.hlcounter == 2)    retcode = rig_get_ptt (myrig, RIG_VFO_CURR, &ptt);
408      {    if (retcode == RIG_OK)
       retcode = rig_get_ptt (myrig, RIG_VFO_CURR, &ptt);  
       state.hlcounter++;  
       if (retcode == RIG_OK)  
409          {          {
410            if (ptt == RIG_PTT_OFF)            if (ptt == RIG_PTT_OFF)
411              state.tx = FALSE;              state.tx = FALSE;
412            else            else
413              state.tx = TRUE;              state.tx = TRUE;
414          }          }
415        else if (retcode != -RIG_ENAVAIL)    else if (retcode != -RIG_ENAVAIL)
416          {          {
417            message =            message =
418              g_strdup_printf (_("Hamlib error %d: %s"), retcode,              g_strdup_printf (_("Hamlib error %d: %s"), retcode, rigerror (retcode));
                              rigerror (retcode));  
419            update_statusbar (message);            update_statusbar (message);
420            g_free (message);            g_free (message);
421            return 1;            return 1;
422          }          }
     }  
423    
424    if (state.hlcounter == 3)    /* get s-meter value if receiving */
425      {    if (!state.tx)
       if (!state.tx)  
426          {          {
427            retcode = rig_get_strength (myrig, RIG_VFO_CURR, &strength);            retcode = rig_get_strength (myrig, RIG_VFO_CURR, &strength);
           state.hlcounter++;  
428            if (retcode == RIG_OK)            if (retcode == RIG_OK)
429              {              {
430                if (strength >= 0)                if (strength >= 0)
431                  spoint = 9;                        spoint = 9;
432                else if (strength < -60)                else if (strength < -60)
433                  spoint = 0;                        spoint = 0;
434                else                else
435                  spoint = (gint) ceil ((strength + 60) / 6);                        spoint = (gint) ceil ((strength + 60) / 6);
   
               svalue[state.scounter] = spoint;  
               state.scounter++;  
               if (state.scounter == 10)  
                 state.scounter = 0;  
   
               /* find maximum of s-meter during last 3 seconds */  
               for (i = 0; i < 10; i++)  
                 if (svalue[i] > smax)  
                   smax = svalue[i];  
436    
437                if ((preferences.hamlib == 3) || (preferences.hamlib == 4))                if ((preferences.hamlib == 3) || (preferences.hamlib == 4))
438                  draw_smeter (smax);                        draw_smeter (spoint);
439    
440                if (smax == 0)                if ((state.rigmode == RIG_MODE_CW) || (state.rigmode == RIG_MODE_RTTY))
441                  smax = 1;                        state.rigrst = g_strdup_printf ("5%d9", spoint);
               if ((state.rigmode == RIG_MODE_CW)  
                   || (state.rigmode == RIG_MODE_RTTY))  
                 state.rigrst = g_strdup_printf ("5%d9", smax);  
442                else                else
443                  state.rigrst = g_strdup_printf ("5%d", smax);                        state.rigrst = g_strdup_printf ("5%d", spoint);
444              }              }
445            else if (retcode != -RIG_ENAVAIL)            else if (retcode != -RIG_ENAVAIL)
446              {              {
447                message =                message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
448                  g_strdup_printf (_("Hamlib error %d: %s"), retcode,            rigerror (retcode));
                                  rigerror (retcode));  
449                update_statusbar (message);                update_statusbar (message);
450                g_free (message);                g_free (message);
451                return 1;                return 1;
452              }              }
   
         }  
       else  
         {  
           state.hlcounter++;  
           if ((preferences.hamlib == 3) || (preferences.hamlib == 4))  
             draw_smeter (0);  
453          }          }
454      }    else if ((preferences.hamlib == 3) || (preferences.hamlib == 4))
455    if (state.hlcounter == 4)            draw_smeter (0);
456      {  
457        state.hlcounter = 0;    /* get power level */
458        retcode = rig_get_level (myrig, RIG_VFO_CURR, RIG_LEVEL_RFPOWER, &val);    retcode = rig_get_level (myrig, RIG_VFO_CURR, RIG_LEVEL_RFPOWER, &val);
459        if (retcode == RIG_OK)    if (retcode == RIG_OK)
460          {          {
461            status =            status = rig_power2mW (myrig, &state.rigpower, val.f, state.rigfrequency,
             rig_power2mW (myrig, &state.rigpower, val.f, state.rigfrequency,  
462                            state.rigmode);                            state.rigmode);
463          }          }
464        else if (retcode != -RIG_ENAVAIL)    else if (retcode != -RIG_ENAVAIL)
465          {          {
466            message =            message = g_strdup_printf (_("Hamlib error %d: %s"), retcode,
467              g_strdup_printf (_("Hamlib error %d: %s"), retcode,        rigerror (retcode));
                              rigerror (retcode));  
468            update_statusbar (message);            update_statusbar (message);
469            g_free (message);            g_free (message);
470            return 1;            return 1;
471          }          }
472      }  
473    return 1;    return 0;
474  }  }
475    
476  /* set appearance of some widgets dependent on preferences.hamlib */  /* set appearance of some widgets dependent on preferences.hamlib */

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26