Channels ▼
RSS

Who Moved My State?


Listing 2: The Keyboard FSM implementation (see state diagram from Figure 2)

Listing 2: The Keyboard FSM implementation (see state diagram from Figure 2)

 1: #include "fsm.h"
 2:
 3: typedef struct Keyboard Keyboard;
 4: struct Keyboard {
 5:    Fsm super_; /* extend the Fsm class */
 6:    /* ... other attributes of Keyboard */
 7: };
 8:
 9: void KeyboardCtor(Keyboard *me);
10: void Keyboard_initial(Keyboard *me, Event const *e);
11: void Keyboard_default(Keyboard *me, Event const *e);
12: void Keyboard_shifted(Keyboard *me, Event const *e);
13:
14: typedef struct KeyboardEvt KeyboardEvt;
15: struct KeyboardEvt {
16:    Event super_; /* extend the Event class */
17:    char code;
18: };
19:
20: enum { /* signals used by the Keyboard FSM */
21:    SHIFT_DEPRESSED_SIG,
22:    SHIFT_RELEASED_SIG,
23:    ANY_KEY_SIG,
24: };
25:
26: void KeyboardCtor(Keyboard *me) {
27:    FsmCtor_(&me->super_, &Keyboard_initial);
28: }
29:  
30: void Keyboard_initial(Keyboard *me, Event const *e) {
31:    /* ... initalization of Keyboard attributes */
32:    printf("Keyboard initialized");
33:    FsmTran_((Fsm *)me, &Keyboard_default);
34: }
35:
36: void Keyboard_default(Keyboard *me, Event const *e) {
37:    switch (e->sig) {
38:    case SHIFT_DEPRESSED_SIG:
39:       printf("default::SHIFT_DEPRESSED");
40:       FsmTran_((Fsm *)me, &Keyboard_shifted);
41:       break;
42:    case ANY_KEY_SIG:
43:       printf("key %c", (char)tolower(((KeyboardEvt *)e)->code));
44:       break;
45:    }
46: }
47:
48: void Keyboard_shifted(Keyboard *me, Event const *e) {
49:    switch (e->sig) {
50:    case SHIFT_RELEASED_SIG:
51:       printf("shifted::SHIFT_RELEASED");
52:       FsmTran_((Fsm *)me, &Keyboard_default);
53:       break;
54:    case ANY_KEY_SIG:
55:       printf("key %c", (char)toupper(((KeyboardEvt *)e)->code));
56:       break;
57:    }
58: }


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video