This program periodically periodically samples the battery sensor and displays the bottom 3 bits on the LEDs
/*
* periodically sample the battery sensor and display the bottom 3 bits on the leds
*/
type ISense is interface ( in bool ticker; out bool battReq; in integer battVal)
component sense presents ISense {
constructor() {}
behaviour {
receive tick from ticker
send true on battReq
receive reading from battVal
if (reading & 4) == 4
then setRedLedPower(true)
else setRedLedPower(false)
if (reading & 2) == 2
then setBlueLedPower(true)
else setBlueLedPower(false)
if (reading & 1) == 1
then setGreenLedPower(true)
else setGreenLedPower(false)
}
}
s = new sense()
connect s.battReq to sensors.batteryRequest
connect s.battVal to sensors.batteryOut
setTimer(s.ticker , 0.098 , true)
The corresponding nesC program for TinyOS is shown below.
configuration SenseAppC
{
}
implementation {
components SenseC, MainC, LedsC, new TimerMilliC(), new DemoSensorC() as Sensor;
SenseC.Boot -> MainC;
SenseC.Leds -> LedsC;
SenseC.Timer -> TimerMilliC;
SenseC.Read -> Sensor;
}
/******************************************************/
#include "Timer.h"
module SenseC
{
uses {
interface Boot;
interface Leds;
interface Timer;
interface Read;
}
}
implementation
{
// sampling frequency in binary milliseconds
#define SAMPLING_FREQUENCY 100
event void Boot.booted() {
call Timer.startPeriodic(SAMPLING_FREQUENCY);
}
event void Timer.fired()
{
call Read.read();
}
event void Read.readDone(error_t result, uint16_t data)
{
if (result == SUCCESS){
if (data & 0x0004)
call Leds.led2On();
else
call Leds.led2Off();
if (data & 0x0002)
call Leds.led1On();
else
call Leds.led1Off();
if (data & 0x0001)
call Leds.led0On();
else
call Leds.led0Off();
}
}
}