Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Spindle.cc

Go to the documentation of this file.
00001 #define SPINDLE
00002 #include "Spindle.hh"
00003 #undef SPINDLE
00004 
00005 Widget
00006 SpindleControl (Dimension x0, Dimension x1, Dimension y1, Dimension y2,
00007                 Dimension boxMarginWidth, Dimension boxMarginHeight,
00008                 Widget parent)
00009 {
00010   arrowFrame =
00011     XtVaCreateManagedWidget ("arrowFrame", xmFrameWidgetClass, parent,
00012                              XmNfractionBase, 5,
00013                              XmNleftPosition, x0,
00014                              XmNrightPosition, x1,
00015                              XmNtopPosition, y1,
00016                              XmNbottomPosition, y2,
00017                              XmNmarginWidth, boxMarginWidth,
00018                              XmNmarginHeight, boxMarginHeight,
00019                              XmNtopAttachment, XmATTACH_POSITION,
00020                              XmNbottomAttachment, XmATTACH_POSITION,
00021                              XmNleftAttachment, XmATTACH_POSITION,
00022                              XmNrightAttachment, XmATTACH_POSITION,
00023                              XmNmarginWidth, 5,
00024                              XmNmarginHeight, 5,
00025                              XmNmappedWhenManaged, False,
00026                              XmNtraversalOn, False, NULL);
00027 
00028   arrowForm =
00029     XtVaCreateManagedWidget ("arrowForm", xmFormWidgetClass, arrowFrame,
00030                              XmNmarginWidth, 5, XmNmarginHeight, 5, NULL);
00031   Dimension width = 100;
00032   Dimension height = 50;
00033   XmPushButtonWidget pb;
00034 
00035   reverseButton = XtVaCreateManagedWidget (SPINDLE_REVERSE_STR,
00036                                            xmPushButtonWidgetClass, arrowForm,
00037                                            XmNtopAttachment, XmATTACH_FORM,
00038                                            XmNbottomAttachment, XmATTACH_NONE,
00039                                            XmNleftAttachment, XmATTACH_FORM,
00040                                            XmNrightAttachment, XmATTACH_NONE,
00041                                            XmNshadowThickness, 4,
00042                                            XmNwidth, width,
00043                                            XmNheight, height, NULL);
00044 
00045   pb = (XmPushButtonWidget) reverseButton;
00046   pb->pushbutton.armed = false;
00047 
00048   XtAddCallback (reverseButton, XmNarmCallback,
00049                  buttonCB, (XtPointer) SPINDLE_REVERSE);
00050 
00051   XmString SpindleLabel =
00052     XmStringCreateLtoR ((char *) SPINDLE_LABEL_STR, XmSTRING_DEFAULT_CHARSET);
00053 
00054   spindleButton =
00055     XtVaCreateManagedWidget ("button", xmPushButtonWidgetClass, arrowForm,
00056                              XmNtopAttachment, XmATTACH_FORM,
00057                              XmNbottomAttachment, XmATTACH_NONE,
00058                              XmNleftAttachment, XmATTACH_WIDGET,
00059                              XmNleftWidget, reverseButton,
00060                              XmNleftOffset, 50,
00061                              XmNrightAttachment, XmATTACH_NONE,
00062                              XmNshadowThickness, 4,
00063                              XmNwidth, width,
00064                              XmNheight, height,
00065                              XmNlabelString, SpindleLabel, NULL);
00066 
00067   XmStringFree (SpindleLabel);
00068 
00069   pb = (XmPushButtonWidget) spindleButton;
00070   pb->pushbutton.armed = false;
00071 
00072   XtAddCallback (spindleButton, XmNarmCallback,
00073                  buttonCB, (XtPointer) SPINDLE_ON_OFF);
00074 
00075   forwardButton = XtVaCreateManagedWidget (SPINDLE_FORWARD_STR,
00076                                            xmPushButtonWidgetClass, arrowForm,
00077                                            XmNtopAttachment, XmATTACH_FORM,
00078                                            XmNbottomAttachment, XmATTACH_NONE,
00079                                            XmNleftAttachment, XmATTACH_WIDGET,
00080                                            XmNleftWidget, spindleButton,
00081                                            XmNleftOffset, 50,
00082                                            XmNrightAttachment, XmATTACH_NONE,
00083                                            XmNshadowThickness, 4,
00084                                            XmNwidth, width,
00085                                            XmNheight, height, NULL);
00086 
00087   pb = (XmPushButtonWidget) forwardButton;
00088   pb->pushbutton.armed = false;
00089 
00090   XtAddCallback (forwardButton, XmNarmCallback,
00091                  buttonCB, (XtPointer) SPINDLE_FORWARD);
00092   // Save the original colors so we can go back to this state.
00093   XtVaGetValues (forwardButton, XmNtopShadowColor, &original_tsc,
00094                  XmNbottomShadowColor, &original_bsc,
00095                  XmNarmColor, &original_ac,
00096                  XmNbackground, &original_bg, NULL);
00097 
00098   if (get_speed () >= 0)
00099     {
00100       PRESS (forwardButton);
00101       UNPRESS (reverseButton);
00102     }
00103   else
00104     {
00105       PRESS (reverseButton);
00106       UNPRESS (forwardButton);
00107     }
00108 
00109 
00110   SEPARATOR ();
00111 
00112   CREATE_PIXMAP (r_arrow, r_pixmap);
00113   CREATE_PIXMAP (r_arrow_in, r_pixmap_in);
00114   CREATE_PIXMAP (l_arrow, l_pixmap);
00115   CREATE_PIXMAP (l_arrow_in, l_pixmap_in);
00116   CREATE_PIXMAP (right_arrows, rr_pixmap);
00117   CREATE_PIXMAP (right_arrows_in, rr_pixmap_in);
00118   CREATE_PIXMAP (left_arrows, ll_pixmap);
00119   CREATE_PIXMAP (left_arrows_in, ll_pixmap_in);
00120 
00121   SPINDLE_AXIS (arrowB1, XmATTACH_FORM, NULL, 0, ll_pixmap, ARROW_LEFT_FAST);
00122   SPINDLE_AXIS (arrowB2, XmATTACH_WIDGET, arrowB1, 5, l_pixmap, ARROW_LEFT);
00123 
00124   SPINDLE_AXIS (arrowB3, XmATTACH_WIDGET, arrowB2, 225, r_pixmap,
00125                 ARROW_RIGHT);
00126   SPINDLE_AXIS (arrowB4, XmATTACH_WIDGET, arrowB3, 5, rr_pixmap,
00127                 ARROW_RIGHT_FAST);
00128 
00129   double mySpeed = get_speed ();
00130   LABEL_SPEED (mySpeed);
00131 
00132   updateSpindleSpeed ();
00133 
00134   return arrowFrame;
00135 }
00136 
00137 static void
00138 buttonCB (Widget mw, XtPointer cd, XtPointer cb)
00139 {
00140   switch ((int) cd)
00141     {
00142     case ARROW_LEFT_FAST:
00143       XtVaSetValues (mw, XmNarmPixmap, ll_pixmap_in, NULL);
00144       break;
00145     case ARROW_LEFT:
00146       XtVaSetValues (mw, XmNarmPixmap, l_pixmap_in, NULL);
00147       break;
00148     case ARROW_RIGHT:
00149       XtVaSetValues (mw, XmNarmPixmap, r_pixmap_in, NULL);
00150       break;
00151     case ARROW_RIGHT_FAST:
00152       XtVaSetValues (mw, XmNarmPixmap, rr_pixmap_in, NULL);
00153       break;
00154     case SPINDLE_FORWARD:
00155       LOCK (forwardButton);
00156       UNLOCK (reverseButton);
00157       break;
00158     case SPINDLE_REVERSE:
00159       LOCK (reverseButton);
00160       UNLOCK (forwardButton);
00161       break;
00162     case SPINDLE_ON_OFF:
00163       isSpindleOn = !isSpindleOn;
00164       if (isSpindleOn)
00165         {
00166           PRESS (spindleButton);
00167           turnSpindleOn ();
00168         }
00169       else
00170         {
00171           UNPRESS (spindleButton);
00172           turnSpindleOff ();
00173         }
00174       updateSpindleSpeed ();
00175       return;
00176     default:
00177       return;
00178     }
00179 
00180   // We separate our change call since it involves our emc interface
00181   change_spindle ((int) cd);
00182 
00183   // If our spindle changes from positive to negative or vice versa
00184   checkSpindleReversal ();
00185 
00186   updateSpindleSpeed ();
00187 
00188 }
00189 
00190 static void
00191 unbuttonCB (Widget mw, XtPointer cd, XtPointer cb)
00192 {
00193   switch ((int) cd)
00194     {
00195     case ARROW_LEFT_FAST:
00196     case ARROW_LEFT:
00197     case ARROW_RIGHT:
00198     case ARROW_RIGHT_FAST:
00199       stop_changing_spindle ((int) cd);
00200       break;
00201     default:
00202       return;
00203     }
00204 
00205   updateSpindleSpeed ();
00206 }
00207 
00208 static void
00209 change_spindle (int type)
00210 {
00211 
00212 // TODO: call the emc spindle interface code here to change speed.
00213 // Note: call updateSpeed () to periodically update the GUI.
00214 
00215 #ifdef DEBUG
00216   char *whichWay[6] =
00217     { "Left fast", "Left", "Right", "Right fast", "Forward", "Reverse" };
00218   cout << "Start " << whichWay[type] << " change of spindle" << endl;
00219 #endif
00220 
00221   // Dummy: showing a synthetic change in speed
00222   switch (type)
00223     {
00224     case ARROW_LEFT_FAST:
00225       theSpeed -= FAST_CHANGE;
00226       break;
00227     case ARROW_LEFT:
00228       theSpeed -= SLOW_CHANGE;
00229       break;
00230     case ARROW_RIGHT:
00231       theSpeed += SLOW_CHANGE;
00232       break;
00233     case ARROW_RIGHT_FAST:
00234       theSpeed += FAST_CHANGE;
00235       break;
00236     case SPINDLE_FORWARD:
00237     case SPINDLE_REVERSE:
00238       changeDirection ();
00239       break;
00240     }
00241   // End of dummy code
00242 
00243   updateSpindleSpeed ();
00244 }
00245 
00246 static void
00247 checkSpindleReversal ()
00248 {
00249   if (wasForward && get_speed () < 0 || !wasForward && get_speed () >= 0)
00250 
00251     {
00252       wasForward = !wasForward;
00253 
00254       TOGGLE (forwardButton);
00255       TOGGLE (reverseButton);
00256     }
00257 }
00258 static void
00259 stop_changing_spindle (int type)
00260 {
00261 
00262 // TODO: call the emc spindle interface code here to stop changing speed
00263 
00264 #ifdef DEBUG
00265   char *whichWay[4] = { "Left fast", "Left", "Right", "Right fast" };
00266   cout << "Stop " << whichWay[type] << " change of spindle" << endl;
00267 #endif
00268 
00269 }
00270 
00271 static double
00272 get_speed ()
00273 {
00274 // TODO: call the emc spindle interface code here to get the actual speed
00275 //
00276 // set the global "theSpeed" to the actual speed.
00277 
00278   return theSpeed;
00279 }
00280 
00281 static void
00282 turnSpindleOn ()
00283 {
00284 #ifdef DEBUG
00285   cout << "Spindle is now On" << endl;
00286 #endif
00287 }
00288 
00289 static void
00290 turnSpindleOff ()
00291 {
00292 #ifdef DEBUG
00293   cout << "Spindle is now Off" << endl;
00294 #endif
00295 }
00296 
00297 // External callable
00298 void
00299 updateSpindleSpeed ()
00300 {
00301   double newSpeed = get_speed ();
00302   LABEL_SPEED_UPDATE (newSpeed);
00303 
00304   // Don't let them change spindle direction while moving
00305   if (isSpindleOn)
00306     {
00307       LOCK (forwardButton);
00308       LOCK (reverseButton);
00309     }
00310   else
00311     {
00312       if (wasForward)
00313         {
00314           LOCK (forwardButton);
00315           UNLOCK (reverseButton);
00316         }
00317       else
00318         {
00319           LOCK (reverseButton);
00320           UNLOCK (forwardButton);
00321         }
00322     }
00323 }
00324 
00325 // The following should be called by emc interface that knows the actual speed
00326 void
00327 setSpindleSpeed (double speed)
00328 {
00329   theSpeed = speed;
00330   LABEL_SPEED_UPDATE (speed);
00331 }
00332 
00333 // The following should be called by emc interface to toggle the spindle direction
00334 void
00335 changeDirection ()
00336 {
00337   // Dummy: for now, assume instant reversal of spindle with same speed
00338   theSpeed = -(theSpeed);
00339 }
00340 
00341 void
00342 showSpindle (void)
00343 {
00344   if (arrowFrame)
00345     XtVaSetValues (arrowFrame, XmNmappedWhenManaged, True, NULL);
00346 }
00347 
00348 void
00349 hideSpindle (void)
00350 {
00351   if (arrowFrame)
00352     XtVaSetValues (arrowFrame, XmNmappedWhenManaged, False, NULL);
00353 }
00354 
00355 void
00356 stopSpindle (void)
00357 {
00358   isSpindleOn = False;
00359   UNPRESS (spindleButton);
00360   turnSpindleOff ();
00361 }

Generated on Sun Dec 2 15:27:43 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001