问题
New wannabe programmer here. I'm currently working on my homework and I encountered with some problems.
The task is the following:
FanControl function shall turn on the fan if the temperature is over the limit and it shall turn off the fan when the temperature is below the limit.
1.1. Hysteresis shall be applied on the switching limits symmetrically.
1.2. The hysteresis and the default limit shall be configurable on compile time.
1.2.1.The default limit shall be 25°C and the default hysteresis value shall be 2°C.
1.3. The temperature shall be acquired by reading the GetTemperature() function.
1.4. The fan shall be turned on by calling SetState() function with the argument FAN_ON and it shall be turned off by calling it with FAN_OFF argument.
- In case of the input temperature goes beyond its valid limits (-20°C..+160°C) then the component shall report an error.
2.1. The error report shall be sent out by SendErrorReport() function. The reason of the report shall be stored into the errorCode field and the temperature value into the temperature field.
2.1.1.In case of over temperature the errorCode shall be set to ERROR_CODE_OVER_TEMP.
2.1.2.In case of under temperature the errorCode shall be set to ERROR_CODE_UNDER_TEMP.
My problem is with the second part of the task. I having difficulties to store the values to the fields in the struct and I also cannot seem to pass the struct into the fields that required, maybe that I am lacking some syntax knowledge.
Since the school haven't provided me the other files the compiler require I cannot debug my code. I've tried the following way, but I'm afraid that won't work.
#ifndef COMPONENTTEST
typedef unsigned char boolean;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef float float32;
typedef struct
{
uint32 errorCode;
float32 temperature;
} dtErrorMessage;
typedef enum
{
FAN_OFF,
FAN_ON
} dtFanState;
#define ERROR_CODE_OVER_TEMP ((uint32)1u)
#define ERROR_CODE_UNDER_TEMP ((uint32)2u)
extern float32 GetTemperature(void);
extern void SetState(dtFanState fanState);
extern void SendErrorReport(dtErrorMessage* errorMsg);
#endif /* !COMPONENTTEST */
/* Please add your own additions here! */
void FanControl(void)
{
uint16 tempLimit=25;
uint16 hystLimit=tempLimit-2;
if(GetTemperature() <= hystLimit)
{
SetState(FAN_OFF);
}
if(GetTemperature() >= tempLimit)
{
SetState(FAN_ON);
}
// From this part I am not sure about!
if(GetTemperature() <= -20)
{
dtErrorMessage low;
low.errorCode=ERROR_CODE_UNDER_TEMP;
low.temperature=GetTemperature();
SendErrorReport(&low);
}
if(GetTemperature() >= 160)
{
dtErrorMessage high;
high.errorCode=ERROR_CODE_OVER_TEMP;
high.temperature=GetTemperature();
SendErrorReport(&high);
}
}
I am continually getting the "undefined reference to 'GetTemperature' and 'SendErrorReport'" but I think it is because of the missing files. I'm afraid that the code I wrote will pass the address of the stored values instead of the current value. I'm desperate for some help.
回答1:
I'm afraid that the code I wrote will pass the address of the stored values instead of the current value. I'm desperate for some help.
Whatever you wrote as below is perfectly fine
if(GetTemperature() <= -20)
{
dtErrorMessage low;
low.errorCode=ERROR_CODE_UNDER_TEMP;
low.temperature=GetTemperature();
SendErrorReport(&low);
}
since SendErrorReport takes dtErrorMessage* as parameter.
extern void SendErrorReport(dtErrorMessage* errorMsg);
But there are things you need to consider such as,
SendErrorReportshould not be asynchronous call, otherwise you will have undefined behavior asdtErrorMessage low;will go out of scope and will be vanished.SendErrorReportshould not try to free the memory fordtErrorMessage* errorMsginside its definition.
These details you should get as part of API Specification.
回答2:
Well, undefined means not implemented. Due to, like you said, you don't have the source files which define the extern functions, that makes sense. I dont see anything wrong with your code but I suggest using an else for the second condition:
if(GetTemperature() <= -20)
{
dtErrorMessage errMsg;
errMsg.errorCode=ERROR_CODE_UNDER_TEMP;
errMsg.temperature=GetTemperature();
SendErrorReport(&errMsg);
}
else if(GetTemperature() >= 160)
{
dtErrorMessage errMsg;
errMsg.errorCode=ERROR_CODE_OVER_TEMP;
errMsg.temperature=GetTemperature();
SendErrorReport(&errMsg);
}
来源:https://stackoverflow.com/questions/57181087/how-can-i-store-values-into-a-multi-parameter-struct-and-pass-typedef-struct-to