Temperature Averager Example

This example contains three components – one that interacts with the temperature sensor, one that performs averaging, and one that performs logging. In the example the logger mearly writes on standard output.

type averagerIF is interface(
    in real input;
    out real output
)
type tempReaderIF is interface (
    out bool requestDeviceValue ;
    in real device ;
    out real output
)
type loggerIF is interface (
    in real input ;
    out any screen
)
 
component TempAverager presents averagerIF {
 
	temps = new real[5] of 0.0
	index = 0
 
	constructor() {
	}	
 
	behaviour {
		receive temp from input
		temps[index] := temp
		index := index + 1
		if index == temps.length then {
			send avgRealArray( temps ) on output
			index := 0
		}
	}
}
 
// Temperature reader
component TempSensor presents tempReaderIF {
 
	constructor() {
	}
 
	behaviour {
		send true on requestDeviceValue
		receive temp from device
		send temp on output
	}
} 
 
// Standard output logger component
component TempLogger presents loggerIF {
 
	constructor() {
	}
 
	behaviour {
		receive reading from input
		send any("\nTemp: ") on screen
		send any(reading) on screen
	}
} 
 
sensor = new TempSensor ()
averager = new TempAverager ()
logger = new TempLogger ()
connect sensor.requestDeviceValue to lightHumidTempSensor.tempRequest
connect sensor.device to lightHumidTempSensor.tempOutput
connect sensor.output to averager.input
connect averager.output to logger.input
connect logger.screen to standardOut