From 376b976a0cf7a516fa8af5308b616f7b75a3c63e Mon Sep 17 00:00:00 2001 From: Aurelien Labrosse Date: Thu, 9 Nov 2023 16:12:25 +0000 Subject: [PATCH] feat: allow player to be changed via serial command --- lib/Domain/TargetHost.cpp | 10 ++++++++++ test/native/test_Target/target.cpp | 17 ++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/Domain/TargetHost.cpp b/lib/Domain/TargetHost.cpp index 858ff67..50041aa 100644 --- a/lib/Domain/TargetHost.cpp +++ b/lib/Domain/TargetHost.cpp @@ -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) { @@ -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() { @@ -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(sender->context); + targetHost->game->changeCurrentPlayerTo(playerId); +} + void TargetHost::resetTargets() {} TargetHost::TargetHost(Game *game, ITargetGui *gui) : game(game), gui(gui) { diff --git a/test/native/test_Target/target.cpp b/test/native/test_Target/target.cpp index 410b392..da3179f 100644 --- a/test/native/test_Target/target.cpp +++ b/test/native/test_Target/target.cpp @@ -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 @@ -218,14 +217,13 @@ void expect_threshold_to_be_settable_via_serial_command() { Mock 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(); @@ -233,6 +231,7 @@ void expect_threshold_to_be_settable_via_serial_command() { Verify(Method(ArduinoFake(Serial), read)).Exactly(5_Times); + static const uint8_t expectedThresholdValue = 96; TEST_ASSERT_EQUAL(expectedThresholdValue, app.getThreshold()); // check UI feedback @@ -245,7 +244,6 @@ void expect_player_to_be_changeable_via_serial_command() { Mock mockGui; Game game(&mockGui.get()); TargetHost app(&game, &mockGui.get()); - uint8_t expectedPlayerId = 2; mockForCommandTest(mockGui); @@ -253,14 +251,14 @@ void expect_player_to_be_changeable_via_serial_command() { // 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); } @@ -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 &mockGui) { +static void mockForCommandTest(Mock &mockGui) { // // setup, provide a value of 10 for threshold // If serial command interpretation does not work, @@ -297,6 +297,9 @@ void mockForCommandTest(Mock &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();