Echelon Neuron C User Manual

Page 71

Advertising
background image

Neuron C Programmer’s Guide

59

Example:
A lamp and switch example could also be written to use explicit polling of the
switch network variable. Complete programs illustrating polling are shown

below.
Listing 1. Lamp Program Using Polling

// LAMP.NC -- Sample lamp actuator program,
// polls the switch periodically

///////////////////// Network Variables ////////////////
network input SNVT_switch nviLampState;

//////////////////////// Constants /////////////////////
#define LED_ON 0
#define LED_OFF 1

/////////////////////// I/O Objects ////////////////////
IO_0 output bit ioLED = LED_OFF;

//////////////////////// Timers ////////////////////////
mtimer tmPoll;

/////////////////////////// Tasks //////////////////////
// NV update task -- handle update to lamp state
// Use the network variable’s value as the new
// state for the lamp
when (nv_update_occurs(nviLampState)) {
io_out(ioLED,

nviLampState.value

&&

nviLampState.state

?

LED_ON

:

LED_OFF);

tmPoll = 500; // Wait 500 msec before polling again

}

////////////////////////////////////////////////////////
// Reset and timer task
// request last value from any switch attached
when (reset) {
tmPoll = 4ul * random(); // >= 1 second
}

when (timer_expires(tmPoll) ) {
poll(nviLampState);
}

Listing 2. Switch Program Using Polling

// SWITCH.NC -- Sample switch sensor program
// Only transmits switch state when polled by the lamp

//////////////////// Network Variables ////////////////
network output polled SNVT_switch nvoSwitchState;

//////////////////////// Constants ////////////////////
#define BUTTON_DOWN 0
#define BUTTON_UP 1

/////////////////////// I/O Objects ////////////////////

Advertising