simply depends on the knowledge-level somebody has. I'm coming from delphi and rarely used call-backs-functions with dephi.
My former experience with call-back-functions is
this pretty different design which I have not touched or modified but just used it
// Callbacks related to BLE connection
class BleKeyboardCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* server) {
isBleConnected = true;
// Allow notifications for characteristics
BLE2902* cccDesc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
cccDesc->setNotifications(true);
Serial.println("Client has connected");
}
void onDisconnect(BLEServer* server) {
isBleConnected = false;
// Disallow notifications for characteristics
BLE2902* cccDesc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
cccDesc->setNotifications(false);
Serial.println("Client has disconnected");
}
};
and this one which looks different again
MyEmailAdressCard.attachCallback([&](String MyEmailStr){
MyEmail = MyEmailStr.c_str();
Serial.print( F("Button Triggered: ") );
Serial.println(MyEmail_SS);
writeData();
MyEmailAdressCard.update(MyEmail); //Make sure we update our value and send update to dashboard */
//mySerial.println(MyEmail_SS);
MyDashboard.sendUpdates();
});
a longer time ago I did things like this
attachInterrupt(digitalPinToInterrupt(2), checkPin, CHANGE);
which has a similarity to your examplecode
Some of the comments I do not yet understand in all details.
void yellow_ON() {
//The yellow ON timer has dinged.
//Turn on the LED, stop the ON timer then start the OFF timer.
digitalWrite(LED_YELLOW_PIN, LED_ON); //Turn on the yellow LED
yellowLED_onTime.stop(); //Stop the ON timer
yellowLED_offTime.setdelay(random(1000, 5000)); //LED will be on for this time.
yellowLED_offTime.start(); //Start the OFF timer
greenVal = GREEN_MAX;
}
I try to describe in my own words what I assume to have understood about the noDelay-library
There is first non-blocking timerobject for switching the yellow LED on
and a second non-blocking timerobject for switching the yellow LED off
you create two timer-objects for the one Yellow-LED to have different times for the LED to be switched on and switched off
yellowLED_onTime.stop(); //Stop the ON timer
not nescessary to call this inside the callback-function
used here as a specialty of this functionality here to make two LEDs blink at randomly changing times.
yellowLED_offTime.setdelay(random(1000, 5000)); //LED will be on for this time.
does what the name says. set the time that has to pass by until the timer-object with the name yellowLED_offTime "fires" its "expiration-signal"
where the expiration-signal is calling (=executing) the callbackfunction
yellowLED_offTime.start(); //Start the OFF timer
here start means activate timer in the sense of start the timer to count-down from the milliseconds set with the .setdelay() function until this countdown reaches zero.
If timer reaches zero execute the callbackfunction
The methodname "update" does some "updating" under the hood.
But this method-name does not really reflect that the user has to call this function very often and at least repeatedly to make the timer-object work.
I'm still thinking about a function name that does explain that thing better.
The function name "start" does start the timer. Yes. But IMHO this name does not reflect all things that are important for the user.
Something like "StartCountingDown" describes it more precise.
I have carefully chosen Counting instead of simply "CountDown" as Counting clearly expresses what is happening.
similar to function "stop" StopCountingDown.
I understand the intention of the demo-code. Show what is possible to do with the noDelay-library.
I'm thinking about derivating a new library from it which has even a different library-name
Including demos that start at the most simple task of a regular blink working up from there to more complex things.
best regards Stefan