Skip to content

Request explaination #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ilzam opened this issue Feb 2, 2018 · 2 comments
Closed

Request explaination #1

Ilzam opened this issue Feb 2, 2018 · 2 comments

Comments

@Ilzam
Copy link

Ilzam commented Feb 2, 2018

Hi,

Could you explain function or meaning of below coding.

// If we are connected to a peer BLE Server, update the characteristic each time we are reached
// with the current time since boot.
if (connected) {
if (onoff) {
Serial.println("Notifications turned on");
pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true);
}
else {
Serial.println("Notifications turned off");
pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOff, 2, true);
}

onoff = onoff ? 0 : 1;

}
`

@frownbreaker
Copy link

frownbreaker commented Mar 17, 2018

The question mark in the code is known as the ternary operator in C. Ternary is an adjective meaning three parts. Its a mathematical term (computer science is a branch of mathematics). It is available in many languages: https://en.wikipedia.org/wiki/%3F:

Also in the code sample you posted "onoff = onoff ? 0 : 1;" there is an = (assignment operator) rather than an == (comparison operator)

Before explaining the code you asked about you should first understand the ternary operator:

This is the typical textbook example..

//Looking at the maximum example

int findMaximum(int a, int b){
    //if a > b, it returns a, if not it returns b
    return (a > b) ? a : b;
} 

Ok so now we know what the ternary operator ? is used for we need to disambiguate == from =. In C to compare something you use "==" and to set something to a value you use "="

One last point - For the avoidance of doubt: In C true is represented by any numeric value not equal to 0 and false is represented by 0.

Ok, now that's covered! We can answer your question! The code you asked about onoff = onoff ? 0 : 1; means "if the value of onoff is true set the value of onoff to 0 otherwise set the value of onoff to 1"

Reading the code initially one might think the value of onoff is never changed from its initial value bool onoff = false;, but on inspection, the assignment is done using the ternary operator. I think many folk might have missed this subtle difference. Excellent question :) Hope this helps?

The code you provided did not have the assignment but I assumed you got the code from here nkolban/esp32-snippets#269

@1043717432
Copy link

How do you request that you compile it?
I have an error using Arduino1.85

In file included from C:\Users\eric8\Documents\Arduino\BLE_Proximity_Sensor\BLE_Proximity_Sensor.ino:23:0:

D:\homeassistant\arduino-1.8.5\arduino-1.8.5\portable\sketchbook\libraries\ESP32_BLE_Arduino-7951347ed68313d75c367e1f2cce763cb56d1eb2\src/BLEDevice.h:10:23: fatal error: sdkconfig.h: No such file or directory

  #include "sdkconfig.h"

                        ^

Compilation terminated.

Exit status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants