// Read in message from slave(s) confirming the completion of tasks.
void confirmCompletion()
{
if (radio.available())
{
radio.read(&confirmation, sizeof(confirmation));
if (confirmation[0] == 'X') // Task is completed.
{
if (libraryMasterDebug)
{
Serial.print("Task ");
Serial.print(confirmation[1]);
Serial.print(" completed by slave");
Serial.println(confirmation[2]);
} // end if
// Set the Finite State Machine flags.
switch (confirmation[2]) // Slave number.
{
case'0':
switch (confirmation[1]) // Task number.
{
case'1':
slave0Task1done = true;
case'2':
slave0Task2done = true;
case'3':
slave0Task3done = true;
default:
if (libraryMasterDebug)
{
Serial.print
("Invalid task confirmation from slave");
Serial.println(confirmation[2]);
} // end if
stallTheProgram();
} // end case switch (confirmation[1])
slave0waiting = true;
case'1':
switch (confirmation[1]) // Task number.
{
case'1':
slave1Task1done = true;
case'2':
slave1Task2done = true;
case'3':
slave1Task3done = true;
default:
if (libraryMasterDebug)
{
Serial.print
("Invalid task confirmation from slave");
Serial.println(confirmation[2]);
} // end if
stallTheProgram();
} // end case switch (confirmation[1])
slave1waiting = true;
// cases must be set for all possible slaves << ===========
default:
if (libraryMasterDebug)
Serial.print("Invalid slave number");
stallTheProgram();
} // end switch (confirmation[2])
} // end if (confirmation[0] == 'X')
} // end if (radio.available())
} // end confirmCompletion()
This code is written to program Arduino stuff so some of the identifiers may look a bit strange. That shouldn't matter, it's C++ structure I am interested in.
Firstly, is it okay to have switch within a switch?
Secondly, is the structure I have correct?
Thirdly, is there any reason (given correct input) why a 'default' should trigger?
Firstly, is it okay to have switch within a switch?
Yes.
Secondly, is the structure I have correct?
Probably not, since:
Thirdly, is there any reason (given correct input) why a 'default' should trigger?
None of the code in your switch cases have a break, so if the code for one case is executed, it will fall through to the next case (until it reaches a break or the end of the compound statement which defines the body of the switch statement) and execute that code too.
Use a break; statement to terminate the code in each case.
Your compiler is likely generating a warning that advises you similarly.