C ? Construct

For the LIFE of me I do not know why this statement is failing.  There are two versions of settings for port outputs here – the one in red and the one in blue. If I choose the blue version it will only work in one direction, not the other.

If I choose the version in red – it works full stop.

The same ? construct is used in sending messages out and that works perfectly. This is C programming in Eclipse for the ESP8266 chips… “intvalue” is just the number 0 or 1

 

else if (strcmp(token,"out2")==0)
        {
            if (sysCfg.out_4_status!=intvalue) { sysCfg.out_4_status=intvalue; do_update=1; } // only update if actual change

            if (sysCfg.out_4_status==1) GPIO_OUTPUT_SET(LED_GPIO_4, OUT_ON); // on or off by default
            else GPIO_OUTPUT_SET(LED_GPIO_4, OUT_OFF);

            //GPIO_OUTPUT_SET(LED_GPIO_4, (sysCfg.out_4_status==1) ? OUT_ON : OUT_OFF); // doesnt work - don't know why
            strcpy(token,tBuf); strcat(token,"/out2"); MQTT_Publish(client, token,(sysCfg.out_4_status==0) ? "OFF" : "ON" ,(sysCfg.out_4_status==0) ? 3 : 2, 0, 0);
        }

Thoughts anyone? I must’ve been starting at this for too long.

Facebooktwittergoogle_pluspinterestlinkedin

5 thoughts on “C ? Construct

  1. GPIO_OUTPUT_SET is a macro not a function so it could expand to more the one statement try:

    if (sysCfg.out_4_status==1) {
    GPIO_OUTPUT_SET(LED_GPIO_4, OUT_ON); // on or off by default
    } else {
    GPIO_OUTPUT_SET(LED_GPIO_4, OUT_OFF);
    }

    1. Hi - thanks yes I did end up using the if-else option - and it works - I still don't understand why the ? version doesn't work.. it does further down the code when it comes to sending an MQTT message so I don't understand why it does not work in the GPIO setting.... any ideas?

      1. My thought went to macro expansion as well. In C(++), macros work on the text level, so unless the macro author uses parentheses every time they use a macro argument in an expression - which they should do - you can easily get undesired interactions due to operator precedence pulling any expression you pass in apart. For example, if a is a macro argument, consider a*2. What happens if you pass 3+4 into the macro? The expression yields 11 rather than 14. A macro author should write, say, (a)*2 instead. Not all do.

        1. And of course you were all right - the version with parentheses around the whole thing solved the problem - I knew it had to work one way or another - but that is valuable knowledge. Thank you.

  2. Pete,
    As Russ said, GPIO_OUTPUT_SET() is a macro and without knowing of if is defined, it is difficult to explain. (I don't have the ESP SKD installed).
    When macro are expended, text are replaced just a like a basic text editor search-and-replace.
    For example consider :
    #define DOUBLE(a) 2*a

    If you use :
    calc = DOUBLE( 5 );
    This is expanded as
    calc = 2*5;

    If you use
    calc = DOUBLE( 1+2 );
    This is expanded as
    calc = 2 *1+2; // result 4 instead of expected 6

    Would have been better to define
    #define DOUBLE(a) (2*(a))
    Which would have expended as
    calc = (2*(1+2)); // provide expected 6

    Imagine also that DOUBLE was defined as
    #define DOUBLE(a) ((a)+(a))
    And you use it with
    i = 1;
    calc = DOUBLE(i++);
    What would be the result of calc ? and the value of i ?

    So you may try again your code above by adding another layer of parenthesis:
    GPIO_OUTPUT_SET(LED_GPIO_4, ((sysCfg.out_4_status==1) ? OUT_ON : OUT_OFF) );

Comments are closed.