GPS4Palm

Source Code Documentation


Utils.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * $RCSfile: Utils_8c-source.html,v $
00004  *
00005  * GPS4Palm Utility Functions
00006  *
00007  * This program is Copyright (C) 03/2003 Matthias Prinke
00008  * <matthias.prinke@surfeu.de> and covered by GNU's GPL.
00009  * In particular, this program is free software and comes WITHOUT
00010  * ANY WARRANTY.
00011  *
00012  * The field scrolling utility functions UpdateScrollbar(),
00013  * ScrollLines(), and PageScroll()) are from the book:
00014  * 
00015  * Neil Rhodes and Julie McKeehan:
00016  * "Palm Programming, the Developer's Guide, 2nd edition"
00017  * by O'Reilly.
00018  *
00019  *
00020  * $Author: mp $
00021  *
00022  * $Date: 2007-10-08 20:40:35 $
00023  *
00024  * $Revision: 1.7.2.1 $
00025  *
00026  * $Log: Utils_8c-source.html,v $
00026  * Revision 1.7.2.1  2007-10-08 20:40:35  mp
00026  * updated for gps4palm V0.9.5 beta
00026  *
00027  * Revision 1.14  2005-02-06 15:00:17  mp
00028  * modified sermux_enable() and sermux_select() to control RTS with SrmControl()
00029  *
00030  * Revision 1.13  2004/12/18 16:14:35  mp
00031  * added PalmOS-compliant serial port muxing with DTR pin (New Serial Manager)
00032  *
00033  * Revision 1.12  2004/11/27 10:17:43  mp
00034  * added Die()
00035  *
00036  * Revision 1.11  2004/11/24 21:26:23  mp
00037  * added UpdateScrollbar(),
00038  * >  * ScrollLines(), and PageScroll()
00039  *
00040  * Revision 1.10  2004/11/23 17:47:16  mp
00041  * removed unused variable
00042  *
00043  * Revision 1.9  2004/11/14 11:02:43  mp
00044  * added MyFrmGetObjectIndexFromPtr()
00045  *
00046  * Revision 1.8  2004/04/30 16:31:12  mp
00047  * modified/added comments for doxygen
00048  *
00049  * Revision 1.7  2004/02/28 17:46:49  mp
00050  * implemented Serial Mux control for Palm III
00051  *
00052  * Revision 1.6  2004/01/10 18:01:01  mp
00053  * added SERMUX_ENABLE and comments
00054  *
00055  * Revision 1.5  2003/11/16 17:42:49  mp
00056  * removed debug messages
00057  *
00058  * Revision 1.4  2003/11/02 11:13:55  mp
00059  * added backlight(), sermux_select() and sermux_enable()
00060  *
00061  * Revision 1.3  2003/10/20 17:31:17  mp
00062  * added SetFieldText(), fixed comment for SetFieldTextFromStr()
00063  *
00064  * Revision 1.2  2003/10/15 19:01:25  mp
00065  * added function nibble2hex
00066  *
00067  * Revision 1.1.1.1  2003/07/14 18:59:29  mp
00068  * Imported GPS4Palm to CVS revision control.
00069  *
00070  *
00071  ****************************************************************************/
00072 #include <PalmOS.h>                     
00073 #include "ResourceDefines.h"
00074 
00075 /* Global Variables */
00076 /* Serial port ID */
00077 extern UInt16   gPortID;
00078 
00079 /* New Serial Manager available */
00080 extern Boolean  gNewSerialManager;
00081 
00082 /*
00083  * Backlight Control
00084  *
00085  * Note: This is strictly hardware specific and will most likely work on
00086  *       Handspring Visor Deluxe only!!!
00087  *
00088  * Backlight is controlled by PGDATA[0]
00089  *   PGDATA[0] = 0 - Backlight On
00090  *   PGDATA[1] = 1 - Backlight Off 
00091  */ 
00092 #define PGDATA  ((volatile UInt8 *)0xFFFFF431L)
00093 #define BLDATA  PGDATA                  /* I/O Data */
00094 #define BLMASK  ((UInt8)  0x01)
00095 
00096 #define BACKLIGHT_ON    (*BLDATA &= ~BLMASK)
00097 #define BACKLIGHT_OFF   (*BLDATA |=  BLMASK)
00098 
00099 /*
00100  * Serial Mux Control
00101  *
00102  * Note: This is strictly hardware specific and will most likely work on
00103  *       the devices explicitely specified only!!!
00104  *       For any other PDA, define SERMUX_* only if you know what you are
00105  *       doing! Otherwise the PDA might not work properly or even be damaged!
00106  *
00107  * Handspring Visor (Deluxe):
00108  * --------------------------
00109  * External Serial Port Multiplexer is controlled by either
00110  *   PE[7] - Cradle Connector Keyboard Input (/KBD) (R18: 330R)
00111  *   or
00112  *   PD[4] - Cradle Connector Hotsync Input (/HS) (R19: 100R)
00113  * To use either of these pins as output, the port pull-up has to be disabled
00114  * and the port direction has to be changed. The Visor's Cradle Connector pins
00115  * are both connected to a series resistor (R18/R19) on the button board.
00116  *
00117  * Palm III:
00118  * ---------
00119  * External Serial Port Multiplexer is controlled by
00120  * PE[6] - /RTS
00121  */
00122 
00123 #if defined(SERMUX_VISOR_HS) | \
00124     defined(SERMUX_VISOR_KBD) | \
00125     defined(SERMUX_PALMIII_RTS)
00126 
00127 #define PEDIR   ((volatile UInt8 *)0xFFFFF420L)
00128 #define PEDATA  ((volatile UInt8 *)0xFFFFF421L)
00129 #define PEPUEN  ((volatile UInt8 *)0xFFFFF422L)
00130 #define PESEL   ((volatile UInt8 *)0xFFFFF423L)
00131 
00132 #define PDDIR   ((volatile UInt8 *)0xFFFFF418L)
00133 #define PDDATA  ((volatile UInt8 *)0xFFFFF419L)
00134 #define PDPUEN  ((volatile UInt8 *)0xFFFFF41AL)
00135 #define PDSEL   ((volatile UInt8 *)0xFFFFF41BL)
00136 #endif
00137 
00138 #if defined(SERMUX_VISOR_KBD) | defined(SERMUX_PALMIII_RTS)
00139   #define MUXDIR PEDIR          /* I/O Direction: 0 - In / 1 - Out */
00140 #endif
00141 #ifdef SERMUX_VISOR_HS
00142   #define MUXDIR PDDIR          /* I/O Direction: 0 - In / 1 - Out */
00143 #endif
00144 
00145 #define MUXDATA (MUXDIR+1)      /* I/O Data */
00146 #define MUXPUEN (MUXDIR+2)      /* I/O Pull-up: 0 - Disabled / 1 - Enabled */
00147 #define MUXSEL  (MUXDIR+3)      /* I/O Select: 0 - Peripheral / 1 - GPIO */
00148 
00149 #ifdef SERMUX_PALMIII_RTS
00150   #define MUXMASK       ((UInt8) 0x40)  /* PE[6] = /RTS */ 
00151 #endif
00152 #ifdef SERMUX_VISOR_KBD
00153   #define MUXMASK       ((UInt8) 0x80)  /* PE[7] = /KBD */
00154 #endif
00155 #ifdef SERMUX_VISOR_HS
00156   #define MUXMASK       ((UInt8) 0x10)  /* PD[4] = /HS */
00157 #endif
00158 
00159 /**********************************************************************/
00160 /**
00161  * \brief       Get pointer to object of specified form.
00162  *
00163  * \param       form            form ptr
00164  * \param       objectID        object ID
00165  *
00166  * \return      ptr to object
00167  **********************************************************************/
00168 void *GetObjectFromForm(FormPtr form, UInt16 objectID)
00169 {
00170         return FrmGetObjectPtr(form, 
00171                 FrmGetObjectIndex(form, objectID));
00172 }
00173 
00174 /**********************************************************************/
00175 /**
00176  * \brief       Get pointer to object of active form.
00177  *
00178  * \param       objectID        object ID
00179  *
00180  * \return      ptr to object
00181  **********************************************************************/
00182 void *GetObjectFromActiveForm(UInt16 objectID)
00183 {
00184         return GetObjectFromForm(FrmGetActiveForm(), objectID);
00185 }
00186 
00187 
00188 /**********************************************************************/
00189 /**
00190  * \brief       Get ID of object of active form given an object pointer.
00191  *
00192  * \param       frmP            form ptr
00193  * \param       objP            object ptr      
00194  *
00195  * \return      object ID
00196  *
00197  * \note        This function implements FrmGetObjectIndexFromPtr()
00198  *              on PalmOS versions < 4.0
00199  **********************************************************************/
00200 UInt16 MyFrmGetObjectIndexFromPtr(const FormPtr frmP, void *objP)
00201 {
00202   UInt16 index;
00203   UInt16 objIndex = frmInvalidObjectId;
00204   UInt16 numObjects = FrmGetNumberOfObjects(frmP);
00205 
00206   for (index = 0; index < numObjects; index++) {
00207     if (FrmGetObjectPtr(frmP, index) == objP) {
00208       /* Found it */
00209       objIndex = index;
00210       break;
00211     }
00212   }
00213 
00214   return objIndex;
00215 }
00216 
00217 
00218 /****************************************************************************/
00219 /**
00220  * \brief       generate SysFatalAlert and stop application
00221  *
00222  * \param       msg             pointer to error message string
00223  *
00224  *****************************************************************************/
00225 void Die(const Char *msg)
00226 {
00227   EventType     event;                  /* event structure */
00228 
00229   event.eType = appStopEvent;
00230   SysFatalAlert(msg);
00231   EvtAddEventToQueue(&event);
00232 }
00233 
00234 
00235 /****************************************************************************/
00236 /**
00237  * \brief       Table paging function using scrollbar.
00238  *
00239  * \param       tbl_id          table ID
00240  * \param       down            paging direction (up/down)
00241  * \param       nr_items        number of items
00242  * \param       top_row         ptr to variable containing top row
00243  *
00244  *****************************************************************************/
00245 void Dopage(UInt16 tbl_id, Boolean down, UInt16 nr_items,
00246   Int32 *top_row)
00247 {
00248   TablePtr      tblP;           /* table ptr */
00249   FormPtr       frmP;           /* form ptr */
00250   UInt32        n;              /* number of table rows */
00251 
00252   frmP = FrmGetActiveForm();
00253   tblP = FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, tbl_id));
00254   n = TblGetNumberOfRows(tblP);
00255   *top_row += (down == true) ? n : -n;
00256   if ((*top_row + n) >= nr_items) {
00257     *top_row = nr_items - n;
00258   }
00259   if (*top_row < 0) {
00260     *top_row = 0;
00261   }
00262 }
00263 
00264 
00265 /****************************************************************************/
00266 /**
00267  * \brief       Table scrolling function (using scrollbar).
00268  *
00269  * \param       tbl_id          table ID
00270  * \param       scl_id          scrollbar ID
00271  * \param       nr_items        number of items
00272  * \param       top_row         ptr to variable containing top row
00273  *
00274  *****************************************************************************/
00275 void Doscroll(UInt16 tbl_id, UInt16 scl_id, UInt16 nr_items, Int32 *top_row)
00276 {
00277   FormPtr       frmP;           /* form ptr */
00278   TablePtr      tblP;           /* table ptr */ 
00279   Int32         n;              /* number of table rows */
00280   ScrollBarPtr  sclP;           /* scrollbar ptr */
00281   Int16         v;              /* current scrollbar value */
00282   Int16         mn;             /* min scrollbar value (top) */
00283   Int16         mx;             /* max scrollbar value (bottom) */
00284   Int16         ps;             /* scrollbar page size */
00285 
00286   frmP = FrmGetActiveForm();
00287   sclP = FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, scl_id));
00288   SclGetScrollBar(sclP, &v, &mn, &mx, &ps);
00289   tblP = FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, tbl_id));
00290   n = TblGetNumberOfRows(tblP);
00291   *top_row = v;
00292   if ((*top_row + n) >= nr_items) {
00293     *top_row = nr_items - n;
00294   }
00295   if (*top_row < 0) {
00296     *top_row = 0;
00297   }
00298 }
00299 
00300 
00301 /****************************************************************************/
00302 /**
00303  * \brief       Field scrolling function (using scrollbar).
00304  *
00305  *              Update scrollbar according to field's values.
00306  *
00307  * \param       form            form ptr
00308  * \param       fieldID         field ID
00309  * \param       scrollbarID     scrollbar ID
00310  *
00311  *****************************************************************************/
00312 void UpdateScrollbar(FormPtr form, UInt16 fieldID, UInt16 scrollbarID)
00313 {
00314    ScrollBarPtr   scroll;
00315    FieldPtr       field;
00316    UInt16         currentPosition;
00317    UInt16         textHeight;
00318    UInt16         fieldHeight;
00319    UInt16         maxValue;
00320    
00321    field = (FieldPtr) FrmGetObjectPtr(form, 
00322      FrmGetObjectIndex(form, fieldID));
00323    FldGetScrollValues(field, &currentPosition, &textHeight, &fieldHeight);
00324 
00325    // if the field is 3 lines, and the text height is 4 lines
00326    // then we can scroll so that the first line is at the top 
00327    // (scroll position 0) or so the second line is at the top
00328    // (scroll postion 1). These two values are enough to see
00329    // the entire text.
00330    if (textHeight > fieldHeight)
00331       maxValue = textHeight - fieldHeight;
00332    else if (currentPosition)
00333       maxValue = currentPosition;
00334    else
00335       maxValue = 0;
00336       
00337    scroll = (ScrollBarPtr) FrmGetObjectPtr(form, FrmGetObjectIndex(form, scrollbarID));
00338    
00339    // on a page scroll, want to overlap by one line (to provide context)
00340    SclSetScrollBar(scroll, (Int16) currentPosition, 0, 
00341      (Int16) maxValue, (Int16) fieldHeight - 1);
00342 }
00343 
00344 
00345 /****************************************************************************/
00346 /**
00347  * \brief       Field scrolling function (using scrollbar).
00348  *
00349  *              Scroll field's contents by specified number of lines.
00350  *
00351  * \param       form                    form ptr
00352  * \param       fieldID                 field ID
00353  * \param       scrollbarID             scrollbar ID
00354  * \param       numLinesToScroll        number of lines to scroll
00355  *                                      (negative means up)
00356  * \param       redraw                  flag: redraw field if true
00357  *
00358  *****************************************************************************/
00359 void ScrollLines(FormPtr form, UInt16 fieldID, UInt16 scrollbarID,
00360   Int16 numLinesToScroll, Boolean redraw)
00361 {
00362    FormPtr        frm = FrmGetActiveForm();
00363    FieldPtr       field;
00364    
00365    field = (FieldPtr) FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID));
00366    if (numLinesToScroll < 0)
00367       FldScrollField(field, (UInt16) -numLinesToScroll, winUp);
00368    else
00369       FldScrollField(field, (UInt16) numLinesToScroll, winDown);
00370       
00371    // if there are blank lines at the end and we scroll up, FldScrollField
00372    // makes the blank lines disappear. Therefore, we've got to update
00373    // the scrollbar
00374    if ((FldGetNumberOfBlankLines(field) && numLinesToScroll < 0) ||
00375       redraw)
00376       UpdateScrollbar(form, fieldID, scrollbarID);
00377 }
00378 
00379 
00380 /****************************************************************************/
00381 /**
00382  * \brief       Field scrolling function (using scrollbar).
00383  *
00384  *              Scroll field's contents by entire page size.
00385  *
00386  * \param       form                    form ptr
00387  * \param       fieldID                 field ID
00388  * \param       scrollbarID             scrollbar ID
00389  * \param       direction               winUp/winDown
00390  *
00391  *****************************************************************************/
00392 void PageScroll(FormPtr form, UInt16 fieldID, UInt16 scrollbarID,
00393   WinDirectionType direction)
00394 {
00395    FieldPtr       field;
00396    
00397    field = (FieldPtr) FrmGetObjectPtr(form, 
00398      FrmGetObjectIndex(form, fieldID));
00399    if (FldScrollable(field, direction)) {
00400       Int16 linesToScroll = (Int16) FldGetVisibleLines(field) - 1;
00401       
00402       if (direction == winUp)
00403          linesToScroll = -linesToScroll;
00404       ScrollLines(form, fieldID, scrollbarID, linesToScroll, true);
00405    }
00406 }
00407 
00408 
00409 /**********************************************************************/
00410 /**
00411  * \brief       Get PalmOS ROM version.
00412  *
00413  * \return      Encoded ROM version.
00414  **********************************************************************/
00415 UInt32 GetRomVersion()
00416 {
00417         UInt32 romVersion;
00418         
00419         // The system records the version number in a feature.  A feature is a
00420         // piece of information which can be looked up by a creator and feature
00421         // number.
00422         FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
00423         return romVersion;
00424 }
00425 
00426 
00427 /**********************************************************************/
00428 /**
00429  *
00430  * \brief       Set field object's text handle.
00431  *
00432  *              Will reuse an existing text handle, if any.
00433  *
00434  * \param       field   field object pointer
00435  * \param       s       source text pointer
00436  * \param       redraw  if true, field will be redrawn
00437  *
00438  * \return      Zero if successful, memory manager error code if
00439  *              allocation/resizing of memory chunk failed.
00440  *
00441  ***********************************************************************/
00442 Err SetFieldTextFromStr(FieldPtr field, Char *s, Boolean redraw)
00443 {
00444    MemHandle      h;
00445    
00446    h = FldGetTextHandle(field);
00447    if (h) {
00448      Err        err;
00449      
00450      FldSetTextHandle(field, NULL);
00451      err = MemHandleResize(h, StrLen(s) + 1);
00452      if (err) {
00453        FldSetTextHandle(field, h);  // restore handle
00454        return err;
00455      }
00456    } else {
00457      h = MemHandleNew(StrLen(s) + 1);
00458      if (!h)
00459        return memErrNotEnoughSpace;
00460    }
00461    // at this point, we have a handle of the correct size
00462 
00463    // copy the string to the locked handle.
00464    StrCopy((Char *) MemHandleLock(h), s);
00465    // unlock the string handle.
00466    MemHandleUnlock(h);
00467    
00468    FldSetTextHandle(field, h);
00469    if (redraw)
00470          FldDrawField(field);
00471    return 0;
00472 } 
00473 
00474 
00475 /**********************************************************************/
00476 /**
00477  *
00478  * \brief       Set field object's text handle.
00479  *
00480  *              Will reuse an existing text handle, if any.
00481  * \note        Set srcP to NULL to free memory allocated for text.
00482  *
00483  * \param       fldID   field object id
00484  * \param       srcP    source text pointer
00485  * \param       append  true: append / false: replace
00486  * \param       redraw  if true, field will be redrawn
00487  *
00488  ***********************************************************************/
00489 void SetFieldText(UInt16 fldID, Char *srcP, Boolean append, Boolean redraw)
00490 {
00491   MemHandle             stringH;
00492   UInt16                oldLength = 0;
00493   UInt16                newSize;
00494   Char                  *stringP;
00495   FieldPtr              fldP;
00496   FormPtr               frmP;
00497 
00498   frmP = FrmGetActiveForm();
00499   fldP = FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP,fldID));
00500 
00501   if (!srcP || !*srcP)
00502         {                               // handle clearing field as well
00503           FldFreeMemory(fldP);
00504           if ( redraw )
00505                 {
00506                   FldEraseField( fldP );
00507                   FldDrawField( fldP );
00508                 }
00509           return;
00510         }
00511 
00512 
00513   newSize = StrLen(srcP) + 1;           // initialize new size
00514   stringH = FldGetTextHandle( fldP );   // get the current text handle
00515   FldSetTextHandle( fldP, 0 );          // release this handle from field
00516 
00517   // Resize the existing handle, if any
00518   if ( stringH )
00519         {
00520           if ( append )
00521                 {
00522                   stringP = MemHandleLock( (MemHandle)stringH );
00523                   oldLength = StrLen(stringP);
00524                   newSize += oldLength;
00525                   MemPtrUnlock( stringP );
00526                 }
00527           if ( MemHandleResize ( (MemHandle)stringH, newSize) )
00528                   goto Exit;
00529         } // Resize the existing handle, if any
00530 
00531   // Otherwise, allocate a new handle
00532   else
00533         {
00534           stringH = MemHandleNew( newSize );    // allocate a new chunk
00535           if ( !stringH )       return;
00536         }
00537 
00538   // Append the new text
00539   stringP = MemHandleLock( (MemHandle)stringH );
00540   StrCopy( stringP + oldLength, srcP );                 // copy the new text
00541   MemPtrUnlock( stringP );
00542 
00543 Exit:
00544   FldSetTextHandle( fldP, stringH );            // set the new text handle
00545   if ( redraw )
00546         {
00547           FldEraseField( fldP );
00548           FldDrawField( fldP );
00549         }
00550 }
00551 
00552 
00553 /**********************************************************************/
00554 /**
00555  * \brief       Check if appropriate ROM version is available.
00556  *
00557  * \param       requiredVersion         required version
00558  * \param       launchFlags             application launch flags
00559  *
00560  * \return      0 on success, sysErrRomIncompatible on failure
00561  *
00562  ***********************************************************************/
00563 Err RomVersionCompatible(UInt32 requiredVersion, UInt16 launchFlags)
00564 {
00565         UInt32 romVersion = GetRomVersion();
00566         
00567         // See if we're on in minimum required version of the ROM or later.
00568         if (romVersion < requiredVersion)
00569                 {
00570                 // If the user launched the app from the launcher, explain
00571                 // why the app shouldn't run.  If the app was contacted for 
00572                 // something else, like it was asked to find a string by the  
00573                 // system find, then don't bother the user with a warning dialog.   
00574                 // These flags tell how the app was launched to decided if a 
00575                 // warning should be displayed.
00576                 if ((launchFlags & 
00577                         (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) 
00578                         == (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) {
00579                         FrmAlert(RomIncompatibleAlert);
00580                 
00581                         // Pilot 1.0 will continuously relaunch this app unless we switch  
00582                         // to another safe one.  The sysFileCDefaultApp is 
00583                         // considered "safe".
00584                         if (sysGetROMVerMajor(romVersion) < 2) {
00585                                 AppLaunchWithCommand(sysFileCDefaultApp,
00586                                         sysAppLaunchCmdNormalLaunch, NULL);
00587                         }
00588                 }
00589                 return sysErrRomIncompatible;
00590         }
00591         return 0;
00592 }
00593 
00594 
00595 /**********************************************************************/
00596 /**
00597  * \brief       Convert nibble value to hex digit (upper case).
00598  *
00599  *              This function can be used instead of the PalmOS
00600  *              functions StrAToH (which always returns a 32 bit
00601  *              hex string) and StrPrintF (which ignores the field
00602  *              width specified in the format string). 
00603  *
00604  * \param       nibble  nibble value
00605  *
00606  * \return    0..9, A..F if nibble is valid, else '\0'.
00607  *
00608  ***********************************************************************/
00609 char nibble2hex(const char nibble)
00610 {
00611   
00612   if (nibble < 10) {
00613     return ('0' + nibble);
00614   } else if (nibble < 16) {
00615     return ('A' + nibble - 10);
00616   } else {
00617     return '\0';
00618   }
00619 }
00620 
00621 
00622 /**********************************************************************/
00623 /**
00624  * \brief       Switch backlight on/off.
00625  * 
00626  * \note        The function directly accesses hardware registers,
00627  *                this will work on specific devices only. 
00628  *
00629  * \param       on      true: on / false: off
00630  *
00631  ***********************************************************************/
00632 void backlight(Boolean on)
00633 {
00634   if (on) {
00635     BACKLIGHT_ON;
00636   } else {
00637     BACKLIGHT_OFF;
00638   }
00639 
00640 }
00641 
00642 
00643 /**********************************************************************/
00644 /**
00645  * \brief       Enable serial multiplexer select output.
00646  * \note        If any of the SERMUX_* flags is defined, the
00647  *                function directly accesses hardware registers,
00648  *                this will work on specific devices only.
00649  *              If the new serial manager is available (and the PDA
00650  *                hardware supports it), the RTS pin is used instead.             
00651  *
00652  * \param       enable  true: enable / false: disable
00653  *
00654  ***********************************************************************/
00655 void sermux_enable(Boolean enable)
00656 {
00657 
00658 #if defined(SERMUX_VISOR_HS) | \
00659     defined(SERMUX_VISOR_KBD) | \
00660     defined(SERMUX_PALMIII_RTS)
00661   if (enable) {
00662     
00663     /* disable pull-up */
00664     *MUXPUEN &= ~MUXMASK;
00665     
00666     /* port bit is output */
00667     *MUXDIR  |= MUXMASK;
00668   
00669     /* I/O function */
00670     *MUXSEL  |= MUXMASK;
00671   } else {
00672   
00673     /* port bit is input */ 
00674     *MUXDIR  &= ~MUXMASK;
00675     
00676     /* enable pull-up */
00677     *MUXPUEN |= MUXMASK;
00678     
00679     /* I/O function */
00680     *MUXSEL  |= MUXMASK;
00681   }
00682 #else
00683   if (gNewSerialManager) {
00684     UInt32              flags;          /* port control flags */
00685     UInt16              valueLen;       /* value length */
00686     Err                 err;            /* error code */    
00687     
00688     err = SrmControl(gPortID, srmCtlGetFlags, &flags, &valueLen);
00689     
00690     if (enable) {
00691       /* disable RTS Flow Control */
00692       flags &= ~srmSettingsFlagRTSAutoM;
00693     
00694     } else {
00695       /* enable RTS Flow Control */
00696       flags |= srmSettingsFlagRTSAutoM;
00697       
00698     }    
00699     
00700     valueLen = sizeof(UInt32);
00701     err = SrmControl(gPortID, srmCtlSetFlags, &flags, &valueLen);
00702   } /* if (gNewSerialManager) */
00703 #endif
00704 
00705 }
00706 
00707 
00708 /**********************************************************************/
00709 /**
00710  * \brief       Set serial multiplexer select output.
00711  * \note        If any of the SERMUX_* flags is defined, the
00712  *                function directly accesses hardware registers,
00713  *                this will work on specific devices only.
00714  *              If the new serial manager is available (and the PDA
00715  *                hardware supports it), the RTS pin is used instead.             
00716  *
00717  * \param       sel     true: set output high / false: set output low
00718  *
00719  ***********************************************************************/
00720 void sermux_select(Boolean sel)
00721 {
00722   
00723 #if defined(SERMUX_VISOR_HS) | \
00724     defined(SERMUX_VISOR_KBD) | \
00725     defined(SERMUX_PALMIII_RTS)
00726 
00727   if (sel) {
00728     *MUXDATA |= MUXMASK;
00729   } else {
00730     *MUXDATA &= ~MUXMASK;
00731   }
00732 
00733 #else
00734   
00735   if (gNewSerialManager) {
00736     UInt32              flags;          /* port control flags */
00737     UInt16              valueLen;       /* value length */
00738     Err                 err;            /* error code */    
00739     
00740     err = SrmControl(gPortID, srmCtlGetFlags, &flags, &valueLen);
00741     
00742     if (sel) {
00743       /* disable RTS override */
00744       flags &= ~srmSettingsFlagRTSInactive;
00745     
00746     } else {
00747       /* set RTS inactive (low) */
00748       flags |= srmSettingsFlagRTSInactive;
00749     }
00750 
00751     valueLen = sizeof(UInt32);
00752     err = SrmControl(gPortID, srmCtlSetFlags, &flags, &valueLen);
00753       
00754   } /* if (gNewSerialManager) */
00755 
00756 #endif
00757 }

Created: Mon, 08 Oct 2007 22:33:16 +0200
Copyright ©2004 M. Prinke