Skip to content

Commit

Permalink
test: Add test for target hit
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 1, 2023
1 parent 64aad8c commit ffc6b7d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 28 deletions.
34 changes: 28 additions & 6 deletions lib/Domain/TargetHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,48 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TargetHost.hpp"
#include <Arduino.h>
#include <arduino/EEPROM.h>

const uint8_t TargetHost::TRESHOLD_LSB_ADDRESS = 0;
const uint8_t TargetHost::TRESHOLD_MSB_ADDRESS = 1;
const uint8_t LED_PIN = 9;
const uint8_t TargetHost::LED_PIN = 9;
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;
return result;
}

void TargetHost::storeThreshold(uint16_t threshold) {
EEPROM.update(TRESHOLD_LSB_ADDRESS, threshold & 0xFF);
EEPROM.update(TRESHOLD_MSB_ADDRESS, (threshold & 0xFF00) >> 8);
}

uint16_t TargetHost::getTargetLightValue(Target target) { return 0; }
uint16_t TargetHost::getTargetLightValue(uint8_t targetIndex) {
uint8_t test = TARGET_ANALOG_PINS[targetIndex];
return analogRead(test);
}

void TargetHost::print(const char *) {}
void TargetHost::LedOn() {}
void TargetHost::LedOff() {}

void TargetHost::setup() {}
void TargetHost::loop() {}
void TargetHost::ledOn() { digitalWrite(LED_PIN, HIGH); }

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

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

void TargetHost::loop() {

uint16_t threshold = getStoredThreshold();

for (uint8_t targetIndex = 0; targetIndex < 5; targetIndex++) {
uint16_t lightValue = getTargetLightValue(targetIndex);
if (lightValue > threshold) {
ledOn();
delay(100);
ledOff();
}
}
}
13 changes: 9 additions & 4 deletions lib/Domain/TargetHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#pragma once

#include <Game.hpp>
#include <stdint.h>

class TargetHost {
Expand All @@ -24,18 +25,22 @@ class TargetHost {
* Minimum light value to validate a hit
*/
uint16_t lightTreshold;
Game &game;

public:
enum Target { One, Two, Three, Four, Five, Last };

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_ANALOG_PINS[5];

TargetHost(Game &game) : game(game) {}

uint16_t getTargetLightValue(Target);
uint16_t getTargetLightValue(uint8_t);
void print(const char *);
void LedOn();
void LedOff();
void ledOn();
void ledOff();

void setup();
void loop();
Expand Down
62 changes: 44 additions & 18 deletions test/native/test_Target/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <Game.hpp>
#include <ITargetGui.hpp>
#include <TargetHost.hpp>

#include <unity.h>

#include <ArduinoFake.h>
Expand All @@ -42,7 +45,9 @@ void expect_threshold_to_be_storable_in_eeprom() {
When(MOCKED_EEPROM_READ.Using(TargetHost::TRESHOLD_MSB_ADDRESS)).Return(1);
When(MOCKED_EEPROM_UPDATE).AlwaysReturn();

TargetHost app;
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(game);
app.storeThreshold(300);
TEST_ASSERT_EQUAL(300, app.getStoredThreshold());

Expand All @@ -53,44 +58,65 @@ 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};
const uint8_t LED_PIN = 9;

for (uint8_t targetId = TargetHost::Target::One;
targetId != TargetHost::Target::Last; targetId++) {
TargetHost app;
for (uint8_t targetIndex = 0; targetIndex < 5; targetIndex++) {

// background
When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn();
When(Method(ArduinoFake(), delay)).AlwaysReturn();
ArduinoFakeReset();

// Given
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(game);

// ARRANGE
// threshold is 100
When(MOCKED_EEPROM_READ.Using(TargetHost::TRESHOLD_LSB_ADDRESS))
.Return(100);
When(MOCKED_EEPROM_READ.Using(TargetHost::TRESHOLD_MSB_ADDRESS)).Return(0);

// and target targetId luminosity is 150
When(Method(ArduinoFake(), analogRead).Using(pins[targetId])).Return(150);
// and target 'targetIndex' luminosity is 150, other target luminosity is 0
When(Method(ArduinoFake(), analogRead)).AlwaysReturn(0);
// override only for 'analogRead(pins[targetIndex])'
When(Method(ArduinoFake(), analogRead).Using(pins[targetIndex]))
.AlwaysReturn(150);

// blink
When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn();
When(Method(ArduinoFake(), delay)).AlwaysReturn();

// when
// ACT
app.loop();

// then luminosity of target has been read
Verify(Method(ArduinoFake(), analogRead).Using(pins[targetId])).Once();
// ASSERT
// luminosity of target has been read
Verify(Method(ArduinoFake(), analogRead).Using(pins[targetIndex])).Once();
Verify(Method(ArduinoFake(), analogRead).Using(Ne(pins[targetIndex])))
.Exactly(4_Times);

// and led has blink during xx ms
Verify(Method(ArduinoFake(), digitalWrite).Using(TargetHost::LED_PIN, HIGH))
.Once();
Verify(Method(ArduinoFake(), digitalWrite).Using(TargetHost::LED_PIN, LOW))
.Once();
// led has blink during 100 ms
Verify(Method(ArduinoFake(), digitalWrite).Using(LED_PIN, HIGH)).Once();
Verify(Method(ArduinoFake(), digitalWrite).Using(LED_PIN, LOW)).Once();
Verify(Method(ArduinoFake(), delay).Using(100)).Exactly(1_Times);
}
}

void expect_setup_to_configure_led_pin() {
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(game);
When(Method(ArduinoFake(), pinMode)).Return();
app.setup();
Verify(Method(ArduinoFake(), pinMode).Using(TargetHost::LED_PIN, OUTPUT))
.Once();
}

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_status_led_to_blink_when_a_target_is_hit);
UNITY_END();
Expand Down

0 comments on commit ffc6b7d

Please sign in to comment.