Selaa lähdekoodia

Added new menu

master
Robin Thoni 4 vuotta sitten
vanhempi
commit
3ee8d9fb5b
Allekirjoittanut: Robin THONI <robin@rthoni.com> GPG Key ID: 4E09DEF46B99E61E
3 muutettua tiedostoa jossa 167 lisäystä ja 51 poistoa
  1. 153
    42
      AppCore.cpp
  2. 2
    4
      AppCore.h
  3. 12
    5
      Boiler.h

+ 153
- 42
AppCore.cpp Näytä tiedosto

@@ -1,6 +1,9 @@
1 1
 #include "AppCore.h"
2 2
 #include "Logs.h"
3 3
 
4
+#define xstr(s) str(s)
5
+#define str(s) #s
6
+
4 7
 #define HIBERNATE_DELAY 5000
5 8
 #define SENSORS_CHECK_INTERVAL 2000
6 9
 #define SENSORS_REQUEST_DELAY 750
@@ -9,8 +12,8 @@
9 12
 #define LCD_CHAR_SENSOR 1
10 13
 
11 14
 #define PIN_ONEWIRE A0
12
-#define PIN_RELAY1 A1
13
-#define PIN_RELAY2 A2
15
+#define PIN_RELAY2 A1
16
+#define PIN_RELAY1 A2
14 17
 #define PIN_BTN_CANCEL 9
15 18
 #define PIN_BTN_OK 10
16 19
 #define PIN_BTN_MINUS 11
@@ -23,8 +26,7 @@
23 26
 #define PIN_LCD_D6 4
24 27
 #define PIN_LCD_D7 3
25 28
 
26
-#define BUTTONS_COUNT (sizeof(g_buttons) / sizeof(*g_buttons))
27
-#define MODE_SEQUENCE_COUNT (sizeof(m_modeSequence) / sizeof(*m_modeSequence))
29
+UiStateEnum AppCore::s_menuSequence[4]{MenuWaterSetting, MenuHeaterSetting, MenuTempTrigger, MenuVersion};
28 30
 
29 31
 AppCore::AppCore()
30 32
         : m_appCoreState(new AppCoreState{
@@ -42,16 +44,17 @@ AppCore::AppCore()
42 44
                         .setting = 0,
43 45
                         .isActive = false,
44 46
                         .pinNo = PIN_RELAY2
45
-                }
47
+                },
48
+                .tempTrigger = TEMP_TRIGGER,
49
+                .pCurrentSettingEdit = nullptr,
50
+                .currentSettingEditTmp = 0
46 51
         },
47 52
         .uiState = {
48
-                .state = Lighting,
53
+                .state = HomeLighting,
49 54
                 .lastOpMs = 0,
50
-                .modeSequenceIndex = MODE_SEQUENCE_COUNT - 1,
51 55
                 .isUpdateNeeded = true
52 56
         }
53 57
 })
54
-          , m_modeSequence{WaterSetting, HeaterSetting, Lighting}
55 58
           , m_pBtnCancel(new Button{PIN_BTN_CANCEL})
56 59
           , m_pBtnOk(new Button{PIN_BTN_OK})
57 60
           , m_pBtnMinus(new Button{PIN_BTN_MINUS})
@@ -171,13 +174,13 @@ void AppCore::loop()
171 174
     }
172 175
 
173 176
 
174
-    if (m_appCoreState->uiState.state == Hibernate)
177
+    if (m_appCoreState->uiState.state == HomeHibernate)
175 178
     {
176 179
         for (auto& pButton : m_pButtons)
177 180
         {
178 181
             if (pButton->wasReleased())
179 182
             {
180
-                setState(Lighting);
183
+                setState(HomeLighting);
181 184
                 break;
182 185
             }
183 186
         }
@@ -186,39 +189,85 @@ void AppCore::loop()
186 189
     {
187 190
         if (currentMs - m_appCoreState->uiState.lastOpMs >= HIBERNATE_DELAY)
188 191
         {
189
-            setState(Hibernate);
192
+            setState(HomeHibernate);
190 193
         }
191 194
         else
192 195
         {
193
-            if (m_pBtnOk->wasReleased())
196
+            if (m_appCoreState->uiState.state == HomeLighting)
194 197
             {
195
-                m_appCoreState->uiState.modeSequenceIndex =
196
-                        (m_appCoreState->uiState.modeSequenceIndex + 1) % MODE_SEQUENCE_COUNT;
197
-                setState(m_modeSequence[m_appCoreState->uiState.modeSequenceIndex]);
198
+                if (m_pBtnCancel->wasReleased())
199
+                {
200
+                    setState(HomeHibernate);
201
+                }
202
+                else if (m_pBtnOk->wasReleased())
203
+                {
204
+                    setState(s_menuSequence[0]);
205
+                }
198 206
             }
199
-            else if (m_pBtnMinus->wasReleased() || m_pBtnPlus->wasReleased())
207
+            else if (m_appCoreState->uiState.state == MenuWaterSetting || m_appCoreState->uiState.state == MenuHeaterSetting || m_appCoreState->uiState.state == MenuTempTrigger || m_appCoreState->uiState.state == MenuVersion)
200 208
             {
201
-                BoilerItemState* itemState = nullptr;
202
-                if (m_appCoreState->uiState.state == WaterSetting)
209
+                if (m_pBtnCancel->wasReleased())
203 210
                 {
204
-                    itemState = &m_appCoreState->appState.water;
211
+                    setState(HomeLighting);
212
+                }
213
+                else if (m_pBtnOk->wasReleased())
214
+                {
215
+                    UiStateEnum nextState = m_appCoreState->uiState.state;
216
+                    if (m_appCoreState->uiState.state == MenuWaterSetting)
217
+                    {
218
+                        nextState = MenuWaterSettingEdit;
219
+                    }
220
+                    else if (m_appCoreState->uiState.state == MenuHeaterSetting)
221
+                    {
222
+                        nextState = MenuHeaterSettingEdit;
223
+                    }
224
+                    else if (m_appCoreState->uiState.state == MenuTempTrigger)
225
+                    {
226
+                        nextState = MenuTempTriggerEdit;
227
+                    }
228
+                    setState(nextState);
205 229
                 }
206
-                else if (m_appCoreState->uiState.state == HeaterSetting)
230
+                else if (m_pBtnMinus->wasReleased() || m_pBtnPlus->wasReleased())
207 231
                 {
208
-                    itemState = &m_appCoreState->appState.heater;
232
+                    int idx = 0;
233
+                    while (m_appCoreState->uiState.state != s_menuSequence[idx])
234
+                    {
235
+                        ++idx;
236
+                    }
237
+                    auto shift = m_pBtnMinus->wasReleased() ? (-1) : 1;
238
+                    setState(s_menuSequence[(idx + shift) % ((sizeof(s_menuSequence) / sizeof(*s_menuSequence)))]);
209 239
                 }
210
-
211
-                if (itemState)
240
+            }
241
+            else if (m_appCoreState->uiState.state == MenuWaterSettingEdit || m_appCoreState->uiState.state == MenuHeaterSettingEdit || m_appCoreState->uiState.state == MenuTempTriggerEdit)
242
+            {
243
+                if (m_pBtnCancel->wasReleased() || m_pBtnOk->wasReleased())
212 244
                 {
213
-                    if (m_pBtnMinus->wasReleased())
245
+                    UiStateEnum nextState = m_appCoreState->uiState.state;
246
+                    if (m_appCoreState->uiState.state == MenuWaterSettingEdit)
214 247
                     {
215
-                        itemState->setting -= TEMP_INTERVAL;
248
+                        nextState = MenuWaterSetting;
216 249
                     }
217
-                    else if (m_pBtnPlus->wasReleased())
250
+                    else if (m_appCoreState->uiState.state == MenuHeaterSettingEdit)
218 251
                     {
219
-                        itemState->setting += TEMP_INTERVAL;
252
+                        nextState = MenuHeaterSetting;
220 253
                     }
221
-                    LOG(1, "Setting temp to %i (%i)", itemState->setting, TEMP_INTERVAL);
254
+                    else if (m_appCoreState->uiState.state == MenuTempTriggerEdit)
255
+                    {
256
+                        nextState = MenuTempTrigger;
257
+                    }
258
+
259
+                    if (m_pBtnOk->wasReleased())
260
+                    {
261
+                        *m_appCoreState->appState.pCurrentSettingEdit = m_appCoreState->appState.currentSettingEditTmp;
262
+                    }
263
+
264
+                    m_appCoreState->appState.currentSettingEditTmp = 0;
265
+                    m_appCoreState->appState.pCurrentSettingEdit = nullptr;
266
+                    setState(nextState);
267
+                }
268
+                else if (m_pBtnMinus->wasReleased() || m_pBtnPlus->wasReleased())
269
+                {
270
+                    m_appCoreState->appState.currentSettingEditTmp += (m_pBtnMinus->wasReleased() ? -1 : 1) * TEMP_INTERVAL;
222 271
                     m_appCoreState->uiState.isUpdateNeeded = true;
223 272
                 }
224 273
             }
@@ -240,16 +289,30 @@ void AppCore::setState(
240 289
     LOG_FN_BEGIN(1);
241 290
     LOG(5, "Changing state %i => %i", m_appCoreState->uiState.state, state);
242 291
 
243
-    if (state == Lighting)
292
+    if (state == HomeLighting)
244 293
     {
245 294
         digitalWrite(PIN_LCD_LED, 1);
246 295
     }
247
-    else if (state == Hibernate)
296
+    else if (state == HomeHibernate)
248 297
     {
249 298
         digitalWrite(PIN_LCD_LED, 0);
250
-        m_appCoreState->uiState.modeSequenceIndex = MODE_SEQUENCE_COUNT - 1;
251 299
         m_storage.save(*m_appCoreState);
252 300
     }
301
+    else if (state == MenuWaterSettingEdit)
302
+    {
303
+        m_appCoreState->appState.currentSettingEditTmp = m_appCoreState->appState.water.setting;
304
+        m_appCoreState->appState.pCurrentSettingEdit = &m_appCoreState->appState.water.setting;
305
+    }
306
+    else if (state == MenuHeaterSettingEdit)
307
+    {
308
+        m_appCoreState->appState.currentSettingEditTmp = m_appCoreState->appState.heater.setting;
309
+        m_appCoreState->appState.pCurrentSettingEdit = &m_appCoreState->appState.heater.setting;
310
+    }
311
+    else if (state == MenuTempTriggerEdit)
312
+    {
313
+        m_appCoreState->appState.currentSettingEditTmp = m_appCoreState->appState.tempTrigger;
314
+        m_appCoreState->appState.pCurrentSettingEdit = &m_appCoreState->appState.tempTrigger;
315
+    }
253 316
     m_appCoreState->uiState.state = state;
254 317
     m_appCoreState->uiState.isUpdateNeeded = true;
255 318
 
@@ -263,7 +326,7 @@ void AppCore::checkBoilerItem(
263 326
     LOG_FN_BEGIN(2);
264 327
 
265 328
     if (!boilerItemState->isActive && boilerItemState->current != TEMP_T_INVALID &&
266
-        boilerItemState->current <= boilerItemState->setting - TEMP_TRIGGER)
329
+        boilerItemState->current <= boilerItemState->setting - m_appCoreState->appState.tempTrigger)
267 330
     {
268 331
         boilerItemState->isActive = true;
269 332
         digitalWrite(boilerItemState->pinNo, HIGH);
@@ -307,12 +370,63 @@ void AppCore::printState()
307 370
 {
308 371
     LOG_FN_BEGIN(2);
309 372
 
310
-    m_pLcd->setCursor(0, 0);
311
-    printStateLine('S', &m_appCoreState->appState.water, m_appCoreState->uiState.state == WaterSetting,
312
-                   m_appCoreState->appState.water.isActive);
313
-    m_pLcd->setCursor(0, 1);
314
-    printStateLine('C', &m_appCoreState->appState.heater, m_appCoreState->uiState.state == HeaterSetting,
315
-                   m_appCoreState->appState.heater.isActive);
373
+    if (m_appCoreState->uiState.state == HomeLighting || m_appCoreState->uiState.state == HomeHibernate)
374
+    {
375
+        m_pLcd->setCursor(0, 0);
376
+        printStateLine('S', &m_appCoreState->appState.water);
377
+        m_pLcd->setCursor(0, 1);
378
+        printStateLine('C', &m_appCoreState->appState.heater);
379
+    }
380
+    else if (m_appCoreState->uiState.state == MenuWaterSetting || m_appCoreState->uiState.state == MenuHeaterSetting || m_appCoreState->uiState.state == MenuTempTrigger ||
381
+            m_appCoreState->uiState.state == MenuWaterSettingEdit || m_appCoreState->uiState.state == MenuHeaterSettingEdit || m_appCoreState->uiState.state == MenuTempTriggerEdit)
382
+    {
383
+        const auto isEdit = m_appCoreState->uiState.state == MenuWaterSettingEdit || m_appCoreState->uiState.state == MenuHeaterSettingEdit || m_appCoreState->uiState.state == MenuTempTriggerEdit;
384
+        const char* pTitle = nullptr;
385
+        temp_t* pTemp = nullptr;
386
+        if (isEdit)
387
+        {
388
+            pTemp = &m_appCoreState->appState.currentSettingEditTmp;
389
+        }
390
+        else
391
+        {
392
+            if (m_appCoreState->uiState.state == MenuWaterSetting ||
393
+                m_appCoreState->uiState.state == MenuWaterSettingEdit)
394
+            {
395
+                pTitle = "Sanitaire";
396
+                pTemp = &m_appCoreState->appState.water.setting;
397
+            }
398
+            else if (m_appCoreState->uiState.state == MenuHeaterSetting ||
399
+                     m_appCoreState->uiState.state == MenuHeaterSettingEdit)
400
+            {
401
+                pTitle = "Chauffage";
402
+                pTemp = &m_appCoreState->appState.heater.setting;
403
+            }
404
+            else if (m_appCoreState->uiState.state == MenuTempTrigger ||
405
+                     m_appCoreState->uiState.state == MenuTempTriggerEdit)
406
+            {
407
+                pTitle = "Delta";
408
+                pTemp = &m_appCoreState->appState.tempTrigger;
409
+            }
410
+        }
411
+        char tmpStr[17], tmpTemp[7];
412
+        m_pLcd->setCursor(0, 0);
413
+        snprintf(tmpStr, sizeof(tmpStr), "%s :                ", pTitle);
414
+        m_pLcd->print(tmpStr);
415
+        m_pLcd->setCursor(0, 1);
416
+        tempToStr(tmpTemp, *pTemp, 4);
417
+        snprintf(tmpStr, sizeof(tmpStr), "%s%s C                ", isEdit ? "> " : "  ", tmpTemp);
418
+        m_pLcd->print(tmpStr);
419
+    }
420
+    else if (m_appCoreState->uiState.state == MenuVersion)
421
+    {
422
+        char tmpStr[17];
423
+        m_pLcd->setCursor(0, 0);
424
+        snprintf(tmpStr, sizeof(tmpStr), "%s :                ", "Version");
425
+        m_pLcd->print(tmpStr);
426
+        m_pLcd->setCursor(0, 1);
427
+        snprintf(tmpStr, sizeof(tmpStr), "%s - %s                ", xstr(APP_CORE_VERSION), xstr(APP_CORE_COMMIT));
428
+        m_pLcd->print(tmpStr);
429
+    }
316 430
     m_appCoreState->uiState.isUpdateNeeded = false;
317 431
 
318 432
     LOG_FN_END(2);
@@ -322,8 +436,6 @@ void
322 436
 AppCore::printStateLine(
323 437
         char prefix
324 438
         , const BoilerItemState* boilerItemState
325
-        , bool isModifying
326
-        , bool isActive
327 439
 )
328 440
 {
329 441
     LOG_FN_BEGIN(2);
@@ -331,8 +443,7 @@ AppCore::printStateLine(
331 443
     char curTmp[7], setTmp[7], tmp[17];
332 444
     tempToStr(curTmp, boilerItemState->current, 5);
333 445
     tempToStr(setTmp, boilerItemState->setting, 4);
334
-    int count = snprintf(tmp, sizeof(tmp), "%c:%s [%s]%c%c", prefix, curTmp, setTmp, isModifying ? '<' : ' ',
335
-                         isActive ? LCD_CHAR_SENSOR : ' ');
446
+    int count = snprintf(tmp, sizeof(tmp), "%c:%s [%s] %c", prefix, curTmp, setTmp, boilerItemState->isActive ? LCD_CHAR_SENSOR : ' ');
336 447
     for (; count < 17; ++count)
337 448
     {
338 449
         tmp[count] = ' ';

+ 2
- 4
AppCore.h Näytä tiedosto

@@ -10,6 +10,8 @@
10 10
 class AppCore
11 11
 {
12 12
 public:
13
+    static UiStateEnum s_menuSequence[4];
14
+
13 15
     AppCore();
14 16
 
15 17
     void setup();
@@ -28,8 +30,6 @@ protected:
28 30
     void printStateLine(
29 31
             char prefix
30 32
             , const BoilerItemState* boilerItemState
31
-            , bool isModifying
32
-            , bool isActive
33 33
     );
34 34
 
35 35
     void tempToStr(
@@ -42,8 +42,6 @@ private:
42 42
     AppCoreState* m_appCoreState;
43 43
     Storage m_storage;
44 44
 
45
-    UiStateEnum m_modeSequence[3];
46
-
47 45
     Button* m_pBtnCancel;
48 46
     Button* m_pBtnOk;
49 47
     Button* m_pBtnMinus;

+ 12
- 5
Boiler.h Näytä tiedosto

@@ -6,10 +6,15 @@ typedef unsigned long timestamp_t;
6 6
 
7 7
 enum UiStateEnum
8 8
 {
9
-    Hibernate,
10
-    Lighting,
11
-    WaterSetting,
12
-    HeaterSetting
9
+    HomeHibernate,
10
+    HomeLighting,
11
+    MenuWaterSetting,
12
+    MenuWaterSettingEdit,
13
+    MenuHeaterSetting,
14
+    MenuHeaterSettingEdit,
15
+    MenuTempTrigger,
16
+    MenuTempTriggerEdit,
17
+    MenuVersion
13 18
 };
14 19
 
15 20
 struct BoilerItemState
@@ -26,13 +31,15 @@ struct AppState
26 31
     bool hasReadSensors;
27 32
     BoilerItemState water;
28 33
     BoilerItemState heater;
34
+    temp_t tempTrigger;
35
+    temp_t* pCurrentSettingEdit;
36
+    temp_t currentSettingEditTmp;
29 37
 };
30 38
 
31 39
 struct UiState
32 40
 {
33 41
     UiStateEnum state;
34 42
     timestamp_t lastOpMs;
35
-    unsigned modeSequenceIndex;
36 43
     bool isUpdateNeeded;
37 44
 };
38 45
 

Loading…
Peruuta
Tallenna