我整天都在摆弄这段代码,但我又被困住了……提前谢谢
基本上这个程序给了我问题,下面是我认为导致问题的代码片段。基本上,用户可以更改他们的“当前温度”,如果它超过上限或下限的阈值,则会调用“compareLimit”函数......然后此函数会调用各种其他函数(例如哔哔声,并允许一条线传感器记录打印在屏幕上)。但是,以下是我的问题(请记住,我必须使用哔哔声,但如果您有更好的方法,请告知):
哔哔声让人头疼,我的整个“警报”模式都围绕哔哔声的持续时间展开。这意味着每次我想按字母“O”来打印报告时,它都会随着哔哔声的结束而随心所欲地打印。我该如何消除这个?
我想让用户即使在警报模式的“while循环”中仍然能够访问最后一个代码块(case语句),以便他们可以通过按 r 或 f 来更改实际退出警报模式温度,并使系统恢复正常。
void updateDisplay() { clrscr(); HANDLE hConsole; //standard c library call hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //used for output screen buffer to allow for coloured text SetConsoleTextAttribute(hConsole, 2); //sets output screen colour for following text printf("\nCurrent Temperature for channel 1 is %d\n\n", temperatureSensor1Reading); printf("Upper Limit for channel 1 is %d\n\n", getHighLimit(CH1)); printf("Lower Limit for channel 1 is %d\n\n", getLowLimit(CH1)); setCurrentTemperature(CH1,temperatureSensor1Reading);
这个代码块是我从同一个项目中的其他 cpp 源文件中调用我的函数的地方,以开始将当前温度与设置的限制进行比较。
comparedLimits1 = compareLimit(CH1);
SetConsoleTextAttribute(hConsole, 14);
if (comparedLimits1 == TRUE) {
printf("please press O to print out a temperature report for channel %i \n \n", selectChannel + 1);
printf("please press P to silence the alarm for channel %i \n \n", selectChannel + 1);
}
while(comparedLimits1 == TRUE) {
activateAlarm(CH1,temperatureSensor1Reading);
comparedLogs1 = sensorLog();
if (comparedLogs1 == TRUE) {
printf("\n Channel %i has registered a temperature of %i \n \n ", selectChannel+1, temperatureSensor1Reading);
}
}
这是我的功能,它将当前温度与设定的温度限制进行比较,并调用其他功能“哔”或允许用户打印出一行“报告”。
temperature_t compareLimit (int channelID)
{
temperature_t limitIsExceeded = FALSE;
if ((temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) | (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit))
limitIsExceeded = TRUE;
return limitIsExceeded;
}
void activateAlarm(int channelID, temperature_t temperature)
{
int key = 0;
if ((temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) | (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit))
callBeep();
sensorLog();
if (_kbhit())
key = _getch();
if ((key == 'P') | (key == 'p')) {
silenceBeep();
}
}
void callBeep()
{
Beep(250,2000);
}
void silenceBeep()
{
Beep(0,2000);
}
temperature_t sensorLog()
{
int key = 0;
temperature_t notedLog = FALSE;
if (_kbhit())
key = _getch();
if ((key == 'O') | (key == 'o')) {
notedLog = TRUE;
return notedLog;
}
}
这是我仍然希望在用户处于“警报”模式时能够操作的一段代码。为了让用户摆脱“警报”模式,他们必须能够降低当前温度,这可以从以下案例语句中完成
if( _kbhit() ) {
selectedCommand = _getch();
switch(selectedCommand) {
case 'R': //if user input is R
case 'r'://if user input is r
(*temperatureSensorReadings[selectChannel])++;
break; //exits loop
case 'F': //if user input is 'F'
case 'f': //if user input is 'f'
(*temperatureSensorReadings[selectChannel])--;
break; //exits loop