From 10bf5dab20112f58ddf346d308dccfcefec34994 Mon Sep 17 00:00:00 2001 From: Aurelien Labrosse Date: Thu, 2 Nov 2023 22:43:37 +0000 Subject: [PATCH] feat: Add ambient light mean sampling --- lib/Domain/TargetHost.cpp | 25 ++++++++++++++++++++++++- lib/Domain/TargetHost.hpp | 11 +++++++++++ platformio.ini | 3 ++- test/native/test_Target/target.cpp | 29 +++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/Domain/TargetHost.cpp b/lib/Domain/TargetHost.cpp index 50613f2..fc295ee 100644 --- a/lib/Domain/TargetHost.cpp +++ b/lib/Domain/TargetHost.cpp @@ -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() { @@ -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() { diff --git a/lib/Domain/TargetHost.hpp b/lib/Domain/TargetHost.hpp index e6024e5..6484016 100644 --- a/lib/Domain/TargetHost.hpp +++ b/lib/Domain/TargetHost.hpp @@ -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) {} diff --git a/platformio.ini b/platformio.ini index a962c46..f918658 100755 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/test/native/test_Target/target.cpp b/test/native/test_Target/target.cpp index 9dcc8d8..df573a5 100644 --- a/test/native/test_Target/target.cpp +++ b/test/native/test_Target/target.cpp @@ -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}; @@ -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 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;