Module-2.1 - Embedded Systems and OO Modelling
Module-2.1 - Embedded Systems and OO Modelling
0
20241022
[module-2.1]
EMBEDDED SYSTEMS
AND OBJECT-ORIENTED
MODELLING
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 1
SUMMARY
• This module considers aspects concerning the modelling
of embedded software, using modelling paradigm such
as OO
isPressed():boolean switchOn()
switchOff()
• Interfaces
– Button
• isPressed(): boolean
– Led
• switchOn()
• switchOff()
lightOn := false
loop {
/* first: sensing */
bool isPressed = button.isPressed();
#endif
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 14
FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
• Light interface (in Light.h)
– interface for any lighting device — in C++: an abstract class
• Led device
– concrete type of Light
– declaration of the Led class in Led.h
– definition/implementation of the Led class in Led.cpp
• Remarks
– C++ allows for separating class declaration from class definition
– to use/access Arduino base library/procedures/constants in *.cpp
files, the header “Arduino.h” must be included
• not needed in the *.ino main file
protected:
int pin;
};
#endif
void setup(){
light = new Led(LED_PIN);
button = new ButtonImpl(BUTTON_PIN);
lightOn = false;
}
void loop(){
bool isPressed = button->isPressed();
if (!lightOn && isPressed){
light->switchOn();
lightOn = true;
} else if (lightOn && !isPressed){
light->switchOff();
lightOn = false;
}
}
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 18
REMARKS
• Object-Oriented level of abstraction
– low-level implementation details (e.g. the specific
wiring primitives used to drive pins) are hidden, not
exposed to the controller
– better readability, portability, reusability
switchOn()
switchOff()
Led <<interface>>
int pin LightExt
switchOn()
switchOff() setIntensity(int)
LedExt
setIntensity(int)
LightExt* led;
int brightness;
int fadeAmount;
void setup(){
brightness = 0;
fadeAmount = 5;
led = new LedExt(LED_PIN,brightness);
led->switchOn();
}
void loop(){
led->setIntensity(brightness);
brightness = brightness + fadeAmount;
delay(20);
}
detected():boolean
SmartLight <<interface>>
Controller Light
<<interface>> switchOn()
LightDetector switchOff()
getIntensity(): double
loop {
detected := presDetector.detected()
intens := lightSensor.getIntensity()
isLowIntens := intens < LIGHT_THRESHOLD
if (!lightOn && detected && isLowIntens){
light.switchOn()
lightOn := true
} else if (lightOn && (!isLowIntens || !detected)){
light.switchOff()
lightOn := false
}
}
ck
dba
pe
fee PERCEPTION
rce
pts
ENVIRONMENT
DECISION
act o
ion
s od
nt
tio
ac ACTION
effectors / actuators