Skip to content

Commit

Permalink
feat: allow player to be changed via serial command
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 13, 2023
1 parent 55e6c99 commit 376b976
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/Domain/TargetHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ static void serial_cmd_unrecognized_callback(SerialCommands *sender,
const char *cmd);

static void serial_cmd_setThreshold_callback(SerialCommands *sender);
static void serial_cmd_changePlayer_callback(SerialCommands *sender);

SerialCommand serial_cmd_setThreshold("T", &serial_cmd_setThreshold_callback);
SerialCommand serial_cmd_changePlayer("P", &serial_cmd_changePlayer_callback);

uint8_t TargetHost::getThreshold() {
if (thresholdCache == 0) {
Expand Down Expand Up @@ -78,6 +81,7 @@ void TargetHost::setup() {
_serial_commands.context = this;
_serial_commands.SetDefaultHandler(serial_cmd_unrecognized_callback);
_serial_commands.AddCommand(&serial_cmd_setThreshold);
_serial_commands.AddCommand(&serial_cmd_changePlayer);
}

void TargetHost::loop() {
Expand Down Expand Up @@ -120,6 +124,12 @@ void serial_cmd_setThreshold_callback(SerialCommands *sender) {
}
}

void serial_cmd_changePlayer_callback(SerialCommands *sender) {
uint8_t playerId = (uint8_t)atoi(sender->Next());
TargetHost *targetHost = static_cast<TargetHost *>(sender->context);
targetHost->game->changeCurrentPlayerTo(playerId);
}

void TargetHost::resetTargets() {}

TargetHost::TargetHost(Game *game, ITargetGui *gui) : game(game), gui(gui) {
Expand Down
17 changes: 10 additions & 7 deletions test/native/test_Target/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static void mockPrintAndPrinln();

static const uint8_t TRESHOLD_ADDRESS = 0;


void expect_threshold_to_be_storable_in_eeprom() {

// return LSB then MSB
Expand Down Expand Up @@ -218,21 +217,21 @@ void expect_threshold_to_be_settable_via_serial_command() {
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(&game, &mockGui.get());
uint8_t expectedThresholdValue = 96;

mockForCommandTest(mockGui);

//
// incoming serial command
//
When(Method(ArduinoFake(Serial), available)).Return(5, 4, 3, 2, 1, 0);

When(Method(ArduinoFake(Serial), read)).Return('T', ' ', '9', '6', '|');

app.setup();
app.loop();

Verify(Method(ArduinoFake(Serial), read)).Exactly(5_Times);

static const uint8_t expectedThresholdValue = 96;
TEST_ASSERT_EQUAL(expectedThresholdValue, app.getThreshold());

// check UI feedback
Expand All @@ -245,22 +244,21 @@ void expect_player_to_be_changeable_via_serial_command() {
Mock<ITargetGui> mockGui;
Game game(&mockGui.get());
TargetHost app(&game, &mockGui.get());
uint8_t expectedPlayerId = 2;

mockForCommandTest(mockGui);

//
// incoming serial command
//
When(Method(ArduinoFake(Serial), available)).Return(4, 3, 2, 1, 0);
When(Method(ArduinoFake(Serial), read))
.Return('P', ' ', expectedPlayerId, '|');
When(Method(ArduinoFake(Serial), read)).Return('P', ' ', '2', '|');

app.setup();
app.loop();

Verify(Method(ArduinoFake(Serial), read)).Exactly(4_Times);

static const uint8_t expectedPlayerId = 2;
TEST_ASSERT_EQUAL(expectedPlayerId, app.game->currentPlayer->id);
}

Expand All @@ -272,11 +270,13 @@ int main(int, char **) {
RUN_TEST(expect_status_led_to_blink_when_a_target_is_hit);
RUN_TEST(expect_gui_to_be_notified_when_a_target_is_hit);
RUN_TEST(expect_threshold_to_be_settable_via_serial_command);
RUN_TEST(expect_player_to_be_changeable_via_serial_command);

UNITY_END();
return 0;
}

void mockForCommandTest(Mock<ITargetGui> &mockGui) {
static void mockForCommandTest(Mock<ITargetGui> &mockGui) {
//
// setup, provide a value of 10 for threshold
// If serial command interpretation does not work,
Expand All @@ -297,6 +297,9 @@ void mockForCommandTest(Mock<ITargetGui> &mockGui) {
// gui notification
//
mockGuiForSetup(mockGui);
When(Method(mockGui, setCurrentPlayer)).AlwaysReturn();
When(Method(mockGui, resetTargets)).AlwaysReturn();
When(Method(mockGui, displayPlayerInfo)).AlwaysReturn();

When(OverloadedMethod(ArduinoFake(Serial), begin, void(unsigned long)))
.AlwaysReturn();
Expand Down

0 comments on commit 376b976

Please sign in to comment.