TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

gdisp_pilotBoard.c

Go to the documentation of this file.
00001 
00043 /*
00044  * System includes.
00045  */
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <assert.h>
00049 #include <string.h>
00050 #include <time.h>
00051 #include <sys/time.h>
00052 
00053 
00054 /*
00055  * GDISP+ includes.
00056  */
00057 #include "gdisp_kernel.h"
00058 #include "gdisp_prototypes.h"
00059 
00060 
00061 /*
00062  --------------------------------------------------------------------
00063                              STATIC ROUTINES
00064  --------------------------------------------------------------------
00065 */
00066 
00067 
00068 /*
00069  * This callback is called whenever play / stop buttons are pressed.
00070  * The argument "data" is the kernel itself.
00071  * The only way to determine which button has been pressed, is to
00072  * compare the argument "widget" (the target button) to both existent
00073  * button pointers stored into the kernel.
00074  */
00075 static void
00076 gdisp_togglePlayModeCallback (GtkWidget *buttonWidget,
00077                               gpointer   data )
00078 {
00079 
00080   Kernel_T *kernel     = (Kernel_T*)data;
00081   gboolean  hasStarted = FALSE;
00082 
00083   if (buttonWidget == kernel->widgets.mainBoardOkButton) {
00084 
00085     hasStarted = gdisp_startSamplingProcess(kernel);
00086 
00087     if (hasStarted == TRUE) {
00088 
00089       gtk_widget_hide(kernel->widgets.mainBoardOkButton  );
00090       gtk_widget_show(kernel->widgets.mainBoardStopButton);
00091 
00092     }
00093 
00094   }
00095   else if (buttonWidget == kernel->widgets.mainBoardStopButton) {
00096 
00097     gdisp_stopSamplingProcess(kernel);
00098 
00099     gtk_widget_hide(kernel->widgets.mainBoardStopButton);
00100     gtk_widget_show(kernel->widgets.mainBoardOkButton  );
00101 
00102   }
00103 
00104 }
00105 
00106 
00107 
00108 /*
00109  * Graphically show the time. Callback of a GTK timer.
00110  */
00111 static void
00112 gdisp_showTime ( Kernel_T *kernel )
00113 {
00114 
00115 #define GD_DIGIT_WIDTH               16
00116 #define GD_DIGIT_START_X GD_DIGIT_WIDTH
00117 #define GD_2PTS_WIDTH                 9
00118 #define GD_DIGIT_HEIGHT              21
00119 
00120   GdkPixmap      *digitPixmap  = (GdkPixmap*)NULL;
00121   GtkWidget      *timeArea     = (GtkWidget*)NULL;
00122   GdkGC          *timeContext  =     (GdkGC*)NULL;
00123 
00124   time_t          nowTime      =     (time_t)NULL;
00125   struct tm      *localNowTime = (struct tm*)NULL;
00126 
00127   guint           seconds      = 0;
00128   guint           minutes      = 0;
00129   guint           hours        = 0;
00130   guint           xPos         = 0;
00131   guint           yPos         = 0;
00132   
00133 
00134   /*
00135    * Get back current time.
00136    */
00137   nowTime      = time((time_t*)NULL);
00138   localNowTime = localtime(&nowTime);
00139 
00140   seconds      = localNowTime->tm_sec;
00141   minutes      = localNowTime->tm_min;
00142   hours        = localNowTime->tm_hour;
00143 
00144 
00145   /*
00146    * Init.
00147    */
00148   digitPixmap = kernel->widgets.pilotBoardDigitPixmap->pixmap;
00149   timeArea    = kernel->widgets.pilotBoardTimeArea;
00150   timeContext = kernel->widgets.pilotBoardTimeContext;
00151 
00152 
00153   /*
00154    * Draw hours.
00155    */
00156   gdk_draw_pixmap(timeArea->window,
00157                   timeContext,
00158                   digitPixmap,
00159                   GD_DIGIT_START_X + (hours / 10) * GD_DIGIT_WIDTH,
00160                   yPos,
00161                   xPos,
00162                   yPos,
00163                   GD_DIGIT_WIDTH,
00164                   GD_DIGIT_HEIGHT);
00165 
00166   xPos += GD_DIGIT_WIDTH;
00167 
00168   gdk_draw_pixmap(timeArea->window,
00169                   timeContext,
00170                   digitPixmap,
00171                   GD_DIGIT_START_X + (hours % 10) * GD_DIGIT_WIDTH,
00172                   yPos,
00173                   xPos,
00174                   yPos,
00175                   GD_DIGIT_WIDTH,
00176                   GD_DIGIT_HEIGHT);
00177 
00178   xPos += GD_DIGIT_WIDTH;
00179 
00180   /*
00181    * Draw 2 points.
00182    */
00183   gdk_draw_pixmap(timeArea->window,
00184                   timeContext,
00185                   digitPixmap,
00186                   GD_DIGIT_START_X + 12 * GD_DIGIT_WIDTH,
00187                   yPos,
00188                   xPos,
00189                   yPos,
00190                   GD_2PTS_WIDTH,
00191                   GD_DIGIT_HEIGHT);
00192 
00193   xPos += GD_2PTS_WIDTH;
00194 
00195   /*
00196    * Draw minutes.
00197    */
00198   gdk_draw_pixmap(timeArea->window,
00199                   timeContext,
00200                   digitPixmap,
00201                   GD_DIGIT_START_X + (minutes / 10) * GD_DIGIT_WIDTH,
00202                   yPos,
00203                   xPos,
00204                   yPos,
00205                   GD_DIGIT_WIDTH,
00206                   GD_DIGIT_HEIGHT);
00207 
00208   xPos += GD_DIGIT_WIDTH;
00209 
00210   gdk_draw_pixmap(timeArea->window,
00211                   timeContext,
00212                   digitPixmap,
00213                   GD_DIGIT_START_X + (minutes % 10) * GD_DIGIT_WIDTH,
00214                   yPos,
00215                   xPos,
00216                   yPos,
00217                   GD_DIGIT_WIDTH,
00218                   GD_DIGIT_HEIGHT);
00219 
00220   xPos += GD_DIGIT_WIDTH;
00221 
00222   /*
00223    * Draw 2 points.
00224    */
00225   gdk_draw_pixmap(timeArea->window,
00226                   timeContext,
00227                   digitPixmap,
00228                   GD_DIGIT_START_X + 12 * GD_DIGIT_WIDTH,
00229                   yPos,
00230                   xPos,
00231                   yPos,
00232                   GD_2PTS_WIDTH,
00233                   GD_DIGIT_HEIGHT);
00234 
00235   xPos += GD_2PTS_WIDTH;
00236 
00237   /*
00238    * Draw seconds.
00239    */
00240   gdk_draw_pixmap(timeArea->window,
00241                   timeContext,
00242                   digitPixmap,
00243                   GD_DIGIT_START_X + (seconds / 10) * GD_DIGIT_WIDTH,
00244                   yPos,
00245                   xPos,
00246                   yPos,
00247                   GD_DIGIT_WIDTH,
00248                   GD_DIGIT_HEIGHT);
00249 
00250   xPos += GD_DIGIT_WIDTH;
00251 
00252   gdk_draw_pixmap(timeArea->window,
00253                   timeContext,
00254                   digitPixmap,
00255                   GD_DIGIT_START_X + (seconds % 10) * GD_DIGIT_WIDTH,
00256                   yPos,
00257                   xPos,
00258                   yPos,
00259                   GD_DIGIT_WIDTH,
00260                   GD_DIGIT_HEIGHT);
00261 
00262   xPos += GD_DIGIT_WIDTH;
00263 
00264 }
00265 
00266 
00267 /*
00268  * Treat 'expose' X event.
00269  * What shall I do when the area has to be refreshed ?
00270  */
00271 static gboolean
00272 gdisp_timeAreaExpose (GtkWidget       *area,
00273                       GdkEventExpose  *event,
00274                       gpointer         data)
00275 {
00276 
00277   Kernel_T *kernel = (Kernel_T*)data;
00278 
00279   /*
00280    * Graphic area has now to be repainted.
00281    */
00282   gdk_gc_set_clip_rectangle(kernel->widgets.pilotBoardTimeContext,
00283                             &event->area);
00284 
00285   gdisp_showTime(kernel);
00286 
00287   gdk_gc_set_clip_rectangle(kernel->widgets.pilotBoardTimeContext,
00288                             (GdkRectangle*)NULL);
00289 
00290   return TRUE;
00291 
00292 }
00293 
00294 
00295 /*
00296  --------------------------------------------------------------------
00297                              PUBLIC ROUTINES
00298  --------------------------------------------------------------------
00299 */
00300 
00301 
00302 /*
00303  * Create GDISP+ pilot board.
00304  */
00305 GtkWidget*
00306 gdisp_createPilotBoard (Kernel_T *kernel)
00307 {
00308 
00309   GtkWidget *pilotBox     = (GtkWidget*)NULL;
00310   GtkWidget *timeFrame    = (GtkWidget*)NULL;
00311   GtkWidget *pixmapWidget = (GtkWidget*)NULL;
00312   Pixmap_T  *pixmap       =  (Pixmap_T*)NULL;
00313   GtkWidget *timeArea     = (GtkWidget*)NULL;
00314   GdkGC     *timeContext  =     (GdkGC*)NULL;
00315 
00316   assert(kernel);
00317 
00318 
00319   /* ----------------------- PILOT BOX ----------------------- */
00320 
00321   /*
00322    * Create a vertical box.
00323    */
00324   pilotBox = gtk_vbox_new(FALSE, /* homogeneous */
00325                           3      /* spacing     */ );
00326   gtk_container_border_width(GTK_CONTAINER(pilotBox),2);
00327   gtk_widget_show(pilotBox);
00328 
00329 
00330   /* --------------------- TIME GRAPHIC AREA --------------------- */
00331 
00332   timeFrame = gtk_frame_new((gchar*)NULL);
00333   gtk_frame_set_shadow_type(GTK_FRAME(timeFrame),GTK_SHADOW_ETCHED_OUT);
00334   gtk_frame_set_shadow_type(GTK_FRAME(timeFrame),GTK_SHADOW_IN);
00335   gtk_box_pack_start(GTK_BOX(pilotBox),
00336                      timeFrame,
00337                      FALSE, /* expand  */
00338                      FALSE, /* fill    */
00339                      0);    /* padding */
00340   gtk_widget_show(timeFrame);
00341 
00342   timeArea = gtk_drawing_area_new();
00343   kernel->widgets.pilotBoardTimeArea = timeArea;
00344 
00345   gtk_drawing_area_size(GTK_DRAWING_AREA(timeArea),
00346                         6 * GD_DIGIT_WIDTH + 2 * GD_2PTS_WIDTH  /* width  */,
00347                         GD_DIGIT_HEIGHT /* height */);
00348 
00349   timeContext =
00350     gdk_gc_new(GTK_WIDGET(kernel->widgets.mainBoardWindow)->window);
00351 
00352   kernel->widgets.pilotBoardTimeContext = timeContext;
00353 
00354   gtk_signal_connect(GTK_OBJECT(timeArea),
00355                      "expose_event",
00356                      (GtkSignalFunc)gdisp_timeAreaExpose,
00357                      (gpointer)kernel);
00358 
00359   gtk_container_add(GTK_CONTAINER(timeFrame),timeArea);
00360 
00361   pixmap = gdisp_getPixmapById(kernel,
00362                                GD_PIX_timeDigits,
00363                                kernel->widgets.mainBoardWindow);
00364 
00365   kernel->widgets.pilotBoardDigitPixmap = pixmap;
00366 
00367   gtk_widget_show(timeArea);
00368 
00369 
00370   /* ----------------------- PLAY BUTTON ----------------------- */
00371 
00372   /*
00373    * OK button.
00374    */
00375   pixmap = gdisp_getPixmapById(kernel,
00376                                GD_PIX_okButton,
00377                                kernel->widgets.mainBoardWindow);
00378 
00379   /*
00380    * Create a pixmap widget to contain the pixmap.
00381    */
00382   pixmapWidget = gtk_pixmap_new(pixmap->pixmap,
00383                                 pixmap->mask);
00384   gtk_widget_show(pixmapWidget);
00385 
00386   /*
00387    * Create the button that contains the pixmap.
00388    */
00389   kernel->widgets.mainBoardOkButton = gtk_button_new();
00390   gtk_container_add(GTK_CONTAINER(kernel->widgets.mainBoardOkButton),
00391                     pixmapWidget);
00392   gtk_box_pack_start(GTK_BOX(pilotBox),
00393                      kernel->widgets.mainBoardOkButton,
00394                      FALSE /* expand  */,
00395                      FALSE /* fill    */,
00396                      0     /* padding */);
00397   gtk_widget_show(kernel->widgets.mainBoardOkButton);
00398 
00399   gtk_signal_connect(GTK_OBJECT(kernel->widgets.mainBoardOkButton),
00400                      "clicked",
00401                      GTK_SIGNAL_FUNC(gdisp_togglePlayModeCallback),
00402                      (gpointer)kernel);
00403 
00404   /*
00405    * STOP button.
00406    */
00407   pixmap = gdisp_getPixmapById(kernel,
00408                                GD_PIX_stopButton,
00409                                kernel->widgets.mainBoardWindow);
00410 
00411   /*
00412    * Create a pixmap widget to contain the pixmap.
00413    */
00414   pixmapWidget = gtk_pixmap_new(pixmap->pixmap,
00415                                 pixmap->mask);
00416   gtk_widget_show(pixmapWidget);
00417 
00418   /*
00419    * Create the button that contains the pixmap.
00420    */
00421   kernel->widgets.mainBoardStopButton = gtk_button_new();
00422   gtk_container_add(GTK_CONTAINER(kernel->widgets.mainBoardStopButton),
00423                     pixmapWidget);
00424   gtk_box_pack_start(GTK_BOX(pilotBox),
00425                      kernel->widgets.mainBoardStopButton,
00426                      FALSE /* expand  */,
00427                      FALSE /* fill    */,
00428                      0     /* padding */);
00429 
00430   /* DO NOT SHOW kernel->widgets.mainBoardStopButton */
00431 
00432   gtk_signal_connect(GTK_OBJECT(kernel->widgets.mainBoardStopButton),
00433                      "clicked",
00434                      GTK_SIGNAL_FUNC(gdisp_togglePlayModeCallback),
00435                      (gpointer)kernel);
00436 
00437 
00438   /* --------------------- REGISTER ACTION ------------------- */
00439 
00440   /*
00441    * This procedure will be called every second by the kernel, in
00442    * the main GTK thread.
00443    */
00444   (*kernel->registerAction)(kernel,
00445                             gdisp_showTime);
00446 
00447   /*
00448    * Return.
00449    */
00450   return pilotBox;
00451 
00452 }
00453 
Framework Home Page.

Beware !! TSP wave is coming...