Application message examples, Lamp program, Switch program – Echelon Neuron C User Manual

Page 141

Advertising
background image

Neuron C Programmer’s Guide

129

If a message were to arrive and the application fail to process it, that message

would remain at the head of the queue forever, blocking the arrival of any other
messages or network variable events and locking up the device until it is reset.

One example of a message that would be sent to all devices, most of which are not

interested in the message, is the service pin message. Probably only a network
tool would want to process the service pin message; all other devices need to

discard the message.

If a program does not process messages (either implicitly through the use of

when(msg_arrives) or explicitly through the use of msg_receive( )), the scheduler

automatically discards all incoming messages.

A device that uses

only

network variables need not be concerned with this

phenomenon, because the scheduler then handles

all

incoming messages.

Application Message Examples

The following example shows how lamp and switch devices could be written using
application messages instead of network variables.

Lamp Program

First, here is the program for the lamp devices:

// lamp.nc - Generic program for a lamp
// The lamp’s state is governed by an incoming
// application message

#define LAMP_ON 1
#define LAMP_OFF 2
#define OFF 0
#define ON 1

// I/O declaration
IO_0 output bit io_lamp_control;

when (msg_arrives) {
switch (msg_in.code) {
case LAMP_ON:
io_out(io_lamp_control, ON);
break;
case LAMP_OFF:
io_out(io_lamp_control, OFF);
break;
} //end switch
} //end when

Switch Program

Here is the program for the switch devices:

// switch.nc - Generic program for a switch
// Send a message when the switch changes state

#define LAMP_ON 1
#define LAMP_OFF 2

Advertising