From 8c058df6acc685a6ad69673c772d7b8f3a6bbfc2 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 22 Nov 2022 18:07:16 +1100 Subject: [PATCH] Create shouldDeviceSleep.cpp --- .../utils/shouldDeviceSleep.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 source/Core/Threads/OperatingModes/utils/shouldDeviceSleep.cpp diff --git a/source/Core/Threads/OperatingModes/utils/shouldDeviceSleep.cpp b/source/Core/Threads/OperatingModes/utils/shouldDeviceSleep.cpp new file mode 100644 index 00000000..248a2004 --- /dev/null +++ b/source/Core/Threads/OperatingModes/utils/shouldDeviceSleep.cpp @@ -0,0 +1,42 @@ +#include "OperatingModeUtilities.h" + +bool shouldBeSleeping(bool inAutoStart) { +#ifndef NO_SLEEP_MODE + // Return true if the iron should be in sleep mode + if (getSettingValue(SettingsOptions::Sensitivity) && getSettingValue(SettingsOptions::SleepTime)) { + if (inAutoStart) { + // In auto start we are asleep until movement + if (lastMovementTime == 0 && lastButtonTime == 0) { + return true; + } + } + if (lastMovementTime > 0 || lastButtonTime > 0) { + if (((xTaskGetTickCount() - lastMovementTime) > getSleepTimeout()) && ((xTaskGetTickCount() - lastButtonTime) > getSleepTimeout())) { + return true; + } + } + } + +#ifdef HALL_SENSOR + // If the hall effect sensor is enabled in the build, check if its over + // threshold, and if so then we force sleep + if (getHallSensorFitted() && lookupHallEffectThreshold()) { + int16_t hallEffectStrength = getRawHallEffect(); + if (hallEffectStrength < 0) + hallEffectStrength = -hallEffectStrength; + // Have absolute value of measure of magnetic field strength + if (hallEffectStrength > lookupHallEffectThreshold()) { + if (lastHallEffectSleepStart == 0) { + lastHallEffectSleepStart = xTaskGetTickCount(); + } + if ((xTaskGetTickCount() - lastHallEffectSleepStart) > TICKS_SECOND) { + return true; + } + } else { + lastHallEffectSleepStart = 0; + } + } +#endif +#endif + return false; +} \ No newline at end of file