28.02.2014 Views

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The EFSM allows us to verify some desirable properties of this system. For instance:<br />

• it is always possible to sound the alarm (we know this is true because there is a path from each<br />

state to the “alarm sounding” state)<br />

• it is always possible to disarm the system (there are paths from all states to “disarmed” too,<br />

though they require the input to be equal to the password)<br />

These are referred to as liveness properties: desirable things that can always (eventually) happen.<br />

Automata-Based <strong>Programming</strong><br />

How would you implement the above EFSM in code? If you don't think it through, this can easily turn<br />

into an unwieldy mess of if/else statements (just look at the original specification!).<br />

A simpler approach, known as automata-based programming, is to use enums to represent the<br />

different possible states and signals, and then simply use switch statements to first figure out which<br />

state you're in, and then which signal you've received, and then act accordingly.<br />

For instance:<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

21<br />

22<br />

23<br />

24<br />

25<br />

26<br />

27<br />

28<br />

29<br />

30<br />

31<br />

enum states {disarmed, armed, alarm_sounding} state;<br />

enum signals {arm, disarm, panic, motion, quiet};<br />

void handle_signal (enum signals signal) {<br />

switch (state) {<br />

case disarmed:<br />

switch (signal) {<br />

case arm:<br />

state = armed;<br />

break;<br />

case panic:<br />

state = alarm_sounding;<br />

break;<br />

}<br />

break;<br />

case armed:<br />

switch (signal) {<br />

case panic:<br />

state = alarm_sounding;<br />

break;<br />

case motion:<br />

state = alarm_sounding;<br />

break;<br />

case disarm:<br />

if (password_correct()) state = disarmed;<br />

else state = alarm_sounding;<br />

break;<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!