-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperatureThread.cpp
137 lines (109 loc) · 3.22 KB
/
temperatureThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "mbed.h"
#include "temperatureThread.h"
#include "displayThread.h"
#include "awsThread.h"
#ifdef TARGET_CY8CPROTO_062_4343W
#define THERMISTOR
#endif
#ifdef TARGET_CY8CKIT_062_WIFI_BT
#define TMP36
#endif
static float temperatureF;
static float setPoint = 75.0;
static void readTemp();
typedef enum {
CMD_setPointDelta,
CMD_setPoint,
} command_t;
typedef struct {
command_t cmd;
float value; /* AD result of measured voltage */
} msg_t;
static Queue<msg_t, 32> queue;
static MemoryPool<msg_t, 16> mpool;
void tempSendUpdateCurrentSetPointF(float delta)
{
msg_t *message = mpool.alloc();
if(message)
{
message->cmd = CMD_setPointDelta;
message->value = delta;
queue.put(message);
}
}
void tempSendUpdateSetpointF(float setPoint)
{
msg_t *message = mpool.alloc();
if(message)
{
message->cmd = CMD_setPoint;
message->value = setPoint;
queue.put(message);
}
}
void temperatureThread()
{
char buffer[128];
displaySendUpdateTemp(temperatureF);
displaySendUpdateSetPoint(setPoint);
while(1)
{
osEvent evt = queue.get(200);
if (evt.status == osEventMessage) {
msg_t *message = (msg_t*)evt.value.p;
switch(message->cmd)
{
case CMD_setPointDelta:
setPoint += message->value;
displaySendUpdateSetPoint(setPoint);
break;
case CMD_setPoint:
setPoint = message->value;
displaySendUpdateSetPoint(setPoint);
break;
}
mpool.free(message);
}
else
{
readTemp();
// Control the HVAC system with +- 0.5 degree of Hystersis
if(temperatureF < setPoint - 0.5)
displaySendUpdateMode(-1.0);
else if (temperatureF > setPoint + 0.5)
displaySendUpdateMode(1.0);
else
displaySendUpdateMode(0.0);
displaySendUpdateTemp(temperatureF);
awsSendUpdateTemperature(temperatureF);
}
}
}
#ifdef THERMISTOR
static DigitalOut thermVDD(P10_3,1);
static DigitalOut thermGND(P10_0,0);
static AnalogIn thermOut(P10_1);
static void readTemp()
{
float refVoltage = thermOut.read() * 2.4; // Range of ADC 0->2*Vref
float refCurrent = refVoltage / 10000.0; // 10k Reference Resistor
float thermVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v
float thermResistance = thermVoltage / refCurrent;
float logrT = (float32_t)log((float64_t)thermResistance);
/* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */
float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) +
((2.041094E-07) * pow((float64)logrT, (float32)3)));
float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
temperatureF = (temperatureC * 9.0/5.0) + 32;
}
#endif
#ifdef TMP36
static AnalogIn tmp36(A5);
static void readTemp()
{
float volts;
volts = tmp36.read() * 2.4;
float temperatureC = 1/.01 * volts - 50.0;
temperatureF = (temperatureC * 9.0/5.0) + 32;
}
#endif