Skip to content

Commit

Permalink
feat: Add ambient light mean sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 2, 2023
1 parent 0d620b8 commit 10bf5da
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
25 changes: 24 additions & 1 deletion lib/Domain/TargetHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
const uint8_t TargetHost::TRESHOLD_LSB_ADDRESS = 0;
const uint8_t TargetHost::TRESHOLD_MSB_ADDRESS = 1;
const uint8_t TargetHost::LED_PIN = 9;
const uint8_t TargetHost::TARGET_A_PIN = A0;
const uint8_t TargetHost::TARGET_Z_PIN = A1;
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() {
Expand All @@ -45,7 +51,24 @@ void TargetHost::ledOn() { digitalWrite(LED_PIN, HIGH); }

void TargetHost::ledOff() { digitalWrite(LED_PIN, LOW); }

void TargetHost::setup() { pinMode(LED_PIN, OUTPUT); }
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;

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;
}

void TargetHost::loop() {

Expand Down
11 changes: 11 additions & 0 deletions lib/Domain/TargetHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ class TargetHost {
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];

TargetHost(Game &game) : game(game) {}
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ lib_deps =
# -cavrisp2
# -cdragon_isp
# -cavrispmkII
upload_command = avrdude -vv -pm328p -c avrisp -P/dev/ttyS0 $UPLOAD_FLAGS -U flash:w:$SOURCE:i

upload_command = avrdude -vv -b57600 -pm328p -c avrisp -Pcom9 $UPLOAD_FLAGS -U flash:w:$SOURCE:i

# upload_protocol=custom
# upload_speed=115200
Expand Down
29 changes: 27 additions & 2 deletions test/native/test_Target/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void expect_threshold_to_be_storable_in_eeprom() {
}

void expect_status_led_to_blink_when_a_target_is_hit() {

// Target is hit means target light value exceed threshold

const uint8_t pins[] = {A0, A1, A2, A3, A4};
Expand Down Expand Up @@ -109,15 +109,40 @@ void expect_setup_to_configure_led_pin() {
Game game(&mockGui.get());
TargetHost app(game);
When(Method(ArduinoFake(), pinMode)).Return();
When(Method(ArduinoFake(), analogRead)).AlwaysReturn(0);
app.setup();
Verify(Method(ArduinoFake(), pinMode).Using(TargetHost::LED_PIN, OUTPUT))
.Once();
}

void expect_ambient_level_to_be_sampled_at_startup() {
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(game);

uint16_t expectedMean = 102;

When(Method(ArduinoFake(), pinMode)).Return();
When(Method(ArduinoFake(), analogRead)).AlwaysReturn(1000);
When(Method(ArduinoFake(), analogRead).Using(A0)).AlwaysReturn(99);
When(Method(ArduinoFake(), analogRead).Using(A1)).AlwaysReturn(108);
When(Method(ArduinoFake(), analogRead).Using(A2)).AlwaysReturn(104);
When(Method(ArduinoFake(), analogRead).Using(A3)).AlwaysReturn(103);
When(Method(ArduinoFake(), analogRead).Using(A4)).AlwaysReturn(96);

app.setup();

TEST_ASSERT_EQUAL(expectedMean, app.ambientLightValue);

Verify(Method(ArduinoFake(), pinMode)).AtLeastOnce();
Verify(Method(ArduinoFake(), analogRead)).AtLeastOnce();
}

int main(int, char **) {
UNITY_BEGIN();
RUN_TEST(expect_setup_to_configure_led_pin);
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_status_led_to_blink_when_a_target_is_hit);
UNITY_END();
return 0;
Expand Down

0 comments on commit 10bf5da

Please sign in to comment.