Seatbelt alarm example

Layered structure

By proper layering we can insulate main logic from hw-dependent things. Main logic can be written in pure c - it only depends on the api functions declared in hwapi.h. The implementation of hwapi-functions depends on compiler, processor and electronics but due to the layered structure they are hidden from the main logic. This approach obeys the software engineering principle named "Separation of Concerns". Discuss the other benefits we achieve by this kind of layering.

beltreminder.c

#pragma NOEXTEND /* Only pure c allowed */
#include "HWAPI.H"
void main() {
  while(1) {
    if (isDriverSitting() &&  isBeltOpen()) 
       setAlarmOn();
    else
       setAlarmOff();
  }
}

hwapi.h

#ifndef HWAPI_H
#define HWAPI_H
 
unsigned char isBeltOpen();
unsigned char isDriverSitting();
void setAlarmOn();
void setAlarmOff();
 
#endif

hwapi.c

#include "HWAPI.H"
#include <REG52.H>
 
sbit driver_sensor = P1^0;
sbit belt_sensor = P1^1;
sbit alarmer = P3^7;
 
void setAlarmOn() {
 alarmer=0;
}
 
void setAlarmOff() {
 alarmer=1;
}
 
unsigned char isDriverSitting() {
 return driver_sensor == 0;
 
}
 
unsigned char isBeltOpen() {
 return belt_sensor == 1;
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License