Skip to main content
SRehm.2
Visitor II
July 19, 2022
Solved

((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?

  • July 19, 2022
  • 2 replies
  • 15212 views

..

This topic has been closed for replies.
Best answer by Peter BENSCH

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

2 replies

Peter BENSCH
Peter BENSCHBest answer
Technical Moderator
August 19, 2022

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
Principal III
October 5, 2022

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);