Software Timer vs FreeRTOS threads

I decided to play around with Software Timers and use them to update my OLED on a regular schedule (10fps). Is this a good use of Software Timers? Should I use a thread instead? Obviously, there are threading issues that need to be accounted for (I assume either way). Thoughts?

Before using the photon for my IOT , I was using the flyport ( openPicus ) platform , which is using a fuller version of FreeRTOS.
So if they set it up right ,threads should be more better , a Delay(1000) should be , go and do something else for a second this thread does need any cpu time.
Its nice to have the O/S multitask between 3 or 4 threads and only put a line in which says dont interrupt until I`ve finish is bit of code.

You can use threads for more freedom. Here’s an example for temperature control in my coffee machine :smile:

Thread hardwareThread;
unsigned int relayDc = 0;
const int PERIOD = 1000;
const int RELAY_PIN = D0;
const int RELAY_ON = LOW;
const int RELAY_OFF = HIGH;

void controlRelay() {
  pinMode(RELAY_PIN, OUTPUT);

  for(;;) {
    unsigned int onTime = (unsigned int)(relayDc * PERIOD / 100);
    if(onTime > 0) {
      digitalWrite(RELAY_PIN, RELAY_ON);
      delay(onTime);
    }
    digitalWrite(RELAY_PIN, RELAY_OFF);
    delay(PERIOD - onTime);
  }
  // Never return from a thread
}

void setup() {
  hardwareThread = Thread("hardware", controlRelay, OS_THREAD_PRIORITY_DEFAULT + 1);
}

void loop() {
  // Code that sets onTime
}
1 Like

I tried to write some code using the Thread class, but is does not compile, is this available in the Web IDE? I am using firmware version 0.4.7.

Thread hardwareThread;

void testMethod() {
}

void setup() {
    hardwareThread = Thread("hardware", testMethod, OS_THREAD_PRIORITY_DEFAULT + 1);
}

void loop() {

}

thanks,
Chris

You will need to use v0.4.9 onwards

1 Like

Also make sure the thread function never returns.

2 Likes

How Task maximum can be defined?
Can I get a little more detail about Thread or a link to a file on github?
Is there documentation, API or anything where I can start?

I found reading the following firmware files useful:

spark_wiring_thread.h
concurrent_hal.cpp

after that you are down to the underlying freeRTOS Task functionality:

xTaskCreate

Not sure what you mean by ‘Task maximum’ can you be more specific?

2 Likes

Thanks for the links.
Who is the maximum number of defined Task?
For example, whether it is limited or depend only from the free RAM memory?

Its going to depend on how much free ram you have and how much stack memory you allocate to each Task. The default stack memory size (OS_THREAD_STACK_SIZE_DEFAULT) is 3072 bytes (see concurrent_hal_impl.h). I did some experiments setting my own value for stack size and I think a got down to 256 bytes before it stopped working, for a task which was not doing very much.

You can use System.freeMemory() to check memory at run time, I used Particle.publish() to publish it, and watched the value in the Particle dashboard.

2 Likes