I’m building the driver for my new module (will be soon released )
I have one method that switches leds on, and one that switches them off.
Because the ‘turn on’ and off process takes a few seconds, I started a new thread to do that work.
My problem is that if I Call TurnOn and TurnOff immediately, I get nonesense as output, because every threads sends it’s commands via SPI to the led controller.
What is the common practice to solve the problem?
I thought about -before starting a new thread - checking if one thread is running, and stopping it?
Or does anyone know another, better solution?
You need to use synchronization primitives. Read about events, critical sections, semaphores etc. In your case I would use lock statement (equivalent of the critical section) to make sure that SPI communication is thread safe.
Ah the lock statement! I’ll try that first, and take a look if it works as expected. I’m not sure about what happens if a few more threads are started. Do they continue in the correct oder?
If you want to ensure an order, the lock statement probably is not enough. In this scenario a set of event objects (auto reset or manual reset events) might be more appropriate. If you give more details, we might narrow it down better.
Oh sorry I forgot to talk about the module:
It uses the WS2803 Led driver. You can control 18 outputs.
One method starts fading on one output after the other, so it takes lets say 5 seconds until all are turned on.
I put that loop into a thread.
If the turn off method is called during the ‘fade on’ period, I have the problem that every thread sends it spi commands.
So what would be the best way to do in that situation?
In my opinion it would be the best to stop the ‘fade on’ thread and start the ‘fade off’ thread immediately.
Do I want a command to complete before another can start.
Do I want to override a command that is currently running.
Do I want a command to have priority IE go first.
Sounds like you have just two commands START, STOP.
The Queue approach simply builds a list of commands and starts the driver if its not running. This way commands can be added to the list while a command is being processed IE First in First out. When the driver finishes a command it looks to see if there is anything in the list to do if so it pulls from the list and processes the command. If you want to override a command then the driver can look in list as it is processing to see if any new commands are there and stop the current command and then start the new command.
There are many ways to do things but I find a queue keeps things neat and tidy.
just did a quick test with simple ‘TurnOn’ ‘TurnOff’ methods - that worked fine
Next step is to implement the methods that require threading like ‘FadeOn’ and ‘FadeOff’.
I hope I can do it next week. Don’t have enough free time at the moment
And the next problem is that during testing, I indicated a fatal error in my layout, so I have to throw the actual boards away and have to design new ones
So the release date delays about 4-8 weeks …but I have another module that will be released very soon
Just working on the driver & presentation