Skip to content

Commit

Permalink
feat: measure ambiant light for each target separately
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 3, 2023
1 parent 10bf5da commit 53c3cc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
31 changes: 9 additions & 22 deletions lib/Domain/TargetHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const uint8_t TargetHost::TARGET_E_PIN = A2;
const uint8_t TargetHost::TARGET_R_PIN = A3;
const uint8_t TargetHost::TARGET_T_PIN = A4;

const uint8_t TargetHost::TARGET_ANALOG_PINS[] = {A0, A1, A2, A3, A4};

uint16_t TargetHost::getStoredThreshold() {
uint16_t result = EEPROM.read(TRESHOLD_LSB_ADDRESS);
result |= EEPROM.read(TRESHOLD_MSB_ADDRESS) << 8;
Expand All @@ -40,9 +38,8 @@ void TargetHost::storeThreshold(uint16_t threshold) {
EEPROM.update(TRESHOLD_MSB_ADDRESS, (threshold & 0xFF00) >> 8);
}

uint16_t TargetHost::getTargetLightValue(uint8_t targetIndex) {
uint8_t test = TARGET_ANALOG_PINS[targetIndex];
return analogRead(test);
inline uint16_t TargetHost::getTargetLightValue(const Target &target) {
return analogRead(target.pin);
}

void TargetHost::print(const char *) {}
Expand All @@ -52,31 +49,21 @@ void TargetHost::ledOn() { digitalWrite(LED_PIN, HIGH); }
void TargetHost::ledOff() { digitalWrite(LED_PIN, LOW); }

void TargetHost::setup() {
pinMode(LED_PIN, OUTPUT);

analogRead(TARGET_A_PIN);
analogRead(TARGET_Z_PIN);
analogRead(TARGET_E_PIN);
analogRead(TARGET_R_PIN);
analogRead(TARGET_T_PIN);

ambientLightValue = 0;
pinMode(LED_PIN, OUTPUT);

ambientLightValue += analogRead(TARGET_A_PIN);
ambientLightValue += analogRead(TARGET_Z_PIN);
ambientLightValue += analogRead(TARGET_E_PIN);
ambientLightValue += analogRead(TARGET_R_PIN);
ambientLightValue += analogRead(TARGET_T_PIN);
ambientLightValue /= 5;
for (Target &target : targets) {
analogRead(target.pin);
target.ambientValue = analogRead(target.pin);
}
}

void TargetHost::loop() {

uint16_t threshold = getStoredThreshold();

for (uint8_t targetIndex = 0; targetIndex < 5; targetIndex++) {
uint16_t lightValue = getTargetLightValue(targetIndex);
if (lightValue > threshold) {
for (Target &target : targets) {
if (getTargetLightValue(target) > threshold) {
ledOn();
delay(100);
ledOff();
Expand Down
33 changes: 20 additions & 13 deletions lib/Domain/TargetHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
#include <Game.hpp>
#include <stdint.h>

class Target {
public:
uint16_t ambientValue;
bool isHit;
uint8_t pin;
Target() : ambientValue(0), isHit(false), pin(0) {}
};

class TargetHost {

/**
Expand All @@ -27,28 +35,27 @@ class TargetHost {
uint16_t lightTreshold;
Game &game;

public:
/**
* @brief populated at startup with the mean
* ambient light sampled with each target
*/
uint16_t ambientLightValue;

static const uint8_t TRESHOLD_LSB_ADDRESS;
static const uint8_t TRESHOLD_MSB_ADDRESS;

static const uint8_t LED_PIN;
static const uint8_t TARGET_A_PIN;
static const uint8_t TARGET_Z_PIN;
static const uint8_t TARGET_E_PIN;
static const uint8_t TARGET_R_PIN;
static const uint8_t TARGET_T_PIN;

static const uint8_t TARGET_ANALOG_PINS[5];
public:
Target targets[5];
static const uint8_t TRESHOLD_LSB_ADDRESS;
static const uint8_t TRESHOLD_MSB_ADDRESS;

TargetHost(Game &game) : game(game) {}
TargetHost(Game &game) : game(game) {
targets[0].pin = TARGET_A_PIN;
targets[1].pin = TARGET_Z_PIN;
targets[2].pin = TARGET_E_PIN;
targets[3].pin = TARGET_R_PIN;
targets[4].pin = TARGET_T_PIN;
}

uint16_t getTargetLightValue(uint8_t);
uint16_t getTargetLightValue(const Target &target);
void print(const char *);
void ledOn();
void ledOff();
Expand Down
13 changes: 9 additions & 4 deletions test/native/test_Target/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ void expect_setup_to_configure_led_pin() {
When(Method(ArduinoFake(), pinMode)).Return();
When(Method(ArduinoFake(), analogRead)).AlwaysReturn(0);
app.setup();
Verify(Method(ArduinoFake(), pinMode).Using(TargetHost::LED_PIN, OUTPUT))
static const uint8_t LED_PIN = 9;
Verify(Method(ArduinoFake(), pinMode).Using(LED_PIN, OUTPUT))
.Once();
}

void expect_ambient_level_to_be_sampled_at_startup() {
void expect_ambient_level_to_be_sampled_at_startup_for_each_target() {
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(game);
Expand All @@ -132,7 +133,11 @@ void expect_ambient_level_to_be_sampled_at_startup() {

app.setup();

TEST_ASSERT_EQUAL(expectedMean, app.ambientLightValue);
TEST_ASSERT_EQUAL(99, app.targets[0].ambientValue);
TEST_ASSERT_EQUAL(108, app.targets[1].ambientValue);
TEST_ASSERT_EQUAL(104, app.targets[2].ambientValue);
TEST_ASSERT_EQUAL(103, app.targets[3].ambientValue);
TEST_ASSERT_EQUAL(96, app.targets[4].ambientValue);

Verify(Method(ArduinoFake(), pinMode)).AtLeastOnce();
Verify(Method(ArduinoFake(), analogRead)).AtLeastOnce();
Expand All @@ -142,7 +147,7 @@ int main(int, char **) {
UNITY_BEGIN();
RUN_TEST(expect_threshold_to_be_storable_in_eeprom);
RUN_TEST(expect_setup_to_configure_led_pin);
RUN_TEST(expect_ambient_level_to_be_sampled_at_startup);
RUN_TEST(expect_ambient_level_to_be_sampled_at_startup_for_each_target);
RUN_TEST(expect_status_led_to_blink_when_a_target_is_hit);
UNITY_END();
return 0;
Expand Down

0 comments on commit 53c3cc1

Please sign in to comment.