Topics

V4.3 arduino code description... #arduino

ajparent1/KB1GMX
 

byte enc_state (void) {
    return (analogRead(ENC_A) > 500 ? 1 : 0) + (analogRead(ENC_B) > 500 ? 2: 0);
}

That's the code from the UI section...  My brain does not want to parse it.

Best I get is analogread(ENC_A) returns greater than 500 its either a 1 or 0 returned.
No where can I find the "?" or ":" usage and meaning.   

Any ideas...  My brain says the library function turns that into something intelligible.

Also why can't it be?

byte enc_state (void) {
    return (digitalRead(ENC_A) ? 1 : 0) + (digitalRead(ENC_B)  ? 2: 0);
}

Allison

Jerry Gaffke
 

The ternary operator has been around since the 70's
    https://en.wikipedia.org/wiki/%3F:#C


This equivalent (but untested) function might be easier to read.


// Returns a value of 0,1,2, or 3
// If ENC_A is high, the LSB is turned on
// If ENC_B is high, then the MSB is turned on
byte enc_state (void)   {
    byte rval = 0;
    if (analogRead(ENC_A) > 500)
        rval =1;
    if (analogRead(ENC_B) > 500)
        rval = rval + 2;
    return(rval);
}
   
Would be better to use your digitalRead(), not the analogRead().
Not sure why it was done this way.
Only the nano analog pins A6 and A7 have no digital IO mode,
but perhaps having done battle with those it was assumed that the others were the same?

Jerry



On Thu, Jul 19, 2018 at 03:02 PM, ajparent1/KB1GMX wrote:
byte enc_state (void) {
    return (analogRead(ENC_A) > 500 ? 1 : 0) + (analogRead(ENC_B) > 500 ? 2: 0);
}

That's the code from the UI section...  My brain does not want to parse it.

Best I get is analogread(ENC_A) returns greater than 500 its either a 1 or 0 returned.
No where can I find the "?" or ":" usage and meaning.   

Any ideas...  My brain says the library function turns that into something intelligible.

Also why can't it be?

byte enc_state (void) {
    return (digitalRead(ENC_A) ? 1 : 0) + (digitalRead(ENC_B)  ? 2: 0);
}

Allison

ajparent1/KB1GMX
 

Thanks Jerry,

Its one I never use as it make code less readable.  As a result the brain locked.
It was just to cryptic for me.

For a test I did try digitalRead() and two things happened, it works, and the compiled
code is smaller.  Less gnashing and mashing.

Trying to figure out the encoder stuff so I can substitute a pair of buttons with the logic of 
tap (up) or tap (down) for a single increment/decrement of frequency or menu item.
Push and hold does the key repeat.  Least that what I want to replace...

Allison

Jerry Gaffke
 

Don't use it much either, I also find it cryptic.
But it's very handy in macros, the *..h files are full of the ternary operator.

I'm a luddite myself when it comes to a fancy user interface for no particular reason.
The KD1JV ATSprint/MTR rigs with their button interface are quite usable,
and considerably more robust than anything with an ebay mechanical encoder. 

Jerry



On Thu, Jul 19, 2018 at 03:33 PM, ajparent1/KB1GMX wrote:
Its one I never use as it make code less readable.  As a result the brain locked.
It was just to cryptic for me.

ajparent1/KB1GMX
 

Steve, KD1JV has a knack for good code and user interfaces.  I have one of his
short run of Slopbucket SSB/CW 20m rig.  Great radio and very solid user
interface.  Fun build.  The code for it is rather interesting.

I tend to write very bland code.  Lots of lines simple and readable.  I try to
understand what the compiler will create so it can be efficient.

That is why I'm digging deep into the code, to make clean changes and make it 
as I wish it to be.

Allison