📄 tiregauge.c
字号:
/***********************************************************************
** simpletemplate.c
***********************************************************************/
/*----------------------------------------------------------------------
** Define the Main TWD window filename.
**
** To use a filename other than "main.twd", you can give the name
** of your TWD file on the command line as the first argument or
** change the following line:
*/
#define MAIN_WINDOW_FILE "main.twd"
/*----------------------------------------------------------------------
** Change the following lines if you are going beyond a simple application
**
** Each engine can work with many applications but APPNAME should be unique
** unless the applications cooperate to share the same objects.
**
** Each application can open many channels to other applications and
** engines but each channel's USERPROG in the application must be unique.
*/
#define APPNAME "simpletemplate"
#define USERPROG "simpletemplate"
/*----------------------------------------------------------------------
** INCLUDE FILES
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tilcon/TRTAPI.h> /* Tilcon API functions */
#if defined (CC_TRT_DOS)
#include <windows.h>
#endif
/*----------------------------------------------------------------------
** define TRT_OS_ENV
**
** NOTE: do not change the order
** CC_TRT_WCE has CC_TRT_DOS defined as well and must follow it
*/
#ifdef CC_TRT_VXWORKS
#define TRT_OS_ENV TRT_VXWORKS
#endif
#ifdef CC_TRT_NTO
#define TRT_OS_ENV TRT_NTO_PHOTON
#endif
#ifdef CC_TRT_DOS
#define TRT_OS_ENV TRT_MS_WINDOWS
#endif
#ifdef CC_TRT_WCE
#define TRT_OS_ENV TRT_WCE
#endif
#ifdef CC_TRT_LINUX
#define TRT_OS_ENV TRT_LNX_X11R6
#endif
/*----------------------------------------------------------------------
** GLOBALS
*/
pid_t TRT_cid; /* Identifies Tilcon Runtime */
TRT_ReceiveData rec_data; /* Record from API */
char *main_window_file = MAIN_WINDOW_FILE; /* twd filename */
char main_window_id[TRT_MAX_ID_LENGTH]; /* window id inside file */
/*----------------------------------------------------------------------
** GUI Abstraction Layer
*/
void gui_finished()
{
TRT_SetValues(TRT_cid,"Start", TRT_ATT_DIM,0,NULL);
TRT_SetValues(TRT_cid,"Stop",TRT_ATT_DIM,1,NULL);
}
void gui_set_pressure(double pressure)
{
TRT_SetValues(TRT_cid,"Pressure",TRT_ATT_VALUE,pressure,NULL);
}
double gui_get_setpoint()
{
double tmp;
TRT_GetValues(TRT_cid,"Setpoint",TRT_ATT_VALUE,&tmp,NULL);
return(tmp);
}
/*----------------------------------------------------------------------
** SIMULATE HARDWARE
**
** the hardware is composed of the following in sequence:
** - a compressor
** - a valve to allow air to escape
** - a pressure sensor on the hose to the item being filled
*/
#define HW_SIMULATION 1
#define COMPRESSOR_OFF_VALVE_CLOSED 0 /* start and finish */
#define COMPRESSOR_OFF_VALVE_OPEN 1 /* pressure dropping */
#define COMPRESSOR_ON_VALVE_CLOSED 2 /* pressure increasing */
#define COMPRESSOR_ON_VALVE_OPEN 3 /* useless -- never use */
static int hw_state = COMPRESSOR_OFF_VALVE_CLOSED;
static double hw_pressure = 0.0;
void hw_simulate()
{
switch(hw_state)
{
case COMPRESSOR_OFF_VALVE_CLOSED:
break;
case COMPRESSOR_OFF_VALVE_OPEN:
hw_pressure -= 0.5;
break;
case COMPRESSOR_ON_VALVE_CLOSED:
hw_pressure += 0.5;
break;
case COMPRESSOR_ON_VALVE_OPEN:
/*
** should never be here
*/
break;
}
}
void hw_simulate_new_tire(double pressure)
{
hw_pressure = pressure;
}
double hw_read_pressure()
{
return(hw_pressure);
}
void hw_incr_pressure()
{
hw_state = COMPRESSOR_ON_VALVE_CLOSED;
}
void hw_decr_pressure()
{
hw_state = COMPRESSOR_OFF_VALVE_OPEN;
}
void hw_hold_pressure()
{
hw_state = COMPRESSOR_OFF_VALVE_CLOSED;
}
/*----------------------------------------------------------------------
** Application Logic
*/
#define APP_START 0
#define APP_FILLING 1
#define APP_EMPTYING 2
static int app_state = APP_START;
static double app_pressure;
static double app_setpoint;
void app_update()
{
/*
** Note that app_setpoint was read from the GUI layer in by app_start()
** and will not be reread until finished
*/
if(app_state != APP_START)
{
app_pressure = hw_read_pressure();
gui_set_pressure(app_pressure);
}
switch(app_state)
{
case APP_FILLING:
if(app_pressure >= app_setpoint)
{
hw_hold_pressure();
app_state = APP_START;
gui_finished();
}
break;
case APP_EMPTYING:
if(app_pressure <= app_setpoint)
{
hw_hold_pressure();
app_state = APP_START;
gui_finished();
}
break;
case APP_START:
/*
** nothing to do
*/
break;
}
}
void app_start()
{
switch(app_state)
{
case APP_START:
app_setpoint = gui_get_setpoint();
app_pressure = hw_read_pressure();
if(app_pressure > (app_setpoint + 0.25))
{
hw_decr_pressure();
app_state = APP_EMPTYING;
}
else if(app_pressure < (app_setpoint - 0.25))
{
hw_incr_pressure();
app_state = APP_FILLING;
}
else
{
/*
** tire is at the correct pressure
*/
#ifdef HW_SIMULATION
/*
** app logic for simulated hardware
**
** simulate a new tire at 0 pressure
*/
hw_simulate_new_tire(0.0);
gui_set_pressure(0.0);
hw_incr_pressure();
app_state = APP_FILLING;
#else
/*
** app logic for real hardware
*/
hw_hold_pressure();
app_state = APP_START;
gui_finished();
#endif
}
break;
case APP_FILLING:
case APP_EMPTYING:
/*
** ignore Start button presses in other states
** user may have pressed it twice quickly
*/
break;
}
}
void app_stop()
{
hw_hold_pressure();
app_state = APP_START;
}
/*----------------------------------------------------------------------
** On Error Exit With Message Box
*/
void ExitBox(char *ErrorMessage)
{
TRT_MessageBox( TRT_cid, NULL, APPNAME,
ErrorMessage, NULL, "OK", NULL,
TRT_MessageBox_FontHelvetica, 14,
TRT_MessageBox_Exclamation|TRT_MessageBox_Wait,
0, NULL, NULL);
TRT_Exit (TRT_cid);
exit(0);
}
/***********************************************************************
************************** Main Entry Point **************************
***********************************************************************/
#if defined(CC_TRT_DOS) && !defined(_CONSOLE)
/*----------------------------------------------------
** MS Windows non-console application entry point
*/
int PASCAL WinMain ( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
{
#else
/*----------------------------------------------------
** standard application entry point
*/
int main ( int argc, char *argv[] )
{
#endif
/*----------------------------------------------
** variables
*/
long errorcode = 0;
int c;
int ContinueLooping = TRUE;
TRT_StartData StartData;
/*----------------------------------------------
** if given an optional command line argument , use it
*/
#if defined(CC_TRT_DOS) && !defined(_CONSOLE)
if(lpszCmdLine && lpszCmdLine[0])
{
/*
** Assume the lpszCmdLine contains only
** the single TWD filename
*/
main_window_file = lpszCmdLine;
}
#else
if(argc > 1)
{
main_window_file = argv[1];
}
#endif
/*----------------------------------------------
** Initialize the StartData structure
*/
StartData.Os_Env = TRT_OS_ENV;
StartData.Display = NULL;
StartData.IPAddr = NULL;
StartData.AppName = APPNAME;
StartData.Userprog = USERPROG;
StartData.Flags = 0;
/*----------------------------------------------
** Start the Embedded Engine and connect to it
*/
errorcode = TRT_StartEx (0, &StartData);
TRT_cid = StartData.TRT_CID;
if(errorcode)
{
printf("Cannot Start Runtime\n");
exit(0);
}
/*----------------------------------------------
** force all API commands to be synchronous
*/
errorcode = TRT_Debug(TRT_cid, 3);
if(errorcode) ExitBox("Cannot run TRT_Debug");
/*----------------------------------------------
** Setup timer hints for 1/10th second
*/
errorcode = TRT_TimerHintEnable(TRT_cid,2); /* 2*50ms */
if(errorcode) ExitBox("Cannot start timerhints");
/*----------------------------------------------
** Load Main Window
*/
errorcode = TRT_WindowLoad(TRT_cid, main_window_file );
if(errorcode) ExitBox("Cannot load the Main Window! File NOT found");
/*----------------------------------------------
** Get the name of the Window
*/
errorcode = TRT_GetWindowID(TRT_cid,main_window_id);
if(errorcode) ExitBox("Cannot Get Main Window ID");
/*----------------------------------------------
** Display Main Window
*/
errorcode = TRT_WindowDisplay(TRT_cid,main_window_id);
if(errorcode) ExitBox("Cannot Display Main Window");
/*----------------------------------------------
** Main Loop
** Wait for communication from Engine
** Act on notification
*/
while(ContinueLooping)
{
/* Wait for a notification */
c = TRT_GetInput(NULL, 0, NULL, 0, &rec_data, TRT_BLOCK);
switch(c)
{
/*------------------
** Received a standard notification
*/
case 0:
{
/*
** what type of object is notifying us?
*/
switch (rec_data.code)
{
/*------------------
** if 'X' close button is presed
** on the window title bar, then stop looping
*/
case TRT_window:
if (rec_data.state == TRT_window_quit)
{
ContinueLooping = FALSE;
}
break;
/*------------------*/
case TRT_timer_hint:
hw_simulate();
app_update();
break;
/*------------------*/
case TRT_button:
if(strcmp(rec_data.ID,"Start") == 0)
{
app_start();
break;
}
/*------------------*/
if(strcmp(rec_data.ID,"Stop") == 0)
{
app_stop();
break;
}
/*
** deliberately fall through to default
** no break;
*/
/*------------------*/
default:
{
char message[40+TRT_MAX_ID_LENGTH];
sprintf(message,"Unhandled notification for object '%s'",rec_data.ID);
TRT_MessageBox( TRT_cid, NULL, APPNAME,
message, NULL, "OK", NULL,
TRT_MessageBox_FontHelvetica, 14,
TRT_MessageBox_Exclamation|TRT_MessageBox_Wait,
0, NULL, NULL);
}
}
}
break;
/*------------------
** A Callback function was called inside TRT_GetInput
*/
case 1:
break;
/*------------------
** Non-blocking mode -- no new events or a critical error
** Blocking mode -- a critical error
*/
case -1:
break;
/*------------------
** should never happen
*/
default:
break;
}
}
/*----------------------------------------------
** Cleanup and exit
*/
TRT_WindowDelete (TRT_cid, main_window_id ); /* Close Window */
TRT_Exit (TRT_cid); /* Notify Runtime to exit */
return(0);
}
/********************** end of file simpletemplate.c ********************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -