/*********************************************************************** * * All rights reserved. * * PROJECT: Logic Analyzer Palm App * * FILE: Logic.c * * * REVISION HISTORY: * Toan Nguyen 3-16-02 * **********************************************************************/ #include // all the system toolbox headers #include #include #include #include "LogicRsc.h" // application resource defines #define myReceiveBuffSize 100 #define k2KBytes 2048 static Word CurrentView; static CharPtr rec0,rec1,rec2,rec3,rec4,rec5,rec6,rec7; static MenuBarPtr CurrentMenu = NULL; /*********************************************************************** * Prototypes for internal functions **********************************************************************/ static void StartApplication(void); static Boolean MainFormHandleEvent(EventPtr event); static void EventLoop(void); static void SetCurrentMenu(Word rscID); static FieldPtr GetFocusObjectPtr(void); static Boolean ApplicationHandleEvent(EventPtr event); static Boolean Result1FormHandleEvent(EventPtr event); static Boolean Resul2FormHandleEvent(EventPtr event); static Boolean WelcomeFormHandleEvent(EventPtr event); static void EditDoMenuCommand(Word command); static VoidPtr GetObjectPtr(Int objectID); /*********************************************************************** * * FUNCTION: GetObjectPtr * * DESCRIPTION: This routine returns a pointer to the object field * * PARAMETERS: Object Id * * RETURNED: Pointer to object * ***********************************************************************/ static VoidPtr GetObjectPtr(Int objectID) { FormPtr frm; frm = FrmGetActiveForm(); return(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objectID))); } /*********************************************************************** * * FUNCTION: StartApplication * * DESCRIPTION: This routine sets up the initial state of the application. * * PARAMETERS: None. * * RETURNED: Nothing. * ***********************************************************************/ static void StartApplication(void) { FormPtr frm; CurrentView = WelcomeForm; // Initialize and draw the main memo pad form. frm = FrmInitForm(WelcomeForm); FrmSetActiveForm(frm); FrmDrawForm(frm); SetCurrentMenu(OptionsMenu); } /*********************************************************************** * * FUNCTION: SetCurrentMenu * * DESCRIPTION: This routine sets the current menu for a certain form * * PARAMETERS: Resource ID * * RETURNED: Nothing. * ***********************************************************************/ static void SetCurrentMenu(Word rscID) { // Dispose of an existing current menu. if (CurrentMenu) MenuDispose(CurrentMenu); // Set the current menu and remember it. CurrentMenu = MenuInit(rscID); } /*********************************************************************** * * FUNCTION: GetFocusObjectPtr * * DESCRIPTION: This routine sets Focus of the current field * * PARAMETERS: Nothing * * RETURNED: Field Pointer * ***********************************************************************/ static FieldPtr GetFocusObjectPtr (void) { FormPtr frm; Word focus; // get a pointer to tha active form and the index of the form object with focus frm = FrmGetActiveForm (); focus = FrmGetFocus (frm); // if no object has the focus return NULL pointer if (focus == noFocus) return (NULL); // return a pointer to the object with focus return (FrmGetObjectPtr (frm, focus)); } /*********************************************************************** * * FUNCTION: EditDoMenuCommand * * DESCRIPTION: This routine process the menu commands * * PARAMETERS: Command * * RETURNED: Nothing * ***********************************************************************/ static void EditDoMenuCommand(Word command) { //FieldPtr fld; FormPtr frm; FieldPtr fld; CharPtr newText; char sendbits[16]; unsigned short num, *ref; Err error; Word fldIndex; Handle newHandle; Boolean handled = false; Long toReceive, numReceived; Long timeout; Err err; char buff[1],display[2]; // Do the appropriate action for the menu command selected. switch (command) { case OptionsAboutUs: // Load the info form, then display it. frm = FrmInitForm(AboutUsForm); FrmDoDialog(frm); // Delete the info form. FrmDeleteForm(frm); break; case OptionsInstructions: // Load the info form, then display it. frm = FrmInitForm(InfoForm); FrmDoDialog(frm); // Delete the info form. FrmDeleteForm(frm); break; case OptionsGetFPGAVersion: // Load the info form, then display it. frm = FrmInitForm(VersionForm); rec0 = "VersionTest"; sendbits[0]='s'; sendbits[1]=0x01; sendbits[2]='\0'; ref = # SysLibFind("Serial Library",ref); SerOpen(num,0,57600); SerSend(num,sendbits,StrLen(sendbits),&error); SerSendWait(num,-1); toReceive = 1; // want to receive 10 bytes timeout = SysTicksPerSecond () *10; /// 2; // 1/2 sec inter-byte timeout ErrFatalDisplayIf (toReceive > sizeof(buff), "buffer overflow"); numReceived = SerReceive (num, buff, toReceive, timeout, &err); if ( err == serErrLineErr ) { // Take some action and clear the error (see SerGetStatus) // Need to clear the error status or all subsequent receive calls will fail SerClearErr (num); } SerClose(num); display[0]=buff[0]; buff[1]='\0'; rec0=buff; newHandle = MemHandleNew(StrLen(rec0)+1); newText = MemHandleLock(newHandle); StrCopy(newText, rec0); fldIndex = FrmGetObjectIndex(frm, VersionVersionField); fld = FrmGetObjectPtr(frm, fldIndex); FldSetTextHandle(fld, newHandle); FrmDoDialog(frm); // Delete the info form. FrmDeleteForm(frm); break; } } /*********************************************************************** * * FUNCTION: MainFormHandleEvent * * DESCRIPTION: Handles processing of events for the ÒmainÓ form. * * PARAMETERS: event - the most recent event. * * RETURNED: True if the event is handled, false otherwise. * ***********************************************************************/ static Boolean MainFormHandleEvent(EventPtr event) { FormPtr frm; //Word fldIndex; //FieldPtr fld; Boolean handled = false; EventType newEvent; char sendbits[16], *pointer; char msg[] = "logon\n"; //CharPtr text; unsigned short num, *ref; Err error, *perr; pointer = &sendbits[0]; perr = &error; ref = # frm = FrmGetActiveForm(); switch (event->eType) { case ctlSelectEvent: if (event->data.ctlEnter.controlID == LogicMainExitButton) { MemSet(&newEvent, sizeof(EventType), 0); newEvent.eType = appStopEvent; EvtAddEventToQueue(&newEvent); handled = true; } else if (event->data.ctlEnter.controlID == LogicMainBsendButton) { sendbits[0]='w'; sendbits[1]=0x01; sendbits[3]=0x03; sendbits[4]='\0'; SysLibFind("Serial Library",ref); //*Channel 0 and 1 if (CtlGetValue(GetObjectPtr(LogicMainCh0dcCheckbox))!=0) if (CtlGetValue(GetObjectPtr(LogicMainCh1dcCheckbox))!=0) //*Both don't care sendbits[2] = 0x77; else //*Don't care and high if (CtlGetValue(GetObjectPtr(LogicMainCh1hlCheckbox))!=0) sendbits[2] = 0x17; //*Don't care and low else sendbits[2] = 0x27; else //*High if (CtlGetValue(GetObjectPtr(LogicMainCh0hlCheckbox))!=0) //*High and don't care if (LogicMainCh1dcCheckbox) sendbits[2] = 0x71; else //*High and High if (LogicMainCh1hlCheckbox) sendbits[2] = 0x11; //*High and low else sendbits[2] = 0x21; else //*low and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh1dcCheckbox))!=0) sendbits[2] = 0x72; else //*low and High if (CtlGetValue(GetObjectPtr(LogicMainCh1hlCheckbox))!=0) sendbits[2] = 0x12; //*low and low else sendbits[2] = 0x22; SerOpen(num,0,57600); SerSend(num,sendbits,StrLen(sendbits),&error); SerSendWait(num,-1); SerClose(num); //*Channel 2 and 3 if (CtlGetValue(GetObjectPtr(LogicMainCh2dcCheckbox))!=0) if (CtlGetValue(GetObjectPtr(LogicMainCh3dcCheckbox))!=0) //*Both don't care sendbits[2] = 0x77; else //*Don't care and high if (CtlGetValue(GetObjectPtr(LogicMainCh3hlCheckbox))!=0) sendbits[2] = 0x17; //*Don't care and low else sendbits[2] = 0x27; else //*High if (CtlGetValue(GetObjectPtr(LogicMainCh2hlCheckbox))!=0) //*High and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh3dcCheckbox))!=0) sendbits[2] = 0x71; else //*High and High if (CtlGetValue(GetObjectPtr(LogicMainCh3hlCheckbox))!=0) sendbits[2] = 0x11; //*High and low else sendbits[2] = 0x21; else //*low and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh3dcCheckbox))!=0) sendbits[2] = 0x72; else //*low and High if (CtlGetValue(GetObjectPtr(LogicMainCh3hlCheckbox))!=0) sendbits[2] = 0x12; //*low and low else sendbits[2] = 0x22; SerOpen(num,0,57600); SerSend(num,sendbits,StrLen(sendbits),&error); SerSendWait(num,-1); SerClose(num); //*Channel 4 and 5 if (CtlGetValue(GetObjectPtr(LogicMainCh4dcCheckbox))!=0) if (CtlGetValue(GetObjectPtr(LogicMainCh5dcCheckbox))!=0) //*Both don't care sendbits[2] = 0x77; else //*Don't care and high if (CtlGetValue(GetObjectPtr(LogicMainCh5hlCheckbox))!=0) sendbits[2] = 0x17; //*Don't care and low else sendbits[2] = 0x27; else //*High if (CtlGetValue(GetObjectPtr(LogicMainCh4hlCheckbox))!=0) //*High and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh5dcCheckbox))!=0) sendbits[2] = 0x71; else //*High and High if (CtlGetValue(GetObjectPtr(LogicMainCh5hlCheckbox))!=0) sendbits[2] = 0x11; //*High and low else sendbits[2] = 0x21; else //*low and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh5dcCheckbox))!=0) sendbits[2] = 0x72; else //*low and High if (CtlGetValue(GetObjectPtr(LogicMainCh5hlCheckbox))!=0) sendbits[2] = 0x12; //*low and low else sendbits[2] = 0x22; SerOpen(num,0,57600); SerSend(num,sendbits,StrLen(sendbits),&error); SerSendWait(num,-1); SerClose(num); //*Channel 6 and 7 if (CtlGetValue(GetObjectPtr(LogicMainCh6dcCheckbox))!=0) if (CtlGetValue(GetObjectPtr(LogicMainCh7dcCheckbox))!=0) //*Both don't care sendbits[2] = 0x77; else //*Don't care and high if (CtlGetValue(GetObjectPtr(LogicMainCh7hlCheckbox))!=0) sendbits[2] = 0x17; //*Don't care and low else sendbits[2] = 0x27; else //*High if (CtlGetValue(GetObjectPtr(LogicMainCh6hlCheckbox))!=0) //*High and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh7dcCheckbox))!=0) sendbits[2] = 0x71; else //*High and High if (CtlGetValue(GetObjectPtr(LogicMainCh7hlCheckbox))!=0) sendbits[2] = 0x11; //*High and low else sendbits[2] = 0x21; else //*low and don't care if (CtlGetValue(GetObjectPtr(LogicMainCh7dcCheckbox))!=0) sendbits[2] = 0x72; else //*low and High if (CtlGetValue(GetObjectPtr(LogicMainCh7hlCheckbox))!=0) sendbits[2] = 0x12; //*low and low else sendbits[2] = 0x22; SerOpen(num,0,57600); SerSend(num,sendbits,StrLen(sendbits),&error); SerSendWait(num,-1); SerClose(num); rec0="101010101";//"Test0"; rec1="101111001";//"Test1"; rec2="101010000";//"Test2"; rec3="110110110";//"Test3"; rec4="110101001";//"Test4"; rec5="111101010";//"Test5"; rec6="110110100";//"Test6"; rec7="111010001";//"Test7"; //Go to the result form FrmGotoForm(Result1Form); handled = true; } case frmOpenEvent: // P3. The form was told to open. // It has already been loaded and activated so just draw it. FrmDrawForm (FrmGetActiveForm()); handled = true; break; case menuEvent: // First clear the menu status from the display. MenuEraseStatus(0); // Process menu commands for the edit form. EditDoMenuCommand(event->data.menu.itemID); /*MenuEraseStatus(CurrentMenu); frm = FrmInitForm(InfoForm); FrmDoDialog(frm);*/ handled = true; break; } return(handled); } /*********************************************************************** * * FUNCTION: MainFormHandleEvent * * DESCRIPTION: Handles processing of events for the ÒmainÓ form. * * PARAMETERS: event - the most recent event. * * RETURNED: True if the event is handled, false otherwise. * ***********************************************************************/ static Boolean WelcomeFormHandleEvent(EventPtr event) { Boolean handled = false; FormPtr frm; //EventType newEvent; switch (event->eType) { case ctlSelectEvent: if (event->data.ctlEnter.controlID == WelcomeWelcomeButton) { FrmGotoForm(LogicMainForm); handled = true; } case frmOpenEvent: // The form was told to open. // It has already been loaded and activated so just draw it. FrmDrawForm (FrmGetActiveForm()); handled = true; break; case menuEvent: MenuEraseStatus(CurrentMenu); frm = FrmInitForm(InfoForm); FrmDoDialog(frm); handled = true; break; } return(handled); } /*********************************************************************** * * FUNCTION: Result1FormHandleEvent * * DESCRIPTION: Handles processing of events for the ÒResult1Ó form. * * PARAMETERS: event - the most recent event. * * RETURNED: True if the event is handled, false otherwise. * ***********************************************************************/ static Boolean Result1FormHandleEvent(EventPtr event) { FormPtr frm; FieldPtr fld,fld1,fld2,fld3; CharPtr newText,newText1,newText2,newText3; Word fldIndex,fldIndex1,fldIndex2,fldIndex3; Handle newHandle,newHandle1,newHandle2,newHandle3; Boolean handled = false; frm = FrmGetActiveForm(); newHandle = MemHandleNew(StrLen(rec0) + 1); newText = MemHandleLock(newHandle); StrCopy(newText, rec0); fldIndex = FrmGetObjectIndex(frm, Result1C0Field); fld = FrmGetObjectPtr(frm, fldIndex); FldSetTextHandle(fld, newHandle); newHandle1 = MemHandleNew(StrLen(rec1) + 1); newText1 = MemHandleLock(newHandle1); StrCopy(newText1, rec1); fldIndex1 = FrmGetObjectIndex(frm, Result1C1Field); fld1 = FrmGetObjectPtr(frm, fldIndex1); FldSetTextHandle(fld1, newHandle1); newHandle2 = MemHandleNew(StrLen(rec2) + 1); newText2 = MemHandleLock(newHandle2); StrCopy(newText2, rec2); fldIndex2 = FrmGetObjectIndex(frm, Result1C2Field); fld2 = FrmGetObjectPtr(frm, fldIndex2); FldSetTextHandle(fld2, newHandle2); newHandle3 = MemHandleNew(StrLen(rec3) + 1); newText3 = MemHandleLock(newHandle3); StrCopy(newText3, rec3); fldIndex3 = FrmGetObjectIndex(frm, Result1C3Field); fld3 = FrmGetObjectPtr(frm, fldIndex3); FldSetTextHandle(fld3, newHandle3); switch (event->eType) { case ctlSelectEvent: // A control button was pressed and released. // If the done button is pressed, go back to the main form. if (event->data.ctlEnter.controlID == Result1OkButton) { FrmGotoForm(LogicMainForm); handled = true; } else if (event->data.ctlEnter.controlID == Result1BnextButton) { FrmGotoForm(Result2Form); handled = true; } case frmOpenEvent: // The form was told to open. // It has already been loaded and activated so just draw it. FrmDrawForm (FrmGetActiveForm()); handled = true; break; } return(handled); } /*********************************************************************** * * FUNCTION: Result2FormHandleEvent * * DESCRIPTION: Handles processing of events for the ÒResult2Ó form. * * PARAMETERS: event - the most recent event. * * RETURNED: True if the event is handled, false otherwise. * ***********************************************************************/ static Boolean Resul2FormHandleEvent(EventPtr event) { FormPtr frm; FieldPtr fld,fld1,fld2,fld3; CharPtr newText,newText1,newText2,newText3; Word fldIndex,fldIndex1,fldIndex2,fldIndex3; Handle newHandle,newHandle1,newHandle2,newHandle3; Boolean handled = false; frm = FrmGetActiveForm(); newHandle = MemHandleNew(StrLen(rec4) + 1); newText = MemHandleLock(newHandle); StrCopy(newText, rec4); fldIndex = FrmGetObjectIndex(frm, Result2C4Field); fld = FrmGetObjectPtr(frm, fldIndex); FldSetTextHandle(fld, newHandle); newHandle1 = MemHandleNew(StrLen(rec5) + 1); newText1 = MemHandleLock(newHandle1); StrCopy(newText1, rec5); fldIndex1 = FrmGetObjectIndex(frm, Result2C5Field); fld1 = FrmGetObjectPtr(frm, fldIndex1); FldSetTextHandle(fld1, newHandle1); newHandle2 = MemHandleNew(StrLen(rec6) + 1); newText2 = MemHandleLock(newHandle2); StrCopy(newText2, rec6); fldIndex2 = FrmGetObjectIndex(frm, Result2C6Field); fld2 = FrmGetObjectPtr(frm, fldIndex2); FldSetTextHandle(fld2, newHandle2); newHandle3 = MemHandleNew(StrLen(rec7) + 1); newText3 = MemHandleLock(newHandle3); StrCopy(newText3, rec7); fldIndex3 = FrmGetObjectIndex(frm, Result2C7Field); fld3 = FrmGetObjectPtr(frm, fldIndex3); FldSetTextHandle(fld3, newHandle3); switch (event->eType) { case ctlSelectEvent: // A control button was pressed and released. // If the done button is pressed, go back to the main form. if (event->data.ctlEnter.controlID == Result2OkButton) { FrmGotoForm(LogicMainForm); handled = true; } else if (event->data.ctlEnter.controlID == Result2BprevButton) { FrmGotoForm(Result1Form); handled = true; } case frmOpenEvent: // The form was told to open. // It has already been loaded and activated so just draw it. FrmDrawForm (FrmGetActiveForm()); handled = true; break; } return(handled); } /*********************************************************************** * * FUNCTION: ApplicationHandleEvent * * DESCRIPTION: Handles processing of events for the Application * * PARAMETERS: event - the most recent event. * * RETURNED: True if the event is handled, false otherwise. * ***********************************************************************/ static Boolean ApplicationHandleEvent (EventPtr event) { FormPtr frm; Word formId; Boolean handled = false; if (event->eType == frmLoadEvent) { // load the form resource specified in the event then activate the form. formId = event->data.frmLoad.formID; frm = FrmInitForm(formId); FrmSetActiveForm(frm); // set the event handler for the form. The handler of the currently // active form is called by FrmDispatchEvent each time it receives an event. switch (formId) { case WelcomeForm: FrmSetEventHandler(frm, WelcomeFormHandleEvent); break; case LogicMainForm: FrmSetEventHandler(frm, MainFormHandleEvent); break; case Result1Form: FrmSetEventHandler(frm, Result1FormHandleEvent); break; case Result2Form: FrmSetEventHandler(frm, Resul2FormHandleEvent); break; } handled = true; } return handled; } /*********************************************************************** * * FUNCTION: EventLoop * * DESCRIPTION: A simple loop that obtains events from the Event * Manager and passes them on to various applications and * system event handlers before passing them on to * FrmHandleEvent for default processing. * * PARAMETERS: None. * * RETURNED: Nothing. * ***********************************************************************/ static void EventLoop(void) { EventType event; Word error; do { // Get the next available event. EvtGetEvent(&event, evtWaitForever); // Give the system a chance to handle the event. if (! SysHandleEvent (&event)) if (! MenuHandleEvent(CurrentMenu, &event, &error)) // Give the application a chance to handle the event. if (! ApplicationHandleEvent(&event)) // Let the form object provide default handling of the event. FrmDispatchEvent(&event); //FrmHandleEvent(FrmGetActiveForm(), &event); } while (event.eType != appStopEvent); // ** SPECIAL NOTE ** // In order for the Emulator to exit // cleanly when the File|Quit menu option is // selected, the running application // must exit. The emulator will generate an // ÒappStopEventÓ when Quit is selected. } /*********************************************************************** * * FUNCTION: PilotMain * * DESCRIPTION: This function is the equivalent of a main() function * in standard ÒCÓ. It is called by the Emulator to begin * execution of this application. * * PARAMETERS: cmd - command specifying how to launch the application. * cmdPBP - parameter block for the command. * launchFlags - flags used to configure the launch. * * RETURNED: Any applicable error code. * ***********************************************************************/ DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags) { // Check for a normal launch. if (cmd == sysAppLaunchCmdNormalLaunch) { // Set up initial form. StartApplication(); FrmGotoForm(CurrentView); //println("\n This is cool"); // Start up the event loop. EventLoop(); } return(0); }