我整天都在弄乱这段代码,但我再次陷入困境......提前谢谢
基本上这个程序给了我一些问题,下面是我认为导致问题的代码片段。基本上用户可以改变它们的“当前温度”,如果它超过上限或下限阈值,则调用“compareLimit”函数......然后该函数调用各种其他函数(例如蜂鸣声,并允许一个行传感器记录)要在屏幕上打印出来)。但是以下是我的问题(请记住我必须使用哔哔声,但如果你有更好的方法,请告知):
嘟嘟声是一种痛苦,我的整个“闹钟”模式围绕着哔哔声的持续时间。这意味着每当我想按下字母“O”来打印报告时,它就会打印出与哔哔声触发器结束重合的奇思妙想。我如何消除这种情况?
我想让用户仍然能够访问最后一段代码(case语句),即使在处于警报模式的“while循环”时也是如此,这样他们才能真正退出警报模式按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