Skip to content

Commit bca24a7

Browse files
authored
Merge pull request #1 from Mecapitronic/fix-codesmell-nested-ifs-S134
avoid nested ifs to reduce cognitive complexity
2 parents 7d91954 + d4a13cf commit bca24a7

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

src/LedRGB.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void LedRGB::Update()
2222

2323
void LedRGB::HandleCommand(Command cmd)
2424
{
25-
//if (cmd.cmd == ("RGB"))
25+
//if (cmd.cmd == "RGB")
2626
//{
2727
// RGB:0;0;0
2828
// print("RGB : ", cmd.data[0]);

src/Motor.cpp

+17-21
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,26 @@ void Motor::Update(float linear_speed_mms, float linear_direction_rad, float ang
8787

8888
void Motor::HandleCommand(Command cmd)
8989
{
90-
91-
if (cmd.cmd.startsWith("Motor"))
90+
if (cmd.cmd == "Motor" && cmd.size == 3)
9291
{
93-
if (cmd.cmd == ("Motor") && cmd.size == 3)
92+
// Motor:1;1000;50
93+
// Motor:1;1;50
94+
if (cmd.data[2] < 0 || 100 < cmd.data[2])
9495
{
95-
// Motor:1;1000;50
96-
// Motor:1;1;50
97-
if (cmd.data[2] >= 0 && cmd.data[2] <= 100)
98-
{
99-
print("Motor ", cmd.data[0]);
100-
print(" with freq ", cmd.data[1], " Hz");
101-
println(" with duty ", cmd.data[2], " %");
102-
if (cmd.data[0] == 1)
103-
stepper1->setPWM(cmd.data[1], cmd.data[2]);
104-
if (cmd.data[0] == 2)
105-
stepper2->setPWM(cmd.data[1], cmd.data[2]);
106-
if (cmd.data[0] == 3 && motorBaseType == OMNIDIRECTIONAL_3_MOTORS)
107-
stepper3->setPWM(cmd.data[1], cmd.data[2]);
108-
}
109-
}
110-
else
111-
{
112-
println("Not a Motor Command ");
96+
println("Invalid Duty Cycle");
97+
return;
11398
}
99+
100+
print("Motor ", cmd.data[0]);
101+
print(" with freq ", cmd.data[1], " Hz");
102+
println(" with duty ", cmd.data[2], " %");
103+
104+
if (cmd.data[0] == 1)
105+
stepper1->setPWM(cmd.data[1], cmd.data[2]);
106+
if (cmd.data[0] == 2)
107+
stepper2->setPWM(cmd.data[1], cmd.data[2]);
108+
if (cmd.data[0] == 3 && motorBaseType == OMNIDIRECTIONAL_3_MOTORS)
109+
stepper3->setPWM(cmd.data[1], cmd.data[2]);
114110
}
115111
}
116112

src/OTOS.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void OpticalTrackingOdometrySensor::Update()
153153

154154
void OpticalTrackingOdometrySensor::HandleCommand(Command cmd)
155155
{
156-
//if (cmd.cmd == ("Otos"))
156+
//if (cmd.cmd == "Otos")
157157
//{
158158
// Otos:0;0
159159
// print("Otos : ", cmd.data[0]);

src/main.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ void loop()
200200
{
201201
Command cmd = ESP32_Helper::GetCommand();
202202

203-
if (cmd.cmd.startsWith("Help"))
203+
otos.HandleCommand(cmd);
204+
motor.HandleCommand(cmd);
205+
206+
if (cmd.cmd == "Help")
204207
{
205208
otos.PrintCommandHelp();
206209
motor.PrintCommandHelp();
207210
}
208-
otos.HandleCommand(cmd);
209-
motor.HandleCommand(cmd);
210-
211-
if (cmd.cmd == ("GoToPose") && cmd.size == 3)
211+
else if (cmd.cmd == "GoToPose" && cmd.size == 3)
212212
{
213213
// GoToPose:500;500;0
214214
// GoToPose:50;50;0
@@ -223,7 +223,7 @@ void loop()
223223
print(" h=", goTo.h);
224224
println();
225225
}
226-
if (cmd.cmd == ("SetPose") && cmd.size == 3)
226+
else if (cmd.cmd == "SetPose" && cmd.size == 3)
227227
{
228228
// SetPose:500;500;0
229229
DisableTimerMotion();
@@ -238,7 +238,7 @@ void loop()
238238
println();
239239
EnableTimerMotion();
240240
}
241-
if(cmd.cmd == ("PF") && cmd.size == 1)
241+
else if(cmd.cmd == "PF" && cmd.size == 1)
242242
{
243243
// PathFinding
244244
// PF:5
@@ -251,21 +251,20 @@ void loop()
251251
{
252252
print("PF Not Found");
253253
}
254-
}
255-
256-
if(cmd.cmd == ("VertexList") && cmd.size == 0)
254+
}
255+
else if(cmd.cmd == "VertexList")
257256
{
258257
Mapping::PrintVertexList();
259258
}
260-
if(cmd.cmd == ("SegmentList") && cmd.size == 0)
259+
else if(cmd.cmd == "SegmentList")
261260
{
262261
Mapping::PrintSegmentList();
263262
}
264-
if(cmd.cmd == ("CircleList") && cmd.size == 0)
263+
else if(cmd.cmd == "CircleList")
265264
{
266265
Mapping::PrintCircleList();
267266
}
268-
if(cmd.cmd == ("MappingList") && cmd.size == 0)
267+
else if(cmd.cmd == "MappingList")
269268
{
270269
Mapping::PrintVertexList();
271270
Mapping::PrintSegmentList();

0 commit comments

Comments
 (0)