When statement – Echelon Neuron C User Manual

Page 29

Advertising
background image

Neuron C Programmer’s Guide

17

In this example above, when the led_timer application timer (definition not

shown in this example) expires, the body of code (the task) that follows the when
clause is executed to turn off the specified I/O object, io_led (also defined

elsewhere in the program). After this task has been executed, the timer_expires

event is automatically cleared. Its task is then ignored until the LED timer
expires again and the when clause again evaluates to TRUE.
The following examples demonstrate various ways of using tasks and events.
More information about tasks and events can be found in Chapter 7,

Additional

Features

, on page 145, and Figure 14 on page 147.

when (reset)
when (io_changes(io_switch))
when (!timer_expires)
when (flush_completes && (y == 5))
when (x == 3)
{

// Turn on the LED and start the timer

. . .

}

The when clauses cannot be nested. For example, the following nested when
clause is not valid:

when (io_changes(io_switch))
{

when (x == 3) {

// Can't nest!

...
}
}

An equivalent result may be achieved by testing the event with an if statement:

when (io_changes(io_switch))
{

if (x == 3) {

...
}
}

When Statement

The syntax for a when statement (the when clause or clauses plus the associated

task) is:

when-clause

[when-clause ... ]

task

The syntax for

when-clause

is:

[priority] [preempt_safe] when (

event

)

priority

Forces evaluation of the following when clause each time the

scheduler runs. See

Priority When Clauses

on page 23.

preempt_safe

Allows the scheduler to execute the associated when task

even if the application is in preemption mode. See the discussions on

preemption mode in Chapter 6,

How Devices Communicate Using

Application Messages

, on page 117.

Advertising