Boolean variable with non-boolean switch cases

In the following code for the ELEGOO Conqueror Robot Tank Kit 2024.06.05. In the file DeviceDriverSet_xxx0.cpp. The following code has a variable called direction_A which is a boolean variable.

In the switch statement for direction_A, there is case direction_void. For a boolean variable, I would expect case True and case False. But in the code below, there is none of true or false. How is this code working? What am I missing?
Thank you all for the help!

/*
 Motor_control:AB / 方向、速度
*/
void DeviceDriverSet_Motor::DeviceDriverSet_Motor_control(boolean direction_A, uint8_t speed_A, //A组电机参数
                                                          boolean direction_B, uint8_t speed_B, //B组电机参数
                                                          boolean controlED                     //AB使能允许 true
                                                          )                                     //电机控制
{
  if (controlED == control_enable) //使能允许?
  {
    digitalWrite(PIN_Motor_STBY, HIGH);
    { //A...Right

      switch (direction_A) //方向控制
      {
      case direction_just:
        digitalWrite(PIN_Motor_AIN_1, HIGH);
        analogWrite(PIN_Motor_PWMA, speed_A);
        break;
      case direction_back:

        digitalWrite(PIN_Motor_AIN_1, LOW);
        analogWrite(PIN_Motor_PWMA, speed_A);
        break;
      case direction_void:
        analogWrite(PIN_Motor_PWMA, 0);
        digitalWrite(PIN_Motor_STBY, LOW);
        break;
      default:
        analogWrite(PIN_Motor_PWMA, 0);
        digitalWrite(PIN_Motor_STBY, LOW);
        break;
      }
    }

    { //B...Left
      switch (direction_B)
      {
      case direction_just:
        digitalWrite(PIN_Motor_BIN_1, HIGH);

        analogWrite(PIN_Motor_PWMB, speed_B);
        break;
      case direction_back:
        digitalWrite(PIN_Motor_BIN_1, LOW);
        analogWrite(PIN_Motor_PWMB, speed_B);
        break;
      case direction_void:
        analogWrite(PIN_Motor_PWMB, 0);
        digitalWrite(PIN_Motor_STBY, LOW);
        break;
      default:
        analogWrite(PIN_Motor_PWMB, 0);
        digitalWrite(PIN_Motor_STBY, LOW);
        break;
      }
    }
  }
  else
  {
    digitalWrite(PIN_Motor_STBY, LOW);
    return;
  }
}

Probably found in one of its library files.

Please show the link to the full code

Look at the library’s code. If indeed they are not defined as Boolean but as an enum then it’s a bug.

Someone uploaded an unzipped copy of the V4 code to GitHub, making it easy to find. Look in the corresponding header file, DeviceDriverSet_xxx0.h

#define direction_just true
#define direction_back false
#define direction_void 3

There's the true and false. _void is 3, which of course will never match a boolean. More precisely, 3 is considered true, but in the reverse, true is considered to be just 1. If you compile this code

void setup() {
  Serial.begin(115200);
}

void loop() {
  bool x = millis() % 2;
  switch (x) {
    case false:
      Serial.println("no");
      break;
    case 1:
      Serial.println("yes");
      break;
    case 3:
      Serial.println("neither");
      break;
  }
  delay(1000);
}

with the "Compiler warnings" set to Default (instead of None, in the Preferences, under the File menu), you get two warnings

warning: case label value exceeds maximum value for type
     case 3:
     ^~~~
warning: switch condition has boolean value [-Wswitch-bool]
   switch (x) {
          ^

That part of the code at least is not very good.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.