cancel
Showing results for 
Search instead for 
Did you mean: 

((HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10) == GPIO_PIN_SET) ? 1:0); I am using this command to read a pin, Actually it is Arduino format. So I want to ask if this works in ST as well and if the same syntax work does it read both 1 and 0?

SRehm.2
Associate
 
1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

Welcome, @SRehm.2​, to the community!

We're sorry that the answer took a little while to come:

The condition mentioned is called a ternary operator, not specific to Arduino, which is based on C++, but also available in C. If you wanted to use this for the assignment to a variable, you can use it exactly like this.

Does it answer your question?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
Peter BENSCH
ST Employee

Welcome, @SRehm.2​, to the community!

We're sorry that the answer took a little while to come:

The condition mentioned is called a ternary operator, not specific to Arduino, which is based on C++, but also available in C. If you wanted to use this for the assignment to a variable, you can use it exactly like this.

Does it answer your question?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Piranha
Chief II

That code is written by someone, who doesn't know the C/C++ languages, because the following code does exactly the same:

(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10) == GPIO_PIN_SET);

But it's safer and more efficient to convert it to this:

(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10) != GPIO_PIN_RESET);