From 504223612dd7a1c6bd58c4241b8177fab32df2ee Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 13 Aug 2020 13:22:39 +0200 Subject: [PATCH] [apps/apps_container] Add timer to circuit breaker poincareCircuitBreaker requires the Back key to be pressed for at least 50 ms before interrupting a computation. This is effectively invisible for the user, but fixes a bug in Calculation : - When leaving the additional results for a result using units by pressing the Back key, a reduction preceding a call to removeUnits would be interrupted, causing an undefined behaviour. Change-Id: Iec667ae964f190de2171850cc22e1726959e6cb5 --- apps/apps_container.cpp | 17 ++++++++++++++++- apps/calculation/calculation.cpp | 3 --- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/apps_container.cpp b/apps/apps_container.cpp index a8cb20aa0..ca3c1be1c 100644 --- a/apps/apps_container.cpp +++ b/apps/apps_container.cpp @@ -51,8 +51,23 @@ AppsContainer::AppsContainer() : } bool AppsContainer::poincareCircuitBreaker() { + constexpr uint64_t minimalPressDuration = 20; + static uint64_t beginningOfInterruption = 0; Ion::Keyboard::State state = Ion::Keyboard::scan(); - return state.keyDown(Ion::Keyboard::Key::Back) || state.keyDown(Ion::Keyboard::Key::Home) || state.keyDown(Ion::Keyboard::Key::OnOff); + bool interrupt = state.keyDown(Ion::Keyboard::Key::Back) || state.keyDown(Ion::Keyboard::Key::Home) || state.keyDown(Ion::Keyboard::Key::OnOff); + if (!interrupt) { + beginningOfInterruption = 0; + return false; + } + if (beginningOfInterruption == 0) { + beginningOfInterruption = Ion::Timing::millis(); + return false; + } + if (Ion::Timing::millis() - beginningOfInterruption > minimalPressDuration) { + beginningOfInterruption = 0; + return true; + } + return false; } App::Snapshot * AppsContainer::hardwareTestAppSnapshot() { diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index 3c0372c8c..2ed67a817 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -254,9 +254,6 @@ Calculation::AdditionalInformationType Calculation::additionalInformationType(Co } if (o.hasUnit()) { Expression unit; - /* FIXME : When this method is accessed via leaving the additional outputs, - * ie via a press on BACK, the reduction is interrupted, and removeUnit - * goes badly.*/ PoincareHelpers::Reduce(&o, App::app()->localContext(), ExpressionNode::ReductionTarget::User, ExpressionNode::SymbolicComputation::ReplaceAllSymbolsWithDefinitionsOrUndefined, ExpressionNode::UnitConversion::None); o = o.removeUnit(&unit); double value = PoincareHelpers::ApproximateToScalar(o, App::app()->localContext());