Browse Source

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

master
Robin Thoni 4 years ago
parent
commit
cde6b93c6f
Signed by: Robin THONI <robin@rthoni.com> GPG Key ID: 4E09DEF46B99E61E
5 changed files with 153 additions and 80 deletions
  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 View File

1
 #include "AppCore.h"
1
 #include "AppCore.h"
2
+#include "Logs.h"
2
 
3
 
3
 #define HIBERNATE_DELAY 5000
4
 #define HIBERNATE_DELAY 5000
4
 #define SENSORS_CHECK_INTERVAL 2000
5
 #define SENSORS_CHECK_INTERVAL 2000
5
 #define SENSORS_REQUEST_DELAY 750
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
 #define LCD_CHAR_SENSOR 1
9
 #define LCD_CHAR_SENSOR 1
8
 
10
 
9
 #define PIN_ONEWIRE 12
11
 #define PIN_ONEWIRE 12
22
 #define MODE_SEQUENCE_COUNT (sizeof(m_modeSequence) / sizeof(*m_modeSequence))
24
 #define MODE_SEQUENCE_COUNT (sizeof(m_modeSequence) / sizeof(*m_modeSequence))
23
 
25
 
24
 AppCore::AppCore()
26
 AppCore::AppCore()
25
-        : m_appCoreState{
27
+        : m_appCoreState(new AppCoreState{
26
         .appState = {
28
         .appState = {
27
                 .lastSensorRequestMs = 0,
29
                 .lastSensorRequestMs = 0,
28
                 .hasReadSensors = true,
30
                 .hasReadSensors = true,
43
                 .modeSequenceIndex = MODE_SEQUENCE_COUNT - 1,
45
                 .modeSequenceIndex = MODE_SEQUENCE_COUNT - 1,
44
                 .isUpdateNeeded = true
46
                 .isUpdateNeeded = true
45
         }
47
         }
46
-}
48
+})
47
           , m_modeSequence{WaterSetting, HeaterSetting, Lighting}
49
           , m_modeSequence{WaterSetting, HeaterSetting, Lighting}
48
           , m_btnMode{PIN_BTN_MODE}
50
           , m_btnMode{PIN_BTN_MODE}
49
           , m_btnMinus{PIN_BTN_MINUS}
51
           , m_btnMinus{PIN_BTN_MINUS}
53
           , m_oneWire{PIN_ONEWIRE}
55
           , m_oneWire{PIN_ONEWIRE}
54
           , m_sensors{&m_oneWire}
56
           , m_sensors{&m_oneWire}
55
           , m_sensor1{0}
57
           , m_sensor1{0}
58
+          , m_sensor2{0}
56
 {
59
 {
57
 }
60
 }
58
 
61
 
59
 void AppCore::setup()
62
 void AppCore::setup()
60
 {
63
 {
64
+    Serial.begin(9600);
65
+    LOG_FN_BEGIN(1);
66
+
61
     m_sensors.begin();
67
     m_sensors.begin();
68
+    LOG(5, "Found %i sensors", m_sensors.getDeviceCount());
62
     m_sensors.getAddress(m_sensor1, 0);
69
     m_sensors.getAddress(m_sensor1, 0);
70
+    m_sensors.getAddress(m_sensor2, 1);
63
     m_sensors.setWaitForConversion(false);
71
     m_sensors.setWaitForConversion(false);
64
 
72
 
65
     for (auto& g_button : m_buttons)
73
     for (auto& g_button : m_buttons)
83
     };
91
     };
84
     m_lcd.createChar(LCD_CHAR_SENSOR, sensorChar);
92
     m_lcd.createChar(LCD_CHAR_SENSOR, sensorChar);
85
 
93
 
86
-    Serial.begin(9600);
87
 
94
 
88
     bool allButtonsPressed = true;
95
     bool allButtonsPressed = true;
89
     for (auto& g_button : m_buttons)
96
     for (auto& g_button : m_buttons)
92
     }
99
     }
93
     if (!allButtonsPressed)
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
     else
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
 void AppCore::loop()
114
 void AppCore::loop()
104
 {
115
 {
116
+    LOG_FN_BEGIN(50);
117
+
105
     const auto& currentMs = millis();
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
         m_sensors.requestTemperaturesByAddress(m_sensor1);
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
         pButton->read();
140
         pButton->read();
135
         if (pButton->isPressed())
141
         if (pButton->isPressed())
136
         {
142
         {
137
-            m_appCoreState.uiState.lastOpMs = currentMs;
143
+            m_appCoreState->uiState.lastOpMs = currentMs;
138
             break;
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
         for (auto& pButton : m_buttons)
151
         for (auto& pButton : m_buttons)
146
         {
152
         {
153
     }
159
     }
154
     else
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
             setState(Hibernate);
164
             setState(Hibernate);
159
         }
165
         }
161
         {
167
         {
162
             if (m_btnMode.wasReleased())
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
             else if (m_btnMinus.wasReleased() || m_btnPlus.wasReleased())
174
             else if (m_btnMinus.wasReleased() || m_btnPlus.wasReleased())
169
             {
175
             {
170
                 BoilerItemState* itemState = nullptr;
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
                 if (itemState)
186
                 if (itemState)
181
                 {
187
                 {
182
                     if (m_btnMinus.wasReleased())
188
                     if (m_btnMinus.wasReleased())
183
                     {
189
                     {
184
-                        --itemState->setting;
190
+                        itemState->setting -= TEMP_INTERVAL;
185
                     }
191
                     }
186
                     else if (m_btnPlus.wasReleased())
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
         printState();
205
         printState();
202
     }
206
     }
207
+
208
+    LOG_FN_END(50);
203
 }
209
 }
204
 
210
 
205
 void AppCore::setState(
211
 void AppCore::setState(
206
         UiStateEnum state
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
     if (state == Lighting)
218
     if (state == Lighting)
214
     {
219
     {
217
     else if (state == Hibernate)
222
     else if (state == Hibernate)
218
     {
223
     {
219
         digitalWrite(PIN_LCD_LED, 0);
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
 void AppCore::checkBoilerItem(
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
 void AppCore::printState()
279
 void AppCore::printState()
246
 {
280
 {
247
-    Serial.println("Updating display");
281
+    LOG_FN_BEGIN(2);
248
 
282
 
249
     m_lcd.setCursor(0, 0);
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
     m_lcd.setCursor(0, 1);
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
 void
294
 void
259
 AppCore::printStateLine(
295
 AppCore::printStateLine(
260
         char prefix
296
         char prefix
261
-        , const BoilerItemState& boilerItemState
297
+        , const BoilerItemState* boilerItemState
262
         , bool isModifying
298
         , bool isModifying
263
         , bool isActive
299
         , bool isActive
264
 )
300
 )
265
 {
301
 {
302
+    LOG_FN_BEGIN(2);
303
+
266
     char curTmp[7], setTmp[7], tmp[17];
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
                          isActive ? LCD_CHAR_SENSOR : ' ');
308
                          isActive ? LCD_CHAR_SENSOR : ' ');
271
     for (; count < 17; ++count)
309
     for (; count < 17; ++count)
272
     {
310
     {
274
     }
312
     }
275
     tmp[count] = 0;
313
     tmp[count] = 0;
276
     m_lcd.print(tmp);
314
     m_lcd.print(tmp);
315
+
316
+    LOG_FN_END(2);
277
 }
317
 }
278
 
318
 
279
 void AppCore::tempToStr(
319
 void AppCore::tempToStr(
280
         char* out
320
         char* out
281
         , temp_t temp
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
     if (temp == TEMP_T_INVALID)
329
     if (temp == TEMP_T_INVALID)
285
     {
330
     {
286
-        strcpy(out, " -- ");
331
+        strcpy(out, " --.-");
287
     }
332
     }
288
     else
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 View File

19
 protected:
19
 protected:
20
     void setState(UiStateEnum state);
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
     void printState();
26
     void printState();
25
 
27
 
26
     void printStateLine(
28
     void printStateLine(
27
             char prefix
29
             char prefix
28
-            , const BoilerItemState& boilerItemState
30
+            , const BoilerItemState* boilerItemState
29
             , bool isModifying
31
             , bool isModifying
30
             , bool isActive
32
             , bool isActive
31
     );
33
     );
33
     void tempToStr(
35
     void tempToStr(
34
             char* out
36
             char* out
35
             , temp_t temp
37
             , temp_t temp
38
+            , signed char width
36
     );
39
     );
37
 
40
 
38
 private:
41
 private:
39
-    AppCoreState m_appCoreState;
42
+    AppCoreState* m_appCoreState;
40
     Storage m_storage;
43
     Storage m_storage;
41
 
44
 
42
     UiStateEnum m_modeSequence[3];
45
     UiStateEnum m_modeSequence[3];
51
     OneWire m_oneWire;
54
     OneWire m_oneWire;
52
     DallasTemperature m_sensors;
55
     DallasTemperature m_sensors;
53
     DeviceAddress m_sensor1;
56
     DeviceAddress m_sensor1;
57
+    DeviceAddress m_sensor2;
54
 };
58
 };

+ 1
- 1
Boiler.h View File

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

+ 5
- 0
CMakeLists.txt View File

22
 set(${CMAKE_PROJECT_NAME}_ALL_SRCS main.ino JC_Button.cpp Storage.cpp AppCore.cpp)
22
 set(${CMAKE_PROJECT_NAME}_ALL_SRCS main.ino JC_Button.cpp Storage.cpp AppCore.cpp)
23
 set(${CMAKE_PROJECT_NAME}_SKETCH main.ino)
23
 set(${CMAKE_PROJECT_NAME}_SKETCH main.ino)
24
 generate_arduino_firmware(${CMAKE_PROJECT_NAME})
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 View File

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

Loading…
Cancel
Save