Skip to content

Commit

Permalink
optim: Add cache for threshold
Browse files Browse the repository at this point in the history
treshold is stored in EEPROM, added a cache to avoid
EEPROM read in loop()
  • Loading branch information
arcadien committed Nov 3, 2023
1 parent 53c3cc1 commit 521cdcb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/Domain/TargetHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions lib/Domain/TargetHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 521cdcb

Please sign in to comment.