📄 twiddle.c
字号:
/******************************************************************** FILE: Twiddle.c** DESCRIPTION: The Starter project module.** VERSION: 1.0*********************************************************************/#include "Twiddle.h"#include "TwiddleRsc.h"#include "Utils.h"#include "UtilsRsc.h"#include "Port.h"#include "Scaffold.h"// Module variablesstatic Char mainInfoBuff[256];static Char mainCommandString[] = { "hello\r" };static Char MainEndCommandString[] = { "\r" };// Scaffold for I/O debugging#define DEBUG_COMM_TRACE ( true )/********************************************************* * FUNCTION: MainFormInit * * DESCRIPTION: This routine initializes the MainForm form. * * RETURNED: nothing *********************************************************/ static void MainFormInit( FormPtr frmP // (in) Pointer to the MainForm form.){ if ( frmP != NULL ) { // Insert code as appropriate }}/********************************************************* * FUNCTION: MainFormDraw * * DESCRIPTION: This routine draws the non-form contents of the MainForm form. * * RETURNED: nothing *********************************************************/ static void MainFormDraw( FormPtr frmP // (in) Pointer to the MainForm form.){ if ( frmP != NULL ) { // Insert code as appropriate }}/********************************************************* * FUNCTION: MainFormDone * * DESCRIPTION: This routine cleans up after the Main form. * * RETURNED: nothing *********************************************************/static void MainFormDone( FormPtr frmP // (in) Pointer to the MainForm form){ if ( frmP != NULL ) { if ( ConnectedPort() ) { ClosePort(); };// Then do this as the very last thing FrmEraseForm ( frmP ); FrmDeleteForm ( frmP ); }}/********************************************************* * * FUNCTION: MainFormLaunch * * DESCRIPTION: This routine launches the Dialz application. * * PARAMETERS: nothing * * RETURNED: nothing * *********************************************************/void MainFormLaunch( void){ LocalID theDBID; UInt theCardNo; DmSearchStateType theSearchState; Err anErr; ClosePort();// Grab the id of the panel we want to launch anErr = DmGetNextDatabaseByTypeCreator(true, &theSearchState, sysFileTApplication, mainFormTargetAppCreator, true, &theCardNo, &theDBID); if ( anErr == noErr ) {// Launch the target application SysUIAppSwitch(theCardNo, theDBID, 0, NULL ); } else {// Tell the user the app wasn't available FrmAlert( AppAlert ); }}/********************************************************* * * FUNCTION: MainFormSendCommand * * DESCRIPTION: Sends the command string to the remote host * * PARAMETERS: nothing * * RETURNED: nothing * *********************************************************/void MainFormSendCommand( void ){// Ignore error Err anErr = SendPort( mainCommandString ); TraceMessage( DEBUG_COMM_TRACE, "Sent Command" ); return;}/********************************************************* * * FUNCTION: MainFormValidateInput * * DESCRIPTION: See if the incoming message confirms our command. * If so, launch the next application. * If not, tell the user after closing the port. * * PARAMETERS: pointer to string of what was read. * * RETURNED: nothing * *********************************************************/void MainFormValidateInput( // (out) nothing Char *inputP // (in) what was read last){ Char *tempP; static Char serialRcvBuff[mainFormBufferSize];// Start by appending the data to the response buffer. StrCat( serialRcvBuff, inputP );// If the response buffer ends in a newline, we have a response. tempP = StrStr( serialRcvBuff, MainEndCommandString ); if ( tempP != NULL ) {// If the response matches our sent command, we know it was executed properly. *(tempP+1) = '\000'; if ( StrCompare( serialRcvBuff, mainCommandString ) == NULL ) { MainFormLaunch(); } else // The response didn't match our command. {// Let's just tell the user it's hosed and bail. TraceMessage( DEBUG_COMM_TRACE, "Received..." ); TraceMessage( DEBUG_COMM_TRACE, serialRcvBuff ); ClosePort( ); FrmAlert( MsgAlert ); } // We didn't have the right response } // Did we have a full line to examine?}/********************************************************* * FUNCTION: MainFormDoMenuCommand * * DESCRIPTION: This routine handles menu commands. * * RETURNED: nothing *********************************************************/ static Boolean MainFormDoMenuCommand // (out) true if the event is handled.( UInt16 command // (in) The ID of menu command to do){ Boolean handled = false; FormPtr frmP; switch ( command ) { case MainOptionsAboutTwiddle: MenuEraseStatus ( 0 ); frmP = FrmInitForm ( AboutForm ); FrmDoDialog ( frmP ); FrmDeleteForm ( frmP ); handled = true; break; } return handled;}/********************************************************* * * FUNCTION: MainFormHandleControl * * DESCRIPTION: This routine handles control selections for this form. * * PARAMETERS: controlID - the ID of the selected object. * * RETURNED: true if the event has handle and should not be passed * to a higher level handler. * *********************************************************/static Boolean MainFormHandleControl( // (out) true if handleded UInt16 controlID // (in) control ID of hit control){ Boolean handledEvt = false; FormPtr frmP; Err anErr; frmP = FrmGetActiveForm();// Execute the menu item switch (controlID) { case MainGoButton: fRetryCount = 0; if ( ( anErr = OpenPort() ) == noErr ) { MainFormSendCommand(); } else { StrPrintF( mainInfoBuff, "An error occurred (%d). The port was not opened.", anErr ); FrmCustomAlert( InfoAlert, mainInfoBuff, "", "" ); } // and return. handledEvt = true; break; default: break; } return handledEvt;}/********************************************************* * FUNCTION: MainFormHandleEvent * * DESCRIPTION: This routine is the event handler for MainForm. * * RETURNED: true if the event is handled. *********************************************************/ static Boolean MainFormHandleEvent // (out) true if the event is handled.( EventPtr eventP // (in) Pointer to an EventType that contains the event to handle){ Boolean handled = false; FormPtr frmP = FrmGetActiveForm (); switch ( eventP->eType ) { // Menu event - call MainFormDoMenuCommand to handle it. case menuEvent: handled = MainFormDoMenuCommand ( eventP->data.menu.itemID ); break; case frmOpenEvent: MainFormInit ( frmP ); // ***** WARNING - falls thru to next case ***** case frmUpdateEvent: FrmDrawForm ( frmP ); MainFormDraw( frmP ); handled = true; break; case frmCloseEvent: MainFormDone ( frmP ); handled = true; break; case ctlSelectEvent: // Handle the control handled = MainFormHandleControl(eventP->data.ctlSelect.controlID); break;// Ooops! Somebody SHUT US OFF -- were comms open? case keyDownEvent: if ( ( eventP->data.keyDown.modifiers & poweredOnKeyMask ) && ConnectedPort() ) { // oh-oh. The UART was on, and we think we're connected. // Reset ourselves and start over. if ( ConnectedPort() ) { ClosePort();// Simulate a tap on the go button anyway. MainFormHandleControl( MainGoButton ); }; handled = true; } break; case nilEvent: handled = true;// Poll and handle input until we get an event. if ( ConnectedPort() ) { if ( RecvPort( MainFormValidateInput ) == serErrTimeOut ) MainFormSendCommand(); } break; default: break; } return handled;}/********************************************************* * FUNCTION: AppHandleEvent * * DESCRIPTION: Loads a form's resources and set its event handler. * * RETURNED: true if the event is handled. *********************************************************/ static Boolean AppHandleEvent // (out) true if the event is handled.( EventPtr eventP // (in) Pointer to an EventType that contains the event to handle){ Boolean handled = false; if ( eventP->eType == frmLoadEvent ) { // Form load event--initialize the form and make it the active form. UInt16 formId = eventP->data.frmLoad.formID; FormPtr frmP = FrmInitForm ( formId ); FrmSetActiveForm ( frmP );// Set the event handler for the form. switch (formId) { case MainForm: FrmSetEventHandler(frmP, MainFormHandleEvent); handled = true; break;// Insert other cases as needed for other forms. } handled = true; } return handled;}/********************************************************* * FUNCTION: AppEventLoop * * DESCRIPTION: The main event loop for the application. * * RETURNED: nothing *********************************************************/ static void AppEventLoop( void){ UInt16 error; EventType event; do { EvtGetEvent ( &event, mainFormEvtWait ); if (! SysHandleEvent ( &event)) if (! MenuHandleEvent ( 0, &event, &error )) if (! AppHandleEvent ( &event )) FrmDispatchEvent ( &event ); } while ( event.eType != appStopEvent );}/********************************************************* * FUNCTION: AppStart * * DESCRIPTION: Do whatever is necessary to get started, like read the app preferences. * * RETURNED: Err value or 0 if nothing went wrong *********************************************************/ static UInt32 AppStart // (out ) Error( void){// Return an error code if necessary. return 0;}/********************************************************* * FUNCTION: AppStop * * DESCRIPTION: Do whatever you need to do, like save the preferences. * * RETURNED: nothing *********************************************************/static void AppStop( void){ FrmCloseAllForms ();}/********************************************************* * FUNCTION: PilotMain * * DESCRIPTION: The application main entry point. * * RETURNED: 0 if launch & execution are successful, non-zero otherwise. *********************************************************/ UInt32 PilotMain // (out) result code( UInt16 cmd, // (in) The launch code MemPtr /* cmdPBP*/, // (in) Pointer to the launch code structure UInt16 launchFlags // (in) Extra launch info){ UInt32 error = 0;// Check the ROM version for compatibility. error = RomVersionCompatible ( MIN_ROM_VERSION, launchFlags); if ( error == 0 ){// ROM OK, check for the various launch codes as needed. switch (cmd) { case sysAppLaunchCmdNormalLaunch: error = AppStart (); if ( error == 0 ) { FrmGotoForm ( MainForm ); AppEventLoop (); AppStop (); } break; } } return error;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -