#pragma once
typedef unsigned char U1;
typedef unsigned short U2;
typedef unsigned long U4;
typedef char S1;
typedef short S2;
typedef long S4;
/* Define the priority and status of the alert */
typedef enum
{
WARNING1,
WARNING2,
WARNING3,
NO_WARNING
}WarningType;
common.h
#pragma once
/********************************/
/* Include File */
/********************************/
#include<stdio.h>
#include<stdbool.h>
#include <windows.h>
#include"type.h"
#define T_DIS_MIN (U1)(3) /* Assume that the minimum display time is 3 unit time */
#define T_DIS_CYCLE (U1)(8) /* It is assumed that the period display time is 8 units of time */
#define MIN_LINE (U1)(1)
/********************************/
/* Function List */
/********************************/
void vd_g_generateWarning(WarningType warning);/* Simulate the function generated by the warning */
void vd_g_updateWarningDisplay(void);/* Update the function for the warning display status */
const S1* s1_pg_warningStrings[] =
{
"WARNING1",
"WARNING2",
"WARNING3",
"NO_WARNING"
};
main.c
#include"common.h"
WarningType WT_g_currentWarning = NO_WARNING;/* Current warning status */
U1 u1_g_timeCounter = 0;/* Time counter */
bool bool_g_warningFlags[3] = { false, false, false }; /* Warning generation flags, corresponding to Warning1, Warning2, Warning3 */
/************************************************************************/
/* Function name:vd_g_updateWarningDisplay */
/* Summary:Simulate the function generated by the warning */
/* Input: warning */
/* return:- */
/************************************************************************/
void vd_g_generateWarning(WarningType warning)
{
bool_g_warningFlags[warning] = true;
}
/************************************************************************/
/* Function name:vd_g_updateWarningDisplay */
/* Summary:Update the function for the warning display status */
/* Input:- */
/* return:- */
/************************************************************************/
void vd_g_updateWarningDisplay(void)
{
/* If there are no current warnings, check if new warnings are generated */
if (NO_WARNING == WT_g_currentWarning)
{
if (true == bool_g_warningFlags[WARNING1])
{
WT_g_currentWarning = WARNING1;
bool_g_warningFlags[WARNING1] = false; /* reset flag */
u1_g_timeCounter = 0;
}
else if (true == bool_g_warningFlags[WARNING2])
{
WT_g_currentWarning = WARNING2;
bool_g_warningFlags[WARNING2] = false;
u1_g_timeCounter = 0;
}
else if (true == bool_g_warningFlags[WARNING3])
{
WT_g_currentWarning = WARNING3;
bool_g_warningFlags[WARNING3] = false;
u1_g_timeCounter = 0;
}
}
/* If there is currently a warning, update time counter */
if (NO_WARNING != WT_g_currentWarning)
{
u1_g_timeCounter = u1_g_timeCounter + 1;
/* Check if the minimum display time is reached, and if it is reached and there are no new warnings, the cycle display continues */
if (u1_g_timeCounter >= T_DIS_MIN && (true == bool_g_warningFlags[WARNING1] || true == bool_g_warningFlags[WARNING2] || true == bool_g_warningFlags[WARNING3] ))
{
/* Check if the cycle display time has been reached, and if so, it will be displayed in a loop according to the priority */
if (( u1_g_timeCounter >= T_DIS_MIN ) && ( u1_g_timeCounter <= T_DIS_CYCLE ))
{
if (WT_g_currentWarning == WARNING3)/* If Warning 3 is current, determine the priority at this time */
{
if (true == bool_g_warningFlags[WARNING1])
{
WT_g_currentWarning = WARNING1;
u1_g_timeCounter = 0; /* reset time counter */
}
else if (true == bool_g_warningFlags[WARNING2])
{
WT_g_currentWarning = WARNING2;
u1_g_timeCounter = 0; /* reset time counter */
}
}
else if (WT_g_currentWarning == WARNING2)/* If Warning 2 is current, determine the priority at this time */
{
if (true == bool_g_warningFlags[WARNING1])
{
WT_g_currentWarning = WARNING1;
u1_g_timeCounter = 0; /* reset time counter */
}
else
{
/* do nothing */
}
}
else/* If Warning 1 is current, determine the priority at this time */
{
/* do nothing,Leave the current warning status unchanged as Warning 1 */
}
}
else
{
u1_g_timeCounter = 0; /* reset time counter */
if (WT_g_currentWarning == WARNING3)/* If Warning 3 is current, determine the priority at this time */
{
if (true == bool_g_warningFlags[WARNING1])
{
WT_g_currentWarning = WARNING1;
}
else if (true == bool_g_warningFlags[WARNING2])
{
WT_g_currentWarning = WARNING2;
}
}
else if (WT_g_currentWarning == WARNING2)/* If Warning 2 is current, determine the priority at this time */
{
if (true == bool_g_warningFlags[WARNING1])
{
WT_g_currentWarning = WARNING1;
}
else
{
/* do nothing */
}
}
else/* If Warning 1 is current, determine the priority at this time */
{
/* do nothing,Leave the current warning status unchanged as Warning 1 */
}
}
}
else
{
/*do nothing*/
}
}
else
{
}
/* The actual display is performed based on the value of currentWarning */
printf("timeCounter: %d\n", u1_g_timeCounter);
printf("Current Warning: %s\n\n", s1_pg_warningStrings[WT_g_currentWarning]);
}
int main()
{
/* Simulated warnings are generated */
vd_g_generateWarning(WARNING3);
/* Simulate the passage of 30 units of time */
for (U1 u1_t_index = 0; u1_t_index < 30; u1_t_index++)
{
printf("timeAllCounter: %d\n", u1_t_index);
vd_g_updateWarningDisplay();
Sleep(200);
/* Simulate an alarm 2 and then continue simulating an alarm 1 */
if (( MIN_LINE > (u1_g_timeCounter % T_DIS_MIN) ) && ( u1_t_index >10) )
{
vd_g_generateWarning(WARNING2);
}
if (( MIN_LINE > ( u1_g_timeCounter % T_DIS_MIN) ) && ( u1_t_index > 20 ))
{
vd_g_generateWarning(WARNING1);
}
}
return 0;
}