Przeglądaj źródła

Added logs; switch temp_t to short; display 1/10 temperature

master
Robin Thoni 4 lat temu
rodzic
commit
cde6b93c6f
Podpisane przez: Robin THONI <robin@rthoni.com> ID klucza GPG: 4E09DEF46B99E61E
5 zmienionych plików z 153 dodań i 80 usunięć
  1. 116
    76
      AppCore.cpp
  2. 7
    3
      AppCore.h
  3. 1
    1
      Boiler.h
  4. 5
    0
      CMakeLists.txt
  5. 24
    0
      Logs.h

+ 116
- 76
AppCore.cpp Wyświetl plik

@@ -1,9 +1,11 @@
1 1
 #include "AppCore.h"
2
+#include "Logs.h"
2 3
 
3 4
 #define HIBERNATE_DELAY 5000
4 5
 #define SENSORS_CHECK_INTERVAL 2000
5 6
 #define SENSORS_REQUEST_DELAY 750
6
-#define TEMP_TRIGGER 10
7
+#define TEMP_TRIGGER ((temp_t)(5 * 10.0f))
8
+#define TEMP_INTERVAL ((temp_t)(0.5 * 10.0f))
7 9
 #define LCD_CHAR_SENSOR 1
8 10
 
9 11
 #define PIN_ONEWIRE 12
@@ -22,7 +24,7 @@
22 24
 #define MODE_SEQUENCE_COUNT (sizeof(m_modeSequence) / sizeof(*m_modeSequence))
23 25
 
24 26
 AppCore::AppCore()
25
-        : m_appCoreState{
27
+        : m_appCoreState(new AppCoreState{
26 28
         .appState = {
27 29
                 .lastSensorRequestMs = 0,
28 30
                 .hasReadSensors = true,
@@ -43,7 +45,7 @@ AppCore::AppCore()
43 45
                 .modeSequenceIndex = MODE_SEQUENCE_COUNT - 1,
44 46
                 .isUpdateNeeded = true
45 47
         }
46
-}
48
+})
47 49
           , m_modeSequence{WaterSetting, HeaterSetting, Lighting}
48 50
           , m_btnMode{PIN_BTN_MODE}
49 51
           , m_btnMinus{PIN_BTN_MINUS}
@@ -53,13 +55,19 @@ AppCore::AppCore()
53 55
           , m_oneWire{PIN_ONEWIRE}
54 56
           , m_sensors{&m_oneWire}
55 57
           , m_sensor1{0}
58
+          , m_sensor2{0}
56 59
 {
57 60
 }
58 61
 
59 62
 void AppCore::setup()
60 63
 {
64
+    Serial.begin(9600);
65
+    LOG_FN_BEGIN(1);
66
+
61 67
     m_sensors.begin();
68
+    LOG(5, "Found %i sensors", m_sensors.getDeviceCount());
62 69
     m_sensors.getAddress(m_sensor1, 0);
70
+    m_sensors.getAddress(m_sensor2, 1);
63 71
     m_sensors.setWaitForConversion(false);
64 72
 
65 73
     for (auto& g_button : m_buttons)
@@ -83,7 +91,6 @@ void AppCore::setup()
83 91
     };
84 92
     m_lcd.createChar(LCD_CHAR_SENSOR, sensorChar);
85 93
 
86
-    Serial.begin(9600);
87 94
 
88 95
     bool allButtonsPressed = true;
89 96
     for (auto& g_button : m_buttons)
@@ -92,40 +99,39 @@ void AppCore::setup()
92 99
     }
93 100
     if (!allButtonsPressed)
94 101
     {
95
-        m_storage.load(m_appCoreState);
102
+        LOG(5, "%s: Loading settings", __FUNCTION__);
103
+        m_storage.load(*m_appCoreState);
96 104
     }
97 105
     else
98 106
     {
99
-        m_storage.save(m_appCoreState);
107
+        LOG(5, "%s: Resetting settings", __FUNCTION__);
108
+        m_storage.save(*m_appCoreState);
100 109
     }
110
+
111
+    LOG_FN_END(1);
101 112
 }
102 113
 
103 114
 void AppCore::loop()
104 115
 {
116
+    LOG_FN_BEGIN(50);
117
+
105 118
     const auto& currentMs = millis();
106 119
 
107
-    if (currentMs - m_appCoreState.appState.lastSensorRequestMs >= SENSORS_CHECK_INTERVAL)
120
+    if (currentMs - m_appCoreState->appState.lastSensorRequestMs >= SENSORS_CHECK_INTERVAL)
108 121
     {
109
-        m_appCoreState.appState.lastSensorRequestMs = currentMs;
110
-        m_appCoreState.appState.hasReadSensors = false;
122
+        m_appCoreState->appState.lastSensorRequestMs = currentMs;
123
+        m_appCoreState->appState.hasReadSensors = false;
111 124
         m_sensors.requestTemperaturesByAddress(m_sensor1);
125
+        m_sensors.requestTemperaturesByAddress(m_sensor2);
112 126
     }
113
-    if (currentMs - m_appCoreState.appState.lastSensorRequestMs >= SENSORS_REQUEST_DELAY &&
114
-        !m_appCoreState.appState.hasReadSensors)
127
+    if (currentMs - m_appCoreState->appState.lastSensorRequestMs >= SENSORS_REQUEST_DELAY &&
128
+        !m_appCoreState->appState.hasReadSensors)
115 129
     {
116
-        m_appCoreState.appState.hasReadSensors = true;
117
-        auto raw = m_sensors.getTempC(m_sensor1);
118
-        temp_t temp = TEMP_T_INVALID;
119
-        if (raw != DEVICE_DISCONNECTED_C)
120
-        {
121
-            temp = (temp_t) (raw * 2);
122
-        }
123
-
124
-        if (temp != m_appCoreState.appState.water.current)
125
-        {
126
-            m_appCoreState.appState.water.current = temp;
127
-            m_appCoreState.uiState.isUpdateNeeded = true;
128
-        }
130
+        m_appCoreState->appState.hasReadSensors = true;
131
+        readAndUpdateSensors(&m_appCoreState->appState.water, m_sensor1);
132
+        readAndUpdateSensors(&m_appCoreState->appState.heater, m_sensor2);
133
+        checkBoilerItem(&m_appCoreState->appState.water);
134
+        checkBoilerItem(&m_appCoreState->appState.heater);
129 135
     }
130 136
 
131 137
 
@@ -134,13 +140,13 @@ void AppCore::loop()
134 140
         pButton->read();
135 141
         if (pButton->isPressed())
136 142
         {
137
-            m_appCoreState.uiState.lastOpMs = currentMs;
143
+            m_appCoreState->uiState.lastOpMs = currentMs;
138 144
             break;
139 145
         }
140 146
     }
141 147
 
142 148
 
143
-    if (m_appCoreState.uiState.state == Hibernate)
149
+    if (m_appCoreState->uiState.state == Hibernate)
144 150
     {
145 151
         for (auto& pButton : m_buttons)
146 152
         {
@@ -153,7 +159,7 @@ void AppCore::loop()
153 159
     }
154 160
     else
155 161
     {
156
-        if (currentMs - m_appCoreState.uiState.lastOpMs >= HIBERNATE_DELAY)
162
+        if (currentMs - m_appCoreState->uiState.lastOpMs >= HIBERNATE_DELAY)
157 163
         {
158 164
             setState(Hibernate);
159 165
         }
@@ -161,54 +167,53 @@ void AppCore::loop()
161 167
         {
162 168
             if (m_btnMode.wasReleased())
163 169
             {
164
-                m_appCoreState.uiState.modeSequenceIndex =
165
-                        (m_appCoreState.uiState.modeSequenceIndex + 1) % MODE_SEQUENCE_COUNT;
166
-                setState(m_modeSequence[m_appCoreState.uiState.modeSequenceIndex]);
170
+                m_appCoreState->uiState.modeSequenceIndex =
171
+                        (m_appCoreState->uiState.modeSequenceIndex + 1) % MODE_SEQUENCE_COUNT;
172
+                setState(m_modeSequence[m_appCoreState->uiState.modeSequenceIndex]);
167 173
             }
168 174
             else if (m_btnMinus.wasReleased() || m_btnPlus.wasReleased())
169 175
             {
170 176
                 BoilerItemState* itemState = nullptr;
171
-                if (m_appCoreState.uiState.state == WaterSetting)
177
+                if (m_appCoreState->uiState.state == WaterSetting)
172 178
                 {
173
-                    itemState = &m_appCoreState.appState.water;
179
+                    itemState = &m_appCoreState->appState.water;
174 180
                 }
175
-                else if (m_appCoreState.uiState.state == HeaterSetting)
181
+                else if (m_appCoreState->uiState.state == HeaterSetting)
176 182
                 {
177
-                    itemState = &m_appCoreState.appState.heater;
183
+                    itemState = &m_appCoreState->appState.heater;
178 184
                 }
179 185
 
180 186
                 if (itemState)
181 187
                 {
182 188
                     if (m_btnMinus.wasReleased())
183 189
                     {
184
-                        --itemState->setting;
190
+                        itemState->setting -= TEMP_INTERVAL;
185 191
                     }
186 192
                     else if (m_btnPlus.wasReleased())
187 193
                     {
188
-                        ++itemState->setting;
194
+                        itemState->setting += TEMP_INTERVAL;
189 195
                     }
190
-                    m_appCoreState.uiState.isUpdateNeeded = true;
196
+                    LOG(1, "Setting temp to %i (%i)", itemState->setting, TEMP_INTERVAL);
197
+                    m_appCoreState->uiState.isUpdateNeeded = true;
191 198
                 }
192 199
             }
193 200
         }
194 201
     }
195 202
 
196
-    checkBoilerItem(m_appCoreState.appState.water);
197
-    checkBoilerItem(m_appCoreState.appState.heater);
198
-
199
-    if (m_appCoreState.uiState.isUpdateNeeded)
203
+    if (m_appCoreState->uiState.isUpdateNeeded)
200 204
     {
201 205
         printState();
202 206
     }
207
+
208
+    LOG_FN_END(50);
203 209
 }
204 210
 
205 211
 void AppCore::setState(
206 212
         UiStateEnum state
207 213
 )
208 214
 {
209
-    char tmp[50];
210
-    snprintf(tmp, sizeof(tmp), "Changing state %i => %i", m_appCoreState.uiState.state, state);
211
-    Serial.println(tmp);
215
+    LOG_FN_BEGIN(1);
216
+    LOG(5, "Changing state %i => %i", m_appCoreState->uiState.state, state);
212 217
 
213 218
     if (state == Lighting)
214 219
     {
@@ -217,56 +222,89 @@ void AppCore::setState(
217 222
     else if (state == Hibernate)
218 223
     {
219 224
         digitalWrite(PIN_LCD_LED, 0);
220
-        m_appCoreState.uiState.modeSequenceIndex = MODE_SEQUENCE_COUNT - 1;
221
-        m_storage.save(m_appCoreState);
225
+        m_appCoreState->uiState.modeSequenceIndex = MODE_SEQUENCE_COUNT - 1;
226
+        m_storage.save(*m_appCoreState);
222 227
     }
223
-    m_appCoreState.uiState.state = state;
224
-    m_appCoreState.uiState.isUpdateNeeded = true;
228
+    m_appCoreState->uiState.state = state;
229
+    m_appCoreState->uiState.isUpdateNeeded = true;
230
+
231
+    LOG_FN_END(1);
225 232
 }
226 233
 
227 234
 void AppCore::checkBoilerItem(
228
-        BoilerItemState& boilerItemState
235
+        BoilerItemState* boilerItemState
229 236
 )
230 237
 {
231
-    if (!boilerItemState.isActive && boilerItemState.current != TEMP_T_INVALID &&
232
-        boilerItemState.current <= boilerItemState.setting - TEMP_TRIGGER)
238
+    LOG_FN_BEGIN(2);
239
+
240
+    if (!boilerItemState->isActive && boilerItemState->current != TEMP_T_INVALID &&
241
+        boilerItemState->current <= boilerItemState->setting - TEMP_TRIGGER)
233 242
     {
234
-        boilerItemState.isActive = true;
235
-        m_appCoreState.uiState.isUpdateNeeded = true;
243
+        boilerItemState->isActive = true;
244
+        m_appCoreState->uiState.isUpdateNeeded = true;
236 245
     }
237
-    else if (boilerItemState.isActive &&
238
-             (boilerItemState.current == TEMP_T_INVALID || boilerItemState.current >= boilerItemState.setting))
246
+    else if (boilerItemState->isActive &&
247
+             (boilerItemState->current == TEMP_T_INVALID || boilerItemState->current >= boilerItemState->setting))
239 248
     {
240
-        boilerItemState.isActive = false;
241
-        m_appCoreState.uiState.isUpdateNeeded = true;
249
+        boilerItemState->isActive = false;
250
+        m_appCoreState->uiState.isUpdateNeeded = true;
242 251
     }
252
+
253
+    LOG_FN_END(2);
254
+}
255
+
256
+void AppCore::readAndUpdateSensors(
257
+        BoilerItemState* boilerItemState
258
+        , const uint8_t* sensor
259
+)
260
+{
261
+    LOG_FN_BEGIN(2);
262
+
263
+    auto raw = m_sensors.getTempC(sensor);
264
+    temp_t temp = TEMP_T_INVALID;
265
+    if (raw != DEVICE_DISCONNECTED_C)
266
+    {
267
+        temp = (temp_t) (raw * 10);
268
+    }
269
+
270
+    if (temp != boilerItemState->current)
271
+    {
272
+        boilerItemState->current = temp;
273
+        m_appCoreState->uiState.isUpdateNeeded = true;
274
+    }
275
+
276
+    LOG_FN_END(2);
243 277
 }
244 278
 
245 279
 void AppCore::printState()
246 280
 {
247
-    Serial.println("Updating display");
281
+    LOG_FN_BEGIN(2);
248 282
 
249 283
     m_lcd.setCursor(0, 0);
250
-    printStateLine('S', m_appCoreState.appState.water, m_appCoreState.uiState.state == WaterSetting,
251
-                   m_appCoreState.appState.water.isActive);
284
+    printStateLine('S', &m_appCoreState->appState.water, m_appCoreState->uiState.state == WaterSetting,
285
+                   m_appCoreState->appState.water.isActive);
252 286
     m_lcd.setCursor(0, 1);
253
-    printStateLine('C', m_appCoreState.appState.heater, m_appCoreState.uiState.state == HeaterSetting,
254
-                   m_appCoreState.appState.heater.isActive);
255
-    m_appCoreState.uiState.isUpdateNeeded = false;
287
+    printStateLine('C', &m_appCoreState->appState.heater, m_appCoreState->uiState.state == HeaterSetting,
288
+                   m_appCoreState->appState.heater.isActive);
289
+    m_appCoreState->uiState.isUpdateNeeded = false;
290
+
291
+    LOG_FN_END(2);
256 292
 }
257 293
 
258 294
 void
259 295
 AppCore::printStateLine(
260 296
         char prefix
261
-        , const BoilerItemState& boilerItemState
297
+        , const BoilerItemState* boilerItemState
262 298
         , bool isModifying
263 299
         , bool isActive
264 300
 )
265 301
 {
302
+    LOG_FN_BEGIN(2);
303
+
266 304
     char curTmp[7], setTmp[7], tmp[17];
267
-    tempToStr(curTmp, boilerItemState.current);
268
-    tempToStr(setTmp, boilerItemState.setting);
269
-    int count = snprintf(tmp, sizeof(tmp), "%c: %s [%s]%c%c", prefix, curTmp, setTmp, isModifying ? '<' : ' ',
305
+    tempToStr(curTmp, boilerItemState->current, 5);
306
+    tempToStr(setTmp, boilerItemState->setting, 4);
307
+    int count = snprintf(tmp, sizeof(tmp), "%c:%s [%s]%c%c", prefix, curTmp, setTmp, isModifying ? '<' : ' ',
270 308
                          isActive ? LCD_CHAR_SENSOR : ' ');
271 309
     for (; count < 17; ++count)
272 310
     {
@@ -274,26 +312,28 @@ AppCore::printStateLine(
274 312
     }
275 313
     tmp[count] = 0;
276 314
     m_lcd.print(tmp);
315
+
316
+    LOG_FN_END(2);
277 317
 }
278 318
 
279 319
 void AppCore::tempToStr(
280 320
         char* out
281 321
         , temp_t temp
322
+        , signed char width
282 323
 )
283 324
 {
325
+    LOG_FN_BEGIN(2);
326
+
327
+    LOG(5, "%s: temp=%i", __FUNCTION__, (int)temp);
328
+
284 329
     if (temp == TEMP_T_INVALID)
285 330
     {
286
-        strcpy(out, " -- ");
331
+        strcpy(out, " --.-");
287 332
     }
288 333
     else
289 334
     {
290
-        char tmp[7];
291
-        dtostrf(temp / 2.0f, 2, 1, tmp);
292
-        auto len = 4 - strlen(tmp);
293
-        for (auto i = 0; i < len; ++i)
294
-        {
295
-            out[i] = ' ';
296
-        }
297
-        strcpy(&out[len], tmp);
335
+        dtostrf(temp / 10.0f, width, 1, out);
298 336
     }
337
+
338
+    LOG_FN_END(2);
299 339
 }

+ 7
- 3
AppCore.h Wyświetl plik

@@ -19,13 +19,15 @@ public:
19 19
 protected:
20 20
     void setState(UiStateEnum state);
21 21
 
22
-    void checkBoilerItem(BoilerItemState& boilerItemState);
22
+    void checkBoilerItem(BoilerItemState* boilerItemState);
23
+
24
+    void readAndUpdateSensors(BoilerItemState* boilerItemState, const uint8_t* sensor);
23 25
 
24 26
     void printState();
25 27
 
26 28
     void printStateLine(
27 29
             char prefix
28
-            , const BoilerItemState& boilerItemState
30
+            , const BoilerItemState* boilerItemState
29 31
             , bool isModifying
30 32
             , bool isActive
31 33
     );
@@ -33,10 +35,11 @@ protected:
33 35
     void tempToStr(
34 36
             char* out
35 37
             , temp_t temp
38
+            , signed char width
36 39
     );
37 40
 
38 41
 private:
39
-    AppCoreState m_appCoreState;
42
+    AppCoreState* m_appCoreState;
40 43
     Storage m_storage;
41 44
 
42 45
     UiStateEnum m_modeSequence[3];
@@ -51,4 +54,5 @@ private:
51 54
     OneWire m_oneWire;
52 55
     DallasTemperature m_sensors;
53 56
     DeviceAddress m_sensor1;
57
+    DeviceAddress m_sensor2;
54 58
 };

+ 1
- 1
Boiler.h Wyświetl plik

@@ -1,6 +1,6 @@
1 1
 #pragma once
2 2
 
3
-typedef char temp_t;
3
+typedef short temp_t;
4 4
 #define TEMP_T_INVALID -127
5 5
 typedef unsigned long timestamp_t;
6 6
 

+ 5
- 0
CMakeLists.txt Wyświetl plik

@@ -22,3 +22,8 @@ enable_language(ASM)
22 22
 set(${CMAKE_PROJECT_NAME}_ALL_SRCS main.ino JC_Button.cpp Storage.cpp AppCore.cpp)
23 23
 set(${CMAKE_PROJECT_NAME}_SKETCH main.ino)
24 24
 generate_arduino_firmware(${CMAKE_PROJECT_NAME})
25
+
26
+if (CMAKE_BUILD_TYPE MATCHES "Debug")
27
+    message(STATUS "Debug build detected. Enabling logs.")
28
+    add_definitions(-DAPP_CORE_LOGS=1)
29
+endif()

+ 24
- 0
Logs.h Wyświetl plik

@@ -0,0 +1,24 @@
1
+#pragma once
2
+
3
+#if APP_CORE_LOGS
4
+
5
+#define APP_CORE_LOGS_LEVEL 1
6
+
7
+#define LOG(level_, ...) do {                           \
8
+    if (level_ <= APP_CORE_LOGS_LEVEL)                  \
9
+    {                                                   \
10
+        char tmp[100];                                  \
11
+        snprintf(tmp, sizeof(tmp), __VA_ARGS__);        \
12
+        Serial.println(tmp);                            \
13
+    }                                                   \
14
+} while (0)
15
+
16
+#define LOG_FN_BEGIN(level_) LOG(level_, "%s: Begin", __FUNCTION__)
17
+#define LOG_FN_END(level_) LOG(level_, "%s: End", __FUNCTION__)
18
+
19
+#else
20
+
21
+#define LOG(x)
22
+#define LOG(x, ...)
23
+
24
+#endif

Ładowanie…
Anuluj
Zapisz