Ticker vs tasks

solexious
Posts: 3
Joined: Thu Oct 01, 2020 3:39 pm

Ticker vs tasks

Postby solexious » Thu Oct 01, 2020 4:03 pm

I'm trying to work out the "best" thing to use between timers and tasks.

I want to call a function every 60 seconds, this will do some updates to a tft screen plugged into my esp32. This update performs some i2c calls and then spits out updates to the screen over spi.

I just learned about tasks and as they were shiny and new to me I looked to use that first, but I want to not use too much of my heap and it looks like running a task takes up a fair bit. I had set one up with a loop to call the update function, then delay for 60 seconds.

Could anyone explain the pros and cons of the 2 methods with any advice on the one that has the lowest system usage?

My criteria is:
Lowest memory usage
Can run in the "background"
Runs every 60 seconds ish*

*This is only roughly, it could be a second or 2 off every 60 seconds without causing issues

Cheers

Sol

ESP_Sprite
Posts: 9745
Joined: Thu Nov 26, 2015 4:08 am

Re: Ticker vs tasks

Postby ESP_Sprite » Fri Oct 02, 2020 10:22 am

It depends. A function that you call in your main loop is indeed easier on the heap (as you don't need to allocate the stack for a new task) and it's way harder to run into race conditions with code executing this way. A task, however, will always be executed, even if the rest of your code is 'stuck' doing something else; if you had e.g. an UI that needs to respond do user inputs, this can make the UI feel much more responsive. For complicated things, a task can also ease up things as you don't have to structure your code like a humongous state machine. Given that your 'UI' seems to be output-only and you already wrote the code, a function likely will suffice just fine here.

solexious
Posts: 3
Joined: Thu Oct 01, 2020 3:39 pm

Re: Ticker vs tasks

Postby solexious » Mon Oct 05, 2020 12:34 am

Ah great, that was what I was thinking by the end of searching about it.

Thanks very much for the clarification!

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot], Majestic-12 [Bot] and 69 guests