📄 mobypalm.c
字号:
// MobyPalm
// Copyright (C) 2005 Olivier Burgard
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <PalmOS.h>
#include "MobyPalm.h"
#include "Common.h"
#include "Mobydic.h"
#include "Serial.h"
#include "Terminal.h"
#define appFileCreator 'OBD!'
#define appVersionNum 0x01
#define appPrefID 0x00
#define appPrefVersionNum 0x01
enum {START, MOBYDICCONNECTED, SUPPORTEDPIDLOADED};
enum {CONT, NONCONT};
static int State = START;
static int tabDisplayed = CONT;
static BOOL refresh = false;
static int Test=0;
static char WaitingString[]="-\\|/";
//static char* Title[] = {"", "Supp.", "Compl."};
static char* ContinuousMonitoring[] = {"Misfire ", "Fuel system ", "Components "};
static char* NonContinuousMonitoring[] = { "Catalyst ", "Heated catalyst ", "Evaporative system ", "Secondary air system ", "A/C system refrigerant ", "Oxygen sensor ", "Oxygen sensor heater ", "EGR system "};
//static char* NA = "N/A";
static void MainFormDisplay(){
setSupportOBDToLabel(OBDSupportLabel);
}
static void MainFormDisplayTable() {
UInt8 answer[10];
int nbanswer;
int params[1];
FormType* pForm;
char text[10];
int row;
TablePtr tableP;
if (isPIDSupported(0x01)){
params[0]=1;
nbanswer=sendGenericOBDCommand(1, params, 1, answer);
if (nbanswer>0){
pForm = FrmGetActiveForm();
if (answer[DataA]>>7 & 0x01) {
FrmHideObject(pForm, FrmGetObjectIndex(pForm, BlackBitmap));
FrmShowObject(pForm, FrmGetObjectIndex(pForm, MILOnBitmap));
FrmHideObject(pForm, FrmGetObjectIndex(pForm, MILOffBitmap));
} else {
FrmHideObject(pForm, FrmGetObjectIndex(pForm, BlackBitmap));
FrmHideObject(pForm, FrmGetObjectIndex(pForm, MILOnBitmap));
FrmShowObject(pForm, FrmGetObjectIndex(pForm, MILOffBitmap));
}
StrPrintF(text, "%u", answer[DataA] & 0x7F);
SetLabelText(MainForm, DTCNbrLabel, text);
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContinuousMonitoringTable));
for (row = 0; row < 3; row++) {
TblSetItemInt (tableP, row, 1, answer[DataB] >> row & 0x01);
TblSetItemInt (tableP, row, 2, answer[DataB] >> (row+4) & 0x01);
}
if (tabDisplayed==CONT)
TblDrawTable (tableP);
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContinuousMonitoringTable));
for (row = 0; row < 8; row++) {
TblSetItemInt (tableP, row, 1, answer[DataC] >> row & 0x01);
TblSetItemInt (tableP, row, 2, answer[DataD] >> row & 0x01);
}
if (tabDisplayed==NONCONT)
TblDrawTable (tableP);
}
}
}
/***********************************************************************
*
* FUNCTION: AboutFormHandleEvent
*
* DESCRIPTION: This routine is the event handler for the
* "AboutForm" of this application.
*
* PARAMETERS: pEvent - a pointer to an EventType structure
*
* RETURNED: true if the event has handle and should not be passed
* to a higher level handler.
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Boolean AboutFormHandleEvent(EventType* pEvent)
{
Boolean handled = false;
FormType* pForm;
switch (pEvent->eType) {
case frmOpenEvent:
pForm = FrmGetActiveForm();
FrmDrawForm(pForm);
CtlSetEnabled(FrmGetObjectPtr(pForm, FrmGetObjectIndex(pForm, AboutButton)), false);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm, AboutButton)), 1);
if (checkMobydic()) {
setMobydicVersionToLabel(VersionMobydicLabel);
setChipIdentToLabel(ChipIdentLabel);
setFoundProtocolToLabel(FoundProtocolLabel);
} else
refresh=true;
handled = true;
break;
case ctlSelectEvent:
switch (pEvent->data.ctlEnter.controlID) {
case TerminalButton:
refresh=false;
FrmGotoForm(TerminalForm);
handled = true;
break;
case GeneralButton:
FrmGotoForm(MainForm);
handled=true;
break;
default:
break;
}
break;
case nilEvent:
if (checkMobydic()) {
setMobydicVersionToLabel(VersionMobydicLabel);
setChipIdentToLabel(ChipIdentLabel);
setFoundProtocolToLabel(FoundProtocolLabel);
refresh=false;
}
handled = true;
break;
default:
break;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: MainFormHandleEvent
*
* DESCRIPTION: This routine is the event handler for the
* "MainForm" of this application.
*
* PARAMETERS: pEvent - a pointer to an EventType structure
*
* RETURNED: true if the event has handle and should not be passed
* to a higher level handler.
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Boolean MainFormHandleEvent(EventType* pEvent)
{
Boolean handled = false;
FormType* pForm;
TablePtr tableP;
Char msg[2];
int row;
int i;
pForm = FrmGetActiveForm();
switch (pEvent->eType) {
case frmOpenEvent:
FrmDrawForm(pForm);
State=START;
CtlSetEnabled(FrmGetObjectPtr(pForm, FrmGetObjectIndex(pForm, GeneralButton)), false);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm, GeneralButton)), 1);
FrmHideObject(pForm, FrmGetObjectIndex(pForm, MILOffBitmap));
FrmHideObject(pForm, FrmGetObjectIndex(pForm, MILOnBitmap));
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContinuousMonitoringTable));
for (row = 0; row < 8; row++) {
TblSetItemStyle (tableP, row, 0, labelTableItem);
TblSetItemStyle (tableP, row, 1, checkboxTableItem);
TblSetItemStyle (tableP, row, 2, checkboxTableItem);
TblSetItemPtr (tableP, row, 0, NonContinuousMonitoring[row]);
TblSetItemInt (tableP, row, 1, 0);
TblSetItemInt (tableP, row, 2, 0);
}
for (i = 0; i < 3; i++) {
TblSetColumnUsable(tableP,i,true);
}
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContinuousMonitoringTable));
for (row = 0; row < 4; row++) {
TblSetItemStyle (tableP, row, 0, labelTableItem);
TblSetItemStyle (tableP, row, 1, checkboxTableItem);
TblSetItemStyle (tableP, row, 2, checkboxTableItem);
TblSetItemPtr (tableP, row, 0, ContinuousMonitoring[row]);
TblSetItemInt (tableP, row, 1, 0);
TblSetItemInt (tableP, row, 2, 0);
}
for (i = 0; i < 3; i++) {
TblSetColumnUsable(tableP,i,true);
}
TblDrawTable (tableP);
tabDisplayed=CONT;
CtlSetEnabled(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), false);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), 1);
refresh=true;
if (checkMobydic()) {
State=MOBYDICCONNECTED;
MainFormDisplay();
UpdateStatus("Loading Supported PID");
if (loadSupportedPID()){
State=SUPPORTEDPIDLOADED;
UpdateStatus("");
MainFormDisplayTable();
refresh=false;
}
}
handled = true;
break;
case ctlSelectEvent:
switch (pEvent->data.ctlEnter.controlID) {
case TerminalButton:
refresh=false;
FrmGotoForm(TerminalForm);
handled = true;
break;
case AboutButton:
refresh=false;
FrmGotoForm(AboutForm);
handled = true;
break;
case ContButton:
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContinuousMonitoringTable));
TblDrawTable (tableP);
CtlSetEnabled(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), false);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), 1);
CtlSetEnabled(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContButton)), true);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContButton)), 0);
tabDisplayed=CONT;
handled = true;
break;
case NonContButton:
tableP = FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContinuousMonitoringTable));
TblDrawTable (tableP);
CtlSetEnabled(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), true);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,ContButton)), 0);
CtlSetEnabled(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContButton)), false);
CtlSetValue(FrmGetObjectPtr( pForm, FrmGetObjectIndex(pForm,NonContButton)), 1);
tabDisplayed=NONCONT;
handled = true;
break;
default:
break;
}
break;
case nilEvent:
if (State==START) {
if (checkMobydic()) {
State=MOBYDICCONNECTED;
MainFormDisplay();
UpdateStatus("");
} else {
msg[0]=WaitingString[Test];
msg[1]=0;
UpdateStatus(msg);
Test=Test+1;
if (Test==StrLen(WaitingString))
Test=0;
}
} else if (State==MOBYDICCONNECTED) {
UpdateStatus("Loading Supported PID");
if (loadSupportedPID()){
State=SUPPORTEDPIDLOADED;
UpdateStatus("");
MainFormDisplayTable();
refresh=false;
}
}
handled = true;
break;
default:
break;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: AppHandleEvent
*
* DESCRIPTION: This routine loads form resources and set the event
* handler for the form loaded.
*
* PARAMETERS: event - a pointer to an EventType structure
*
* RETURNED: true if the event has handle and should not be passed
* to a higher level handler.
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Boolean AppHandleEvent(EventType* pEvent)
{
UInt16 formId;
FormType* pForm;
Boolean handled = false;
if (pEvent->eType == frmLoadEvent) {
// Load the form resource.
formId = pEvent->data.frmLoad.formID;
pForm = FrmInitForm(formId);
FrmSetActiveForm(pForm);
// Set the event handler for the form. The handler of the currently
// active form is called by FrmHandleEvent each time is receives an
// event.
switch (formId) {
case MainForm:
FrmSetEventHandler(pForm, MainFormHandleEvent);
break;
case TerminalForm:
FrmSetEventHandler(pForm, TerminalFormHandleEvent);
break;
case AboutForm:
FrmSetEventHandler(pForm, AboutFormHandleEvent);
break;
default:
break;
}
handled = true;
}
return handled;
}
/***********************************************************************
*
* FUNCTION: AppStart
*
* DESCRIPTION: Get the current application's preferences.
*
* PARAMETERS: nothing
*
* RETURNED: Err value errNone if nothing went wrong
*
* REVISION HISTORY:
*
*
***********************************************************************/
static Err AppStart(void)
{
FrmGotoForm(MainForm);
return errNone;
}
/***********************************************************************
*
* FUNCTION: AppStop
*
* DESCRIPTION: Save the current state of the application.
*
* PARAMETERS: nothing
*
* RETURNED: nothing
*
* REVISION HISTORY:
*
*
***********************************************************************/
static void AppStop(void)
{
// Close all the open forms.
FrmCloseAllForms();
}
/***********************************************************************
*
* FUNCTION: AppEventLoop
*
* DESCRIPTION: This routine is the event loop for the application.
*
* PARAMETERS: nothing
*
* RETURNED: nothing
*
* REVISION HISTORY:
*
*
***********************************************************************/
static void AppEventLoop(void)
{
Err error;
EventType event;
do {
if (!refresh)
EvtGetEvent(&event, evtWaitForever);
else
EvtGetEvent(&event, SysTicksPerSecond()/2);
if (SysHandleEvent(&event))
continue;
if (MenuHandleEvent(0, &event, &error))
continue;
if (AppHandleEvent(&event))
continue;
FrmDispatchEvent(&event);
} while (event.eType != appStopEvent);
}
/***********************************************************************
*
* FUNCTION: PilotMain
*
* DESCRIPTION: This is the main entry point for the application.
*
* PARAMETERS: cmd - word value specifying the launch code.
* cmdPB - pointer to a structure that is associated with the launch code.
* launchFlags - word value providing extra information about the launch.
* RETURNED: Result of launch
*
* REVISION HISTORY:
*
*
***********************************************************************/
UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
Err error = errNone;
switch (cmd) {
case sysAppLaunchCmdNormalLaunch:
if ((error = AppStart()) == 0) {
AppEventLoop();
AppStop();
}
break;
default:
break;
}
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -