Sleeping tasks without Sleep()
Posted: 20 January 2008, 16:47 PM
I came across a situation today in which I am unsure how to implement.
Consider the basic flashing LED routine:
If we add a concurrent task to monitor some user input...
...how can I get Sub Main to interrupt the sleep call to do something else if the user presses the button (i.e. to blink a different LED) if the task is suspended via the call to Sleep?
Perhaps like this?
Then there is just the issue of the RTC rolling over to deal with. Is this the correct method to implement this, or is there another?
-Don
Consider the basic flashing LED routine:
Code: Select all
Sub Main()
Const FlashTime as Single = 1.0
Const RedLED as Byte = C.1
Do
Call PutPin(RedLED, zxOutputLow)
Call Sleep(FlashTime)
Call PutPin(RedLED, zxOutputHigh)
Call Sleep(FlashTime)
Loop
End Sub
Code: Select all
Public ButtonActive as Boolean
Sub ButtonTask()
Const ButtonPin as Byte = C.2
Do
If NOT(CBool(GetPin(ButtonPin))) Then 'inverted logic
ButtonActive = True
Call Sleep(0.1) 'Sleep to debounce
End If
Loop
End Sub
...how can I get Sub Main to interrupt the sleep call to do something else if the user presses the button (i.e. to blink a different LED) if the task is suspended via the call to Sleep?
Perhaps like this?
Code: Select all
Sub Main()
Const FlashTime as Single = 1.0 * 512.0
Const RedLED as Byte = C.1
Dim SavedTime as Single
Do
Call PutPin(RedLED, zxOutputLow) 'Turn on the LED
SavedTime = Timer() 'Store the current Time
Do while NOT(ButtonActive) OR ((Timer() + FlashTime) < SavedTime)
Call Sleep(0) 'Do Nothing
Loop
Call PutPin(RedLED, zxOutputHigh) 'Turn off the LED
SavedTime = Timer() 'and store the current time again
Do while NOT(ButtonActive) OR ((Timer() + FlashTime) < SavedTime)
Call Sleep(0) 'ZZzzz
Loop
Loop
End Sub
-Don