From fa8734a9a8b190bb8367df2d859bd60b7b52a490 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Thu, 27 Oct 2022 16:33:36 +0300 Subject: [PATCH] state-handler: fix auto mode --- esp-vent-main/inc/Timer.h | 2 ++ esp-vent-main/inc/common_control_values.h | 4 ++-- .../src/StateHandler/StateHandler.cpp | 12 ++++++++---- esp-vent-main/src/Timer.cpp | 4 +++- esp-vent-main/src/esp-vent-main.cpp | 19 +++++++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/esp-vent-main/inc/Timer.h b/esp-vent-main/inc/Timer.h index a297876..d657d87 100644 --- a/esp-vent-main/inc/Timer.h +++ b/esp-vent-main/inc/Timer.h @@ -75,8 +75,10 @@ public: private: volatile std::atomic_int counter; + volatile std::atomic_int prev_ticks; uint32_t freq; bool mode; + }; #endif /* TIMER_H_ */ diff --git a/esp-vent-main/inc/common_control_values.h b/esp-vent-main/inc/common_control_values.h index 3f29b07..4d17b60 100644 --- a/esp-vent-main/inc/common_control_values.h +++ b/esp-vent-main/inc/common_control_values.h @@ -18,9 +18,9 @@ enum _global_values { LCD_SIZE = 16, - TIMER_GLOBAL_TIMEOUT = 15000, + TIMER_GLOBAL_TIMEOUT = 120000, TIMER_SENSORS_TIMEOUT = 5000, - TIMER_PRESSURE_TIMEOUT = 500, + TIMER_PRESSURE_TIMEOUT = 250, TIMER_ERROR_VALUE = -255, }; diff --git a/esp-vent-main/src/StateHandler/StateHandler.cpp b/esp-vent-main/src/StateHandler/StateHandler.cpp index 5f8cec5..ddfd18c 100644 --- a/esp-vent-main/src/StateHandler/StateHandler.cpp +++ b/esp-vent-main/src/StateHandler/StateHandler.cpp @@ -150,8 +150,8 @@ StateHandler::stateAuto (const Event &event) break; case Event::eTick: handleTickValue (event.value); - pid (); - this->_propeller->spin (fan_speed.getCurrent ()); + pid (); + this->_propeller->spin (fan_speed.getCurrent ()); break; } } @@ -190,13 +190,16 @@ StateHandler::handleControlButtons (uint8_t button) { case BUTTON_CONTROL_DOWN: this->value[(current_mode)].dec (); + state_timer->resetCounter(); break; case BUTTON_CONTROL_UP: this->value[(current_mode)].inc (); + state_timer->resetCounter(); break; case BUTTON_CONTROL_TOG_MODE: current_mode = !current_mode; SetState (&StateHandler::stateInit); + state_timer->resetCounter(); return; break; default: @@ -220,9 +223,10 @@ StateHandler::handleTickValue (int value) updateSensorValues (); // displaySet (SENSORS); } - if (value % TIMER_PRESSURE_TIMEOUT == 0) + if (value > TIMER_PRESSURE_TIMEOUT) { SetState (&StateHandler::stateGetPressure); + state_timer->resetCounter(); } if (value == TIMER_ERROR_VALUE) { @@ -282,7 +286,7 @@ StateHandler::updateSensorValues () { sensors_data[TEMPERATURE] = humidity.readT (); - sensors_data[PRESSUREDAT] = _pressure->getPressure (); +// sensors_data[PRESSUREDAT] = _pressure->getPressure (); sensors_data[CO2] = co2.read (); state_timer->tickCounter (5); sensors_data[HUMIDITY] = humidity.readRH (); diff --git a/esp-vent-main/src/Timer.cpp b/esp-vent-main/src/Timer.cpp index 6183cbe..17ce1d3 100644 --- a/esp-vent-main/src/Timer.cpp +++ b/esp-vent-main/src/Timer.cpp @@ -28,6 +28,7 @@ Timer::Timer (uint32_t freq, bool setup) : freq (freq), mode (setup) } resetCounter (); timer = 0; + prev_ticks = 0; systicks.store (0, std::memory_order_relaxed); } @@ -43,8 +44,9 @@ Timer::tickCounter (int ms) { resetCounter (); } - counter.fetch_add (ms, std::memory_order_relaxed); Sleep (ms); + counter += (systicks - prev_ticks); + prev_ticks = systicks; } void diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 705116b..3559acb 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -71,8 +71,21 @@ main (void) /* Other declarations */ int getcounterValue; + uint32_t sleep_Arr[100] = {0}; + + CoreDebug->DEMCR |= 1 << 24; + DWT->CTRL |= 1; + + volatile static int i1 = 0 ; + volatile static int i2 = 0 ; + volatile static int i = 0 ; + + + while (1) { + i1 = DWT->CYCCNT; + getcounterValue = glob_time.getCounter (); if (getcounterValue > TIMER_GLOBAL_TIMEOUT) { @@ -84,6 +97,12 @@ main (void) sw_toggle.listen (); ventMachine.HandleState (Event (Event::eTick, getcounterValue)); glob_time.tickCounter (1); + +// i2 = DWT->CYCCNT; +// sleep_Arr[i] = (i2 - i1) / 72; +// ++i; +// if (i == 100) +// while(1); } return 0;