Skip to content

Commit

Permalink
Ticker fix solving #6155 (#7664)
Browse files Browse the repository at this point in the history
* Wrapped Ticker functions with #pragma disabling -Wcast-function-type

* Revert "Wrapped Ticker functions with #pragma disabling -Wcast-function-type"

This reverts commit 160be7e.

* Fixed Ticker example

* Modified Ticker example

* Fixed LED_BUILTIN err for ESP32

---------

Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
  • Loading branch information
PilnyTomas and P-R-O-C-H-Y committed Feb 6, 2023
1 parent d054889 commit 7b72da6
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions libraries/Ticker/examples/Arguments/Arguments.ino
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
/*
* This example demonstrates used of Ticker with arguments.
* You can call the same callback function with different argument on different times.
* Based on the argument the callback can perform different tasks.
*/

#include <Arduino.h>
#include <Ticker.h>

// attach a LED to GPIO 21
#define LED_PIN 21
// Arguments for the function must remain valid (not run out of scope) otherwise the function would read garbage data.
int LED_PIN_1 = 4;
#ifdef LED_BUILTIN
int LED_PIN_2 = LED_BUILTIN;
#else
int LED_PIN_2 = 8;
#endif

Ticker tickerSetHigh;
Ticker tickerSetLow;

void setPin(int state) {
digitalWrite(LED_PIN, state);
// Argument to callback must always be passed a reference
void swapState(int *pin) {
static int led_1_state = 1;
static int led_2_state = 1;
if(*pin == LED_PIN_1){
Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_1_state);
digitalWrite(*pin, led_1_state);
led_1_state = led_1_state ? 0 : 1; // reverse for next pass
}else if(*pin == LED_PIN_2){
Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_2_state);
digitalWrite(*pin, led_2_state);
led_2_state = led_2_state ? 0 : 1; // reverse for next pass
}
}

void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(1, LOW);
Serial.begin(115200);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
//digitalWrite(1, LOW);

// every 25 ms, call setPin(0)
tickerSetLow.attach_ms(25, setPin, 0);
// Blink LED every 500 ms on LED_PIN_1
tickerSetLow.attach_ms(500, swapState, &LED_PIN_1);

// every 26 ms, call setPin(1)
tickerSetHigh.attach_ms(26, setPin, 1);
// Blink LED every 1000 ms on LED_PIN_2
tickerSetHigh.attach_ms(1000, swapState, &LED_PIN_2);
}

void loop() {
Expand Down

0 comments on commit 7b72da6

Please sign in to comment.