Skip to content

Commit b48de08

Browse files
Fixed Ctrl+C handling and improved Arduino initialization/shutdown with proper interrupt and error handling.
1 parent 77576a0 commit b48de08

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

ArduinoStrike/ArduinoStrike/Arduino.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "Logger.h"
22
#include "Arduino.h"
33

4+
extern volatile bool g_shouldExit;
5+
46
Arduino::~Arduino()
57
{
68
if (serial_port.is_open())
@@ -13,12 +15,18 @@ Arduino::~Arduino()
1315
Arduino::Arduino(LPCSTR name) : io_context(), serial_port(io_context)
1416
{
1517
char port[100] = "\\.\\";
16-
1718
Logger::LogMessage("Searching for device...");
19+
1820
while (!GetDevice(name, port))
1921
{
2022
Logger::LogMessage("Device not found. Retrying...");
2123
sleep_for(milliseconds(1000));
24+
25+
if (g_shouldExit)
26+
{
27+
Logger::LogMessage("Search interrupted by user request.");
28+
throw runtime_error("Device search interrupted");
29+
}
2230
}
2331

2432
Logger::LogMessage(string("Device found: ") + name + " (" + port + ")");
@@ -150,7 +158,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
150158

151159
if (devices.empty())
152160
{
153-
Logger::LogMessage("No devices found in the system.");
161+
Logger::LogMessage("No devices found in the system!");
154162
return false;
155163
}
156164

@@ -168,7 +176,7 @@ bool Arduino::LoadConfiguration(const string& file_name, DeviceInfo& config_devi
168176

169177
if (!config)
170178
{
171-
Logger::LogMessage("Configuration file not found.", boost::log::trivial::debug);
179+
Logger::LogMessage("Configuration file not found!", boost::log::trivial::debug);
172180
return false;
173181
}
174182

ArduinoStrike/ArduinoStrike/ArduinoStrike.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,22 @@ int main()
3939
}
4040

4141
utils.PrintAscii(ASCII_INTRO);
42-
Arduino arduino("Arduino Leonardo");
43-
utils.PrintAscii(ASCII_OUTRO), utils.PrintHotkeys(config.GenerateHotkeysString());
42+
43+
Arduino* arduino = nullptr;
44+
45+
try
46+
{
47+
arduino = new Arduino("Arduino Leonardo");
48+
}
49+
catch (const runtime_error& e)
50+
{
51+
Logger::LogMessage("Failed to initialize Arduino: " + string(e.what()));
52+
Logger::LogMessage("Exiting...");
53+
return 1;
54+
}
55+
56+
utils.PrintAscii(ASCII_OUTRO);
57+
utils.PrintHotkeys(config.GenerateHotkeysString());
4458

4559
ModuleManager manager;
4660
manager.AddModule<Bhop>("Bhop", VK_SPACE);
@@ -52,7 +66,9 @@ int main()
5266

5367
while (!g_shouldExit)
5468
{
55-
manager.ProcessModules(arduino, config);
69+
manager.ProcessModules(*arduino, config);
5670
sleep_for(milliseconds(10));
5771
}
72+
73+
delete arduino;
5874
}

0 commit comments

Comments
 (0)