From 521cdcb41639443e7d9ece96e7d11655d7b316aa Mon Sep 17 00:00:00 2001 From: Aurelien Labrosse Date: Fri, 3 Nov 2023 18:56:17 +0000 Subject: [PATCH] optim: Add cache for threshold treshold is stored in EEPROM, added a cache to avoid EEPROM read in loop() --- lib/Domain/TargetHost.cpp | 8 +++++--- lib/Domain/TargetHost.hpp | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Domain/TargetHost.cpp b/lib/Domain/TargetHost.cpp index be60b90..e985059 100644 --- a/lib/Domain/TargetHost.cpp +++ b/lib/Domain/TargetHost.cpp @@ -28,9 +28,11 @@ const uint8_t TargetHost::TARGET_R_PIN = A3; const uint8_t TargetHost::TARGET_T_PIN = A4; uint16_t TargetHost::getStoredThreshold() { - uint16_t result = EEPROM.read(TRESHOLD_LSB_ADDRESS); - result |= EEPROM.read(TRESHOLD_MSB_ADDRESS) << 8; - return result; + if (thresholdCache == 0) { + thresholdCache = EEPROM.read(TRESHOLD_LSB_ADDRESS); + thresholdCache |= EEPROM.read(TRESHOLD_MSB_ADDRESS) << 8; + } + return thresholdCache; } void TargetHost::storeThreshold(uint16_t threshold) { diff --git a/lib/Domain/TargetHost.hpp b/lib/Domain/TargetHost.hpp index 37ceb7e..b3fedf3 100644 --- a/lib/Domain/TargetHost.hpp +++ b/lib/Domain/TargetHost.hpp @@ -30,9 +30,11 @@ class Target { class TargetHost { /** - * Minimum light value to validate a hit + * Minimum light value to validate a hit. + * Shall be stored in EEPROM */ - uint16_t lightTreshold; + uint16_t thresholdCache; + Game &game; static const uint8_t LED_PIN; @@ -53,6 +55,7 @@ class TargetHost { targets[2].pin = TARGET_E_PIN; targets[3].pin = TARGET_R_PIN; targets[4].pin = TARGET_T_PIN; + thresholdCache = 0; } uint16_t getTargetLightValue(const Target &target);